Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,35 @@ def possible_bipartition(dislikes):
""" Will return True or False if the given graph
can be bipartitioned without neighboring nodes put
into the same partition.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n*m) n dogs m how disliked they are?
Space Complexity: O(n)
"""
pass
groups = [set(), set()]

not_checked = {i for i in range(len(dislikes))}

# this could be a queue
to_check = []

while not_checked:
Comment on lines +13 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really clever use of a set.

if not to_check:
dog = not_checked.pop()

else:
dog = to_check.pop()
not_checked.remove(dog)

dog_dislikes = set(dislikes[dog])
for other_dog in dislikes[dog]:
if other_dog in not_checked:
to_check.append(other_dog)

if not groups[0].intersection(dog_dislikes):
groups[0].add(dog)
elif not groups[1].intersection(dog_dislikes):
groups[1].add(dog)
else:
return False

return True