From 640038d8628b0e8dd7bb978b98d6ccfae4e3b0c2 Mon Sep 17 00:00:00 2001 From: verasazonova Date: Mon, 26 Dec 2022 16:18:25 -0800 Subject: [PATCH] VeraSa C17 --- graphs/possible_bipartition.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index ca55677..6254947 100644 --- a/graphs/possible_bipartition.py +++ b/graphs/possible_bipartition.py @@ -8,5 +8,35 @@ def possible_bipartition(dislikes): Time Complexity: ? Space Complexity: ? """ - pass + + if dislikes == None: + return True + + color = {} + + for dog in dislikes: + if dog in color: + continue + + q = deque() + q.append(dog) + color[dog] = 0 + + while (len(q) > 0): + dog = q.pop() + current_color = color[dog] + fight_set = dislikes[dog] + for fight_dog in fight_set: + if fight_dog in color: + if color[fight_dog] == current_color: + return False + else: + if current_color == 0: + next_color = 1 + else: + next_color = 0 + color[fight_dog] = next_color + q.append(fight_dog) + return True +