Algorithm Search⌘K

Proofs: Even-Shiloach tree

These are the human proofs of Even-Shiloach tree, at full length.

The levels of an Even-Shiloach tree are the capped distances from the source full

This proof shows: correctness.

Let GG be the current graph, with positive integer weights, and let d(v)=d(s,v)d(v) = d(s, v) and dD(v)d_D(v) be as on the page. Write ℓ(v)\ell(v) for the level, with ∞\infty allowed. An edge (u,v)(u, v) supports vv if it is present and ℓ(u)+w(u,v)=ℓ(v)\ell(u) + w(u, v) = \ell(v).

We prove that the following invariants hold before and after every iteration of the loop of line L4, and at the start and end of every deletion.

  • (A) ℓ(s)=0\ell(s) = 0, and ℓ(v)≤dD(v)\ell(v) \le d_D(v) for every vv.
  • (B) For every present edge (u,v)(u, v) with v≠sv \ne s and ℓ(v)<∞\ell(v) < \infty: ℓ(v)≤ℓ(u)+w(u,v)\ell(v) \le \ell(u) + w(u, v).
  • (C) For every v≠sv \ne s with ℓ(v)<∞\ell(v) < \infty, no edge before p(v)p(v) in in(v)\mathrm{in}(v) supports vv.
  • (D) Every v≠sv \ne s with ℓ(v)<∞\ell(v) < \infty that is not supported by the edge at p(v)p(v) is in the queue QQ.

Initialization. Line L1 sets ℓ=dD\ell = d_D, so (A) holds. For a present edge (u,v)(u, v) with dD(v)<∞d_D(v) < \infty: if d(u)=∞d(u) = \infty or d(u)+w>Dd(u) + w > D, the right side of (B) is ∞\infty or larger than D≥ℓ(v)D \ge \ell(v); otherwise d(v)≤d(u)+wd(v) \le d(u) + w. So (B) holds. The pointer is set to the first supporting edge, which exists for a finite v≠sv \ne s because the last edge of a shortest path supports it; so (C) holds and (D) holds with an empty queue.

A deletion (lines L2 and L3). Deleting an edge can only increase distances, so dDd_D does not decrease and (A) still holds. (B) and (C) speak only about present edges or about edges that do not support, and a deleted edge supports nothing, so they still hold. The only vertex that may lose the support at its pointer is the head vv of the deleted edge, and line L3 puts it into QQ, so (D) holds.

An iteration of the loop. Let xx be taken from QQ. If x=sx = s or ℓ(x)=∞\ell(x) = \infty, nothing changes. Otherwise line L5 moves p(x)p(x) forward over edges that do not support xx, which keeps (C). If it stops at a supporting edge, xx leaves QQ and (D) holds for xx.

Otherwise no edge of in(x)\mathrm{in}(x) supports xx: the edges before the old pointer by (C), and the others by the scan. Together with (B), every present edge (u,x)(u, x) has ℓ(u)+w(u,x)≥ℓ(x)+1\ell(u) + w(u, x) \ge \ell(x) + 1, because levels and weights are integers. Line L6 sets ℓ(x)\ell(x) to ℓ(x)+1\ell(x) + 1.

  • (A) still holds for xx. Suppose dD(x)=ℓ(x)d_D(x) = \ell(x) before the raise, so d(x)=ℓ(x)≤Dd(x) = \ell(x) \le D is finite and x≠sx \ne s. Let (u,x)(u, x) be the last edge of a shortest path to xx; then d(u)=d(x)−w(u,x)≤Dd(u) = d(x) - w(u, x) \le D, so dD(u)=d(u)d_D(u) = d(u), and by (A) for uu, ℓ(u)+w(u,x)≤d(u)+w(u,x)=ℓ(x)\ell(u) + w(u, x) \le d(u) + w(u, x) = \ell(x). This contradicts the inequality above. So dD(x)≥ℓ(x)+1d_D(x) \ge \ell(x) + 1. If the new level exceeds DD, then dD(x)>Dd_D(x) > D, which means dD(x)=∞d_D(x) = \infty, and line L7 sets ℓ(x)=∞\ell(x) = \infty, still within (A).
  • (B) holds for the edges into xx, by the inequality above. For an edge (x,y)(x, y) out of xx, raising ℓ(x)\ell(x) only increases the right side of (B) for yy.
  • (C) holds for xx, since the pointer is reset to the first edge (or xx became ∞\infty). For a vertex y≠xy \ne x, raising ℓ(x)\ell(x) can only make an edge (x,y)(x, y) stop supporting yy, never start: if (x,y)(x, y) supported yy before, it does not after, and if it did not, then ℓ(x)+w>ℓ(y)\ell(x) + w > \ell(y) by (B), which stays true. So no edge before p(y)p(y) starts to support yy.
  • (D): if xx is still finite, line L7 puts it back into QQ. Line L8 puts every out-neighbor yy into QQ, which covers every yy whose support at p(y)p(y) was an edge from xx. Every other vertex keeps the support it had.

Termination. Each raise increases a level, levels are bounded by D+1D + 1 before they become ∞\infty, and each queue entry that does not raise a vertex removes one entry. So the loop ends.

Theorem. After each deletion, ℓ(v)=dD(v)\ell(v) = d_D(v) for every vertex vv.

Proof. When the queue is empty, by (D) every finite v≠sv \ne s is supported by the edge (u,v)(u, v) at p(v)p(v), so ℓ(u)=ℓ(v)−w(u,v)<ℓ(v)\ell(u) = \ell(v) - w(u, v) < \ell(v) and ℓ(u)\ell(u) is finite. Following the supporting edges from vv backward visits vertices with strictly decreasing finite levels, so it ends, and it can only end at ss, the one finite vertex that needs no support. This gives a path from ss to vv of length ℓ(v)\ell(v), so d(v)≤ℓ(v)d(v) \le \ell(v). By (A), ℓ(v)≤dD(v)\ell(v) \le d_D(v), and ℓ(v)\ell(v) is finite, so dD(v)=d(v)≤ℓ(v)≤dD(v)d_D(v) = d(v) \le \ell(v) \le d_D(v), and equality holds. If ℓ(v)=∞\ell(v) = \infty, then by (A) dD(v)=∞d_D(v) = \infty as well. Line L9 returns ℓ(v)\ell(v). ■\blacksquare

Back to Even-Shiloach tree