Bellman-Ford algorithm 2026-10-14
For an all-to-one shortest path problem with root , initialize and all other labels to infinity. Set , keeping the root at zero. Induction identifies the labels with the shortest directed paths using at most directed edges. Without a reachable negative directed cycle, rounds suffice after removing directed cycles from a shortest walk. In-place or queue variants repeatedly relax incoming directed edges to an improved label. Labels may decrease multiple times, so this is a label-correcting shortest-path algorithm.
Past exam of the mathematics course of the University of Cambridge 2001 iii Paper 31 4 Solution Created 2026-10-03 Updated 2026-10-14
The all-to-one shortest path problem asks for the minimum length of a directed path from every vertex to the root , with and if no such directed path exists. Positive directed edge lengths allow directed cycles to be deleted, so a shortest directed path, when one exists, uses at most directed edges. Its optimality equations areA Bellman-Ford algorithm initializes and other labels to infinity, then repeatskeeping the root label zero. Induction on proves that is the least length among directed paths using at most directed edges: either the old best directed path remains, or the first directed edge is followed by a directed path with at most directed edges. Thus rounds give the exact distances. An in-place variant repeatedly relaxes ; each decreased label triggers further incoming-arc relaxations. It terminates when no finite label can improve. The Bellman inequalities then telescope along each directed path to show the labels are lower bounds on all directed path lengths, while every finite label represents a discovered directed path and is an upper bound on the optimum. This proves correctness. The method is a label-correcting shortest-path algorithm: a processed label can be revised many times. Full scans cost .
Let and choose attaining it. Every directed path from a nonroot vertex to has a final directed edge costing at least , and all preceding directed edges have positive lengths. Therefore every . The direct directed edge from to has length , soIf no incoming root directed edge exists, all these distances are infinite and there is no finite next vertex to settle.
The all-to-one Dijkstra algorithm sets the root's label to zero and all others to infinity. Repeatedly choose an unsettled vertex with least finite label, make that label permanent, and relax every incoming directed edge of an unsettled vertex:Store the corresponding successor whenever the label improves. Stop when all vertices are settled or every remaining label is infinite. This applies the usual source version to the reversed graph; relaxing outgoing original directed edges would solve the wrong direction of the problem.
For its correctness, inductively assume every settled distance is exact. Any directed path from an unsettled vertex to the root must first enter the settled set at some vertex , through an directed edge . That directed edge has already been relaxed, so . The newly selected label is no greater than . The directed path's preceding nonnegative lengths therefore make its total length at least . On the other hand, itself is the length of a discovered directed path. Hence , and it can never need correction. This is a label-setting shortest-path algorithm. Straight array selection costs .
For four vertices and root 4, the full symbolic execution is as follows. After settling 4, the tentative labels are . Selectset , and update the two other labels to . Select the smaller of those labels at vertex , set , then for the last vertex setThe stored successors give the corresponding shortest routes; infinity conventions handle missing directed edges and disconnected vertices.
The supplied PDF contains no visible network diagram or directed edge lengths in the space for this example, and the converted TeX omits the diagram as well. Numerical distances and route choices cannot be determined from the supplied data. The symbolic run above gives the exact four-node calculation once those lengths are available; no network has been guessed.
Shortest-path predecessor 2026-10-14
A shortest-path predecessor records the previous vertex on a chosen route from source to . When edge relaxation strictly improves through , set . For a multigraph, store the actual directed edge, since two edges with the same endpoints can have different weights. Initialize all vertices, including the source, without a shortest-path predecessor. The authored implementation reconstructs a route by following these incoming edges backwards, then reversing their order.[ref]
https://raw.githubusercontent.com/kevin-wayne/algs4/master/src/main/java/edu/princeton/cs/algs4/DijkstraSP.java. Sedgewick and Wayne, DijkstraSP.java, edgeTo and pathTo
At the end of the edge relaxation trace, , , and . Following them gives , so the forward directed path is , of cost . This makes the distance useful: a number tells us the best cost, whereas the shortest-path predecessors tell us which connections to take. In the Dijkstra algorithm, reconstruct a destination's shortest route after its label becomes permanent. In a label-correcting shortest-path algorithm, wait for convergence and the appropriate negative-cost directed cycle check. During correction, a predecessor's label may have changed since its child's last update: immediately after in the trace, the current chain to costs although still records the earlier discovered route of cost .
Strict improvement is a useful guard against directed cycles in the recorded pointers. Consider and of cost zero, followed by and , also of cost zero. After the first two edge relaxations, both labels are zero and both shortest-path predecessors are . If equality also allows pointer replacement, the last two operations can set and : following pointers loops forever despite correct distance labels. Keeping the old pointer on ties prevents this example; choosing among equal-cost routes requires a separate rule that preserves a route to the source. Strict improvement alone does not protect against reachable negative-cost directed cycles, which can force endless decreases. Equality on recorded edges plus the Bellman inequalities certifies route costs only when the pointer chains actually terminate at the source.
For all-to-one distances to , record a next vertex whenever relaxing improves . Follow these successors forward in the original directed graph to reach . They are shortest-path predecessors when viewed in the reversed directed graph; keeping the direction convention explicit prevents returning a route whose arrows point the wrong way.