Shortest paths
A shortest-path method minimizes a sum of edge weights, unless a different path score is explicitly stated. Use for unreachable vertices and zero for the empty source-to-itself path. All distance and edge-weight additions must use the same unit.
8.1 Relaxation and path properties
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks · DA 2024–2026 (3 papers): 1 question, 2 marks
Let be edge weight, the true shortest distance and a tentative distance from source . Distances are finite only where no reachable negative cycle can occur on a route to the target.
Every subpath of a shortest path is shortest between its own endpoints; otherwise replacing it improves the whole route. With no relevant negative cycle, a shortest path can be chosen simple and uses at most edges. Distinct edge weights do not imply distinct path sums or unique shortest paths. In an undirected graph represented by two arcs per edge, a negative edge creates a negative closed walk by traversing it both ways, so shortest-walk distances can be unbounded below.
8.2 Dijkstra and algorithm selection
Dijkstra repeatedly settles an unsettled vertex of minimum tentative distance and relaxes its outgoing edges. Nonnegative edge weights ensure that no route through unsettled vertices later improves its settled distance. Zero-weight edges are allowed.
When using a heap without decrease-key, insert a new pair after each improvement and discard stale pairs when popped. This lazy form may store pairs and has time; do not claim the indexed-heap storage bound for it. Stop a target-only search when the target is settled, not when first discovered.
8.3 Bellman–Ford and bounded-edge paths
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks
Bellman–Ford repeatedly relaxes every edge. Without a reachable negative cycle, passes suffice; a further possible improvement witnesses a reachable negative cycle. Early termination is valid if a full pass makes no change.
Unreachable negative cycles are not detected by a single-source run. To test for any negative cycle, add a new source with zero-weight edges to every vertex, or initialize all distances to zero. A target has distance only if a reachable negative cycle can also reach that target. To label affected vertices, propagate reachability from vertices that still improve after the normal passes.
8.4 DAG paths and Floyd–Warshall
Past papers · CS 2014–2026 (23 papers): 3 questions, 5 marks
In a DAG, process vertices in topological order and relax their outgoing edges. Every possible predecessor is processed before its successor, so negative weights cause no difficulty. For all pairs in a general graph, Floyd–Warshall enlarges the allowed set of intermediate vertices.
A negative final diagonal entry detects a negative cycle. A pair has unbounded-below walk cost if some negative-diagonal vertex is reachable from and can reach . Finite matrix entries after the algorithm are not valid shortest distances for those affected pairs. For path recovery, maintain a next-hop or split matrix with the same update decision.
8.5 Weight changes and potentials
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks
Multiplying all weights by a positive constant preserves path comparisons. Adding the same constant to every edge does not generally do so: paths with more edges receive a larger total increment.
After a shortest-path computation under the new weights, recover . The potential identity itself does not require nonnegative new weights; nonnegativity is what permits subsequent Dijkstra runs.
Last-minute recall
- Relaxation improves an upper bound on distance.
- BFS minimizes edge count; Dijkstra minimizes nonnegative weighted cost.
- Stop Dijkstra on settlement, not discovery.
- A DAG permits negative edges with one topological scan.
- Exact edge-budget DP needs the previous layer, not in-place propagation.
- Bellman–Ford detects only source-reachable negative cycles unless initialization changes.
- Floyd–Warshall uses the intermediate-vertex loop outermost.
- CS: potential offsets telescope; a uniform edge addition does not.