Algorithm Search⌘K

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 G=(V,E)G = (V, E) have weights w:E→{0,1}w : E \to \{0, 1\} and source ss. A copy in the deque DD is a pair (v,k)(v, k), where kk is the value of d[v]d[v] when line L2, L10 or L11 pushed it. Line L4 pops a copy, and line L5 skips it when vv is already finished.

Fact 1 (labels are path weights). At every moment, if d[v]<∞d[v] < \infty then d[v]=w(p)d[v] = w(p) for some walk pp from ss to vv, so d[v]≥δ(s,v)d[v] \ge \delta(s, v). Also, for v≠sv \ne s with d[v]<∞d[v] < \infty, d[v]=d[π[v]]+w(π[v],v)d[v] = d[\pi[v]] + w(\pi[v], v) held at the moment of the last change of d[v]d[v], and d[π[v]]d[\pi[v]] has not changed since.

Proof. Line L2 sets d[s]=0d[s] = 0, the weight of the empty walk. Line L9 sets d[v]=d[u]+w(u,v)d[v] = d[u] + w(u, v), which is the weight of a walk to uu extended by the arc (u,v)(u, v). The second claim holds because line L9 runs only while uu is scanned, after line L6 finished uu, and the label of a finished vertex never changes: by Fact 3 below, after uu is finished no relaxation can lower d[u]d[u]. □\square

Fact 2 (invariant D). Before each test of line L3, if the keys of the copies in DD, front to back, are k1,…,krk_1, \ldots, k_r with r≥1r \ge 1, then k1≤k2≤⋯≤kr≤k1+1k_1 \le k_2 \le \cdots \le k_r \le k_1 + 1. Moreover, while a vertex uu is scanned after the pop of a copy with key kk, every key in DD lies in [k,k+1][k, k + 1] and the keys stay sorted.

Proof. At the start DD holds one copy, so (D) holds. Suppose (D) holds and line L4 pops the copy with key k=k1k = k_1. The rest of DD is sorted with keys in [k,k+1][k, k + 1]. If line L5 skips the copy, (D) holds again for the rest. Otherwise uu is scanned. We show below that the key of the popped copy equals d[u]d[u], so each relaxation sets d[v]=k+w(u,v)d[v] = k + w(u, v). A push to the front of key kk (when w=0w = 0) puts a key no larger than every key in DD at the front, and it is still at least kk, so DD stays sorted in [k,k+1][k, k + 1]. A push to the back of key k+1k + 1 (when w=1w = 1) puts a key at least every key in DD at the back, and it is k+1k + 1, so DD stays sorted in [k,k+1][k, k + 1]. When the scan ends, the front key k1′k_1' is at least kk and every key is at most k+1≤k1′+1k + 1 \le k_1' + 1, so (D) holds.

It remains to see that the popped copy (u,k)(u, k) that is not skipped has k=d[u]k = d[u]. Each push of uu follows a strict drop of d[u]d[u], so the copies of uu ever pushed have strictly decreasing keys, and the last one has key d[u]d[u]. None of them was popped and scanned before, because uu is not finished. All of them are therefore still in DD, since a copy leaves DD only through line L4. The popped copy has the least key in DD, so k≤d[u]k \le d[u]; and every copy of uu has key at least d[u]d[u], so k≥d[u]k \ge d[u]. □\square

Fact 3 (scans in key order). Let u1,u2,…u_1, u_2, \ldots be the vertices in the order line L6 finishes them, with labels d[u1],d[u2],…d[u_1], d[u_2], \ldots at those moments. Then d[u1]≤d[u2]≤⋯d[u_1] \le d[u_2] \le \cdots, and after uiu_i is finished, no relaxation lowers d[ui]d[u_i].

Proof. By Fact 2, the popped keys never decrease: each pop takes the least key, and the scan of a vertex with key kk only pushes keys at least kk. The finished labels are popped keys, so they are nondecreasing. After uiu_i is finished with label d[ui]d[u_i], each later relaxation sets a label to (a later finished label) +w≥d[ui]+ w \ge d[u_i], which is not less than d[ui]d[u_i], so the strict test of line L8 fails for v=uiv = u_i. □\square

Lemma (finished labels are distances). When line L6 finishes uu, d[u]=δ(s,u)d[u] = \delta(s, u).

Proof. Suppose not, and let uu be the first vertex finished with d[u]≠δ(s,u)d[u] \ne \delta(s, u). By Fact 1, d[u]>δ(s,u)d[u] > \delta(s, u), so δ(s,u)<∞\delta(s, u) < \infty and there is a shortest path pp from ss to uu. Since u≠su \ne s (ss is finished first, with d[s]=0=δ(s,s)d[s] = 0 = \delta(s, s) as all weights are nonnegative), the path pp leaves the set FF of vertices finished before uu. Let (x,y)(x, y) be the first arc of pp with x∈Fx \in F and y∉Fy \notin F. By the choice of uu, d[x]=δ(s,x)d[x] = \delta(s, x), and when xx was scanned line L8 made d[y]≤d[x]+w(x,y)=δ(s,y)d[y] \le d[x] + w(x, y) = \delta(s, y), where the last equality is optimal substructure along pp. So d[y]=δ(s,y)d[y] = \delta(s, y) by Fact 1, and a copy of yy with key δ(s,y)\delta(s, y) was pushed (or yy already had that label and a copy with that key). That copy is still in DD or has been popped; it was not popped, since y∉Fy \notin F and a pop of a copy with key d[y]d[y] would have finished yy. Weights are nonnegative, so δ(s,y)≤δ(s,u)<d[u]\delta(s, y) \le \delta(s, u) < d[u]. But uu was popped with the least key in DD, which is at most δ(s,y)\delta(s, y), so d[u]≤δ(s,y)d[u] \le \delta(s, y), a contradiction. □\square

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 mm times, and at most m+1m + 1 copies are ever pushed. So the loop of line L3 runs at most m+1m + 1 times and stops.

Theorem. When ZERO-ONE-BFS returns, d[v]=δ(s,v)d[v] = \delta(s, v) for every v∈Vv \in V, and for every v≠sv \ne s with d[v]<∞d[v] < \infty, the parent π[v]\pi[v] satisfies d[π[v]]+w(π[v],v)=d[v]d[\pi[v]] + w(\pi[v], v) = d[v], and following parents from vv reaches ss.

Proof. If vv is reachable, every vertex on a shortest path to vv 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 DD is empty. So vv is finished, and the lemma gives d[v]=δ(s,v)d[v] = \delta(s, v). If vv is not reachable, Fact 1 shows d[v]d[v] stays ∞\infty, which is δ(s,v)\delta(s, v). For the parents, Fact 1 gives d[v]=d[π[v]]+w(π[v],v)d[v] = d[\pi[v]] + w(\pi[v], v) at the last change of d[v]d[v], and π[v]\pi[v] was finished before that change, with its final label. So π[v]\pi[v] was finished strictly before vv, 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 ss, so it reaches ss. ■\blacksquare

Back to 0-1 BFS