MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / breadth_first_search

Function breadth_first_search

graphs/breadth_first_search_2.py:32–50  ·  view source on GitHub ↗

Implementation of breadth first search using queue.Queue. >>> ''.join(breadth_first_search(G, 'A')) 'ABCDEF'

(graph: dict, start: str)

Source from the content-addressed store, hash-verified

30
31
32def breadth_first_search(graph: dict, start: str) -> list[str]:
33 """
34 Implementation of breadth first search using queue.Queue.
35
36 >>> ''.join(breadth_first_search(G, 'A'))
37 'ABCDEF'
38 """
39 explored = {start}
40 result = [start]
41 queue: Queue = Queue()
42 queue.put(start)
43 while not queue.empty():
44 v = queue.get()
45 for w in graph[v]:
46 if w not in explored:
47 explored.add(w)
48 result.append(w)
49 queue.put(w)
50 return result
51
52
53def breadth_first_search_with_deque(graph: dict, start: str) -> list[str]:

Callers

nothing calls this directly

Calls 6

putMethod · 0.95
getMethod · 0.95
QueueClass · 0.90
emptyMethod · 0.45
addMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected