From 36b60fae7181f1a2b798ec06540baa248dd09454 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 28 Oct 2022 02:19:21 +0300 Subject: [PATCH 1/5] Cleanup the BFS --- graphs/breadth_first_search_2.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index 2f060a90d40d..4fb01d3e218d 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -14,7 +14,7 @@ """ from __future__ import annotations -from queue import Queue +from collections import deque G = { "A": ["B", "C"], @@ -26,21 +26,23 @@ } -def breadth_first_search(graph: dict, start: str) -> set[str]: +def breadth_first_search(graph: dict, start: str) -> list[str]: """ - >>> ''.join(sorted(breadth_first_search(G, 'A'))) + >>> ''.join(breadth_first_search(G, 'A')) 'ABCDEF' """ - explored = {start} - queue: 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.put(w) - return explored + visited = {start} + result = [start] + queue: deque = deque() + queue.append(start) + while queue: + v = queue.popleft() + for child in graph[v]: + if child not in visited: + visited.add(child) + result.append(child) + queue.append(child) + return result if __name__ == "__main__": From e16de020ae849c033a149847f32b1584c7d95484 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 28 Oct 2022 20:47:56 +0300 Subject: [PATCH 2/5] Add both functions and timeit --- graphs/breadth_first_search_2.py | 35 ++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index 4fb01d3e218d..4252fc1fceaa 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -15,6 +15,8 @@ from __future__ import annotations from collections import deque +from queue import Queue +from timeit import timeit G = { "A": ["B", "C"], @@ -31,10 +33,28 @@ def breadth_first_search(graph: dict, start: str) -> list[str]: >>> ''.join(breadth_first_search(G, 'A')) 'ABCDEF' """ + explored = {start} + result = [start] + queue: 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) + result.append(w) + queue.put(w) + return result + + +def breadth_first_search_with_deque(graph: dict, start: str) -> list[str]: + """ + >>> ''.join(breadth_first_search_with_deque(G, 'A')) + 'ABCDEF' + """ visited = {start} result = [start] - queue: deque = deque() - queue.append(start) + queue = deque([start]) while queue: v = queue.popleft() for child in graph[v]: @@ -45,8 +65,15 @@ def breadth_first_search(graph: dict, start: str) -> list[str]: return result +def benchmark_function(name: str) -> None: + setup = f"from __main__ import G, {name}" + number = 10000 + res = timeit(f"{name}(G, 'A')", setup=setup, number=number) + print(f"{name:<35} finished {number} runs in {res:.5f} seconds") + + if __name__ == "__main__": import doctest - doctest.testmod() - print(breadth_first_search(G, "A")) + benchmark_function('breadth_first_search') + benchmark_function('breadth_first_search_with_deque') From 58fc8df45e786e0f024d4e07ad83216ad88664af Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Oct 2022 17:49:57 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/breadth_first_search_2.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index 4252fc1fceaa..c35c4397e8d6 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -74,6 +74,7 @@ def benchmark_function(name: str) -> None: if __name__ == "__main__": import doctest + doctest.testmod() - benchmark_function('breadth_first_search') - benchmark_function('breadth_first_search_with_deque') + benchmark_function("breadth_first_search") + benchmark_function("breadth_first_search_with_deque") From ca0fc8f7d9ebb32d9ade77bf1333f9fc84d46d9a Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 28 Oct 2022 23:23:53 +0300 Subject: [PATCH 4/5] Add performace results as comment --- graphs/breadth_first_search_2.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index c35c4397e8d6..ada22a0e36b3 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -30,6 +30,8 @@ def breadth_first_search(graph: dict, start: str) -> list[str]: """ + Implementation of BFS using queue.Queue. + >>> ''.join(breadth_first_search(G, 'A')) 'ABCDEF' """ @@ -49,6 +51,8 @@ def breadth_first_search(graph: dict, start: str) -> list[str]: def breadth_first_search_with_deque(graph: dict, start: str) -> list[str]: """ + Implementation of BFS using collection.queue. + >>> ''.join(breadth_first_search_with_deque(G, 'A')) 'ABCDEF' """ @@ -76,5 +80,8 @@ def benchmark_function(name: str) -> None: import doctest doctest.testmod() + benchmark_function("breadth_first_search") benchmark_function("breadth_first_search_with_deque") + # breadth_first_search finished 10000 runs in 0.20999 seconds + # breadth_first_search_with_deque finished 10000 runs in 0.01421 seconds From 34d94b67b1d52647194ece7626cd82f1ae4e08fb Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 28 Oct 2022 22:26:58 +0200 Subject: [PATCH 5/5] Update breadth_first_search_2.py --- graphs/breadth_first_search_2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index ada22a0e36b3..a0b92b90b456 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -30,7 +30,7 @@ def breadth_first_search(graph: dict, start: str) -> list[str]: """ - Implementation of BFS using queue.Queue. + Implementation of breadth first search using queue.Queue. >>> ''.join(breadth_first_search(G, 'A')) 'ABCDEF' @@ -51,7 +51,7 @@ def breadth_first_search(graph: dict, start: str) -> list[str]: def breadth_first_search_with_deque(graph: dict, start: str) -> list[str]: """ - Implementation of BFS using collection.queue. + Implementation of breadth first search using collection.queue. >>> ''.join(breadth_first_search_with_deque(G, 'A')) 'ABCDEF'