The Bellman–Ford algorithm #
Single-source shortest paths with possibly negative edge weights, and detection of negative cycles, by the Bellman–Ford algorithm Bellman Cormen et al., Section 24.1.
Model #
A weighted directed graph is given by adjacency lists adj : V → List (V × W): the entry
(v, w) ∈ adj u is an edge from u to v of weight w; parallel edges and self-loops are
allowed. Weights lie in a linearly ordered cancellative additive commutative monoid W (for
example ℤ, ℚ or ℝ), and estimates in WithTop W, with ⊤ for "no walk found yet". Walks
are paths (Quiver.Path) in the quiver Cslib.Algorithms.Lean.BellmanFord.Vertex adj, as for
Dijkstra's Cslib.Algorithms.Dijkstra.Instance.dist, and
Cslib.Algorithms.Lean.BellmanFord.walkWeight is the sum of the edge weights.
The algorithm is given a list vs of all the vertices (hvs : ∀ v, v ∈ vs). It starts from 0 at
the source and ⊤ elsewhere, relaxes every edge in |vs| - 1 rounds, and then checks whether
some edge can still be relaxed.
Main definitions #
Cslib.Algorithms.Lean.BellmanFord.NegCycleReachable: a closed walk of negative weight is reachable from the source.Cslib.Algorithms.Lean.BellmanFord.IsShortestDist:dis the least weight of a walk fromstov, and⊤when there is none.Cslib.Algorithms.Lean.BellmanFord.bellmanFord: the algorithm, returning the estimates, ornonewhen an edge can still be relaxed.
Main results #
Cslib.Algorithms.Lean.BellmanFord.bellmanFord_eq_none_iff: Bellman–Ford returnsnoneexactly when a negative cycle is reachable from the source.Cslib.Algorithms.Lean.BellmanFord.isShortestDist_of_bellmanFord_eq_some: otherwise every returned estimate is the shortest-path weight. Together these are Cormen et al., Theorem 24.4 without the predecessor tree.Cslib.Algorithms.Lean.BellmanFord.iterate_round_le_walkWeight: afterkrounds each estimate is at most the weight of every walk from the source with at mostkedges (the path-relaxation argument of Cormen et al., Lemma 24.2).Cslib.Algorithms.Lean.BellmanFord.exists_walk_of_iterate_round_eq: every finite estimate is the weight of a walk from the source.Cslib.Algorithms.Lean.BellmanFord.exists_nodup_walk_le: with no negative cycle reachable from the source, every walk from it can be shortened to one without repeated vertices.
References #
Induction on walks, with vertices in V.
Relax the edge from u to v of weight w: lower the estimate of v to d u + w if that
is smaller.
One round of Bellman–Ford: relax every edge, scanning the vertices vs and their adjacency
lists in order.
The initial estimates: 0 at the source and ⊤ elsewhere.
The estimates after |vs| - 1 rounds from the source s.
Whether no edge can be relaxed further.
The Bellman–Ford algorithm from the source s: the estimates after |vs| - 1 rounds if no
edge can be relaxed further, and none (a negative cycle) otherwise.
Monotonicity and soundness #
A round only lowers the estimates.
Soundness: every finite estimate after any number of rounds is the weight of a walk from the source.
Upper bound #
After a round, every edge (u, v) of weight w satisfies d v ≤ d u + w for the estimates
d before the round.
Upper bound: after k rounds the estimate of v is at most the weight of every walk from
s to v with at most k edges.
Simple walks #
With no negative cycle reachable from s, every walk from s can be shortened to a walk with
no repeated vertex.
Correctness #
Upper bound: with no negative cycle reachable from s, the estimates after |vs| - 1
rounds are at most the weight of every walk from s.
With no negative cycle reachable from s, no edge can be relaxed after |vs| - 1 rounds.
Estimates passing the check satisfy d b ≤ d a + w(p) along every walk p from a to b.
If the check passes after |vs| - 1 rounds, no negative cycle is reachable from s.
Negative-cycle detection: Bellman–Ford reports none exactly when a cycle of negative
weight is reachable from the source.
Shortest paths: when Bellman–Ford returns estimates, each is the shortest-path weight from the source.
With no negative cycle reachable from s, Bellman–Ford returns the estimates after
|vs| - 1 rounds.