Breadth-First Search (BFS) explores layer by layer, like a ripple in a pond. It visits all immediate neighbors before going deeper.
We begin at 'A'. We add it to our Queue and mark it as 'Seen'.
We take 'A' out of the Queue. Now we look for its neighbors.
'A' has neighbors 'B' and 'C'. We add BOTH to the Queue.
Queue says 'B' is next (FIFO). We take 'B' out.
'B' has neighbors 'D' and 'E'. Add them to the back of the Queue.
We don't go to D yet! 'C' has been waiting longer. Queue Fairness.
'C' has neighbor 'F'. Add it to the Queue.
Now we simply drain the queue: Process D, then E, then F. All nodes visited layer by layer.
BFS is perfect for finding the shortest path in unweighted graphs because it guarantees finding the closest target first.
BFS guarantees finding the shortest path in unweighted graphs.