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

Ari - ✂️ - Bi-partition-Graph #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,6 @@ dmypy.json
# Cython debug symbols
cython_debug/

# scratch file
scratch.py
scratch_2.py
58 changes: 54 additions & 4 deletions graphs/possible_bipartition.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,62 @@
# Can be used for BFS
from collections import deque
from pprint import pprint

def possible_bipartition(dislikes):
""" Will return True or False if the given graph
can be bipartitioned without neighboring nodes put
can be bi-partitioned without neighboring nodes put
into the same partition.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(nm)
Space Complexity: O(n)
Comment on lines +9 to +10

Choose a reason for hiding this comment

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

You need to elaborate on what m is. I get that n is the number of nodes.

"""
pass
if not dislikes:
return True

dislikes_graph = {}
for index, value in enumerate(dislikes):
if index not in dislikes_graph:
dislikes_graph[index] = value
else:
dislikes_graph[index] += value
pprint(dislikes_graph)

visited = set()
red = set()
green = set()

dogs_queue = deque()

for i in range(len(dislikes_graph)): # if graph nodes are not connected
if i in visited:
continue

dogs_queue.append((i, "red")) # target color/ add it to queue

while dogs_queue:
dog, pen = dogs_queue.popleft() # process dog
visited.add(i) # once dog processed

# if we've seen this dog before and it's in the opposite pen already
if dog in red and pen == "green":
return False
elif dog in green and pen == "red":
return False

if dog not in red and dog not in green: # haven't separated dog
if pen == "green":
green.add(dog)
visited.add(dog)

elif pen == "red":
red.add(dog)
visited.add(dog)

# process neighboors of dog / putting dislike dogs in opposite pen
for neighbor in dislikes_graph[dog]:
if neighbor not in red and neighbor not in green:
if pen == "red": # opposite pen!
dogs_queue.append((neighbor, "green"))
elif pen == "green":
dogs_queue.append((neighbor, "red"))

return True
26 changes: 14 additions & 12 deletions tests/test_possible_bipartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ def test_example_1():
[1],
[2]
]
# {0, 2, 1 , 1, 2}
# {3, 4}
# TRUE

# Act
answer = possible_bipartition(dislikes)

print(f' FIRST TEST ANSWER {answer}')
# Assert
assert answer

Expand All @@ -32,7 +35,7 @@ def test_example_2():

def test_example_r():
# Arrange
dislikes = [ [],
dislikes = [[],
[2, 5],
[1, 3],
[2, 4],
Expand Down Expand Up @@ -80,21 +83,20 @@ def test_will_return_false_for_graph_which_cannot_be_bipartitioned():
# Assert
assert not answer


def test_will_return_true_for_empty_graph():
assert possible_bipartition([])

def test_will_return_false_for_another_graph_which_cannot_be_bipartitioned():
# Arrange
dislikes = [ [3, 6],
[2, 5],
[1, 3],
[0, 2, 4],
[3, 5],
[1, 4],
[0],
[8],
[7]
dislikes = [[3, 6], # [0]
[2, 5], # [1]
[1, 3], # [2]
[0, 2, 4], # [3]
[3, 5], # [4]
[1, 4], # [5]
[0], # [6]
[8], # [7]
[7] # [8]
]

# Act
Expand Down