Algorithm Search⌘K

Proofs: Thorup's linear-time undirected SSSP

These are the human proofs of Thorup's linear-time undirected SSSP, at full length.

Thorup's VISIT settles every vertex reachable from s at its distance full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be undirected with weights w:E→{1,2,…}w : E \to \{1, 2, \ldots\} and source ss, and let δ(v)=δ(s,v)\delta(v) = \delta(s, v). For a vertex vv and a level jj, write Cj(v)C_j(v) for the level-jj component that holds vv; for a component xx on level ii and j≥ij \ge i, write Cj(x)C_j(x) for the level-jj component that holds xx. Let R=Cr(s)R = C_r(s); every edge has weight below 2r2^r, so RR is the connected component of ss, and no edge joins RR to V∖RV \setminus R.

Fact 1 (crossing). If z∉Cj(x)z \notin C_j(x), every path from zz to a vertex of Cj(x)C_j(x) has an edge of weight at least 2j2^j.

Proof. If all its edges had weight below 2j2^j, all its vertices would be in one level-jj component, which would hold zz and meet Cj(x)C_j(x), so it would be Cj(x)C_j(x). □\square

Invariants. At every time, (I1) every settled vertex uu has D(u)=δ(u)D(u) = \delta(u) and the edges at uu have been relaxed in line L7; (I2) every D(u)D(u) is ∞\infty or the weight of a path from ss to uu, so D(u)≥δ(u)D(u) \ge \delta(u). (I2) is clear: D(s)=0D(s) = 0, and line L7 extends a path to vv by one edge. (I1) is shown below, in Lemma 4.

Lemma 2 (first unsettled vertex). Assume (I1). If uu is unsettled and δ(u)<∞\delta(u) < \infty, then on every shortest path PP from ss to uu the first unsettled vertex zz has D(z)=δ(z)D(z) = \delta(z).

Proof. If z=sz = s, D(s)=0=δ(s)D(s) = 0 = \delta(s), as weights are positive. Otherwise the vertex pp before zz on PP is settled, so by (I1) D(p)=δ(p)D(p) = \delta(p) and the edge {p,z}\{p, z\} was relaxed, giving D(z)≤δ(p)+w(p,z)=δ(z)D(z) \le \delta(p) + w(p, z) = \delta(z), since a prefix of a shortest path is shortest. With (I2), D(z)=δ(z)D(z) = \delta(z). □\square

The precondition. For a component xx on level ii and a vertex z∉xz \notin x, let ℓ(z,x)\ell(z, x) be the largest j∈{i,…,r}j \in \{i, \ldots, r\} with z∉Cj(x)z \notin C_j(x); if z∉Rz \notin R, let ℓ(z,x)=r\ell(z, x) = r. A call VISIT(xx, ii, hi) has the precondition at a given time if

  • (P1) when x≠Rx \ne R: every unsettled u∈xu \in x has D(u)≥hi−2iD(u) \ge \mathrm{hi} - 2^i, and
  • (P2) every unsettled z∉xz \notin x has D(z)+2ℓ(z,x)≥hiD(z) + 2^{\ell(z, x)} \ge \mathrm{hi}.

The root call VISIT(RR, rr, ∞\infty) has it trivially: (P1) does not apply, and a vertex z∉Rz \notin R has no path from ss, so D(z)=∞D(z) = \infty by (I2). For other calls, hi is a multiple of 2i2^i, by line L10.

Lemma 3 (the precondition holds for the whole call). If VISIT(xx, ii, hi) has the precondition when it starts, it has it at every time until it returns, provided (I1) holds.

Proof. While the call is active, every vertex settled is in xx, as every nested call is on a component inside xx. DD values only decrease, by line L7 at a settled vertex u∈xu \in x. By (P1), when x≠Rx \ne R, D(u)≥hi−2iD(u) \ge \mathrm{hi} - 2^i at the time uu is settled (it was unsettled just before). Consider a relaxation of {u,z}\{u, z\}.

If z∈xz \in x, the new value is D(u)+w(u,z)≥D(u)≥hi−2iD(u) + w(u, z) \ge D(u) \ge \mathrm{hi} - 2^i, so (P1) stays true.

If z∉xz \notin x, let ℓ=ℓ(z,x)≥i\ell = \ell(z, x) \ge i. Since u∈x⊆Cℓ(x)u \in x \subseteq C_\ell(x) and z∉Cℓ(x)z \notin C_\ell(x), Fact 1 gives w(u,z)≥2ℓw(u, z) \ge 2^\ell. The new value satisfies

D(z)+2ℓ≥hi−2i+2ℓ+2ℓ≥hi,D(z) + 2^\ell \ge \mathrm{hi} - 2^i + 2^\ell + 2^\ell \ge \mathrm{hi},

since 2ℓ≥2i2^\ell \ge 2^i. So (P2) stays true. For the root, (P2) concerns only vertices outside RR, which no edge reaches. □\square

