Documentation

CslibFrontier.Algorithms.Lean.MaxFlow.MinCostFlow

Minimum-Cost Flows and Klein's Negative-Cycle Optimality Theorem #

Formalizes minimum-cost integral network flows over arbitrary vertex balances, Klein's negative-cycle optimality theorem and complementary slackness characterization ([Kle67], pp. 205–220, locator not re-verified against primary PDF; [Sch03], Chapter 12, locator not re-verified against primary PDF), and executable cycle-canceling algorithms.

Model and source mapping #

Main definitions #

Main results #

Cost model and scope #

References #

Cost networks, feasible flows, and residual arcs #

structure Cslib.Algorithms.Lean.MaxFlow.CostNetwork (V : Type u_1) (W : Type u_2) :
Type (max u_1 u_2)

A directed capacitated network with arc costs in W and vertex net-supply balances: positive balance v is a supply, negative is a demand, and balance = 0 is a circulation.

  • cap : V → V → ℕ

    Nonnegative integral capacity of each directed arc (u, v).

  • cost : V → V → W

    Per-unit cost of flow on directed arc (u, v).

  • balance : V → ℤ

    Required net outflow ∑ w, f v w - ∑ u, f u v at each vertex v.

A directed step in the residual network: isFwd = true increases flow on (src, dst) and isFwd = false decreases flow on (dst, src).

  • src : V

    Tail vertex of the residual step.

  • dst : V

    Head vertex of the residual step.

  • isFwd : Bool

    true for a forward step (src, dst); false for canceling reverse arc (dst, src).

def Cslib.Algorithms.Lean.MaxFlow.instDecidableEqResArc.decEq {V✝ : Type u_1} [DecidableEq V✝] (x✝ x✝¹ : ResArc V✝) :
Decidable (x✝ = x✝¹)
@[instance_reducible]
structure Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow {V : Type u_1} {W : Type u_2} [Fintype V] (M : CostNetwork V W) :
Type u_1

A feasible integral flow on M: bounded by M.cap and matching M.balance at every vertex.

  • toFun : V → V → ℕ

    Integral flow assigned to each directed arc (u, v).

  • le_cap (u v : V) : self.toFun u v ≤ M.cap u v

    Capacity bound on every directed arc.

  • balance_eq (v : V) : ∑ w : V, ↑(self.toFun v w) - ∑ u : V, ↑(self.toFun u v) = M.balance v

    Exact vertex net-outflow conservation against M.balance.

