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
36 changes: 33 additions & 3 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,38 @@ 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+E)
Space Complexity: O(N)
"""
pass
if not dislikes:
Comment on lines +8 to +11

Choose a reason for hiding this comment

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

👍 Nice BFS solution

return True

if len(dislikes[0]) > 0:
to_visit = [0]
else:
to_visit = [1]

group_a = set()
group_b = set()
visited = []
while to_visit:
current = to_visit.pop(0)
visited.append(current)
a = True
b = True
for dislike in dislikes[current]:
if dislike in group_a:
a = False
if dislike in group_b :
b = False
if dislike not in visited and dislike not in to_visit:
to_visit.append(dislike)
if a == True:
group_a.add(current)
elif b == True:
group_b.add(current)
else:
return False

return True