Lemma 4 (children get the precondition). Suppose VISIT(xx, ii, hi) with i≥1i \ge 1 has the precondition, and line L10 calls VISIT(yy, i−1i - 1, hiy\mathrm{hi}_y) with y=Ci−1(v)y = C_{i-1}(v), b=⌊D(v)/2i−1⌋b = \lfloor D(v) / 2^{i-1} \rfloor and hiy=(b+1)2i−1\mathrm{hi}_y = (b + 1) 2^{i-1}, where vv has the least DD among the unsettled vertices of xx and D(v)<hiD(v) < \mathrm{hi}. Then the child call has the precondition.

Proof. (P1) for yy: every unsettled u∈y⊆xu \in y \subseteq x has D(u)≥D(v)≥b⋅2i−1=hiy−2i−1D(u) \ge D(v) \ge b \cdot 2^{i-1} = \mathrm{hi}_y - 2^{i-1}.

(P2) for yy, case z∈x∖yz \in x \setminus y: then z∈Ci(y)=xz \in C_i(y) = x and z∉Ci−1(y)=yz \notin C_{i-1}(y) = y, so ℓ(z,y)=i−1\ell(z, y) = i - 1. As above, D(z)≥D(v)≥b⋅2i−1D(z) \ge D(v) \ge b \cdot 2^{i-1}, so D(z)+2i−1≥hiyD(z) + 2^{i-1} \ge \mathrm{hi}_y.

(P2) for yy, case z∉xz \notin x: Cj(y)=Cj(x)C_j(y) = C_j(x) for j≥ij \ge i, so ℓ(z,y)=ℓ(z,x)\ell(z, y) = \ell(z, x), and by (P2) for xx, D(z)+2ℓ(z,y)≥hiD(z) + 2^{\ell(z, y)} \ge \mathrm{hi}. Finally hiy≤hi\mathrm{hi}_y \le \mathrm{hi}: if hi=∞\mathrm{hi} = \infty this is clear, and otherwise hi is a multiple of 2i2^i, hence of 2i−12^{i-1}, and b⋅2i−1≤D(v)<hib \cdot 2^{i-1} \le D(v) < \mathrm{hi} gives (b+1)2i−1≤hi(b + 1) 2^{i-1} \le \mathrm{hi}. □\square

Lemma 5 (settling is correct). Assume (I1) before line L6 runs in VISIT({v}\{v\}, 0, hi), and that this call has the precondition. Then D(v)=δ(v)D(v) = \delta(v), so (I1) still holds after lines L6 and L7.

Proof. The call comes from line L10 with i=1i = 1, so b=D(v)b = D(v) and hi=D(v)+1\mathrm{hi} = D(v) + 1; in particular D(v)<∞D(v) < \infty and δ(v)≤D(v)<∞\delta(v) \le D(v) < \infty. Suppose δ(v)<D(v)\delta(v) < D(v). Take a shortest path PP from ss to vv and its first unsettled vertex zz; by Lemma 2, D(z)=δ(z)D(z) = \delta(z), so z≠vz \ne v. The part of PP from zz to vv goes from z∉{v}z \notin \{v\} into Cℓ({v})C_{\ell}(\{v\}) with ℓ=ℓ(z,{v})\ell = \ell(z, \{v\}), so by Fact 1 it has an edge of weight at least 2ℓ2^\ell, and all its weights are positive. Hence, using (P2),

δ(v)≥δ(z)+2ℓ=D(z)+2ℓ≥hi=D(v)+1>D(v),\delta(v) \ge \delta(z) + 2^\ell = D(z) + 2^\ell \ge \mathrm{hi} = D(v) + 1 > D(v),

which contradicts δ(v)<D(v)\delta(v) < D(v). So D(v)=δ(v)D(v) = \delta(v). Line L7 then relaxes all edges at vv, so (I1) holds for vv. □\square

By induction over the sequence of calls, Lemmas 3, 4 and 5 show that every call has the precondition when it starts and that (I1) holds throughout.

Lemma 6 (each child call makes progress). Every call VISIT(yy, jj, hiy\mathrm{hi}_y) made in line L10 settles at least one vertex.

Proof. By induction on jj. When it is called, yy holds an unsettled vv with D(v)<hiyD(v) < \mathrm{hi}_y. If j=0j = 0, line L6 settles vv. If j≥1j \ge 1, the condition of line L8 holds, so the loop runs at least once and makes a call that settles a vertex by induction. □\square

Theorem. THORUP-SSSP terminates and returns D(v)=δ(v)D(v) = \delta(v) for every vv.

Proof. Termination: a vertex is settled only while unsettled, so at most nn vertices are ever settled. By Lemma 6 every iteration of a loop in line L8 settles a vertex, so each call runs at most nn iterations, and the depth of the calls is at most r+1r + 1. Hence the algorithm stops.

When the root call returns, the condition of line L8 fails for RR with hi=∞\mathrm{hi} = \infty, so every unsettled vertex of RR has D=∞D = \infty. If some uu with δ(u)<∞\delta(u) < \infty were unsettled, Lemma 2 would give an unsettled zz on a shortest path to uu with D(z)=δ(z)<∞D(z) = \delta(z) < \infty, and z∈Rz \in R; a contradiction. So every vertex reachable from ss is settled, and by (I1) has D(u)=δ(u)D(u) = \delta(u). A vertex not reachable from ss keeps D=∞=δ(u)D = \infty = \delta(u) by (I2). ■\blacksquare

Back to Thorup's linear-time undirected SSSP