Single-Source Shortest Paths

Algorithms · Advanced Placement

Single-Source Shortest Paths: the big picture

The single-source shortest path problem asks for the minimum-weight path from one vertex to every other, and the algorithms available depend on what the weights can be. Shortest paths have the optimal substructure that any subpath of a shortest path is itself shortest, which is what all the algorithms exploit, and all of them operate by repeated relaxation: if a known route to one vertex plus an edge beats the current best estimate for the edge's other endpoint, the estimate is improved. Dijkstra's algorithm requires non-negative weights and processes vertices in increasing order of final distance, using a priority queue; the correctness argument depends on non-negativity, since a negative edge could improve a distance already settled. Its running time depends on the queue, with a binary heap giving a bound in terms of edges times the logarithm of vertices and a Fibonacci heap improving the vertex term. The Bellman–Ford algorithm relaxes every edge a number of times one less than the vertex count, which handles negative weights and detects negative cycles by testing whether any further relaxation succeeds, at the cost of a product bound. For a directed acyclic graph, relaxing edges in topological order gives a linear-time algorithm.

Relaxation, Dijkstra's Algorithm & Priority Queues

Every shortest-path algorithm works by relaxation: if going to u and then taking the edge to v beats your current best estimate for v, improve it. Dijkstra adds an ordering — always finalise the unfinalised vertex with the smallest estimate — and that ordering is correct precisely because weights are non-negative, so nothing can later arrive more cheaply. Make one weight negative and the argument collapses, which is the single most important thing to remember about the algorithm.

{{illustration:algos-l16-f1}}

{{illustration:algos-l16-f2}}

Key relations

- Relaxation and Dijkstra's bound. if d[u] + w(u,v) < d[v] then d[v] ← d[u] + w(u,v) ; T = O(E + Vlog V)

Bellman–Ford, Negative Cycles & DAG Relaxation

Bellman–Ford gives up Dijkstra's ordering and simply relaxes every edge V−1 times, which is enough because a shortest path has at most V−1 edges. That brute-force approach is what lets it handle negative weights, and it detects negative cycles elegantly: if any edge can still be relaxed after V−1 rounds, a negative cycle must exist. The algorithm is also naturally distributed, since each vertex needs only its neighbours' estimates, which is why distance-vector routing protocols use it.

{{illustration:algos-l16-f3}}

{{illustration:algos-l16-f4}}

Key relations

- Bellman–Ford and negative cycle detection. repeat V−1 times: relax all E edges ; any further relaxation ⇒ negative cycle