Proofs: Breadth-first search
These are the human proofs of Breadth-first search, at full length.
Breadth-first search computes the shortest-path distances full
This proof shows: correctness, termination.
Let be a directed or undirected graph and . For an undirected graph, read each edge as the two directed edges and . Let be the least number of edges on a path from to , and if there is no path. We use .
Lemma 1 (edge bound). For each edge , .
Proof. If , there is nothing to prove. Else take a path from to with edges, and add the edge . This is a walk from to with edges. A walk contains a path with at most as many edges, so .
Lemma 2 (upper bound and single entry). At each moment of the run:
- for each ;
- a finite value of never changes;
- each vertex enters the queue at most one time, and it enters exactly when its distance becomes finite.
Proof. Lines L1 and L2 set and for , so statement 1 holds, and line L3 puts , the only vertex with a finite distance, in the queue. After that, only line L8 writes to . It runs only if (line L7), so it never changes a finite value, which is statement 2. It sets for an edge , and by the induction hypothesis and Lemma 1, , which keeps statement 1. Line L10 enqueues exactly when line L8 makes its distance finite. Because this happens at most one time for each by statement 2, statement 3 holds.
Lemma 3 (the queue is sorted). While the loop body of lines L5 to L10 handles a vertex , let be followed by the queue from front to back. Outside the loop body, let be the queue. In both cases, write for the sequence. Then
Proof. By induction on the steps of the run. After line L3, the sequence is , and the claim holds. There are three kinds of steps.
- Line L5. The sequence becomes with . This is the same sequence of vertices, so the claim still holds.
- Line L10. The vertex goes to the back, with and . Before the step, , so the sequence stays sorted. The new last value is , so the last inequality still holds.
- End of the loop body. The vertex leaves the sequence. A sorted sequence stays sorted when its first item is removed. If the rest is not empty, , and is the new first item.
No other line changes the queue or a finite distance, by Lemma 2.
Corollary 4 (order of scanning). The vertices are dequeued in non-decreasing order of . If is dequeued before , then .
Proof. When is dequeued, it is the first item of the sequence of Lemma 3, so is at most the distance of each vertex in the queue at that moment. The next vertex dequeued is in the queue at that moment, or is enqueued later during the scan of with distance . In both cases its distance is at least . Distances do not change after they are finite (Lemma 2), so the claim follows by induction along the order of dequeues.
Termination. By Lemma 2, at most vertices enter the queue. Each pass of the loop of line L4 removes one of them. So the loop runs at most times, and each pass runs the loop of line L6 over a finite list. When the loop stops, the queue is empty, so each vertex that was discovered has also been dequeued and scanned.
Theorem. When BFS returns, for each . For each with and , and .
Proof. Assume that some vertex has at the end. Among these vertices, choose with the least . By Lemma 2, , so is finite. Also , because . Take a shortest path from to , and let be the vertex just before on it. Then and . By the choice of , , which is finite. So was discovered, and by the termination argument it was dequeued and scanned. Thus
Look at the moment when the scan of reaches in line L7. There are three cases.
- at this moment. Then line L8 sets , and by Lemma 2 this value stays to the end. This contradicts (1).
- was already dequeued. Then was dequeued before , and Corollary 4 gives . This contradicts (1).
- is in the queue. Then is in the sequence of Lemma 3, whose first item is , so . This contradicts (1).
A vertex with a finite distance was enqueued by Lemma 2, so it is in the queue or already dequeued, and the three cases cover all possibilities. So no such exists, and for each .
For the parents, let be reachable. Then is finite, so line L8 ran for during the scan of some , and line L9 set at the same time, with and . No later step changes , because line L9 runs together with line L8. With the first part, . By induction on , the chain reaches after exactly steps, and read backwards it is a path from to with edges, which is a shortest path.