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

def possible_bipartition(dislikes):

def possible_bipartition(dislikes_dict):
""" 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

# return true if the graph is empty
if not dislikes_dict:
return True

graph = {}
for node in dislikes_dict:
graph[node] = dislikes_dict[node]

color = {}
for node in graph:
# check if the node has been visited
if node not in color:
# assign a color to the current node
color[node] = 0
queue = deque([node])
while queue:
# get the next node to visit
current = queue.popleft()
# visit the neighbors of the current node
for neighbor in graph[current]:
if neighbor not in color:
# assign the opposite color to the neighbor
color[neighbor] = 1 - color[current]
queue.append(neighbor)
elif color[neighbor] == color[current]:
# if the neighbor already has the same color as the current node, the graph is not bipartite
return False
return True