Definition · RelaxationGiven labels d and an edge from u to v of weight w, to relax that edge is to test whether d[u] + w is less than d[v], and if so to set d[v] = d[u] + w and record u as v's predecessor. The label d[v] is initialised to infinity for every vertex but the source, whose label is 0.
Theorem · The upper-bound invariantAt every moment during any sequence of relaxations, d[v] is either infinite or the length of some path from the source to v. In particular d[v] is never less than the true shortest distance to v.
By induction on the number of relaxations performed. Before any, only the source has a finite label and 0 is the length of the empty path.
Suppose the claim holds and the edge from u to v is relaxed, setting d[v] = d[u] + w. By hypothesis d[u] is the length of some path from the source to u; appending the edge gives a path to v of length exactly d[u] + w. So the new label is again the length of a path, and no path is shorter than the shortest one.
□
That theorem is why a schedule cannot be wrong in the direction of underestimating, and it is what makes the three schedules comparable. Each is a rule for which edge to relax next; none of them can produce a number below the truth; the only questions left are whether the labels ever reach the truth, and how much work that takes.
Three schedules
- Nearest unfinished first. Keep the unfinished vertices in a heap keyed by their current labels. Pop the smallest, declare it finished, and relax every edge out of it. This is Dijkstra's schedule.
- Linear scan. The same choice made by reading every vertex's label and taking the smallest unfinished one. No heap, V reads per step.
- Arc order. Ignore distance entirely: sweep the whole edge list relaxing everything, and repeat until a full sweep changes nothing.
Example · Six vertices, one source, three schedulesThe lab opens on 1-2 7, 1-3 9, 1-6 14, 2-3 10, 2-4 15, 3-4 11, 3-6 2, 4-5 6, 5-6 9 from vertex 1. All three schedules reach 0, 7, 9, 20, 20, 11, and so does an independently written Dijkstra from another Subject that knows nothing about this representation. The route to vertex 5 is 1, 3, 6, 5, at 9 + 2 + 9 = 20. The counts: heap 18 relaxations, 8 pushes, 8 pops, 15 key comparisons; scan 18 relaxations and 42 label reads; arc order 36 relaxations.
Two numbers in there are worth dwelling on. The heap and the scan perform the same 18 relaxations, because they make the same choices — they differ only in how the choice is found, which is 15 heap comparisons against 42 label reads. And the arc-order schedule does exactly twice as many relaxations, because it needs a second sweep to confirm that the first one settled everything.
The lab's other presets move those numbers in opposite directions. On a chain of six edges the scan does 56 label reads where the heap does 7 pushes, 7 pops and no key comparisons at all; on ten edges over five vertices the scan does 30 reads against the heap's 14 comparisons, 7 pushes and 7 pops. The heap's advantage is largest where the frontier is small, which is the sparse case, and that is the shape of the usual advice.
The reference column, and what it is for
The panel prints E times the integer part of log₂ V, labelled for reference: 18 on the opening graph, since it has nine edges and six vertices. The measured relaxation count is also 18 and that is a coincidence of this graph; on the chain the reference is 12 and the heap does 12 relaxations with quite different heap traffic. No verdict is read off the column, and none should be: it is a scale to watch the counts against as the graph changes.
The one weight this mode will not take
The edges here are undirected, and that is deliberate, because it is what lets the answers be checked live against an undirected implementation written for another Subject. It has a consequence the panel states outright: on an undirected graph a negative weight is a negative cycle. Walk such an edge and walk back, and the total has fallen by twice the weight; do it again and it falls further. No shortest path exists to anything on that side of the graph.
The lab will let you type one so that you can see what happens, and what happens is not an error message. On 1-2 -3, 2-3 4 the heap schedule returns labels of -6, -3 and 1 — including a label of -6 for the source itself, whose distance to itself is 0. The rounds method on the same graph returns the cycle 1, 2, 1 as a certificate. Negative weights belong to directed arcs, and they are the subject of “Negative Weights and Bellman–Ford”.
Measured beside proved, one last time. The three schedules agreeing on six numbers, on four presets, is evidence that the step is indifferent to order. The theorem is that on any graph with no negative weight, the nearest-first schedule settles every vertex exactly once with its true distance, and the proof is a separate argument about the smallest unfinished label — not something the agreement could establish, because agreement on four graphs is agreement on four graphs.