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 for the stored distances and for the true distances in the current graph; , and if is not reachable. The invariant is:
- (D) for every .
- (C) For every with , is the number of edges with .
Line L1 establishes both by Dijkstra's algorithm and direct counting. We use two standard facts.
Fact 1 (the Bellman conditions). Let be a function on with , for every edge, and such that every with has an in-edge with . Then .
Proof. The first condition gives by induction along a shortest path. For the reverse, follow tight in-edges back from ; the values strictly decrease along the way since weights are positive, so the walk does not repeat a vertex and must end at , giving a path of length , so .
Fact 2 (Dijkstra with fixed sources). Let , and let every vertex start with a value , the least of the values over edges from vertices (fixed values). Dijkstra's algorithm restricted to edges inside , started from all of with these values, ends with for all , and is the least length of a walk that starts at a vertex with value and then stays in .
Proof. This is Dijkstra's algorithm on the graph plus a super source joined to each by an edge of weight ; all weights are non-negative.
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 keeps (D) and (C).
Proof. Let be the new distances and the old ones. Distances can only drop. If , then no path through the new edge is shorter than , and no vertex beyond improves: a path through to has length at least . So . The edge is the only one that can change its tightness, and line L2 adds one to exactly when it becomes tight; it was not tight before, since its old weight was larger or it was absent.
Otherwise, let be the set of vertices with . Every shortest path to such a in the new graph uses , so , where is a distance in the new graph, and the part of the path after consists of vertices of (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 with initial value , where every other vertex starts at its old value and only lower values are accepted. By the standard proof of Dijkstra's algorithm on non-negative weights, it ends with , which is for every . So (D) holds.
For (C): an edge changes its tightness only if or changed. Line L5 recounts every changed vertex from all its in-edges. For an unchanged and a changed in-neighbor , it removes the old tightness of and adds the new one, which is exactly the change of .
Lemma 2 (increase: phase 1). Suppose was tight and after line L6. Let be the new distances. Phase 1 (line L7) ends with .
Proof. Distances can only grow. Let be the set of edges tight for in the old graph; with positive weights is acyclic, since strictly increases along a tight edge.
First, every has . When enters , every tight in-edge of in the new graph comes from a vertex of (the counter counts the tight in-edges not yet known to come from , and for , the edge no longer exists or is no longer tight). Order by entry time. By induction, suppose all earlier vertices of have . A shortest new path to of length would end with an edge with and , and by (D), so is tight with . But tight edges into come from , whose vertices entered before or are itself; this contradicts the induction hypothesis (and is impossible). So .
Second, every with is in . Go by induction on . Such a is not and has . Each tight in-edge of the old graph either is , which no longer counts, or comes from a vertex with . If for such an , then , a contradiction. So every such has and, by induction, is in . When was taken from , phase 1 decreased for the edge . After all of them, together with the decrement of line L6 if , and entered .
Lemma 3 (increase: phase 2). After line L9, .
Proof. Outside the distances do not change by Lemma 2. For , a shortest new path to has a last vertex (possibly , which is never in ), after which it stays in . So is the least length of a walk that starts at some with value and stays in after it. Line L8 gives each the best one-edge start, and line L9 is Dijkstra's algorithm inside , so by Fact 2 it computes exactly this least length; if no such walk exists, keeps , which is right.
Lemma 4 (increase: counts). After line L10, (C) holds.
Proof. Tightness of an edge changes only if , or , or . Vertices of are recounted from all their in-edges. For : the change of was handled by line L6 if . For an edge with , phase 1 removed its old tightness from when was taken from , 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 keeps a tight in-edge from a vertex whose distance does not change (by the argument of Lemma 2 with empty), and only the edge changed its tightness, which line L6 recorded.
Theorem. After every update, (D) and (C) hold, so QUERY returns .
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 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.