Queues (FIFO) are essential for exploring graphs and trees level-by-level. Breadth-first search processes all nodes at distance k before distance k+1, naturally maintained by a queue. This is fundamental for shortest-path problems in unweighted graphs.
Implement BFS from scratch on a few graph problems (find shortest path, shortest cycle, all reachable nodes). Observe how the queue ensures exploration order and why depth-first search (using a stack) would visit in a different order.
You already understand that a queue processes elements in first-in, first-out (FIFO) order and that breadth-first search explores a graph layer by layer. This topic connects those two ideas: the queue is not just a convenient choice for BFS — it is the mechanism that makes level-by-level exploration work. When you enqueue the starting node and then repeatedly dequeue a node, process it, and enqueue its unvisited neighbors, the FIFO discipline guarantees that all nodes at distance 1 are processed before any node at distance 2, all at distance 2 before distance 3, and so on. No other data structure produces this ordering naturally.
Consider a concrete example: level-order traversal of a binary tree. You start by enqueuing the root. Then you enter a loop: dequeue a node, record its value, and enqueue its left and right children (if they exist). Because the queue preserves insertion order, the root's children (level 1) are processed before the root's grandchildren (level 2). If you want to know which nodes belong to each level — useful for problems like "find the maximum value at each depth" — you can track the queue's size at the start of each iteration. That size tells you exactly how many nodes are at the current level. Dequeue that many nodes, enqueue all their children, and you have cleanly separated one level from the next.
This same pattern extends from trees to arbitrary graphs with one critical addition: a visited set. In a tree, every node has exactly one path from the root, so you never encounter the same node twice. In a general graph, multiple paths can lead to the same node, so without marking nodes as visited when you first enqueue them, you would process them repeatedly and potentially loop forever in cyclic graphs. The rule is simple: mark a node as visited the moment you enqueue it, not when you dequeue it. Marking at enqueue time prevents duplicate entries in the queue and ensures each node is processed exactly once.
The practical payoff of BFS via a queue is shortest paths in unweighted graphs. Because BFS processes nodes in order of their distance from the source, the first time you reach any node is guaranteed to be via a shortest path. This is why BFS solves problems like "minimum number of moves to reach a target" or "fewest hops between two routers." If you substituted a stack for the queue, you would get depth-first search instead — exploring one branch deeply before backtracking — which does not preserve distance ordering and cannot guarantee shortest paths. The choice of data structure is the entire difference between BFS and DFS, making the queue the essential ingredient in level-order exploration.
No topics depend on this one yet.