2019-02-11 09:52:14 +00:00
|
|
|
"""
|
2020-06-25 15:54:41 +00:00
|
|
|
https://en.wikipedia.org/wiki/Breadth-first_search
|
2019-07-10 20:00:30 +00:00
|
|
|
pseudo-code:
|
2020-06-25 15:54:41 +00:00
|
|
|
breadth_first_search(graph G, start vertex s):
|
2019-02-11 09:52:14 +00:00
|
|
|
// all nodes initially unexplored
|
|
|
|
mark s as explored
|
|
|
|
let Q = queue data structure, initialized with s
|
|
|
|
while Q is non-empty:
|
|
|
|
remove the first node of Q, call it v
|
|
|
|
for each edge(v, w): // for w in graph[v]
|
|
|
|
if w unexplored:
|
|
|
|
mark w as explored
|
|
|
|
add w to Q (at the end)
|
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
2020-06-25 15:54:41 +00:00
|
|
|
|
2022-10-28 20:27:39 +00:00
|
|
|
from collections import deque
|
2021-10-30 11:06:25 +00:00
|
|
|
from queue import Queue
|
2022-10-28 20:27:39 +00:00
|
|
|
from timeit import timeit
|
2021-10-30 11:06:25 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
G = {
|
|
|
|
"A": ["B", "C"],
|
|
|
|
"B": ["A", "D", "E"],
|
|
|
|
"C": ["A", "F"],
|
|
|
|
"D": ["B"],
|
|
|
|
"E": ["B", "F"],
|
|
|
|
"F": ["C", "E"],
|
|
|
|
}
|
2019-02-11 09:52:14 +00:00
|
|
|
|
|
|
|
|
2022-10-28 20:27:39 +00:00
|
|
|
def breadth_first_search(graph: dict, start: str) -> list[str]:
|
2019-07-25 07:49:00 +00:00
|
|
|
"""
|
2022-10-28 20:27:39 +00:00
|
|
|
Implementation of breadth first search using queue.Queue.
|
|
|
|
|
|
|
|
>>> ''.join(breadth_first_search(G, 'A'))
|
2019-07-25 07:49:00 +00:00
|
|
|
'ABCDEF'
|
|
|
|
"""
|
2020-06-25 15:54:41 +00:00
|
|
|
explored = {start}
|
2022-10-28 20:27:39 +00:00
|
|
|
result = [start]
|
2021-11-08 18:18:30 +00:00
|
|
|
queue: Queue = Queue()
|
2021-10-30 11:06:25 +00:00
|
|
|
queue.put(start)
|
|
|
|
while not queue.empty():
|
|
|
|
v = queue.get()
|
2019-02-11 09:52:14 +00:00
|
|
|
for w in graph[v]:
|
|
|
|
if w not in explored:
|
|
|
|
explored.add(w)
|
2022-10-28 20:27:39 +00:00
|
|
|
result.append(w)
|
2021-10-30 11:06:25 +00:00
|
|
|
queue.put(w)
|
2022-10-28 20:27:39 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def breadth_first_search_with_deque(graph: dict, start: str) -> list[str]:
|
|
|
|
"""
|
|
|
|
Implementation of breadth first search using collection.queue.
|
|
|
|
|
|
|
|
>>> ''.join(breadth_first_search_with_deque(G, 'A'))
|
|
|
|
'ABCDEF'
|
|
|
|
"""
|
|
|
|
visited = {start}
|
|
|
|
result = [start]
|
|
|
|
queue = deque([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
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
2019-02-11 09:52:14 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2021-11-08 18:18:30 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
2022-10-28 20:27:39 +00:00
|
|
|
|
|
|
|
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
|