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

Scissors - Maria M. #16

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
49 changes: 45 additions & 4 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,49 @@ 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
# Time Complexity: O(nm)
# Space Complexity: O(n)
# """
Comment on lines +8 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work on a BFS solution.


if dislikes == []:
return True

visited_dogs = [False] * len(dislikes)
group_one = set()
group_two = set()
dog_queue = deque()
dog_queue.append(0)

while dog_queue != deque([]):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit simpler

Suggested change
while dog_queue != deque([]):
while not dog_queue.empty():

current_dog = dog_queue.popleft()
visited_dogs[current_dog] = True
if not dislikes[current_dog]:
dog_queue.append(current_dog + 1)

for dog in dislikes[current_dog]:
if visited_dogs[dog] == False:
dog_queue.append(dog)

if current_dog not in group_one and dog in group_two:
return False
elif current_dog not in group_one:
group_one.add(dog)
elif dog in group_one:
return False
else:
group_two.add(dog)
return True

# print(possible_bipartition([ [],
# [2, 3],
# [1, 4],
# [1],
# [2]
# ]) == True)

# print(possible_bipartition([ [],
# [2, 3],
# [1, 3],
# [1, 2]
# ]) == False)