Edmonds–Karp Shortest-Augmenting-Path Algorithm #
Formalizes the Edmonds–Karp algorithm ([EK72], Section 1, Theorem 1, pp. 249–253;
[CLRS09], Section 26.2, Lemma 26.7 and Theorem 26.8, pp. 727–730): augmenting an integral network
flow along breadth-first (fewest-arc) residual s-t paths guarantees monotonic residual
shortest-path distances and terminates in strongly polynomial time independent of arc capacities.
Mathematical and algorithmic overview #
- Residual shortest-path distance (
Flow.resDist): for any finite vertex typeV,f.resDist v : ℕis the minimum arc length< |V|of a positive-residual-capacity walk fromN.stov, or|V|whenvis unreachable. - Distance monotonicity (
Flow.resDist_le_augmentByBottleneck): augmentingfalong any shortest augmenting pathp(IsShortestAugmentingPath) never decreasesresDist vfor any vertexv([EK72], p. 251; [CLRS09], Lemma 26.7, pp. 727–728). - Critical-arc bound (
Network.shortestAugmentingSeq_length_le): each directed residual pair(u, v) ∈ N.symArcsbecomes a bottleneck critical arc at most|V| / 2times becauseresDist uincreases by at least2between critical occurrences, bounding any sequence of shortest-path augmentations byN.symArcs.card * (|V| / 2) ≤ |V| * N.posArcs.card([CLRS09], Theorem 26.8, p. 729; compare [EK72], Theorem 1, p. 251, which obtains(|V|^3 - |V|) / 4by combining source- and sink-distance monotonicity). - Executable
TimeMsolver (edmondsKarpM,edmondsKarpDenseM): overNetwork (Fin n)with candidate neighbor listsadj : Fin n → List (Fin n), maintains an explicitn × nVectormatrix (FlowMatrix N) forO(1)flow lookups and arc-by-arc bottleneck updates, finishing in at most(N.symArcs.card * (n / 2) + 2) * (adjSteps adj + 5 * n)charged steps (O(|V| |E|^2)on sparse nonempty adjacency lists andO(n^5)ondefaultAdj n).
Cost model and limitations #
- Charged (
TimeM ℕticks):2 * nticks ininitFlowMatrixM(nticks to allocate an all-zero row of lengthnandnticks to replicate that row reference acrossnvertices);n + adjSteps adjticks incountAdjMto compute sparse fueladjSteps adj * (n / 2)without denseFin n × Fin nenumeration;- per
bfsMcall (adjSteps adj + 3 * nticks upper bound,≥ nticks lower bound):nticks for visited/path-table initialization (replicateM),1tick per popped frontier vertex (≤ n),1tick per candidate neighborv ∈ adj u(≤ adjSteps adj), and eitherp.lengthticks to reverse the found augmenting path (≤ n) ornticks to extract the reachable cut (extractFinsetM); - per augmentation (
augmentFlowMatrixM,2 * (pathArcs p).length ≤ 2 * nticks):1tick per path arc to computeδ ← bottleneckMand1tick per path arc inupdateArcsMatMto update the forward and reverse entries ofFlowMatrix Nusingδ.
- Abstract unit-cost primitives:
Vectorindexing/update (vget,vset,fget,fset),Finset.insert, arithmetic onℕ, and evaluatingN.cap u vare treated asO(1)operations within each charged step; this models abstract RAM-style algorithmic steps inTimeM ℕ, not bit-level Turing-machine time or persistent-array reference-counting copy costs when shared zero rows are first mutated.
Main definitions #
Cslib.Algorithms.Lean.MaxFlow.Network.Flow.HasResWalkOfLen,Flow.resDist,Flow.IsShortestAugmentingPath: residual walk lengths, capped shortest-path distances, and fewest-arc augmenting paths.Cslib.Algorithms.Lean.MaxFlow.Network.symArcs,Network.posArcs,Network.IsShortestAugmentingSeq: active directed residual pairs and shortest-augmentation traces.Cslib.Algorithms.Lean.MaxFlow.AdjValid,adjSteps,defaultAdj,FlowMatrix,bfsM,Output,edmondsKarpM,edmondsKarpDenseM: executable BFS and Edmonds–Karp solver inTimeM ℕ.
Main results #
Cslib.Algorithms.Lean.MaxFlow.Network.Flow.resDist_le_augmentByBottleneck: shortest-path distance monotonicity.Cslib.Algorithms.Lean.MaxFlow.Network.shortestAugmentingSeq_length_leandshortestAugmentingSeq_length_le_card_mul_posArcs: polynomial augmentation bounds.Cslib.Algorithms.Lean.MaxFlow.edmondsKarpM_correctandedmondsKarpM_time_le: max-flow / min-cut certification and explicitTimeMstep bound.
References #
Active directed pairs (u, v) with u ≠ v that can ever carry positive residual capacity in
N (either N.cap u v > 0 or N.cap v u > 0).
Instances For
Non-loop directed arcs (u, v) with positive capacity N.cap u v > 0 in N.
Instances For
Existence of a positive-residual-capacity walk of exact arc length k from N.s to v.
- zero {V : Type u_1} [Fintype V] {N : Network V} {f : N.Flow} : f.HasResWalkOfLen N.s 0
- succ {V : Type u_1} [Fintype V] {N : Network V} {f : N.Flow} {u v : V} {k : ℕ} : f.HasResWalkOfLen u k → 0 < f.resCap u v → f.HasResWalkOfLen v (k + 1)
Instances For
Residual shortest-path distance from N.s to v, capped at Fintype.card V when v is
unreachable.
Instances For
Residual shortest-path triangle inequality: positive residual capacity on (u, v) gives
f.resDist v ≤ f.resDist u + 1 unconditionally (including when u is unreachable).
Every vertex on a positive-residual-capacity walk starting at N.s has resDist bounded by
the walk's arc length.
Every consecutive arc (u, v) along a shortest augmenting path advances f.resDist by 1 and
satisfies f.resDist v ≤ f.resDist N.t.
A shortest-augmenting-path trace of length T on N: flows 0, …, flows T linked by shortest
augmenting paths paths 0, …, paths (T - 1).
Instances For
Edmonds–Karp polynomial augmentation bound ([EK72], Section 1, Theorem 1,
p. 251; [CLRS09], Theorem 26.8, p. 729): when N.s ≠ N.t, every sequence of shortest-path
augmentations has length at most N.symArcs.card * (Fintype.card V / 2).
Sparse |V| * |E| corollary of shortestAugmentingSeq_length_le ([CLRS09], Theorem 26.8,
p. 729): every shortest-path augmentation sequence has length at most
Fintype.card V * N.posArcs.card.
Allocate Vector.replicate m a in m TimeM ticks (1 tick per slot).
Instances For
Total length of all candidate neighbor lists (2 * |E| on sparse symmetric adjacency lists,
n^2 on defaultAdj n).
Instances For
Dense candidate neighbor list enumerating List.finRange n at every vertex.
Instances For
Reversed-walk validity and executable BFS #
Result of a single BFS shortest-augmenting-path search for flow f.
Shortest residual
s-taugmenting path whentis reachable, ornone.Erased proof that any returned
some pis a valid augmenting path forf.Residual-reachability cut (
f.reachableCutwhenaugPath = none).
Instances For
Scan candidate neighbors vs of u, charging 1 tick per neighbor inspection.
Instances For
Specification of bfsM: when augPath = some p, p is a shortest residual s-t
augmenting path of length at most n; when augPath = none, f has no augmenting path and
cut = f.reachableCut.
Explicit n × n FlowMatrix and arc-by-arc bottleneck augmentation #
Concrete n × n Vector matrix storing arc flows, paired with erased feasibility proofs so
toFlow performs O(1) matrix lookups with zero stacked closures.
Concrete
n × nmatrix of arc flows.Capacity bound on every entry (erased
Prop).- conserve (v : Fin n) : v ≠ N.s → v ≠ N.t → ∑ u : Fin n, fget self.mat u v = ∑ w : Fin n, fget self.mat v w
Flow conservation at intermediate vertices (erased
Prop).
Instances For
View a FlowMatrix N as an N.Flow whose toFun is O(1) fget lookup.
Instances For
Allocate the all-zero FlowMatrix N in 2 * n TimeM ticks.
Instances For
Compute δ ← bottleneckM M.toFlow p ((pathArcs p).length ticks) and update M arc-by-arc
along pathArcs p using δ ((pathArcs p).length ticks).
Instances For
Bounded-fuel Edmonds–Karp loop over FlowMatrix N: runs bfsM and augments along shortest
paths while fuel remains, finishing with one certifying bfsM search when fuel = 0 or
augPath = none.
Instances For
Dense specialization of edmondsKarpM using defaultAdj n (List.finRange n at every
vertex), requiring no external adjacency structure.
Instances For
Edmonds–Karp correctness theorem ([EK72], Theorem 1, p. 251; [CLRS09],
Theorems 26.6 & 26.8, pp. 723, 729): on any integral network N : Network (Fin n) with N.s ≠ N.t
and valid candidate neighbor lists adj, edmondsKarpM N adj returns an integral maximum flow
out.flow and minimum s-t cut out.cut = out.flow.reachableCut with
out.flow.value = N.cutCapacity out.cut in at most N.symArcs.card * (n / 2) (and at most
n * N.posArcs.card) augmentations.
Edmonds–Karp TimeM complexity bound ([EK72], Theorem 1, p. 251; [CLRS09],
Theorem 26.8, p. 729): edmondsKarpM N adj terminates in at most
(N.symArcs.card * (n / 2) + 2) * (adjSteps adj + 5 * n) charged ticks (O(|V| |E|^2) when
adjSteps adj = O(|E|) and |V| = O(|E|)).
Limitations / cost model: each charged tick treats Vector indexing/update (FlowMatrix),
Finset.insert, ℕ arithmetic, and N.cap u v evaluation as unit-cost primitives; persistent
Vector copy-on-write and bit-level arithmetic costs are not modeled.
Sparse posArcs corollary of edmondsKarpM_time_le: edmondsKarpM N adj takes at most
(n * N.posArcs.card + 2) * (adjSteps adj + 5 * n) charged ticks.
Correctness of edmondsKarpDenseM without external adjacency obligations.
Polynomial O(n^5) TimeM step bound for edmondsKarpDenseM under the same unit-cost
Vector, Finset.insert, ℕ, and N.cap primitive model as edmondsKarpM_time_le.