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
39 changes: 36 additions & 3 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,41 @@ 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)
Space Complexity: O(n) ??
"""
Comment on lines +8 to 10

Choose a reason for hiding this comment

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

Nice BFS solution

pass

if not dislikes:
return True

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

group_one = set()
group_two = set()
hold_visited = []

while upcoming:
current_node = upcoming.pop(0)
hold_visited.append(current_node)

a = True
b = True
Comment on lines +28 to +29

Choose a reason for hiding this comment

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

not the best choices in variable names.


for elem in dislikes[current_node]:
if elem in group_one:
a = False # flag to false
if elem in group_two :
b = False
if (elem not in hold_visited) and (elem not in upcoming):
upcoming.append(elem)

if a == True:
group_one.add(current_node)
elif b == True:
group_two.add(current_node)
else:
return False
return True