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

Kayla - Pine #38

Open
wants to merge 4 commits 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
50 changes: 47 additions & 3 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
# Can be used for BFS
from collections import deque
from collections import deque

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: ?
Time Complexity: O(n+e) number of nodes in the graph and e is the numbner of edges
Space Complexity: O(n) storing 4 variables? Checked, G1, G2, queue. drop the constant

Choose a reason for hiding this comment

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

✨ Yes, because queue, checked, group1, and group2 each can hold at most n elements, where n is the number of nodes (or dogs) in dislikes. Nice work!

"""
pass

if not dislikes:
return True

# keep track of nodes checked
# Create empty queue
# add start_node (in this case 0) to queue
checked = [None] * len(dislikes)
queue = deque()
queue.append(0)
group1 = set()
group2 = set()

# So long as the queue is not empty, remove current dog
# Mark that current dog has been checked
# if dog is not matched to a another hating ass dog, add the next dog to the queue
while queue:
current = queue.popleft()
checked[current] = True
if not dislikes[current]:
queue.append(current + 1)
Comment on lines +31 to +32

Choose a reason for hiding this comment

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

Creative way to handle disconnected nodes in the graph!


# Loope through list of dogs.
# If the next dog has NOT been checked
# Add it to the queue to be assigned
for dog in dislikes[current]:
if not checked[dog]:
queue.append(dog)

# If current dog is not in group 1 and if the next dog is in group 2
# Then return false becuase there is no peaceful way to group these doggs.
# But if the current dog is not in group 1 and the next dog is not in group 2
# Then add the current dog to group 1
if current not in group1:
if dog in group2:
return False
group1.add(dog)

# Additionally, if the next dog is NOT group 1, add the current dog to group 2
# But if the next dog is NOT in group 1 then add it to group 2
else:
Copy link
Author

Choose a reason for hiding this comment

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

^^^ This shold actually say, if the current dog IS in group 1, then check to see if the next dog is also in group 1, and if it it is return false, cuz aint no way these dogs will get along. But if the next dog is NOT in group 1, then add it to group 2.

if dog in group1:
return False
group2.add(dog)
return True