Skip to content

Commit e6cf13c

Browse files
Crowtonpoyea
andauthored
Update queue implementation (#5388)
* Update queue implementation Popping the first element of a list takes O(n) time. Using a cyclic queue takes O(1) time. * Add queue changes from extra files * Update indentation * Add empty line between imports * Fix lines * Apply suggestions from code review Co-authored-by: John Law <[email protected]> Co-authored-by: John Law <[email protected]>
1 parent 3a4cc7e commit e6cf13c

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

graphs/breadth_first_search.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
""" Author: OMKAR PATHAK """
44
from __future__ import annotations
55

6+
from queue import Queue
7+
68

79
class Graph:
810
def __init__(self) -> None:
@@ -51,19 +53,19 @@ def bfs(self, start_vertex: int) -> set[int]:
5153
visited = set()
5254

5355
# create a first in first out queue to store all the vertices for BFS
54-
queue = []
56+
queue = Queue()
5557

5658
# mark the source node as visited and enqueue it
5759
visited.add(start_vertex)
58-
queue.append(start_vertex)
60+
queue.put(start_vertex)
5961

60-
while queue:
61-
vertex = queue.pop(0)
62+
while not queue.empty():
63+
vertex = queue.get()
6264

6365
# loop through all adjacent vertex and enqueue it if not yet visited
6466
for adjacent_vertex in self.vertices[vertex]:
6567
if adjacent_vertex not in visited:
66-
queue.append(adjacent_vertex)
68+
queue.put(adjacent_vertex)
6769
visited.add(adjacent_vertex)
6870
return visited
6971

graphs/breadth_first_search_2.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"""
1515
from __future__ import annotations
1616

17+
from queue import Queue
18+
1719
G = {
1820
"A": ["B", "C"],
1921
"B": ["A", "D", "E"],
@@ -30,13 +32,14 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:
3032
'ABCDEF'
3133
"""
3234
explored = {start}
33-
queue = [start]
34-
while queue:
35-
v = queue.pop(0) # queue.popleft()
35+
queue = Queue()
36+
queue.put(start)
37+
while not queue.empty():
38+
v = queue.get()
3639
for w in graph[v]:
3740
if w not in explored:
3841
explored.add(w)
39-
queue.append(w)
42+
queue.put(w)
4043
return explored
4144

4245

graphs/check_bipartite_graph_bfs.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
77
# or u belongs to V and v to U. We can also say that there is no edge that connects
88
# vertices of same set.
9+
from queue import Queue
10+
11+
912
def checkBipartite(graph):
10-
queue = []
13+
queue = Queue()
1114
visited = [False] * len(graph)
1215
color = [-1] * len(graph)
1316

1417
def bfs():
15-
while queue:
16-
u = queue.pop(0)
18+
while not queue.empty():
19+
u = queue.get()
1720
visited[u] = True
1821

1922
for neighbour in graph[u]:
@@ -23,7 +26,7 @@ def bfs():
2326

2427
if color[neighbour] == -1:
2528
color[neighbour] = 1 - color[u]
26-
queue.append(neighbour)
29+
queue.put(neighbour)
2730

2831
elif color[neighbour] == color[u]:
2932
return False
@@ -32,7 +35,7 @@ def bfs():
3235

3336
for i in range(len(graph)):
3437
if not visited[i]:
35-
queue.append(i)
38+
queue.put(i)
3639
color[i] = 0
3740
if bfs() is False:
3841
return False

0 commit comments

Comments
 (0)