Algorithm Search⌘K

Proofs: Ramalingam-Reps algorithm

These are the human proofs of Ramalingam-Reps algorithm, at full length.

The Ramalingam-Reps algorithm keeps the exact distances full

This proof shows: correctness.

All weights are positive. Write dd for the stored distances and dist\mathrm{dist} for the true distances in the current graph; dist(s)=0\mathrm{dist}(s) = 0, and dist(v)=∞\mathrm{dist}(v) = \infty if vv is not reachable. The invariant is:

  • (D) d(v)=dist(v)d(v) = \mathrm{dist}(v) for every vv.
  • (C) For every v≠sv \ne s with d(v)<∞d(v) < \infty, c(v)c(v) is the number of edges (u,v)(u, v) with d(u)+w(u,v)=d(v)d(u) + w(u, v) = d(v).

Line L1 establishes both by Dijkstra's algorithm and direct counting. We use two standard facts.

Fact 1 (the Bellman conditions). Let DD be a function on VV with D(s)=0D(s) = 0, D(v)≤D(u)+w(u,v)D(v) \le D(u) + w(u, v) for every edge, and such that every v≠sv \ne s with D(v)<∞D(v) < \infty has an in-edge (u,v)(u, v) with D(u)+w(u,v)=D(v)D(u) + w(u, v) = D(v). Then D=distD = \mathrm{dist}.

Proof. The first condition gives D≤distD \le \mathrm{dist} by induction along a shortest path. For the reverse, follow tight in-edges back from vv; the values DD strictly decrease along the way since weights are positive, so the walk does not repeat a vertex and must end at ss, giving a path of length D(v)D(v), so dist(v)≤D(v)\mathrm{dist}(v) \le D(v). □\square

Fact 2 (Dijkstra with fixed sources). Let S⊆VS \subseteq V, and let every vertex x∈Sx \in S start with a value D0(x)D_0(x), the least of the values D(y)+w(y,x)D(y) + w(y, x) over edges from vertices y∉Sy \notin S (fixed values). Dijkstra's algorithm restricted to edges inside SS, started from all of SS with these values, ends with D(x)=min⁡(D0(x),min⁡(y,x),y∈SD(y)+w(y,x))D(x) = \min(D_0(x), \min_{(y, x), y \in S} D(y) + w(y, x)) for all x∈Sx \in S, and D(x)D(x) is the least length of a walk that starts at a vertex y∉Sy \notin S with value D(y)D(y) and then stays in SS.

Proof. This is Dijkstra's algorithm on the graph SS plus a super source joined to each x∈Sx \in S by an edge of weight D0(x)D_0(x); all weights are non-negative. □\square

Both facts imply the counts: once (D) holds, (C) holds if the counts are recomputed wherever a tight edge may have appeared or disappeared, and we check that below.

Lemma 1 (decrease). DECREASE(u,v,w)(u, v, w) keeps (D) and (C).

Proof. Let dist′\mathrm{dist}' be the new distances and dd the old ones. Distances can only drop. If d(u)+w≥d(v)d(u) + w \ge d(v), then no path through the new edge is shorter than d(v)d(v), and no vertex beyond vv improves: a path through (u,v)(u, v) to zz has length at least d(u)+w+dist(v,z)≥d(v)+dist(v,z)≥d(z)d(u) + w + \mathrm{dist}(v, z) \ge d(v) + \mathrm{dist}(v, z) \ge d(z). So dist′=d\mathrm{dist}' = d. The edge (u,v)(u, v) is the only one that can change its tightness, and line L2 adds one to c(v)c(v) exactly when it becomes tight; it was not tight before, since its old weight was larger or it was absent.

Otherwise, let CC be the set of vertices with dist′(z)<d(z)\mathrm{dist}'(z) < d(z). Every shortest path to such a zz in the new graph uses (u,v)(u, v), so dist′(z)=d(u)+w+dist′(v,z)\mathrm{dist}'(z) = d(u) + w + \mathrm{dist}'(v, z), where dist′(v,z)\mathrm{dist}'(v, z) is a distance in the new graph, and the part of the path after vv consists of vertices of CC (each prefix of it is also shortest and uses the new edge, so its endpoint improved). Lines L3 and L4 are Dijkstra's algorithm from vv with initial value d(u)+wd(u) + w, where every other vertex starts at its old value d(z)d(z) and only lower values are accepted. By the standard proof of Dijkstra's algorithm on non-negative weights, it ends with D(z)=min⁡(d(z),d(u)+w+dist′(v,z))D(z) = \min(d(z), d(u) + w + \mathrm{dist}'(v, z)), which is dist′(z)\mathrm{dist}'(z) for every zz. So (D) holds.

For (C): an edge (x,z)(x, z) changes its tightness only if d(x)d(x) or d(z)d(z) changed. Line L5 recounts every changed vertex from all its in-edges. For an unchanged zz and a changed in-neighbor xx, it removes the old tightness of (x,z)(x, z) and adds the new one, which is exactly the change of c(z)c(z). □\square

Lemma 2 (increase: phase 1). Suppose (u,v)(u, v) was tight and c(v)−1=0c(v) - 1 = 0 after line L6. Let dist′\mathrm{dist}' be the new distances. Phase 1 (line L7) ends with A={z:dist′(z)>d(z)}A = \{z : \mathrm{dist}'(z) > d(z)\}.

