Documentation

CslibFrontier.Algorithms.Lean.BellmanFord.Basic

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 #

Main results #

References #

def Cslib.Algorithms.Lean.BellmanFord.Vertex {V : Type u_1} {W : Type u_2} (_adj : V → List (V × W)) :
Type u_1

The vertices of the weighted digraph adj, as a quiver: an arrow from u to v is a weight w with (v, w) ∈ adj u.

Instances For
    @[instance_reducible]
    instance Cslib.Algorithms.Lean.BellmanFord.instQuiverVertex {V : Type u_1} {W : Type u_2} (adj : V → List (V × W)) :
    @[reducible, inline]
    abbrev Cslib.Algorithms.Lean.BellmanFord.Walk {V : Type u_1} {W : Type u_2} (adj : V → List (V × W)) (u v : V) :
    Type (max u_1 u_2)

    A walk in adj from u to v: a path in the quiver Vertex adj.

    def Cslib.Algorithms.Lean.BellmanFord.Walk.snoc {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u v x : V} (p : Walk adj u v) {w : W} (h : (x, w) ∈ adj v) :
    Walk adj u x

    Extend a walk by an edge.

    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.induction {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u : V} {motive : (v : V) → Walk adj u v → Prop} (nil : motive u Quiver.Path.nil) (snoc : ∀ {v x : V} {w : W} (p : Walk adj u v) (h : (x, w) ∈ adj v), motive v p → motive x (p.snoc h)) {v : V} (p : Walk adj u v) :
    motive v p

    Induction on walks, with vertices in V.

    def Cslib.Algorithms.Lean.BellmanFord.Walk.support {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u v : V} (p : Walk adj u v) :

    The vertices visited by a walk, in order.

    @[simp]
    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.support_nil {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u : V} :
    @[simp]
    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.support_snoc {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u v x : V} (p : Walk adj u v) {w : W} (h : (x, w) ∈ adj v) :
    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.support_comp {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u v x : V} (p : Walk adj u v) (q : Walk adj v x) :
    @[simp]
    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.dropLast_support_append {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u v : V} (p : Walk adj u v) :
    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.start_mem_support {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u v : V} (p : Walk adj u v) :
    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.length_support {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u v : V} (p : Walk adj u v) :
    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.exists_eq_comp_of_mem_support {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {u v x : V} (p : Walk adj u v) (hx : x ∈ p.support) :
    ∃ (p₁ : Walk adj u x) (p₂ : Walk adj x v), p = Quiver.Path.comp p₁ p₂

    A walk through x splits at x.

    theorem Cslib.Algorithms.Lean.BellmanFord.Walk.length_lt_of_nodup {V : Type u_1} {W : Type u_2} {adj : V → List (V × W)} {vs : List V} (hvs : ∀ (v : V), v ∈ vs) {u v : V} (p : Walk adj u v) (hp : p.support.Nodup) :

    A walk with no repeated vertex has fewer edges than there are vertices.

    def Cslib.Algorithms.Lean.BellmanFord.walkWeight {V : Type u_1} {W : Type u_2} [AddMonoid W] {adj : V → List (V × W)} {u v : V} (p : Walk adj u v) :
    W

    The weight of a walk: the sum of the weights of its edges.

    @[simp]
    theorem Cslib.Algorithms.Lean.BellmanFord.walkWeight_nil {V : Type u_1} {W : Type u_2} [AddMonoid W] {adj : V → List (V × W)} {u : V} :
    @[simp]
    theorem Cslib.Algorithms.Lean.BellmanFord.walkWeight_snoc {V : Type u_1} {W : Type u_2} [AddMonoid W] {adj : V → List (V × W)} {u v x : V} (p : Walk adj u v) {w : W} (h : (x, w) ∈ adj v) :
    @[simp]
    theorem Cslib.Algorithms.Lean.BellmanFord.walkWeight_comp {V : Type u_1} {W : Type u_2} [AddMonoid W] {adj : V → List (V × W)} {u v x : V} (p : Walk adj u v) (q : Walk adj v x) :
    def Cslib.Algorithms.Lean.BellmanFord.NegCycleReachable {V : Type u_1} {W : Type u_2} [AddMonoid W] [Preorder W] (adj : V → List (V × W)) (s : V) :

    A cycle of negative weight is reachable from s: some vertex v reachable from s lies on a closed walk of negative weight.

    def Cslib.Algorithms.Lean.BellmanFord.IsShortestDist {V : Type u_1} {W : Type u_2} [AddMonoid W] [Preorder W] (adj : V → List (V × W)) (s v : V) (d : WithTop W) :

    d is the shortest-path weight from s to v: the least weight of a walk from s to v, and ⊤ when there is none.

    def Cslib.Algorithms.Lean.BellmanFord.relax {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] (d : V → WithTop W) (u v : V) (w : W) :
    V → WithTop W

    Relax the edge from u to v of weight w: lower the estimate of v to d u + w if that is smaller.

    def Cslib.Algorithms.Lean.BellmanFord.round {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] (adj : V → List (V × W)) (vs : List V) (d : V → WithTop W) :
    V → WithTop W

    One round of Bellman–Ford: relax every edge, scanning the vertices vs and their adjacency lists in order.

    def Cslib.Algorithms.Lean.BellmanFord.init {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] (s : V) :
    V → WithTop W

    The initial estimates: 0 at the source and ⊤ elsewhere.

    def Cslib.Algorithms.Lean.BellmanFord.distances {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] (adj : V → List (V × W)) (vs : List V) (s : V) :
    V → WithTop W

    The estimates after |vs| - 1 rounds from the source s.

    def Cslib.Algorithms.Lean.BellmanFord.check {V : Type u_1} {W : Type u_2} [AddCommMonoid W] [LinearOrder W] (adj : V → List (V × W)) (vs : List V) (d : V → WithTop W) :

    Whether no edge can be relaxed further.

    def Cslib.Algorithms.Lean.BellmanFord.bellmanFord {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] (adj : V → List (V × W)) (vs : List V) (s : V) :
    Option (V → WithTop W)

    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 #

    theorem Cslib.Algorithms.Lean.BellmanFord.relax_le {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] (d : V → WithTop W) (u v : V) (w : W) :
    relax d u v w ≤ d
    theorem Cslib.Algorithms.Lean.BellmanFord.relax_apply_le {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] (d : V → WithTop W) (u v : V) (w : W) :
    relax d u v w v ≤ d u + ↑w
    theorem Cslib.Algorithms.Lean.BellmanFord.round_le {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} (d : V → WithTop W) :
    round adj vs d ≤ d

    A round only lowers the estimates.

    theorem Cslib.Algorithms.Lean.BellmanFord.iterate_round_le {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} (k : ℕ) (d : V → WithTop W) :
    (round adj vs)^[k] d ≤ d
    theorem Cslib.Algorithms.Lean.BellmanFord.exists_walk_of_iterate_round_eq {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} (k : ℕ) {v : V} {x : W} (h : (round adj vs)^[k] (init s) v = ↑x) :
    ∃ (p : Walk adj s v), walkWeight p = x

    Soundness: every finite estimate after any number of rounds is the weight of a walk from the source.

    theorem Cslib.Algorithms.Lean.BellmanFord.check_iff {V : Type u_1} {W : Type u_2} [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} (d : V → WithTop W) :
    check adj vs d = true ↔ ∀ u ∈ vs, ∀ (v : V) (w : W), (v, w) ∈ adj u → d v ≤ d u + ↑w

    Upper bound #

    theorem Cslib.Algorithms.Lean.BellmanFord.round_apply_le {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} [IsOrderedCancelAddMonoid W] {u : V} (hu : u ∈ vs) {v : V} {w : W} (hv : (v, w) ∈ adj u) (d : V → WithTop W) :
    round adj vs d v ≤ d u + ↑w

    After a round, every edge (u, v) of weight w satisfies d v ≤ d u + w for the estimates d before the round.

    theorem Cslib.Algorithms.Lean.BellmanFord.iterate_round_le_walkWeight {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} [IsOrderedCancelAddMonoid W] (hvs : ∀ (v : V), v ∈ vs) {v : V} (p : Walk adj s v) (k : ℕ) (hk : Quiver.Path.length p ≤ k) :
    (round adj vs)^[k] (init s) v ≤ ↑(walkWeight p)

    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 #

    theorem Cslib.Algorithms.Lean.BellmanFord.exists_nodup_walk_le {V : Type u_1} {W : Type u_2} [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {s : V} [IsOrderedCancelAddMonoid W] (hneg : ¬NegCycleReachable adj s) {v : V} (p : Walk adj s v) :
    ∃ (q : Walk adj s v), q.support.Nodup ∧ walkWeight q ≤ walkWeight p

    With no negative cycle reachable from s, every walk from s can be shortened to a walk with no repeated vertex.

    Correctness #

    theorem Cslib.Algorithms.Lean.BellmanFord.distances_self_le {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} :
    distances adj vs s s ≤ 0
    theorem Cslib.Algorithms.Lean.BellmanFord.distances_le_walkWeight {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} [IsOrderedCancelAddMonoid W] (hvs : ∀ (v : V), v ∈ vs) (hneg : ¬NegCycleReachable adj s) {v : V} (p : Walk adj s v) :
    distances adj vs s v ≤ ↑(walkWeight p)

    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.

    theorem Cslib.Algorithms.Lean.BellmanFord.check_distances {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} [IsOrderedCancelAddMonoid W] (hvs : ∀ (v : V), v ∈ vs) (hneg : ¬NegCycleReachable adj s) :
    check adj vs (distances adj vs s) = true

    With no negative cycle reachable from s, no edge can be relaxed after |vs| - 1 rounds.

    theorem Cslib.Algorithms.Lean.BellmanFord.le_add_walkWeight_of_check {V : Type u_1} {W : Type u_2} [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} [IsOrderedCancelAddMonoid W] {d : V → WithTop W} (hd : check adj vs d = true) (hvs : ∀ (v : V), v ∈ vs) {a b : V} (p : Walk adj a b) :
    d b ≤ d a + ↑(walkWeight p)

    Estimates passing the check satisfy d b ≤ d a + w(p) along every walk p from a to b.

    theorem Cslib.Algorithms.Lean.BellmanFord.not_negCycleReachable_of_check {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} [IsOrderedCancelAddMonoid W] (hvs : ∀ (v : V), v ∈ vs) (hd : check adj vs (distances adj vs s) = true) :

    If the check passes after |vs| - 1 rounds, no negative cycle is reachable from s.

    theorem Cslib.Algorithms.Lean.BellmanFord.bellmanFord_eq_none_iff {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} [IsOrderedCancelAddMonoid W] (hvs : ∀ (v : V), v ∈ vs) :

    Negative-cycle detection: Bellman–Ford reports none exactly when a cycle of negative weight is reachable from the source.

    theorem Cslib.Algorithms.Lean.BellmanFord.isShortestDist_of_bellmanFord_eq_some {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} [IsOrderedCancelAddMonoid W] (hvs : ∀ (v : V), v ∈ vs) {d : V → WithTop W} (h : bellmanFord adj vs s = some d) (v : V) :
    IsShortestDist adj s v (d v)

    Shortest paths: when Bellman–Ford returns estimates, each is the shortest-path weight from the source.

    theorem Cslib.Algorithms.Lean.BellmanFord.bellmanFord_eq_some {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommMonoid W] [LinearOrder W] {adj : V → List (V × W)} {vs : List V} {s : V} [IsOrderedCancelAddMonoid W] (hvs : ∀ (v : V), v ∈ vs) (hneg : ¬NegCycleReachable adj s) :
    bellmanFord adj vs s = some (distances adj vs s)

    With no negative cycle reachable from s, Bellman–Ford returns the estimates after |vs| - 1 rounds.