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

Hailey M. cs-fun-class-a #75

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: 30 additions & 7 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 start_new_queue(dislikes, visited):
first_item = list(dislikes.keys())[0]
visited[first_item] = 0
return [first_item]

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

# BFS solution
if len(dislikes) == 0:
return True

visited = {}
queue = start_new_queue(dislikes, visited)

while queue:
current = queue.pop(0)
curr_color = visited[current]
for neighbor in dislikes[current]:
if neighbor not in visited:
visited[neighbor] = 1 if curr_color == 0 else 0
queue.append(neighbor)
else:
if curr_color == visited[neighbor]:
return False
if len(queue) == 0 and len(visited) < len(dislikes):
new_dislikes = {}
for key, value in dislikes.items():
if key not in visited:
new_dislikes[key] = value
dislikes = new_dislikes
queue = start_new_queue(dislikes, visited)
return True