diff --git a/graphs/breadth_first_search.py b/graphs/breadth_first_search.py index 7c626429e5c0..ae4bdf7e949a 100644 --- a/graphs/breadth_first_search.py +++ b/graphs/breadth_first_search.py @@ -2,6 +2,7 @@ """ Author: OMKAR PATHAK """ from __future__ import annotations +from queue import Queue class Graph: @@ -51,19 +52,19 @@ def bfs(self, start_vertex: int) -> set[int]: visited = set() # create a first in first out queue to store all the vertices for BFS - queue = [] + queue = Queue() # mark the source node as visited and enqueue it visited.add(start_vertex) - queue.append(start_vertex) + queue.put(start_vertex) while queue: - vertex = queue.pop(0) + vertex = queue.get() # loop through all adjacent vertex and enqueue it if not yet visited for adjacent_vertex in self.vertices[vertex]: if adjacent_vertex not in visited: - queue.append(adjacent_vertex) + queue.put(adjacent_vertex) visited.add(adjacent_vertex) return visited