Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

final submission #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
42 changes: 34 additions & 8 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
# Can be used for BFS
from collections import deque
from collections import deque


def possible_bipartition(dislikes):

Choose a reason for hiding this comment

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

👍 Nice BFS Solution! Good use deque.

""" Will return True or False if the given graph
can be bipartitioned without neighboring nodes put
into the same partition.
Time Complexity: ?
Space Complexity: ?
"""
pass
if dislikes == []:
return True

start_node = 0
visted = [False] * len(dislikes)
q = deque()
q.append(start_node)

group_a = []
group_b = []

while q:
current = q.popleft()
visted[current] = True
if not dislikes[current]:
q.append(current + 1)

for i in dislikes[current]:
if visted[i] == False:
q.append(i)

if current not in group_a:
if i in group_b:
return False
group_a.append(i)
else:
if i in group_a:
return False
group_b.append(i)

return True

pass