Documentation

CslibFrontier.Algorithms.Lean.MaxFlow.Dinitz

Dinitz's Blocking-Flow Maximum-Flow Algorithm #

Formalizes an executable version of Dinitz's algorithm ([Din70]; [Sch03], Section 10.6) over Network (Fin n): repeatedly compute the breadth-first levels of the residual network, find a blocking flow of the level network, and augment by it, until N.t is no longer residually reachable from N.s.

Mathematical and algorithmic overview #

  1. Greedy blocking flows (greedyBlockingFlow): inside a level network L, while a path of unsaturated arcs joins the terminals (found by bfs on the slack network Flow.slackNet), push its bottleneck. Since a level network has no antiparallel arcs, each push only increases arc flows and saturates a new arc (Flow.card_unsatArcs_augmentByBottleneck_lt), so the loop stops with a blocking flow after at most |Flow.unsatArcs| pushes (greedyBlockingFlow_spec), which is at most |N.posArcs| at the zero flow of a level network (Flow.card_unsatArcs_zero_levelNet_le).
  2. Phases (dinitzLoop, dinitz): each phase computes Flow.bfsDist (equal to Flow.resDist), stops if N.t is unreachable, and otherwise augments by a greedy blocking flow of the level network. The phases form a run of Dinitz phases (dinitz_isDinitzRun), hence there are at most n - 1 of them (Network.IsDinitzRun.length_le).
  3. Correctness (dinitz_correct): the final flow has no augmenting path, so it is a maximum flow and its residual-reachability cut is a minimum cut of equal capacity, by the max-flow min-cut API of CslibFrontier.Algorithms.Lean.MaxFlow.Basic.

Cost model and limitations #

Main definitions #

Main results #

References #

Greedy blocking flows #

theorem Cslib.Algorithms.Lean.MaxFlow.Network.Flow.le_augmentByBottleneck_of_slackNet {V : Type u_1} [Fintype V] {L : Network V} [DecidableEq V] (hanti : ∀ (u v : V), 0 < L.cap u v → L.cap v u = 0) {g : L.Flow} {p : List V} (hp : (zero g.slackNet).IsAugmentingPath p) (u v : V) :
g.toFun u v ≤ (g.augmentByBottleneck p ⋯).toFun u v

Pushing the bottleneck along a path of unsaturated arcs of a network without antiparallel arcs only increases arc flows.

theorem Cslib.Algorithms.Lean.MaxFlow.Network.Flow.exists_saturated_augmentByBottleneck_of_slackNet {V : Type u_1} [Fintype V] {L : Network V} [DecidableEq V] (hst : L.s ≠ L.t) {g : L.Flow} {p : List V} (hp : (zero g.slackNet).IsAugmentingPath p) :
∃ (e : V × V), g.toFun e.1 e.2 < L.cap e.1 e.2 ∧ L.cap e.1 e.2 ≤ (g.augmentByBottleneck p ⋯).toFun e.1 e.2

The bottleneck push saturates an arc that was unsaturated.

theorem Cslib.Algorithms.Lean.MaxFlow.Network.Flow.card_unsatArcs_augmentByBottleneck_lt {V : Type u_1} [Fintype V] {L : Network V} [DecidableEq V] (hanti : ∀ (u v : V), 0 < L.cap u v → L.cap v u = 0) (hst : L.s ≠ L.t) {g : L.Flow} {p : List V} (hp : (zero g.slackNet).IsAugmentingPath p) :

Each bottleneck push along unsaturated arcs saturates a new arc and keeps saturated arcs saturated.

A level network has at most |N.posArcs| arcs: it has no antiparallel arcs and its arcs lie in N.symArcs.

Executable Dinitz over Fin n #

Tabulate a flow as a FlowMatrix, so that later lookups are O(1).

Breadth-first distances Flow.bfsDist of f, tabulated.

@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.vget_bfsDistVec {n : ℕ} {N : Network (Fin n)} (f : N.Flow) (v : Fin n) :

