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

Dani Sanchez - Paper #2

Open
wants to merge 2 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
45 changes: 41 additions & 4 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
# 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 log n)
Space Complexity: O(n)
"""
Comment on lines 5 to 11

Choose a reason for hiding this comment

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

👍 Although I think the time complexity is O(NE) where n is the number of nodes (dogs) and E is the number of edges (dislikes between dogs).

pass
n = len(dislikes)
if n < 3:
return True

# creates an array of length n, will keep track of nodes visited
visited = [False] * (n)

# initializes a queue and two sets to hold two groups of puppies
q = deque()
q.append(1)
visited[0] = True
red = set()
blue = set()
red.add(1)

#creates a dictionary to hold puppies and their enemies
dislike_dict = {}
for i in range(0, n):
dislike_dict[i] = dislikes[i]

# loops through queue of nodes.
while len(q) > 0:
pointerKey = q.popleft()
# appends node to queue if it has not been visited.
# returns False if node is in other set already.
for node in dislike_dict[pointerKey]:
if visited[pointerKey] == False:
q.append(node)
if pointerKey not in red:
red.add(node)
if node in blue:
return False
else:
blue.add(node)
if node in red:
return False
visited[pointerKey] = True
return True