AFTD Auto-Formalizing Theoretical Domains

Kruskal.processEdges

defimported Lean

$\mathtt{processEdges}\,\mathit{es}\,\mathit{uf}\,\mathit{acc}$ scans the edge list $\mathit{es}$ left-to-right: an edge $e$ is added to the accumulator and its endpoints are merged whenever $\mathit{uf}.\mathtt{find}\,e.u \ne \mathit{uf}.\mathtt{find}\,e.v$; otherwise $e$ is skipped. The reversed accumulator is returned when the list is exhausted.

Statement

set_option relaxedAutoImplicit false in
set_option autoImplicit false in
def Kruskal.processEdges {n : ℕ} : List (WEdge n) → UF n → List (WEdge n) → List (WEdge n)
Defined in
TCSlib, `Kruskal.processEdges`
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.
Verified
07 Oct 2026
Axioms
none listed
Built on 5
  • Kruskal.UFThe union-find state for $n$ nodes is defined as a function $\mathrm{Fin}\,n \to \mathbb{N}$ mapping each node to its current…
  • Kruskal.UF.find$\mathtt{find}\,\mathit{uf}\,i$ returns the representative of node $i$ in the union-find state $\mathit{uf}$, defined simply as…
  • 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.WEdgeA weighted edge over $n$ vertices is a structure with two endpoints $u, v : \mathrm{Fin}\,n$ and a natural-number weight…
  • Kruskal.UF.merge$\mathtt{merge}\,\mathit{uf}\,i\,j$ unifies the components of nodes $i$ and $j$ by reassigning every node whose representative equals…
Used by 5
  • Kruskal.kruskal$\mathtt{kruskal}\,n\,\mathit{edges}$ sorts the edge list by weight and then runs processEdges on the sorted list starting from…
  • 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.processEdges_skipSkipping an intra-component edge in the sweep. Fix a number of nodes $n$, a union-find state $\mathit{uf} : \mathrm{Fin}\,n \to \bbn$, a…
  • Kruskal.processEdges_take_weightWeight contributed by an accepted edge. Fix a natural number $n$ and a union-find state $\mathit{uf}$ over $n$ nodes, and let $e$ be a…
  • Kruskal.processEdges_accAccumulator splits off as a reversed prefix. Fix $n \in \bbn$, and let $\mathit{es}$ be a list of weighted edges over $n$ vertices,…

Lean source view module on GitHub

set_option relaxedAutoImplicit false in
set_option autoImplicit false in
def Kruskal.processEdges {n : ℕ} : List (WEdge n) → UF n → List (WEdge n) → List (WEdge n)
  | [], _, acc => acc.reverse
  | e :: rest, uf, acc =>
    if uf.find e.u ≠ uf.find e.v then
      processEdges rest (uf.merge e.u e.v) (e :: acc)
    else
      processEdges rest uf acc
See it in the graphDiscuss this resultChallenge it