Result of the greedy blocking-flow search.

  • flow : FlowMatrix L

    The flow found.

  • pushes : ℕ

    Number of bottleneck pushes performed.

Greedy blocking flow: while a path of unsaturated arcs joins the terminals (found by bfs on slackNet), push its bottleneck.

theorem Cslib.Algorithms.Lean.MaxFlow.greedyBlockingFlow_spec {n : ℕ} {L : Network (Fin n)} {adj : Fin n → List (Fin n)} (hadj : AdjValid L adj) (hanti : ∀ (u v : Fin n), 0 < L.cap u v → L.cap v u = 0) (hst : L.s ≠ L.t) (fuel : ℕ) (G : FlowMatrix L) (k : ℕ) :

Greedy blocking flows: on a network L without antiparallel arcs and with L.s ≠ L.t, with valid neighbor lists and enough fuel, greedyBlockingFlow returns a blocking flow after at most |G.toFlow.unsatArcs| further bottleneck pushes.

Output of Dinitz's algorithm on N : Network (Fin n).

  • flow : N.Flow

    Final integral flow on N.

  • cut : Finset (Fin n)

    Residual-reachability cut certifying optimality (flow.reachableCut).

  • phases : ℕ

    Number of blocking-flow phases.

  • augmentations : ℕ

    Total number of bottleneck pushes over all phases.

def Cslib.Algorithms.Lean.MaxFlow.dinitzLoop {n : ℕ} (N : Network (Fin n)) (adj : Fin n → List (Fin n)) :
ℕ → FlowMatrix N → ℕ → ℕ → DinitzOutput N

Phases of Dinitz's algorithm with fuel remaining phases: compute the breadth-first levels; stop if N.t is unreachable, and otherwise find a blocking flow of the level network greedily and augment by it.

def Cslib.Algorithms.Lean.MaxFlow.dinitz {n : ℕ} (N : Network (Fin n)) (adj : Fin n → List (Fin n)) :

Dinitz's blocking-flow algorithm on N : Network (Fin n) with candidate neighbor lists adj.

Dense specialization of dinitz using defaultAdj n.

theorem Cslib.Algorithms.Lean.MaxFlow.dinitz_isDinitzRun {n : ℕ} {N : Network (Fin n)} {adj : Fin n → List (Fin n)} (hadj : AdjValid N adj) :
∃ (flows : ℕ → N.Flow), Network.IsDinitzRun (dinitz N adj).phases flows ∧ flows 0 = Network.Flow.zero N ∧ flows (dinitz N adj).phases = (dinitz N adj).flow

The phases of dinitz form a run of Dinitz phases from the zero flow to the output flow.

theorem Cslib.Algorithms.Lean.MaxFlow.dinitz_correct {n : ℕ} {N : Network (Fin n)} {adj : Fin n → List (Fin n)} (hadj : AdjValid N adj) (hst : N.s ≠ N.t) :
have out := dinitz N adj; out.flow.IsMaxFlow ∧ N.IsMinCut out.cut ∧ out.flow.value = ↑(N.cutCapacity out.cut) ∧ out.cut = out.flow.reachableCut ∧ out.phases ≤ n - 1 ∧ out.augmentations ≤ (n - 1) * N.posArcs.card

Correctness of Dinitz's algorithm ([Din70]; [Sch03], Section 10.6): on N : Network (Fin n) with N.s ≠ N.t and valid neighbor lists, dinitz N adj returns a maximum flow and a minimum cut of equal value, after at most n - 1 phases of at most |N.posArcs| bottleneck pushes each.

theorem Cslib.Algorithms.Lean.MaxFlow.dinitzDense_correct {n : ℕ} {N : Network (Fin n)} (hst : N.s ≠ N.t) :
have out := dinitzDense N; out.flow.IsMaxFlow ∧ N.IsMinCut out.cut ∧ out.flow.value = ↑(N.cutCapacity out.cut) ∧ out.cut = out.flow.reachableCut ∧ out.phases ≤ n - 1 ∧ out.augmentations ≤ (n - 1) * N.posArcs.card

Correctness of dinitzDense without external adjacency obligations.