Explore the graph layer by layer from the source.
def bfs(graph, start):
visited = []
queue = [start]
while queue:
node = queue.pop(0) # Dequeue
visited.append(node) # Visit
for nb in graph[node]:
if nb not in visited:
queue.append(nb) # Enqueue
Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.