The Need for Fairness
If you start at Node A and it has neighbors B, C, D, you must explore all three before exploring B's neighbors. A Queue (FIFO) guarantees that earlier discovered nodes are processed before later ones.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Breadth-First Search (BFS) is a core graph algorithm that systematically explores layers of a graph. A Queue is fundamentally required to maintain this level-by-level exploration order.
If you start at Node A and it has neighbors B, C, D, you must explore all three before exploring B's neighbors. A Queue (FIFO) guarantees that earlier discovered nodes are processed before later ones.
Enqueue the start node. Loop while the queue is not empty: Dequeue a node, process it, mark it visited, and enqueue all its unvisited neighbors. Repeat.
Because of the Queue's FIFO nature, you are guaranteed that the first time you discover a node, you have found the absolute shortest path to it (in an unweighted graph).