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 <johnlaw.po@gmail.com>

Co-authored-by: John Law <johnlaw.po@gmail.com>
This commit is contained in:
Casper Rysgaard 2021-10-30 13:06:25 +02:00 committed by GitHub
parent 3a4cc7e310
commit e6cf13cc03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View File

@ -3,6 +3,8 @@
""" Author: OMKAR PATHAK """ """ Author: OMKAR PATHAK """
from __future__ import annotations from __future__ import annotations
from queue import Queue
class Graph: class Graph:
def __init__(self) -> None: def __init__(self) -> None:
@ -51,19 +53,19 @@ class Graph:
visited = set() visited = set()
# create a first in first out queue to store all the vertices for BFS # 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 # mark the source node as visited and enqueue it
visited.add(start_vertex) visited.add(start_vertex)
queue.append(start_vertex) queue.put(start_vertex)
while queue: while not queue.empty():
vertex = queue.pop(0) vertex = queue.get()
# loop through all adjacent vertex and enqueue it if not yet visited # loop through all adjacent vertex and enqueue it if not yet visited
for adjacent_vertex in self.vertices[vertex]: for adjacent_vertex in self.vertices[vertex]:
if adjacent_vertex not in visited: if adjacent_vertex not in visited:
queue.append(adjacent_vertex) queue.put(adjacent_vertex)
visited.add(adjacent_vertex) visited.add(adjacent_vertex)
return visited return visited

View File

@ -14,6 +14,8 @@ while Q is non-empty:
""" """
from __future__ import annotations from __future__ import annotations
from queue import Queue
G = { G = {
"A": ["B", "C"], "A": ["B", "C"],
"B": ["A", "D", "E"], "B": ["A", "D", "E"],
@ -30,13 +32,14 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:
'ABCDEF' 'ABCDEF'
""" """
explored = {start} explored = {start}
queue = [start] queue = Queue()
while queue: queue.put(start)
v = queue.pop(0) # queue.popleft() while not queue.empty():
v = queue.get()
for w in graph[v]: for w in graph[v]:
if w not in explored: if w not in explored:
explored.add(w) explored.add(w)
queue.append(w) queue.put(w)
return explored return explored

View File

@ -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, # 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 # or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set. # vertices of same set.
from queue import Queue
def checkBipartite(graph): def checkBipartite(graph):
queue = [] queue = Queue()
visited = [False] * len(graph) visited = [False] * len(graph)
color = [-1] * len(graph) color = [-1] * len(graph)
def bfs(): def bfs():
while queue: while not queue.empty():
u = queue.pop(0) u = queue.get()
visited[u] = True visited[u] = True
for neighbour in graph[u]: for neighbour in graph[u]:
@ -23,7 +26,7 @@ def checkBipartite(graph):
if color[neighbour] == -1: if color[neighbour] == -1:
color[neighbour] = 1 - color[u] color[neighbour] = 1 - color[u]
queue.append(neighbour) queue.put(neighbour)
elif color[neighbour] == color[u]: elif color[neighbour] == color[u]:
return False return False
@ -32,7 +35,7 @@ def checkBipartite(graph):
for i in range(len(graph)): for i in range(len(graph)):
if not visited[i]: if not visited[i]:
queue.append(i) queue.put(i)
color[i] = 0 color[i] = 0
if bfs() is False: if bfs() is False:
return False return False