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
32 changes: 31 additions & 1 deletion graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,35 @@ def possible_bipartition(dislikes):
Time Complexity: ?
Space Complexity: ?
Comment on lines 8 to 9

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

"""
pass
queue = deque()
set_a = set()
set_b = set()
visited = set()

if not dislikes:
return True
if dislikes[0]:
starting_node = 0
else:
starting_node = 1
Comment on lines +18 to +21

Choose a reason for hiding this comment

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

🤔Consider an edge case where dislikes contains multiple nodes at the beginning of the adjacency list that are disconnected from the graph. How might you refactor your code to account for this situation?


queue.append(starting_node)
visited.add(starting_node)
set_a.add(starting_node)

while len(queue) > 0:

Choose a reason for hiding this comment

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

🐩 Nice BFS implementation

node = queue.popleft()
for i in dislikes[node]:
if i not in visited:
queue.append(i)
visited.add(i)
if node in set_a:
set_b.add(i)
else:
set_a.add(i)
else:
if node in set_a and i in set_a or node in set_b and i in set_b:
return False

return True