From ea8a31cc598703c696f6cafd777b4da674d515df Mon Sep 17 00:00:00 2001 From: Raquel Mena Date: Mon, 24 Jan 2022 09:46:28 -0800 Subject: [PATCH] final submission --- graphs/possible_bipartition.py | 42 +++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index ca55677..d6723d5 100644 --- a/graphs/possible_bipartition.py +++ b/graphs/possible_bipartition.py @@ -1,12 +1,38 @@ # Can be used for BFS -from collections import deque +from collections import deque + 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: ? - """ - pass + if dislikes == []: + return True + + start_node = 0 + visted = [False] * len(dislikes) + q = deque() + q.append(start_node) + + group_a = [] + group_b = [] + + while q: + current = q.popleft() + visted[current] = True + if not dislikes[current]: + q.append(current + 1) + for i in dislikes[current]: + if visted[i] == False: + q.append(i) + + if current not in group_a: + if i in group_b: + return False + group_a.append(i) + else: + if i in group_a: + return False + group_b.append(i) + + return True + + pass