AFTD Auto-Formalizing Theoretical Domains

Kruskal.kruskal_optimal

theoremverifiedformalizationimported Lean

Optimality of Kruskal's algorithm. Let $E$ be a list of weighted edges on $n$ vertices, and let $S$ be another such list in which every edge is also an edge of $E$. Suppose that $S$ spans like $E$: whenever two vertices $u$ and $v$ are joined by a path through the edges of $E$, they are also joined by a path through the edges of $S$. Let $K$ denote the edge list produced by Kruskal's algorithm on $E$, and write $w(e)$ for the weight of an edge $e$. Then the total weight of $K$ is no greater than that of $S$: \[ \sum_{e \in K} w(e) \;\le\; \sum_{e \in S} w(e). \]

Statement

set_option relaxedAutoImplicit false in
set_option autoImplicit false in
theorem Kruskal.kruskal_optimal {n : ℕ} (E S : List (WEdge n))
    (hSsub : ∀ e ∈ S, e ∈ E) (hSspan : SpansLike S E) :
    totalWeight (kruskal n E) ≤ totalWeight S
Formalizes
Optimality of Kruskal's algorithm, as formalized in TCSlib (`Kruskal.kruskal_optimal`)
Lean proof
By Harsha Polavaram, from github.com/Shilun-Allan-Li/tcslib at c8a02591b1e5 (Copyright (c) 2026 Harsha Polavaram. All rights reserved, Apache-2.0). Compiled here against leanprover/lean4:v4.33.1, Mathlib v4.33.1; written for leanprover/lean4:v4.25.0.
What was changed in Kruskal.kruskal_optimal
--- TCSlib/GraphTheory/Kruskal/Optimality.lean:165 (Kruskal.kruskal_optimal)
+++ as compiled here
@@ -1,2 +1,4 @@
-theorem kruskal_optimal {n : ℕ} (E S : List (WEdge n))
+set_option relaxedAutoImplicit false in
+set_option autoImplicit false in
+theorem Kruskal.kruskal_optimal {n : ℕ} (E S : List (WEdge n))
     (hSsub : ∀ e ∈ S, e ∈ E) (hSspan : SpansLike S E) :
@@ -15,3 +17,3 @@
       fun h => hSspan a b <| reach_mono h fun e he => List.mem_mergeSort.mp he⟩
-  · have h := List.sorted_mergeSort
+  · have h := List.pairwise_mergeSort
       (le := fun e₁ e₂ : WEdge n => decide (e₁.weight ≤ e₂.weight))
Verified
16 Oct 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 14
  • Kruskal.processEdges_optimalOptimality of the greedy edge-selection sweep. Let $\mathit{edges}$ be a list of weighted edges on $n$ vertices, sorted in nondecreasing…
  • Kruskal.mergeAll_init_iffCorrectness of union-find merging from the initial state. Fix a natural number $n$, and let $\mathit{edges}$ be a list of weighted edges…
  • Kruskal.WEdgeA weighted edge over $n$ vertices is a structure with two endpoints $u, v : \mathrm{Fin}\,n$ and a natural-number weight…
  • Kruskal.kruskal$\mathtt{kruskal}\,n\,\mathit{edges}$ sorts the edge list by weight and then runs processEdges on the sorted list starting from…
  • Kruskal.SpansLike$\mathtt{SpansLike}\,F\,E$ holds when the edge list $F$ is at least as connected as $E$: for every pair of vertices $u, v$, if $u$ and $v$…
  • Kruskal.reach_monoMonotonicity of reachability under edge addition. Fix $n$, let $e_1$ and $e_2$ be lists of weighted edges on the vertex set…
  • Kruskal.totalWeightThe total weight of an edge list is the sum of the weights of all its edges:…
  • Kruskal.UF.init_findFind on the initial union-find. Consider the identity union-find on $n$ nodes, in which every node is its own representative. For every…
  • Kruskal.UF.find_defFind equals direct lookup. Let $\mathit{uf} \colon \mathrm{Fin}\,n \to \bbn$ be a union-find state on $n$ nodes, and let $i$ be one of its…
  • Kruskal.totalWeight_nilTotal weight of the empty edge list. Over any number of vertices $n$, the total weight of the empty list of weighted edges is $0$; that…
  • Kruskal.UF.mergeAll$\mathtt{mergeAll}\,\mathit{uf}\,\mathit{es}$ folds a list of weighted edges $\mathit{es}$ into the union-find $\mathit{uf}$ by calling…
  • Kruskal.UF.init$\mathtt{init}\,n$ constructs the identity union-find on $n$ nodes: every node $i$ is its own representative, i.e.\…
  • Kruskal.Reach$\mathtt{Reach}\,\mathit{edges}$ is the reflexive--transitive closure of $\mathtt{SymAdj}\,\mathit{edges}$; that is,…
  • Kruskal.SymAdjGiven a list of weighted edges $\mathit{edges}$ on $n$ vertices, $\mathtt{SymAdj}\,\mathit{edges}\,u\,v$ holds when there exists an edge…

Lean source view module on GitHub

set_option relaxedAutoImplicit false in
set_option autoImplicit false in
theorem Kruskal.kruskal_optimal {n : ℕ} (E S : List (WEdge n))
    (hSsub : ∀ e ∈ S, e ∈ E) (hSspan : SpansLike S E) :
    totalWeight (kruskal n E) ≤ totalWeight S := by
  refine processEdges_optimal _ _ [] ?_ S ?_ ?_ ?_
  · intro a b
    refine ⟨fun h => ?_, fun h => ?_⟩
    · cases Fin.ext h; exact Relation.ReflTransGen.refl
    · induction h with
      | refl => rfl
      | tail _ hcd _ => obtain ⟨_, h, _⟩ := hcd; cases h
  · exact fun e he => List.mem_mergeSort.mpr (hSsub e he)
  · intro a b
    rw [mergeAll_init_iff, mergeAll_init_iff]
    refine ⟨fun h => reach_mono h fun e he => List.mem_mergeSort.mpr (hSsub e he),
      fun h => hSspan a b <| reach_mono h fun e he => List.mem_mergeSort.mp he⟩
  · have h := List.pairwise_mergeSort
      (le := fun e₁ e₂ : WEdge n => decide (e₁.weight ≤ e₂.weight))
      (by intro a b c; simp; exact le_trans) (by intro a b; simp; exact le_total _ _) E
    exact h.imp (by intro a b h; simpa using h)
See it in the graphDiscuss this resultChallenge it