diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 00b771649b5d..b5203b4c5c7d 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -6,14 +6,17 @@ # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. +from queue import Queue + + def checkBipartite(graph): - queue = [] + queue = Queue() visited = [False] * len(graph) color = [-1] * len(graph) def bfs(): - while queue: - u = queue.pop(0) + while not queue.empty(): + u = queue.get() visited[u] = True for neighbour in graph[u]: @@ -23,7 +26,7 @@ def bfs(): if color[neighbour] == -1: color[neighbour] = 1 - color[u] - queue.append(neighbour) + queue.put(neighbour) elif color[neighbour] == color[u]: return False @@ -32,7 +35,7 @@ def bfs(): for i in range(len(graph)): if not visited[i]: - queue.append(i) + queue.put(i) color[i] = 0 if bfs() is False: return False