Skip to content
Part IA Lent Term

The Single-Source Shortest Paths Problem

Problem Statement

Given a weighted, directed graph G=(V,E)G = (V, E) with weight function w:ERw: E \to \mathbb{R} and a designated source vertex sVs \in V, find the shortest path (minimum total weight) from ss to every other vertex.

Shortest-path weight from uu to vv:

δ(u,v)={minp:uvw(p)if a path existsotherwise\delta(u, v) = \begin{cases} \displaystyle\min_{p: u \leadsto v} w(p) & \text{if a path exists} \\[8pt] \infty & \text{otherwise} \end{cases}

where w(p)=i=1k1w(vi,vi+1)w(p) = \sum_{i=1}^{k-1} w(v_i, v_{i+1}) for path p=v1,v2,,vkp = v_1, v_2, \ldots, v_k.

A shortest path from uu to vv is any path pp satisfying w(p)=δ(u,v)w(p) = \delta(u, v). Shortest paths are not necessarily unique; there may be multiple paths with the same minimum weight.

Variants of Shortest-Path Problems

VariantInputOutput
SSSP (Single-Source)G,w,sG, w, sδ(s,v)\delta(s, v) for all vVv \in V
SDSP (Single-Destination)G,w,tG, w, tδ(v,t)\delta(v, t) for all vv
SPSP (Single-Pair)G,w,s,tG, w, s, tδ(s,t)\delta(s, t)
APSP (All-Pairs)G,wG, wδ(u,v)\delta(u, v) for all pairs

SDSP reduces to SSSP on GTG^T. SPSP has no known algorithm asymptotically faster than SSSP in the worst case. APSP can be solved by V|V| iterations of SSSP or by dedicated algorithms (Floyd-Warshall, Johnson).

Assumptions and Edge Cases

  • No negative-weight cycles reachable from ss. If a negative cycle is reachable, you can loop forever, decreasing path weight without bound: δ(s,v)=\delta(s, v) = -\infty for any vertex on the cycle, and the problem is ill-defined.
  • Positive-weight cycles are never part of a shortest path: removing such a cycle strictly reduces the total weight. Hence shortest paths can be assumed simple (no vertex repeated).
  • Zero-weight cycles do not affect path weight; they can be removed without changing the result. So we assume shortest paths have at most V1|V|-1 edges.
  • Distances are undefined (or -\infty) if a negative-weight cycle is reachable on the path to that vertex.

Optimal Substructure

Shortest paths exhibit optimal substructure: any subpath of a shortest path is itself a shortest path.

Proof: Let p=v1vkp = v_1 \leadsto v_k be a shortest path, and let vivjv_i \leadsto v_j be any contiguous subpath. If there were a shorter path pp' from viv_i to vjv_j, replacing the subpath with pp' would produce a v1vkv_1 \leadsto v_k path strictly shorter than pp, contradicting optimality of pp.

This property is the foundation for dynamic programming (Floyd-Warshall) and greedy (Dijkstra) approaches.

The Relaxation Operation

All shortest-path algorithms in this course share a common subroutine: relaxation of an edge. The idea is to test whether going through uu improves the current estimate for vv:

RELAX(u, v, w):
  if d[v] > d[u] + w(u, v):
    d[v] = d[u] + w(u, v)
    π[v] = u
  • d[v]d[v] is the current estimate of δ(s,v)\delta(s, v).
  • π[v]\pi[v] is the predecessor of vv on the current candidate shortest path.
  • Initialisation sets d[s]=0d[s] = 0 and d[v]=d[v] = \infty for all vsv \neq s, and all π[v]=NIL\pi[v] = \text{NIL}.

Upper-bound property: After any sequence of relaxations, d[v]δ(s,v)d[v] \ge \delta(s, v) always holds. Relaxation never makes estimates too low; it only lowers overestimates towards the true value.

Predecessor Subgraph

The edges (π[v],v)(\pi[v], v) for all vV{s}v \in V \setminus \{s\} with π[v]NIL\pi[v] \neq \text{NIL} form the predecessor subgraph GπG_\pi. At any point during execution, GπG_\pi is a tree rooted at ss (a “shortest-path tree” in progress). When the algorithm terminates correctly, the unique path in GπG_\pi from ss to any reachable vv is a shortest path in GG.

To reconstruct a path: start at vv, follow π\pi pointers backwards until reaching ss, then reverse the sequence.

Algorithm Landscape

All SSSP algorithms differ only in the order in which they relax edges:

AlgorithmConstraintEdge OrderTime
BFSUnweighted (w=1w=1 for all edges)By distance levelsΘ(V+E)\Theta(V+E)
DAG-SSSPDAG onlyTopological orderΘ(V+E)\Theta(V+E)
Dijkstraw0w \ge 0Greedy by d[u]d[u]O((V+E)logV)O((V+E)\log V)
Bellman-FordNone (no neg. cycles)All edges V1V-1 timesO(VE)O(VE)

Worked Example: Relaxation Sequence

Graph: sas \to a (5), sbs \to b (3), aba \to b (1), bcb \to c (2).

Operationd[s]d[s]d[a]d[a]d[b]d[b]d[c]d[c]
Initialise0\infty\infty\infty
RELAX(s,a)05\infty\infty
RELAX(s,b)053\infty
RELAX(a,b)053\infty
RELAX(b,c)0535

Final: δ(s,a)=5\delta(s,a)=5, δ(s,b)=3\delta(s,b)=3, δ(s,c)=5\delta(s,c)=5 (path sbcs \to b \to c).

The order of relaxation matters: if (a,b)(a,b) were relaxed before (s,b)(s,b), we would temporarily set d[b]=6d[b]=6, but then (s,b)(s,b) corrects it to 3. Bellman-Ford handles this by relaxing everything repeatedly; Dijkstra avoids it by always processing vertices in increasing distance order.

Summary

ConceptDescription
δ(s,v)\delta(s, v)Minimum path weight from ss to vv
Optimal substructureSubpath of shortest path is shortest
Relaxationif d[v] > d[u] + w(u,v): update
Upper boundd[v]δ(s,v)d[v] \ge \delta(s,v) always
Predecessor π\piEncodes shortest-path tree
Max simple path lengthV1\lvert V \rvert - 1 edges

Past paper questions: y2024p1q9 (opportunities, relaxed costs), y2023p1q9 (SSSP variants).