The algorithm
- Compute a topological order. If none exists, stop: the graph has a cycle and this method does not apply.
- Set the source's label to 0 and every other to infinity.
- Visit the vertices in topological order; at each one, relax every arc out of it.
- For the longest path, reverse the comparison in the relaxation step and change nothing else.
Theorem · One pass sufficesOn an acyclic digraph processed in topological order, d[v] equals the shortest distance from the source at the moment v is visited, and each arc is relaxed exactly once.
By induction along the order. The source's label is 0 and correct. Let v be the next vertex, and let s, ..., u, v be a shortest path to it.
Every arc of that path goes forwards in the topological order, so u precedes v and was visited earlier. By hypothesis d[u] was correct when u was visited, and the arc from u to v was relaxed then, giving d[v] ≤ d[u] + w, which is the true distance. The upper-bound invariant says it is no smaller.
Each arc is relaxed when its tail is visited, and each vertex is visited once, so the arc count is exact.
□
Nothing in that argument mentions the sign of a weight. A negative arc changes which number is smaller and changes nothing about the order in which vertices are visited, which is why this is the one shortest-path method on the course that handles negative weights at no extra cost at all.
Example · Seven arcs, one pass, two answersThe lab opens on 1>2 3, 1>3 2, 2>4 4, 3>4 1, 4>5 2, 3>5 7, 5>6 1. A topological order is 1, 3, 2, 4, 5, 6. The shortest pass gives 0, 3, 2, 3, 5, 6 in 7 relaxations, one per arc, by the route 1, 3, 4, 5, 6. The longest pass, on the same 7 relaxations, gives 0, 3, 2, 7, 9, 10 by the route 1, 3, 5, 6. Both are confirmed by the rounds method on the shortest reading.
Slack, and the backward pass that produces it
Read the longest reading as a project: each arc is a task with a duration and each vertex is a milestone. The longest distance to a vertex is the earliest it can happen. The latest it may happen without delaying the end is computed the other way, and the difference is the slack — how much the corresponding task may slip for free.
Three details in that backward pass all produce something that looks like slack when they are wrong. It must be seeded ONLY at the far end, and at the far end's own extreme distance. Its extremum is the opposite of the forward pass's: under the longest reading the backward pass takes a minimum over successors. And the subtraction goes the arc's own way, latest of the head minus the weight. Get any of the three wrong and the answer is a vector of plausible non-negative numbers.
On the opening project every vertex has slack 0 under the longest reading, because every vertex lies on a critical path: the latest vector equals the earliest vector exactly. On the lab's wide preset 1>2 5, 2>3 5, 3>6 5, 1>4 7, 4>5 4, 5>6 3, 1>6 2 the longest distances are 0, 5, 10, 7, 11, 15 and the slack is 0, 0, 0, 1, 1, 0: the route through 4 and 5 finishes at 14 where the critical route finishes at 15, so those two milestones may each slip by one.
The shortest reading has a slack of its own and it means something different: on the opening project it is 0, 4, 0, 0, 0, 0, so vertex 2 is four longer than the cheapest route needs. Slack is never negative under either reading, which is a cheap check that the backward pass was done correctly.
Where the settling schedule goes wrong and this one does not
Example · Four vertices, one negative arcOn 1>2 2, 1>3 3, 3>2 -2, 2>4 1 the topological order is 1, 3, 2, 4 and the one pass gives 0, 1, 3, 2 in 4 relaxations. The rounds method agrees. The nearest-unfinished-first schedule gives 0, 1, 3, 3: it settled vertex 2 at label 2, relaxed the arc out of it from that label, and by the time vertex 2 improved to 1 it had already been declared finished. The lab prints all three.
The gap is one at vertex 4, which is small enough to be missed and large enough to be wrong. It is the clearest demonstration on this course that settling is an assumption rather than an optimisation: the topological schedule settles too, in the sense that d[v] is final when v is visited, but it settles in an order the arcs themselves justify.
The graph this does not work on, and how it says so
On 1>2 3, 2>3 2, 3>1 1, 3>4 5 there is no topological order, and the lab returns no distances rather than numbers. That refusal is the right behaviour and it is worth seeing why the longest reading in particular cannot be rescued: negate every weight and the cycle 1, 2, 3 becomes a cycle of weight -6, so the rounds method reports it as a negative cycle. A longest walk on a graph with a cycle does not exist, and the trick that computes it on a DAG does not extend.
Measured against proved. On the opening project: 7 relaxations for the shortest reading and 7 for the longest, on 7 arcs, plus one topological sort. That is Θ(V + E) for that graph. The bound is Θ(V + E) on every acyclic graph, and the proof is that each arc is relaxed when its tail is visited and each vertex is visited once — so unlike the rounds method, the measurement here cannot vary with the arc order at all.