From 7730b542254a161ad47c030b0ad696df0926f55a Mon Sep 17 00:00:00 2001 From: Juliana DeCarvalho Sutherland Date: Sun, 16 Jan 2022 15:20:00 -0800 Subject: [PATCH] Rock - Juliana's Solution. All tests passing. --- graphs/possible_bipartition.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index ca55677..b9afaed 100644 --- a/graphs/possible_bipartition.py +++ b/graphs/possible_bipartition.py @@ -5,8 +5,28 @@ 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 + N = len(dislikes) + dogs = [None] * N + queued = deque() + for i in range(N): + if dogs[i] != None: + continue + queued.append((i, "Baker")) + while queued: + node, dog = queued.popleft() + puppy = "Baker" + if not dogs[node]: + dogs[node] = dog + for next in dislikes[node]: + if dogs[next] == dogs[node]: + return False + elif dogs[next] and dogs[next] != dogs[node]: + continue + elif not dogs[next] and dogs[node] == "Baker": + puppy = "Jpug" + queued.append((next, puppy)) + return True \ No newline at end of file