mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
ed4c92d98a
* Type annotations for `breadth_first_search.py` * Type annotations for `breadth_first_search_2.py` * Remove from excluded in mypy.ini * Add doctest.testmod() * Type annotations for `graphs/check_cycle.py` * Type annotations for `graphs/greedy_min_vertex_cover.py` * Remove from excluded in mypy.ini
51 lines
1.1 KiB
Python
51 lines
1.1 KiB
Python
"""
|
|
https://en.wikipedia.org/wiki/Breadth-first_search
|
|
pseudo-code:
|
|
breadth_first_search(graph G, start vertex s):
|
|
// 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)
|
|
"""
|
|
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"],
|
|
}
|
|
|
|
|
|
def breadth_first_search(graph: dict, start: str) -> set[str]:
|
|
"""
|
|
>>> ''.join(sorted(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
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
print(breadth_first_search(G, "A"))
|