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
31 changes: 28 additions & 3 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,33 @@ 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(V + E)
Space Complexity: O(n)
Comment on lines +8 to +9
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✨ Correct for a breadth first search implementation, but I would suggest you define your variables particularly because you use different variables to represent the same thing here. V and n both represent the number of vertices/nodes and E represents the number of edges.

"""
pass
if not dislikes:
return True

groups = [None] * len(dislikes)
queue = deque()
first_group = 0
second_group = 1
first_dog = 0
for i in range(len(dislikes)):
if dislikes[i]:
first_dog = i
break
if first_dog == None:
return True
Comment on lines +19 to +24
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you refactor this to avoid a break statement? Perhaps use a while loop?

queue.append(first_dog)
groups[first_dog] = first_group
groups[first_dog] = True
Comment on lines +26 to +27
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Line 27 overwrites line 26 here.


while queue:
curr_dog = queue.popleft()
for i in dislikes[curr_dog]:
if groups[i] is None:
groups[i] = first_group if groups[curr_dog] else second_group
queue.append(i)
elif groups[i] == groups[curr_dog]:
return False
return True