From 50ed2ba9af399724fe9e61c0abeea95c3cd43405 Mon Sep 17 00:00:00 2001 From: chrisss111 Date: Tue, 19 Jul 2022 23:47:39 -0400 Subject: [PATCH] completed exercise --- .idea/.gitignore | 3 ++ .idea/Bipartition-Graph.iml | 14 ++++++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 +++ graphs/possible_bipartition.py | 49 +++++++++++++++++-- 7 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Bipartition-Graph.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Bipartition-Graph.iml b/.idea/Bipartition-Graph.iml new file mode 100644 index 0000000..8e5446a --- /dev/null +++ b/.idea/Bipartition-Graph.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6aec03f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f975647 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index ca55677..3dd0a08 100644 --- a/graphs/possible_bipartition.py +++ b/graphs/possible_bipartition.py @@ -5,8 +5,51 @@ 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) """ - pass + + if not dislikes: + return True + + queue = deque() + dogs = set() + group_1 = set() + group_2 = set() + + if dislikes[0]: + start = 0 + else: + start = 1 + + queue.append(start) + dogs.add(start) + group_1.add(start) + + while len(queue): + current = queue.popleft() + + for disliked_dog in dislikes[current]: + if disliked_dog not in dogs: + dogs.add(disliked_dog) + queue.append(disliked_dog) + if current in group_1: + group_2.add(disliked_dog) + else: + group_1.add(disliked_dog) + elif disliked_dog in dogs: + if current in group_1 and disliked_dog in group_1: + return False + elif current in group_2 and disliked_dog in group_2: + return False + + return True + + + + + + + +