Instances For
    theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.ext {V : Type u_1} {W : Type u_2} {inst✝ : Fintype V} {M : CostNetwork V W} {x y : M.Flow} (toFun : x.toFun = y.toFun) :
    x = y
    theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.ext_iff {V : Type u_1} {W : Type u_2} {inst✝ : Fintype V} {M : CostNetwork V W} {x y : M.Flow} :
    x = y ↔ x.toFun = y.toFun
    @[instance_reducible]
    instance Cslib.Algorithms.Lean.MaxFlow.CostNetwork.instCoeFunFlowForallForallNat {V : Type u_1} {W : Type u_2} [Fintype V] (M : CostNetwork V W) :
    CoeFun M.Flow fun (x : M.Flow) => V → V → ℕ
    def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.zero {V : Type u_1} {W : Type u_2} [Fintype V] (M : CostNetwork V W) (h0 : ∀ (v : V), M.balance v = 0) :

    Zero circulation on a network whose vertex balances all vanish.

    def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.resCost {V : Type u_1} {W : Type u_2} [Neg W] (M : CostNetwork V W) (a : ResArc V) :
    W

    Residual cost of a step a : ResArc V: cost u v forward, -cost v u backward.

    Total cost of an arc list in the residual network.

    @[simp]
    theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.walkCost_cons {V : Type u_1} {W : Type u_2} [AddCommGroup W] (M : CostNetwork V W) (a : ResArc V) (as : List (ResArc V)) :
    M.walkCost (a :: as) = M.resCost a + M.walkCost as
    @[simp]
    theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.walkCost_append {V : Type u_1} {W : Type u_2} [AddCommGroup W] (M : CostNetwork V W) (as bs : List (ResArc V)) :
    M.walkCost (as ++ bs) = M.walkCost as + M.walkCost bs

    Total cost ∑ u, ∑ v, f u v • M.cost u v of a feasible flow f.

    f is a minimum-cost feasible flow on M.

    def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.resCap {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} (f : M.Flow) (a : ResArc V) :

    Residual capacity of step a : ResArc V with respect to f.

    def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.IsResWalk {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} (f : M.Flow) :
    V → V → List (ResArc V) → Prop

    Walk predicate in f's residual network.

    Instances For

      arcs is a nonempty simple directed cycle in f's residual network (distinct start vertices, allowing self-loops [⟨v, v, b⟩] and 2-cycles [⟨u, v, b₁⟩, ⟨v, u, b₂⟩]).

      Instances For
        @[instance_reducible]
        instance Cslib.Algorithms.Lean.MaxFlow.CostNetwork.instDecidableIsResWalk {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} [DecidableEq V] (f : M.Flow) (u v : V) (arcs : List (ResArc V)) :
        Decidable (f.IsResWalk u v arcs)
        @[instance_reducible]

        Minimum residual capacity along a list of residual arcs (0 when empty).

        def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.CompSlackness {V : Type u_1} {W : Type u_2} [Fintype V] [AddCommGroup W] [LE W] (M : CostNetwork V W) (f : M.Flow) (π : V → W) :

        Complementary slackness optimality condition for f and potentials π : V → W.

        Residual walks, bottleneck, and cycle cancellation #

        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.isResCycle_nodup {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} {f : M.Flow} {arcs : List (ResArc V)} (h : f.IsResCycle arcs) :
        arcs.Nodup
        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.isResCycle_ne_nil {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} {f : M.Flow} {arcs : List (ResArc V)} (h : f.IsResCycle arcs) :
        arcs ≠ []
        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.pos_resCap_of_mem_isResCycle {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} {f : M.Flow} {arcs : List (ResArc V)} (h : f.IsResCycle arcs) {a : ResArc V} (ha : a ∈ arcs) :
        0 < f.resCap a
        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.cycleBottleneck_le {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} {f : M.Flow} {arcs : List (ResArc V)} {a : ResArc V} (ha : a ∈ arcs) :
        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.cycleBottleneck_pos {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} {f : M.Flow} {arcs : List (ResArc V)} (hne : arcs ≠ []) (hpos : ∀ a ∈ arcs, 0 < f.resCap a) :
        def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.cancelCycleFun {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} [DecidableEq V] (f : M.Flow) (arcs : List (ResArc V)) (δ : ℕ) (u v : V) :

        Raw arc-flow update adding δ on forward arcs of arcs and subtracting δ on backward arcs of arcs.

        def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.cancelCycle {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} [DecidableEq V] (f : M.Flow) (arcs : List (ResArc V)) (hc : f.IsResCycle arcs) (δ : ℕ) (hδ : ∀ a ∈ arcs, δ ≤ f.resCap a) :

        Cancel δ units of flow around a residual cycle arcs (δ ≤ f.resCap a for all a ∈ arcs).

        @[simp]
        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.cancelCycle_apply {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} [DecidableEq V] (f : M.Flow) {arcs : List (ResArc V)} (hc : f.IsResCycle arcs) {δ : ℕ} (hδ : ∀ a ∈ arcs, δ ≤ f.resCap a) (u v : V) :
        (f.cancelCycle arcs hc δ hδ).toFun u v = f.cancelCycleFun arcs δ u v
        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.totalCost_cancelCycle {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} [DecidableEq V] [AddCommGroup W] (f : M.Flow) {arcs : List (ResArc V)} (hc : f.IsResCycle arcs) {δ : ℕ} (hδ : ∀ a ∈ arcs, δ ≤ f.resCap a) :
        (f.cancelCycle arcs hc δ hδ).totalCost - f.totalCost = δ • M.walkCost arcs

        Canceling δ units along a residual cycle arcs changes totalCost by δ • M.walkCost arcs.

        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.totalCost_cancelCycle_lt {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} [DecidableEq V] [AddCommGroup W] [LinearOrder W] [IsOrderedAddMonoid W] (f : M.Flow) {arcs : List (ResArc V)} (hc : f.IsResCycle arcs) {δ : ℕ} (hδ_pos : 0 < δ) (hδ : ∀ a ∈ arcs, δ ≤ f.resCap a) (hneg : M.walkCost arcs < 0) :
        (f.cancelCycle arcs hc δ hδ).totalCost < f.totalCost

        Complementary slackness optimality #

        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.sum_balance_smul_potential {V : Type u_1} {W : Type u_2} [Fintype V] {M : CostNetwork V W} [AddCommGroup W] (f : M.Flow) (π : V → W) :
        ∑ u : V, ∑ v : V, f.toFun u v • (π u - π v) = ∑ u : V, M.balance u • π u

        Flow conservation identity against vertex potentials: ∑ u, ∑ v, f u v • (π u - π v) = ∑ u, M.balance u • π u.

        Complementary slackness implies minimum cost ([Kle67], [Sch03], Chapter 12, locator not re-verified against primary PDF): any feasible flow admitting potentials π with CompSlackness M f π minimizes totalCost among all feasible flows of M.

        Simple cycle extraction from walks #

        A decomposition pre ++ cyc ++ post of a residual walk extracting a simple directed cycle cyc.

        • pre : List (ResArc V)

          Prefix walk before the extracted cycle.

        • cyc : List (ResArc V)

          Nonempty simple directed cycle.

        • post : List (ResArc V)

          Suffix walk after the extracted cycle.

        Split arcs right after the first arc whose dst equals target.

        Extract a simple directed cycle (pre, cyc, post) from an arc walk ending at v, scanning right-to-left, or none when arcs.map src ++ [v] has no duplicate vertices.

        Structural-fuel helper for peelNegCycle.

        Peel simple cycles from arcs (ending at v) until returning the first negative-cost simple cycle.

        Bellman–Ford relaxation DP for negative cycles and potentials #

        def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.allResArcs {V : Type u_1} {W : Type u_2} [Fintype V] (M : CostNetwork V W) (vs : List V) (f : M.Flow) :

        All positive-residual-capacity arcs of f whose endpoints lie in vs.

        def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.dpStep {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommGroup W] [LinearOrder W] (M : CostNetwork V W) (arcs : List (ResArc V)) (prev : V → W × List (ResArc V)) (v : V) :
        W × List (ResArc V)

        Synchronous one-round Bellman–Ford relaxation storing (bestWeight, arcTrail) per vertex.

        def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.dpRelax {V : Type u_1} {W : Type u_2} [DecidableEq V] [AddCommGroup W] [LinearOrder W] (M : CostNetwork V W) (arcs : List (ResArc V)) (k : ℕ) :
        V → W × List (ResArc V)

        k-round synchronous Bellman–Ford relaxation from (0, []) at every vertex.

        Run Bellman–Ford relaxation DP for vs.length - 1 steps from (0, []) and return either a negative simple residual cycle (Sum.inl cyc) or complementary-slackness potentials (Sum.inr π).

        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.findNegCycleOrPotential_spec {V : Type u_1} {W : Type u_2} [Fintype V] [DecidableEq V] [AddCommGroup W] [LinearOrder W] [IsOrderedAddMonoid W] {M : CostNetwork V W} {vs : List V} (hvs : ∀ (v : V), v ∈ vs) (f : M.Flow) :
        match M.findNegCycleOrPotential vs f with | Sum.inl cyc => f.IsResCycle cyc ∧ M.walkCost cyc < 0 | Sum.inr π => M.CompSlackness f π

        Klein's negative-cycle optimality theorem #

        Klein's negative-cycle optimality theorem ([Kle67], pp. 205–220, locator not re-verified against primary PDF; [Sch03], Chapter 12, locator not re-verified against primary PDF): a feasible flow has minimum cost if and only if its residual network contains no negative-cost simple cycle.

        Complementary slackness optimality theorem ([Sch03], Chapter 12, locator not re-verified against primary PDF): a feasible flow has minimum cost if and only if it admits complementary-slackness node potentials.

        Cycle canceling algorithm and existence of minimum-cost flows #

        structure Cslib.Algorithms.Lean.MaxFlow.CostNetwork.CycleCancelOutput {V : Type u_1} {W : Type u_2} [Fintype V] (M : CostNetwork V W) :
        Type (max u_1 u_2)

        Result of executable negative-cycle canceling on M : CostNetwork V W.

        • flow : M.Flow

          Optimal feasible flow returned upon termination.

        • potential : V → W

          Complementary-slackness node potentials certifying optimality of flow.

        • cancellations : ℕ

          Number of negative-cycle cancellations performed.

        Iteratively find and cancel negative residual cycles up to fuel steps, returning an optimal flow and complementary-slackness potentials once none remain.

        Any feasible flow network with linearly ordered additive group costs admits a minimum-cost feasible flow and complementary-slackness potentials.

        Explicit upper bound on |f.totalCost| over a vertex enumeration vs for integer-cost networks.

        Executable integral negative-cycle canceling ([Kle67], pp. 205–220, locator not re-verified against primary PDF): repeatedly extracts and cancels a negative simple residual cycle starting from f₀. Terminates in at most 2 * absCostBound M vs cancellations.

        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.cycleCancelInt_correct {V : Type u_1} [Fintype V] [DecidableEq V] {M : CostNetwork V ℤ} {vs : List V} (hvs : ∀ (v : V), v ∈ vs) (f₀ : M.Flow) :

        Correctness of executable cycle canceling ([Kle67], pp. 205–220, locator not re-verified against primary PDF): on any finite vertex list vs covering V, cycleCancelInt returns a minimum-cost feasible flow certified by complementary-slackness potentials after at most 2 * absCostBound M vs cancellations.

        Fixed-value s-t flows and minimum-cost maximum flow #

        def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.ofNetwork {V : Type u_1} {W : Type u_2} [DecidableEq V] (N : Network V) (cost : V → V → W) (val : ℤ) :

        Embed an s-t Network V with fixed net flow value val into a CostNetwork V W with arc costs cost.

        def Cslib.Algorithms.Lean.MaxFlow.Network.Flow.flowCost {V : Type u_1} {W : Type u_2} [Fintype V] {N : Network V} [AddCommMonoid W] (cost : V → V → W) (f : N.Flow) :
        W

        Total cost ∑ u, ∑ v, f u v • cost u v of an s-t flow f : N.Flow.

        def Cslib.Algorithms.Lean.MaxFlow.Network.Flow.IsMinCostOfValue {V : Type u_1} {W : Type u_2} [Fintype V] {N : Network V} [AddCommMonoid W] [LE W] (cost : V → V → W) (f : N.Flow) :

        Fixed-value minimum-cost s-t flow optimality: f minimizes flowCost cost among all feasible flows on N with the same value as f.

        def Cslib.Algorithms.Lean.MaxFlow.Network.Flow.toCostFlowOfVal {V : Type u_1} {W : Type u_2} [Fintype V] [DecidableEq V] {N : Network V} (cost : V → V → W) {val : ℤ} (g : N.Flow) (hg : g.value = val) :

        View an s-t flow g : N.Flow with g.value = val as a feasible flow in CostNetwork.ofNetwork N cost val.

        def Cslib.Algorithms.Lean.MaxFlow.Network.Flow.toCostFlow {V : Type u_1} {W : Type u_2} [Fintype V] [DecidableEq V] {N : Network V} (cost : V → V → W) (f : N.Flow) :

        View an s-t flow f : N.Flow as a feasible flow of value f.value in CostNetwork.ofNetwork N cost f.value.

        def Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.toNetworkFlow {V : Type u_1} {W : Type u_2} [Fintype V] [DecidableEq V] {N : Network V} {cost : V → V → W} {val : ℤ} (f : (ofNetwork N cost val).Flow) :

        View a feasible flow f in CostNetwork.ofNetwork N cost val back as an s-t flow on N.

        @[simp]
        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.toNetworkFlow_apply {V : Type u_1} {W : Type u_2} [Fintype V] [DecidableEq V] {N : Network V} {cost : V → V → W} {val : ℤ} (f : (ofNetwork N cost val).Flow) (u v : V) :
        @[simp]
        theorem Cslib.Algorithms.Lean.MaxFlow.Network.Flow.toCostFlowOfVal_apply {V : Type u_1} {W : Type u_2} [Fintype V] [DecidableEq V] {N : Network V} (cost : V → V → W) {val : ℤ} (g : N.Flow) (hg : g.value = val) (u v : V) :
        (toCostFlowOfVal cost g hg).toFun u v = g.toFun u v
        @[simp]
        theorem Cslib.Algorithms.Lean.MaxFlow.CostNetwork.Flow.value_toNetworkFlow {V : Type u_1} {W : Type u_2} [Fintype V] [DecidableEq V] {N : Network V} {cost : V → V → W} {val : ℤ} (f : (ofNetwork N cost val).Flow) :

        Klein's negative-cycle optimality theorem for s-t flows ([Kle67], pp. 205–220, locator not re-verified against primary PDF): an s-t flow f has minimum cost among all flows of value f.value if and only if its residual network contains no negative-cost simple cycle.

        Complementary slackness optimality theorem for s-t flows ([Sch03], Chapter 12, locator not re-verified against primary PDF): an s-t flow f has minimum cost among all flows of value f.value if and only if it admits complementary-slackness potentials.

        Output of minCostMaxFlow: an integral maximum s-t flow of minimum cost, node potentials certifying fixed-value minimum cost, and the number of negative-cycle cancellations.

        • flow : N.Flow

          Maximum s-t flow of minimum cost.

        • potential : Fin n → ℤ

          Complementary-slackness node potentials certifying optimality for flow.value.

        • cancellations : ℕ

          Number of negative-cycle cancellations performed in phase 2.

        Compute a minimum-cost maximum s-t flow on N : Network (Fin n) by finding an initial maximum flow via dinitzDense (or Network.Flow.zero N when N.s = N.t) and canceling negative residual cycles via CostNetwork.cycleCancelInt.

        Correctness of minCostMaxFlow ([Kle67], pp. 205–220; [Sch03], Chapter 12, locators not re-verified against primary PDF): returns a maximum s-t flow that minimizes flowCost cost among all flows of its value (hence among all maximum flows), together with complementary-slackness potentials certifying optimality.