diff --git a/graphs/possible_bipartition.py b/graphs/possible_bipartition.py index ca55677..2bc2a6c 100644 --- a/graphs/possible_bipartition.py +++ b/graphs/possible_bipartition.py @@ -1,6 +1,16 @@ # Can be used for BFS from collections import deque +def bipartition_helper(counter_list, key, dislikes): + if len(counter_list) % 2 != 0 and counter_list[0] == key: + return False + if key in counter_list: + return True + for value in dislikes[key]: + if bipartition_helper(counter_list + [key], value, dislikes) == False: + return False + return True + def possible_bipartition(dislikes): """ Will return True or False if the given graph can be bipartitioned without neighboring nodes put @@ -8,5 +18,8 @@ def possible_bipartition(dislikes): Time Complexity: ? Space Complexity: ? """ - pass + for key in dislikes.keys(): + if bipartition_helper(list(), key, dislikes) == False: + return False + return True