From ee5d71e5bc4b74ccaa23cb49400eaf4e65f1ce15 Mon Sep 17 00:00:00 2001 From: Nataliia Getlin Date: Sat, 8 Jan 2022 23:52:48 -0800 Subject: [PATCH 1/3] Complete assignment --- graphs/possible_bipartition.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index ca55677..8fbc3cb 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 len(dislikes) == 0: + 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 len(queue) > 0: + 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 + From 74bab48ed10df35c0120873a3b4e0ab5dc21c024 Mon Sep 17 00:00:00 2001 From: Nataliia Getlin Date: Sun, 9 Jan 2022 10:16:04 -0800 Subject: [PATCH 2/3] Slight refactor --- graphs/possible_bipartition.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index 8fbc3cb..b23bbab 100644 --- a/graphs/possible_bipartition.py +++ b/graphs/possible_bipartition.py @@ -10,7 +10,7 @@ def possible_bipartition(dislikes): Space Complexity: O(N) where N - number of vertices/nodes, E - number of edges """ - if len(dislikes) == 0: + if not dislikes: return True belongs_to_group = [False for i in range(len(dislikes))] @@ -19,7 +19,7 @@ def possible_bipartition(dislikes): queue.append(1) belongs_to_group[1] = "group_a" - while len(queue) > 0: + while queue: current_dog = queue.popleft() for unwanted_dog in dislikes[current_dog]: if belongs_to_group[unwanted_dog] == belongs_to_group[current_dog]: From 0b2a5561fc48dc7510d0defdd0907b1916c327fd Mon Sep 17 00:00:00 2001 From: Nataliia Getlin Date: Sun, 9 Jan 2022 10:16:04 -0800 Subject: [PATCH 3/3] Slight refactor --- graphs/possible_bipartition.py | 4 +- tests/test_possible_bipartition.py | 116 ++++++++++++++++++----------- 2 files changed, 73 insertions(+), 47 deletions(-) diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index 8fbc3cb..b23bbab 100644 --- a/graphs/possible_bipartition.py +++ b/graphs/possible_bipartition.py @@ -10,7 +10,7 @@ def possible_bipartition(dislikes): Space Complexity: O(N) where N - number of vertices/nodes, E - number of edges """ - if len(dislikes) == 0: + if not dislikes: return True belongs_to_group = [False for i in range(len(dislikes))] @@ -19,7 +19,7 @@ def possible_bipartition(dislikes): queue.append(1) belongs_to_group[1] = "group_a" - while len(queue) > 0: + while queue: current_dog = queue.popleft() for unwanted_dog in dislikes[current_dog]: if belongs_to_group[unwanted_dog] == belongs_to_group[current_dog]: 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)