Proof. Distances can only grow. Let TT be the set of edges tight for dd in the old graph; with positive weights TT is acyclic, since dd strictly increases along a tight edge.

First, every z∈Az \in A has dist′(z)>d(z)\mathrm{dist}'(z) > d(z). When zz enters AA, every tight in-edge of zz in the new graph comes from a vertex of AA (the counter counts the tight in-edges not yet known to come from AA, and for vv, the edge (u,v)(u, v) no longer exists or is no longer tight). Order AA by entry time. By induction, suppose all earlier vertices of AA have dist′>d\mathrm{dist}' > d. A shortest new path to zz of length d(z)d(z) would end with an edge (x,z)(x, z) with dist′(x)+w(x,z)=d(z)\mathrm{dist}'(x) + w(x, z) = d(z) and dist′(x)≥d(x)\mathrm{dist}'(x) \ge d(x), and d(x)+w(x,z)≥d(z)d(x) + w(x, z) \ge d(z) by (D), so (x,z)(x, z) is tight with dist′(x)=d(x)\mathrm{dist}'(x) = d(x). But tight edges into zz come from AA, whose vertices entered before zz or are zz itself; this contradicts the induction hypothesis (and x=zx = z is impossible). So dist′(z)>d(z)\mathrm{dist}'(z) > d(z).

Second, every zz with dist′(z)>d(z)\mathrm{dist}'(z) > d(z) is in AA. Go by induction on d(z)d(z). Such a zz is not ss and has d(z)<∞d(z) < \infty. Each tight in-edge (x,z)(x, z) of the old graph either is (u,v)(u, v), which no longer counts, or comes from a vertex xx with d(x)<d(z)d(x) < d(z). If dist′(x)=d(x)\mathrm{dist}'(x) = d(x) for such an xx, then dist′(z)≤d(x)+w(x,z)=d(z)\mathrm{dist}'(z) \le d(x) + w(x, z) = d(z), a contradiction. So every such xx has dist′(x)>d(x)\mathrm{dist}'(x) > d(x) and, by induction, is in AA. When xx was taken from AA, phase 1 decreased c(z)c(z) for the edge (x,z)(x, z). After all of them, together with the decrement of line L6 if z=vz = v, c(z)=0c(z) = 0 and zz entered AA. □\square

Lemma 3 (increase: phase 2). After line L9, d=dist′d = \mathrm{dist}'.

Proof. Outside AA the distances do not change by Lemma 2. For x∈Ax \in A, a shortest new path to xx has a last vertex y∉Ay \notin A (possibly ss, which is never in AA), after which it stays in AA. So dist′(x)\mathrm{dist}'(x) is the least length of a walk that starts at some y∉Ay \notin A with value dist′(y)=d(y)\mathrm{dist}'(y) = d(y) and stays in AA after it. Line L8 gives each x∈Ax \in A the best one-edge start, and line L9 is Dijkstra's algorithm inside AA, so by Fact 2 it computes exactly this least length; if no such walk exists, xx keeps ∞\infty, which is right. □\square

Lemma 4 (increase: counts). After line L10, (C) holds.

Proof. Tightness of an edge (x,z)(x, z) changes only if (x,z)=(u,v)(x, z) = (u, v), or x∈Ax \in A, or z∈Az \in A. Vertices of AA are recounted from all their in-edges. For z∉Az \notin A: the change of (u,v)(u, v) was handled by line L6 if z=vz = v. For an edge (x,z)(x, z) with x∈Ax \in A, phase 1 removed its old tightness from c(z)c(z) when xx was taken from AA, and line L10 adds one if it is tight for the new values. If the edge was not tight before, phase 1 did not touch it, and line L10 adds its new tightness. If line L6 returns early, no distance changes, since vv keeps a tight in-edge from a vertex whose distance does not change (by the argument of Lemma 2 with AA empty), and only the edge (u,v)(u, v) changed its tightness, which line L6 recorded. □\square

Theorem. After every update, (D) and (C) hold, so QUERY returns dist(v)\mathrm{dist}(v).

Proof. Every update is a decrease (an insertion, or a smaller weight) or an increase (a deletion, or a larger weight). An increase of a non-tight edge changes no distance by Fact 1, since dd still satisfies the Bellman conditions, and changes no count, since a non-tight edge stays non-tight. The other cases are Lemmas 1 to 4. By induction on the updates, (D) and (C) hold after each of them. ■\blacksquare

Back to Ramalingam-Reps algorithm