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

c17 otters - nikki #79

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
39 changes: 31 additions & 8 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
# Can be used for BFS
from collections import deque

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: ?
"""
pass

if not dislikes:
return True

dogs = {dog: -1 for dog in dislikes.keys()}

def breadth_first(start):
dogs[start]
q = deque()
q.append(start)

while q:
curr = q.popleft()
for node in dislikes[curr]:

if dogs[node] == -1:
dogs[node] = 1 - dogs[curr]
q.append(node)

elif dogs[node] == dogs[curr]:
return False

return True

for dog in dislikes.keys():

if dogs[dog] == -1:
if not breadth_first(dog):
return False

return True