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
28 changes: 25 additions & 3 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,30 @@ 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) where is the number of dogs and n is the number of dislikes
Space Complexity: O(n) where n is the number of dogs
"""
pass
def can_partition_dog(current_dog):
dog_group = { current_dog: True }
to_visit = [current_dog]
visited = set()

while len(to_visit) > 0:
dog = to_visit.pop()
if dog in visited: continue
visited.add(dog)

for other_dog in dislikes[dog]:
if other_dog in dog_group and dog_group[dog] == dog_group[other_dog]:
return False

dog_group[other_dog] = not dog_group[dog]
to_visit.append(other_dog)

return True

for dog in dislikes.keys():
if not can_partition_dog(dog):
return False

return True