Breadth-First Search (BFS)
Explores the neighbor nodes first, before moving to the next level neighbors.
A
B
C
D
E
F
G
Queue
Empty
Status: Ready
Speed
Algorithm Logic
1function bfs(start):
2 q = [start]
3 visited = {start}
4 while q:
5 curr = q.dequeue()
6 visit(curr)
7 for neighbor in adj[curr]:
8 if neighbor not in visited:
9 visited.add(neighbor)
10 q.enqueue(neighbor)