From 02ecae716078f831b11cea439069a84aef2c28b2 Mon Sep 17 00:00:00 2001 From: Casper Rysgaard Date: Sun, 17 Oct 2021 21:49:49 +0200 Subject: [PATCH 1/6] Update queue implementation Popping the first element of a list takes O(n) time. Using a cyclic queue takes O(1) time. --- graphs/breadth_first_search_2.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index a90e963a4043..1efe569a70a4 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -13,15 +13,17 @@ add w to Q (at the end) """ from __future__ import annotations +from queue import Queue + G = { - "A": ["B", "C"], - "B": ["A", "D", "E"], - "C": ["A", "F"], - "D": ["B"], - "E": ["B", "F"], - "F": ["C", "E"], -} + "A": ["B", "C"], + "B": ["A", "D", "E"], + "C": ["A", "F"], + "D": ["B"], + "E": ["B", "F"], + "F": ["C", "E"], + } def breadth_first_search(graph: dict, start: str) -> set[str]: @@ -30,13 +32,14 @@ def breadth_first_search(graph: dict, start: str) -> set[str]: 'ABCDEF' """ explored = {start} - queue = [start] - while queue: - v = queue.pop(0) # queue.popleft() + queue = Queue() + queue.put(start) + while not queue.empty(): + v = queue.get() for w in graph[v]: if w not in explored: explored.add(w) - queue.append(w) + queue.put(w) return explored From e1cd0ec8832e2dcf1453bb1ddc81a8b245b785fd Mon Sep 17 00:00:00 2001 From: Casper Rysgaard Date: Tue, 19 Oct 2021 23:40:20 +0200 Subject: [PATCH 2/6] Add queue changes from extra files --- graphs/breadth_first_search.py | 9 +++++---- graphs/check_bipartite_graph_bfs.py | 13 ++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) 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 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 From 1756076ffefa63e05052b1039d7de9cc620a963b Mon Sep 17 00:00:00 2001 From: Casper Rysgaard Date: Tue, 19 Oct 2021 23:42:37 +0200 Subject: [PATCH 3/6] Update indentation --- graphs/breadth_first_search_2.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index 1efe569a70a4..b78ed622c21b 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -17,13 +17,13 @@ G = { - "A": ["B", "C"], - "B": ["A", "D", "E"], - "C": ["A", "F"], - "D": ["B"], - "E": ["B", "F"], - "F": ["C", "E"], - } + "A": ["B", "C"], + "B": ["A", "D", "E"], + "C": ["A", "F"], + "D": ["B"], + "E": ["B", "F"], + "F": ["C", "E"], +} def breadth_first_search(graph: dict, start: str) -> set[str]: From 1efcea455ae3b077d7abbbd4f311b68564fb0d96 Mon Sep 17 00:00:00 2001 From: Casper Rysgaard Date: Wed, 20 Oct 2021 10:35:13 +0200 Subject: [PATCH 4/6] Add empty line between imports --- graphs/breadth_first_search.py | 1 + graphs/breadth_first_search_2.py | 1 + 2 files changed, 2 insertions(+) diff --git a/graphs/breadth_first_search.py b/graphs/breadth_first_search.py index ae4bdf7e949a..40682d02920c 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 diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index b78ed622c21b..ef9bdbd76b70 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -13,6 +13,7 @@ add w to Q (at the end) """ from __future__ import annotations + from queue import Queue From 1aa2cfd3bb06faa7ebca16395df6378bc153c45f Mon Sep 17 00:00:00 2001 From: Casper Rysgaard Date: Sat, 23 Oct 2021 12:04:57 +0200 Subject: [PATCH 5/6] Fix lines --- graphs/breadth_first_search_2.py | 1 - 1 file changed, 1 deletion(-) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index ef9bdbd76b70..4c8b69faf656 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -16,7 +16,6 @@ from queue import Queue - G = { "A": ["B", "C"], "B": ["A", "D", "E"], From 700613e9329d0dc9dc82ff7697e5db0ef1bf2c49 Mon Sep 17 00:00:00 2001 From: Casper Rysgaard Date: Wed, 27 Oct 2021 12:46:26 +0200 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: John Law --- graphs/breadth_first_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/breadth_first_search.py b/graphs/breadth_first_search.py index 40682d02920c..9264f57b41b2 100644 --- a/graphs/breadth_first_search.py +++ b/graphs/breadth_first_search.py @@ -59,7 +59,7 @@ def bfs(self, start_vertex: int) -> set[int]: visited.add(start_vertex) queue.put(start_vertex) - while queue: + while not queue.empty(): vertex = queue.get() # loop through all adjacent vertex and enqueue it if not yet visited