Graph Theory37 sections · 1633 units
Open in Course

The BFS Algorithm Step 2

Initialization

Pick your starting node. Call it ss. Create a queue and push ss into it. Mark ss as visited in your visited array. Set distance[s] to 00 because the source is 00 steps from itself.

Now you are ready to run the main BFS loop. The queue has one node, the visited array has one true entry, and the distance array has one finite value. Everything else is waiting to be discovered. This initialization ensures BFS starts correctly and maintains its invariants throughout execution.