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
41 changes: 40 additions & 1 deletion graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,44 @@ def possible_bipartition(dislikes):
Time Complexity: ?
Space Complexity: ?
"""
pass
# dislikes = {
# "Fido": [],
# "Nala": ["Cooper", "Spot"],
# "Cooper": ["Nala", "Bruno"],
# "Spot": ["Nala"],
# "Bruno": ["Cooper"]
# }

if not dislikes:
return True

dog_pen = {dog:0 for dog in dislikes}

start_dog = list(dislikes.keys())[0]
dog_pen[start_dog] = 1

dog_list = [dog for dog in dislikes]
queue = [start_dog]

for dog in dog_pen:
while queue:
current_dog = queue.pop(0)
dog_list.remove(current_dog)

for dog in dislikes[current_dog]:
# if the disliked dog is not sorted in the dogpen
if dog_pen[dog] == 0:
# make the value of the disliked dog in the group
if dog_pen[current_dog] == 1:
dog_pen[dog] = 2
else:
dog_pen[dog] = 1
if dog in dog_list:
queue.append(dog)
elif dog_pen[dog] == dog_pen[current_dog]:
return False
if dog_list:
queue.append(dog_list[0])
else:
return True