Graph Theory37 sections · 1633 units
Open in Course

The BFS Strategy

Level by Level

BFS processes nodes in waves. Wave 11: the starting node. Wave 22: all nodes 11 step from the start. Wave 33: all nodes 22 steps from the start. Each wave completes before the next begins. You use a queue to enforce this order. Add the start node to the queue.

Remove it, process it, and add all its neighbors. Remove the first neighbor, process it, add its neighbors. The queue automatically groups nodes by distance because you process parents before children. This creates the layer-by-layer exploration pattern.

photo_۲۰۲۵-۱۲-۲۶_۰۱-۲۹-۴۲.jpg