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

Ada Barries - C17 Otters #89

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
37 changes: 34 additions & 3 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,39 @@ 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+E) (N is dogs, E is edges (dogs who got beef))
Space Complexity: O(N) (nodes (dogs) reliant on amount of dogs)
"""
pass
# return true if dogs can be split into two groups
# and no fighting will occur. otherwise return false.
if not dislikes:
return True

# dict for dog groups. default is 0, other group is 1.
kennel = { dog:0 for dog in dislikes }

first_dog = list(dislikes.keys())[0]
kennel[first_dog] = 1

#BFS
queue = [first_dog]
visited = [first_dog]

for dog in dislikes:
while queue:
current_dog = queue.pop(0)
visited.append(current_dog)

if dislikes[current_dog]:
for angy_dog in dislikes[current_dog]:
if kennel[angy_dog] == 0:
kennel[angy_dog] = kennel[current_dog] + 1
queue.append(angy_dog)
else:
if kennel[current_dog] == kennel[angy_dog]:
return False
if dog not in visited:
queue.append(dog)

return True