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

possible bipartition #22

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
53 changes: 49 additions & 4 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
# Can be used for BFS
from collections import deque
from collections import deque
import collections
from email.policy import default

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(V+E)
Space Complexity: O(N)
"""
Comment on lines 6 to 12

Choose a reason for hiding this comment

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

👍 Nice BFS solution. Well done.

pass
# input = [ [], [2, 3], [1, 4], [1], [2]]
# output = True

# input1 = [ [],[2, 3],[1, 3],[1, 2]]
# output = False

### Note ###
# I had a hard time to come with a solution on my own
# After looking up online resources/videos, this solution made sense to me

start_node = 0
visited = [False] * len(dislikes)
q = deque()
q.append(start_node)

# initialize two groups for neighboring nodes
group_a = set()
group_b = set()

# edge cases when dislikes is empty or the len is 1
if not dislikes or len(dislikes) == 1:
return True

while q:
current = q.popleft() #start_node is 0,
visited[current] = True

if not dislikes[current]:
q.append(current + 1)

# iterate through the nodes in dislikes
for neighbor in dislikes[current]:
if not visited[neighbor]: # not visited is append to q
q.append(neighbor)

if current not in group_a:
if neighbor in group_b:
return False
group_a.add(neighbor)
else:
if neighbor in group_a:
return False
group_b.add(neighbor)

return True