Why does BFS find shortest paths? — The ripple analogy
BFS finds shortest paths in unweighted graphs, but why should breadth-first exploration guarantee optimality? Using the analogy of ripples on a pond, we build intuition for the correctness of BFS, see why DFS fails, and understand Dijkstra as a generalization to variable-speed ripples.
Every algorithms textbook states matter-of-factly that BFS (breadth-first search) finds shortest paths in unweighted graphs. But why does exploring breadth-first yield shortest paths? Why does DFS fail where BFS succeeds? In this article, we use the analogy of ripples on water to reach the heart of the matter.
1 The ripple analogy
Drop a stone into a still pond. Circular ripples spread outward, expanding at the same speed in every direction. Because the speed is uniform, the first ripple to reach any given point has necessarily traveled the shortest distance.
BFS does exactly the same thing.
Starting from the source and "sending out ripples," layers form naturally:
Distance 0: (the source itself)
Distance 1: (neighbors of )
Distance 2: (unvisited neighbors of distance-1 vertices)
Distance 3: (unvisited neighbors of distance-2 vertices)
The queue structure of BFS reproduces this layer-by-layer expansion precisely. All vertices at distance are processed before any vertex at distance .
2 Correctness of BFS – why the ripple never lies
Let us state the core guarantee.
The intuitive reason is as follows.
If could be reached in fewer than steps, it would already have been discovered before the layer- processing began.
Because BFS uses a FIFO queue, vertices with smaller distances are always processed first.
The crucial point is that in an unweighted graph, every edge has the same cost (namely 1). The ripple travels at uniform speed, so the first arrival is guaranteed to be the shortest route.
3 A counterexample: DFS does not find shortest paths
DFS (depth-first search) plunges as deep as possible before backtracking. Its behavior is nothing like a ripple.
The fundamental issue with DFS is that by prioritizing depth, it may skip nearby vertices in favor of distant ones. It behaves not like a ripple but like a person navigating a maze by keeping one hand on the wall.
4 Weighted graphs – Dijkstra as a variable-speed ripple
When edges carry weights (distances, costs), the simple ripple of BFS no longer suffices. Different edges have different costs, so the assumption "one step = one unit of distance" breaks down.
This is where Dijkstra's algorithm enters the picture.
The ripple analogy also explains why non-negative weights are essential. If a negative-weight edge existed, it would be possible to reach a location more cheaply after the wavefront has already passed through it – a ripple flowing backward. The arrival order would no longer correspond to the cost order, and the greedy choice would break down.
5 The meaning of
The time complexity of BFS is , meaning that each vertex is visited once and each edge is inspected once.
Dijkstra's algorithm, with a binary heap, runs in . The overhead compared to BFS is the price of the heap operations – the computational cost of allowing the ripple to travel at different speeds along different edges.
6 Summary
BFS is a uniform-speed ripple. Because every edge has the same cost, the first arrival at any vertex is the shortest path.
DFS is a wall-following walk through a maze; it makes no shortest-path guarantee.
Dijkstra is a variable-speed ripple, correctly computing shortest paths when all weights are non-negative.
When negative edges are present, the ripple analogy breaks down, and a fundamentally different approach – such as Bellman–Ford – is required.
In the next article, we examine the most elegant structure within graph theory: the tree.
Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.