Interactive Lab · DSA & Algorithms
Half of graph interview questions are really one question: find the shortest path. Two ways to explore. Breadth-first search (a queue) spreads out evenly, it visits every cell one step away, then every cell two steps away, in perfect rings, so the first time it touches the target, it's by the shortest route, guaranteed. Depth-first search (a stack) plunges down one corridor as far as it can before backing up, it'll find a path, but not necessarily the short one. Toggle the two and scrub the exploration to watch the frontier move.
🎬 Watch the 75-second explainer, then flood the maze yourself below ↓
▶ The playground, pick a strategy, then scrub the search outward
Green S is the start, red T the target, dark cells are walls. In BFS each cell is tinted by its distance from S (the rings); in DFS by the order it was visited. The bold line is the path found, for BFS it's the shortest.
BFS is the shortest-path workhorse wherever edges are unweighted: LinkedIn's degrees-of-separation, web crawlers exploring level by level, garbage collectors marking reachable objects, and package managers resolving dependency layers. Its guarantee, first touch IS the shortest path, is why it, not DFS, powers “how are these two users connected?”.
The code that's actually running, the only difference is queue vs stack
In BFS, drag the ring slider up one notch at a time and watch the coloured frontier stay a perfect diamond (bent around walls). Predict: the step at which the ring first touches Tthat number is the shortest-path length, because every cell at distance d is reached before any cell at distance d+1. Now switch to DFS: it reaches T too, but count its path, it's longer. Why does swapping the queue for a stack change a shortest-path finder into a just-get-there finder? And when would you actually prefer DFS?
The takeaway
O(V + E): on a grid, linear in the number of cells. The magic isn't speed, it's the order.