diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index ca55677..b23bbab 100644 --- a/graphs/possible_bipartition.py +++ b/graphs/possible_bipartition.py @@ -1,12 +1,33 @@ # Can be used for BFS -from collections import deque +from collections import deque +import collections 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) + 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 + diff --git a/tests/test_possible_bipartition.py b/tests/test_possible_bipartition.py index 88ba735..ddb6fa4 100644 --- a/tests/test_possible_bipartition.py +++ b/tests/test_possible_bipartition.py @@ -1,14 +1,35 @@ 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) @@ -16,13 +37,14 @@ def test_example_1(): # 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) @@ -30,15 +52,16 @@ def test_example_2(): # 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) @@ -46,16 +69,17 @@ def test_example_r(): # 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) @@ -63,16 +87,17 @@ def test_will_return_true_for_a_graph_which_can_be_bipartitioned(): # 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) @@ -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)