mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
9200a2e543
* from __future__ import annotations * fixup! from __future__ import annotations * fixup! from __future__ import annotations * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
45 lines
1.0 KiB
Python
45 lines
1.0 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
|
|
|
|
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 = [start]
|
|
while queue:
|
|
v = queue.pop(0) # queue.popleft()
|
|
for w in graph[v]:
|
|
if w not in explored:
|
|
explored.add(w)
|
|
queue.append(w)
|
|
return explored
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(breadth_first_search(G, "A"))
|