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
32 changes: 31 additions & 1 deletion graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,35 @@ def possible_bipartition(dislikes):
Time Complexity: ?
Space Complexity: ?
"""
pass

if dislikes == None:
return True

color = {}

for dog in dislikes:
if dog in color:
continue

q = deque()
q.append(dog)
color[dog] = 0

while (len(q) > 0):
dog = q.pop()
current_color = color[dog]
fight_set = dislikes[dog]
for fight_dog in fight_set:
if fight_dog in color:
if color[fight_dog] == current_color:
return False
else:
if current_color == 0:
next_color = 1
else:
next_color = 0
color[fight_dog] = next_color
q.append(fight_dog)
return True