Proofs: 0-1 BFS
These are the human proofs of 0-1 BFS, at full length.
0-1 BFS returns the distances and a shortest-path tree full
This proof shows: correctness, termination.
Let have weights and source . A copy in the deque is a pair , where is the value of when line L2, L10 or L11 pushed it. Line L4 pops a copy, and line L5 skips it when is already finished.
Fact 1 (labels are path weights). At every moment, if then for some walk from to , so . Also, for with , held at the moment of the last change of , and has not changed since.
Proof. Line L2 sets , the weight of the empty walk. Line L9 sets , which is the weight of a walk to extended by the arc . The second claim holds because line L9 runs only while is scanned, after line L6 finished , and the label of a finished vertex never changes: by Fact 3 below, after is finished no relaxation can lower .
Fact 2 (invariant D). Before each test of line L3, if the keys of the copies in , front to back, are with , then . Moreover, while a vertex is scanned after the pop of a copy with key , every key in lies in and the keys stay sorted.
Proof. At the start holds one copy, so (D) holds. Suppose (D) holds and line L4 pops the copy with key . The rest of is sorted with keys in . If line L5 skips the copy, (D) holds again for the rest. Otherwise is scanned. We show below that the key of the popped copy equals , so each relaxation sets . A push to the front of key (when ) puts a key no larger than every key in at the front, and it is still at least , so stays sorted in . A push to the back of key (when ) puts a key at least every key in at the back, and it is , so stays sorted in . When the scan ends, the front key is at least and every key is at most , so (D) holds.
It remains to see that the popped copy that is not skipped has . Each push of follows a strict drop of , so the copies of ever pushed have strictly decreasing keys, and the last one has key . None of them was popped and scanned before, because is not finished. All of them are therefore still in , since a copy leaves only through line L4. The popped copy has the least key in , so ; and every copy of has key at least , so .
Fact 3 (scans in key order). Let be the vertices in the order line L6 finishes them, with labels at those moments. Then , and after is finished, no relaxation lowers .
Proof. By Fact 2, the popped keys never decrease: each pop takes the least key, and the scan of a vertex with key only pushes keys at least . The finished labels are popped keys, so they are nondecreasing. After is finished with label , each later relaxation sets a label to (a later finished label) , which is not less than , so the strict test of line L8 fails for .
Lemma (finished labels are distances). When line L6 finishes , .
Proof. Suppose not, and let be the first vertex finished with . By Fact 1, , so and there is a shortest path from to . Since ( is finished first, with as all weights are nonnegative), the path leaves the set of vertices finished before . Let be the first arc of with and . By the choice of , , and when was scanned line L8 made , where the last equality is optimal substructure along . So by Fact 1, and a copy of with key was pushed (or already had that label and a copy with that key). That copy is still in or has been popped; it was not popped, since and a pop of a copy with key would have finished . Weights are nonnegative, so . But was popped with the least key in , which is at most , so , a contradiction.
Termination. Each pop removes one copy, and each push follows a successful relaxation of line L8. A vertex is scanned at most once, by lines L5 and L6, so line L8 runs at most times, and at most copies are ever pushed. So the loop of line L3 runs at most times and stops.
Theorem. When ZERO-ONE-BFS returns, for every , and for every with , the parent satisfies , and following parents from reaches .
Proof. If is reachable, every vertex on a shortest path to is finished: by induction along the path, when a vertex is finished its successor gets a finite label and a copy, and that copy is popped before is empty. So is finished, and the lemma gives . If is not reachable, Fact 1 shows stays , which is . For the parents, Fact 1 gives at the last change of , and was finished before that change, with its final label. So was finished strictly before , and following parents walks back through vertices finished earlier and earlier. It cannot repeat a vertex, and the only finished vertex without a parent is , so it reaches .