Documentation

CslibFrontier.Algorithms.Lean.Arborescence.ChuLiuEdmonds

Minimum-cost arborescences: the Chu–Liu/Edmonds algorithm #

Given a finite digraph G with decidable arcs, arc costs c : V → V → β in a linearly ordered additive commutative monoid (for example ℕ, ℤ or ℚ; negative costs are allowed), and a root r, the algorithm Cslib.Algorithms.Lean.Arborescence.edmonds returns a spanning in-arborescence of G rooted at r of minimum total arc cost, or none if G has no spanning in-arborescence rooted at r ([CL65], [Edm67a]).

An in-arborescence is represented by its parent function p : V → V: p r = r, v → p v is an arc of G for v ≠ r, and every vertex reaches r by iterating p (Cslib.Algorithms.Lean.Arborescence.IsInArborescenceParent). Its cost is ∑ v ≠ r, c v (p v). Parent functions correspond exactly to the spanning in-arborescences T ≤ G of Digraph.IsInArborescence (isInArborescenceParent_iff, built on the parent-function API Digraph.parentDigraph of CslibFrontier.Foundations.Combinatorics.Digraph.Arborescence), and edmonds_optimal states optimality directly over those digraphs. Out-arborescences (arcs directed away from the root, Edmonds' convention) are handled by reversing the arcs (edmondsOut_spec).

The algorithm #

Every non-root vertex selects a cheapest arc v → cheapestParent v to another vertex. If the selection has no cycle, it is an arborescence, and it is optimal because each vertex pays the least it can. Otherwise, take a cycle C of the selection and contract it onto one of its vertices x0, keeping the vertex type:

Then the contracted instance is solved recursively and the result expanded (expandParent): the vertex of C that attains the exit leaves C, the other vertices of C keep their selected arcs, and an arc into x0 becomes the cheapest arc into C. Expansion preserves cost exactly (expandParent_spec). Conversely, every arborescence of the original instance contracts to one of the contracted instance of at most the same cost (exists_contract_le), the exchange argument: the vertex of C closest to the root keeps its arc out of C, and the other vertices of C gain nothing over their selected arcs. Hence an optimal solution of the contracted instance expands to an optimal solution of the original one.

The contracted cost of leaving the cycle includes the selected costs of the other cycle vertices, rather than subtracting the selected cost of the leaving vertex as in the usual presentation with reduced costs. This is the same up to a constant, and needs no subtraction, so the costs may lie in an ordered monoid such as ℕ. Contraction strictly shrinks the set of vertices with an arc to a vertex other than themselves and the root, so |V| levels of recursion suffice (edmonds runs edmondsAux with fuel FinEnum.card V).

Main definitions #

Main results #

Source mapping #

[Edm67a] treats spanning arborescences with arcs directed away from a prescribed root, maximum or minimum weight, real weights. Section 2 reduces optimum spanning arborescences to optimum branchings. Theorem 1, stated in Section 3, asserts a good (polynomially bounded) algorithm for optimum branchings; Section 4 gives the algorithm, which selects the best arc into each node and contracts cycles, and its proof. This module proves correctness and optimality of that cycle-contraction algorithm, specialised to rooted minimum-cost spanning arborescences. It does not prove the polynomial-time content of Theorem 1 (see Limitations). [CL65] gives the same contraction algorithm for shortest arborescences (not inspected here). [Sch03], Section 52.1 ("Finding a shortest r-arborescence"), is a textbook locator (from the table of contents; not inspected). The theorems here are for in-arborescences, with costs in any linearly ordered additive commutative monoid; edmondsOut_spec is Edmonds' orientation.

Limitations #

edmonds is executable but not efficient, and no running-time bound is proved. Each level of the recursion receives the contracted arcs and costs as closures over those of the previous level, without memoisation, so evaluating one contracted arc or cost re-evaluates the previous level many times, and the worst-case running time is exponential in the number of nested contractions. A polynomial version, such as the O(n m) bound, needs materialised per-level arc and cost tables; Tarjan's O(m log n) and the Gabow–Galil–Spencer–Tarjan O(m + n log n) implementations are not formalized either. Optimum branchings (forests of maximum weight, Edmonds' Theorem 1 itself) and the branching polytope (Edmonds' Theorem 2) are not treated.

References #

Parent functions of arborescences #

structure Cslib.Algorithms.Lean.Arborescence.IsInArborescenceParent {V : Type u_1} (R : V → V → Prop) (r : V) (p : V → V) :

p is the parent function of a spanning in-arborescence rooted at r in the relation R: the root is fixed, every other vertex has an arc v → p v, and every vertex reaches r by iterating p.

  • root : p r = r

    The root is its own parent.

  • adj (v : V) : v ≠ r → R v (p v)

    Every other vertex has an arc to its parent.

  • reach (v : V) : ∃ (n : ℕ), p^[n] v = r

    Every vertex reaches the root.

theorem Cslib.Algorithms.Lean.Arborescence.IsInArborescenceParent.ne {V : Type u_1} {R : V → V → Prop} {r : V} {p : V → V} (h : IsInArborescenceParent R r p) {v : V} (hv : v ≠ r) :
p v ≠ v

In an arborescence no vertex other than the root is its own parent.

def Cslib.Algorithms.Lean.Arborescence.parentCost {V : Type u_1} {β : Type u_2} [Fintype V] [DecidableEq V] [AddCommMonoid β] (c : V → V → β) (r : V) (p : V → V) :
β

The cost ∑ v ≠ r, c v (p v) of the arcs v → p v of a parent function.

Correspondence with Digraph.IsInArborescence #

Parent functions of spanning in-arborescences of G are exactly the parent functions whose digraph is a spanning in-arborescence (Digraph.IsInArborescence) contained in G.

def Cslib.Algorithms.Lean.Arborescence.arcCost {V : Type u_1} {β : Type u_2} [Fintype V] [AddCommMonoid β] (T : Digraph V) [DecidableRel T.Adj] (c : V → V → β) :
β

The arc cost ∑_{v → w ∈ T} c v w of a digraph.

theorem Cslib.Algorithms.Lean.Arborescence.arcCost_parentDigraph {V : Type u_1} {β : Type u_2} [Fintype V] [DecidableEq V] [AddCommMonoid β] (r : V) (p : V → V) (c : V → V → β) :

The arc cost of the digraph of a parent function is its cost.

Cheapest arcs #

def Cslib.Algorithms.Lean.Arborescence.candidates {V : Type u_1} [FinEnum V] (a : V → V → Bool) (v : V) :

The possible parents of v: the vertices w ≠ v with an arc v → w.

theorem Cslib.Algorithms.Lean.Arborescence.mem_candidates {V : Type u_1} [FinEnum V] (a : V → V → Bool) {v w : V} :
w ∈ candidates a v ↔ a v w = true ∧ w ≠ v
def Cslib.Algorithms.Lean.Arborescence.cheapestParent {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] (a : V → V → Bool) (c : V → V → β) (r v : V) :
V

The root, for r; otherwise a cheapest candidate parent (or v itself if there is none).

theorem Cslib.Algorithms.Lean.Arborescence.cheapestParent_root {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] (a : V → V → Bool) (c : V → V → β) (r : V) :
cheapestParent a c r r = r
theorem Cslib.Algorithms.Lean.Arborescence.cheapestParent_spec {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] (a : V → V → Bool) (c : V → V → β) (r : V) {v : V} (hv : v ≠ r) (hne : candidates a v ≠ []) :
a v (cheapestParent a c r v) = true ∧ cheapestParent a c r v ≠ v ∧ ∀ (w : V), a v w = true → w ≠ v → c v (cheapestParent a c r v) ≤ c v w

Cycles of the selection #

def Cslib.Algorithms.Lean.Arborescence.findCycle {V : Type u_1} [FinEnum V] (r : V) (f : V → V) :

A vertex other than r on a cycle of f, if there is one.

theorem Cslib.Algorithms.Lean.Arborescence.reach_of_findCycle_none {V : Type u_1} [FinEnum V] (r : V) {f : V → V} (h : findCycle r f = none) (v : V) :
∃ (n : ℕ), f^[n] v = r

If f has no cycle avoiding r, then every vertex reaches r by iterating f.

def Cslib.Algorithms.Lean.Arborescence.cycleOf {V : Type u_1} [FinEnum V] (f : V → V) (x0 : V) :

The orbit of x0 under f; the cycle through x0 when x0 is periodic.

theorem Cslib.Algorithms.Lean.Arborescence.mem_cycleOf {V : Type u_1} [FinEnum V] {f : V → V} {x0 y : V} :
y ∈ cycleOf f x0 ↔ ∃ n < FinEnum.card V, f^[n] x0 = y
theorem Cslib.Algorithms.Lean.Arborescence.iterate_mem_cycleOf {V : Type u_1} [FinEnum V] {f : V → V} {x0 : V} {m : ℕ} (hm : Function.IsPeriodicPt f m x0) (hm0 : 0 < m) (hmc : m ≤ FinEnum.card V) (n : ℕ) :
f^[n] x0 ∈ cycleOf f x0
theorem Cslib.Algorithms.Lean.Arborescence.map_mem_cycleOf {V : Type u_1} [FinEnum V] {f : V → V} {x0 : V} {m : ℕ} (hm : Function.IsPeriodicPt f m x0) (hm0 : 0 < m) (hmc : m ≤ FinEnum.card V) {y : V} (hy : y ∈ cycleOf f x0) :
f y ∈ cycleOf f x0
theorem Cslib.Algorithms.Lean.Arborescence.cycleOf_reach {V : Type u_1} [FinEnum V] {f : V → V} {x0 : V} {m : ℕ} (hm : Function.IsPeriodicPt f m x0) (hm0 : 0 < m) {y z : V} (hy : y ∈ cycleOf f x0) (hz : z ∈ cycleOf f x0) :
∃ (n : ℕ), f^[n] y = z

On a cycle, every vertex reaches every other one.

theorem Cslib.Algorithms.Lean.Arborescence.root_notMem_cycleOf {V : Type u_1} [FinEnum V] (r : V) {f : V → V} {x0 : V} {m : ℕ} (hr : f r = r) (hx0 : x0 ≠ r) (hm : Function.IsPeriodicPt f m x0) (hm0 : 0 < m) (hmc : m ≤ FinEnum.card V) :
r ∉ cycleOf f x0

Contraction and expansion #

def Cslib.Algorithms.Lean.Arborescence.exitCost {V : Type u_1} {β : Type u_2} [FinEnum V] [AddCommMonoid β] (c : V → V → β) (f : V → V) (C : Finset V) (x w : V) :
β

The cost of leaving C from x along x → w, plus the selected arcs y → f y of the other vertices of C.

def Cslib.Algorithms.Lean.Arborescence.bestExit {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] (a : V → V → Bool) (c : V → V → β) (f : V → V) (C : Finset V) (w : V) :

A vertex of C with an arc to w of least exit cost, if any.

def Cslib.Algorithms.Lean.Arborescence.bestEntry {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] (a : V → V → Bool) (c : V → V → β) (C : Finset V) (u : V) :

A cheapest arc from u into C, by its head, if any.

def Cslib.Algorithms.Lean.Arborescence.contractAdj {V : Type u_1} [FinEnum V] (a : V → V → Bool) (C : Finset V) (x0 r u w : V) :

The arcs after contracting C onto x0: x0 has an arc to w ∉ C if some vertex of C has, u ∉ C has an arc to x0 if it has one into C, the other vertices of C have only an arc to r, and arcs outside C are unchanged.

def Cslib.Algorithms.Lean.Arborescence.contractCost {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] (a : V → V → Bool) (c : V → V → β) (f : V → V) (C : Finset V) (x0 u w : V) :
β

The arc costs after contracting C onto x0: leaving x0 costs the best exit cost, entering x0 costs the cheapest arc into C, the other vertices of C pay 0, and arcs outside C keep their costs.

def Cslib.Algorithms.Lean.Arborescence.expandParent {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] (a : V → V → Bool) (c : V → V → β) (f : V → V) (C : Finset V) (x0 : V) (q : V → V) (v : V) :
V

Expand a parent function q of the contracted instance: the vertex of C attaining the best exit towards q x0 takes that arc, the other vertices of C keep f, and an arc into x0 becomes the cheapest arc into C.

theorem Cslib.Algorithms.Lean.Arborescence.expandParent_spec {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] {a : V → V → Bool} {c : V → V → β} {f : V → V} {C : Finset V} {x0 r : V} (hfa : ∀ y ∈ C, a y (f y) = true) (hfC : ∀ y ∈ C, f y ∈ C) (hx0 : x0 ∈ C) (hrC : r ∉ C) (hreach : ∀ y ∈ C, ∀ z ∈ C, ∃ (n : ℕ), f^[n] y = z) {q : V → V} (hq : IsInArborescenceParent (fun (u w : V) => contractAdj a C x0 r u w = true) r q) :
IsInArborescenceParent (fun (u w : V) => a u w = true) r (expandParent a c f C x0 q) ∧ parentCost c r (expandParent a c f C x0 q) = parentCost (contractCost a c f C x0) r q

Expansion. If C is a cycle of f avoiding r, along arcs of a, then every arborescence q of the contracted instance expands to an arborescence of the original instance of the same cost.

theorem Cslib.Algorithms.Lean.Arborescence.exists_contract_le {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] {a : V → V → Bool} {c : V → V → β} {f : V → V} {C : Finset V} {x0 r : V} [IsOrderedAddMonoid β] (hfmin : ∀ (v : V), v ≠ r → ∀ (w : V), a v w = true → w ≠ v → c v (f v) ≤ c v w) (hx0 : x0 ∈ C) (hrC : r ∉ C) {t : V → V} (ht : IsInArborescenceParent (fun (u w : V) => a u w = true) r t) :
∃ (q : V → V), IsInArborescenceParent (fun (u w : V) => contractAdj a C x0 r u w = true) r q ∧ parentCost (contractCost a c f C x0) r q ≤ parentCost c r t

Exchange. If every vertex v ≠ r selects a cheapest arc v → f v, then every arborescence t of the original instance contracts to an arborescence of the contracted instance of at most the same cost: the vertex of C closest to r in t keeps its arc out of C, the other vertices of C point to r, and arcs into C are redirected to x0.

The algorithm #

def Cslib.Algorithms.Lean.Arborescence.liveVertices {V : Type u_1} [FinEnum V] (a : V → V → Bool) (r : V) :

The vertices other than r with an arc to a vertex other than themselves and r. Contraction strictly shrinks this set (liveVertices_ssubset).

theorem Cslib.Algorithms.Lean.Arborescence.liveVertices_ssubset {V : Type u_1} [FinEnum V] {a : V → V → Bool} {f : V → V} {C : Finset V} {x0 r : V} (hfC : ∀ y ∈ C, f y ∈ C) (hfa : ∀ y ∈ C, a y (f y) = true ∧ f y ≠ y) (hx0 : x0 ∈ C) (hrC : r ∉ C) :

Contraction strictly shrinks the set of live vertices.

A graph has strictly fewer live vertices than FinEnum.card V (since r is not live).

def Cslib.Algorithms.Lean.Arborescence.edmondsAux {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] (r : V) :
ℕ → (V → V → Bool) → (V → V → β) → Option (V → V)

The Chu–Liu/Edmonds recursion with fuel n, on arcs a and costs c: select cheapest arcs; return them if they form no cycle, otherwise contract a cycle, recurse and expand.

def Cslib.Algorithms.Lean.Arborescence.edmonds {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] (G : Digraph V) [DecidableRel G.Adj] (c : V → V → β) (r : V) :
Option (V → V)

The Chu–Liu/Edmonds algorithm: a minimum-cost spanning in-arborescence of G rooted at r, as a parent function, or none if G has no spanning in-arborescence rooted at r.

def Cslib.Algorithms.Lean.Arborescence.edmondsOut {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] (G : Digraph V) [DecidableRel G.Adj] (c : V → V → β) (r : V) :
Option (V → V)

The Chu–Liu/Edmonds algorithm for out-arborescences, with arcs directed away from the root: p v → v is an arc of G for v ≠ r, and the cost is ∑ v ≠ r, c (p v) v.

theorem Cslib.Algorithms.Lean.Arborescence.edmonds_spec {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] [IsOrderedAddMonoid β] {G : Digraph V} [DecidableRel G.Adj] {c : V → V → β} {r : V} :
(∀ (p : V → V), edmonds G c r = some p → IsInArborescenceParent G.Adj r p ∧ ∀ (t : V → V), IsInArborescenceParent G.Adj r t → parentCost c r p ≤ parentCost c r t) ∧ (edmonds G c r = none → ∀ (t : V → V), ¬IsInArborescenceParent G.Adj r t)

Correctness of Chu–Liu/Edmonds. If edmonds G c r = some p, then p is the parent function of a spanning in-arborescence of G rooted at r whose cost is at most that of every other one; if edmonds G c r = none, then G has no spanning in-arborescence rooted at r.

theorem Cslib.Algorithms.Lean.Arborescence.edmonds_eq_none_iff {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] [IsOrderedAddMonoid β] {G : Digraph V} [DecidableRel G.Adj] {c : V → V → β} {r : V} :
edmonds G c r = none ↔ ∀ (t : V → V), ¬IsInArborescenceParent G.Adj r t

edmonds fails exactly when there is no spanning in-arborescence rooted at r.

theorem Cslib.Algorithms.Lean.Arborescence.edmonds_optimal {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] [IsOrderedAddMonoid β] {G : Digraph V} [DecidableRel G.Adj] {c : V → V → β} {r : V} {p : V → V} (hp : edmonds G c r = some p) :

Optimality over spanning in-arborescences. If edmonds G c r = some p, then the digraph of p is a spanning in-arborescence of G rooted at r (Digraph.IsInArborescence), and its arc cost is at most that of every spanning in-arborescence T ≤ G rooted at r.

theorem Cslib.Algorithms.Lean.Arborescence.edmondsOut_spec {V : Type u_1} {β : Type u_2} [FinEnum V] [LinearOrder β] [AddCommMonoid β] [IsOrderedAddMonoid β] {G : Digraph V} [DecidableRel G.Adj] {c : V → V → β} {r : V} :
(∀ (p : V → V), edmondsOut G c r = some p → IsInArborescenceParent (fun (u w : V) => G.Adj w u) r p ∧ ∀ (t : V → V), IsInArborescenceParent (fun (u w : V) => G.Adj w u) r t → parentCost (fun (u w : V) => c w u) r p ≤ parentCost (fun (u w : V) => c w u) r t) ∧ (edmondsOut G c r = none → ∀ (t : V → V), ¬IsInArborescenceParent (fun (u w : V) => G.Adj w u) r t)

Correctness for out-arborescences. If edmondsOut G c r = some p, then every vertex v ≠ r has an arc p v → v of G, iterating p reaches r, and the cost ∑ v ≠ r, c (p v) v is least among all such p; if it is none, there is no such p.