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

Nataliia Getlin - Rock - C15 #17

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

def possible_bipartition(dislikes):

Choose a reason for hiding this comment

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

👍 Nice BFS solution

""" 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)
Space Complexity: O(N)
where N - number of vertices/nodes, E - number of edges
"""
pass
if not dislikes:
return True

belongs_to_group = [False for i in range(len(dislikes))]
queue = collections.deque()

queue.append(1)
belongs_to_group[1] = "group_a"

while queue:
current_dog = queue.popleft()
for unwanted_dog in dislikes[current_dog]:
if belongs_to_group[unwanted_dog] == belongs_to_group[current_dog]:
return False
elif belongs_to_group[unwanted_dog] == False:
belongs_to_group[unwanted_dog] = "group_b" if belongs_to_group[current_dog] == "group_a" else "group_a"
queue.append(unwanted_dog)

return True


116 changes: 71 additions & 45 deletions tests/test_possible_bipartition.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,103 @@
from graphs.possible_bipartition import possible_bipartition


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

# Act
answer = possible_bipartition(dislikes)

# Assert
assert answer


def test_example_1():
# Arrange
dislikes = [ [],
[2, 3],
[1, 4],
[1],
[2]
]
dislikes = [[],
[2, 3],
[1, 4],
[1],
[2]
]

# Act
answer = possible_bipartition(dislikes)

# Assert
assert answer


def test_example_2():
# Arrange
dislikes = [ [],
[2, 3],
[1, 3],
[1, 2]
]
dislikes = [[],
[2, 3],
[1, 3],
[1, 2]
]

# Act
answer = possible_bipartition(dislikes)

# Assert
assert not answer


def test_example_r():
# Arrange
dislikes = [ [],
[2, 5],
[1, 3],
[2, 4],
[3, 5],
[1, 4]
]
dislikes = [[],
[2, 5],
[1, 3],
[2, 4],
[3, 5],
[1, 4]
]

# Act
answer = possible_bipartition(dislikes)

# Assert
assert not answer


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

# Act
answer = possible_bipartition(dislikes)

# Assert
assert answer


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

# Act
answer = possible_bipartition(dislikes)
Expand All @@ -83,19 +108,20 @@ def test_will_return_false_for_graph_which_cannot_be_bipartitioned():

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],
[2, 5],
[1, 3],
[0, 2, 4],
[3, 5],
[1, 4],
[0],
[8],
[7]
]

# Act
answer = possible_bipartition(dislikes)
Expand Down