Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Kruskal: termination, correctness and inverse-Ackermann running time

Proved
KruskalAnalysis.call_complete

by lunjia · Oct 11, 2026 · Mathlib 0df444a (Lean v4.33.1)

algo2leanalgorithmsclrsdata-structuresrunning-timetextbook

Let the initial heap contain allocated source, target and weight sequences for a graph with V≥0V\ge0V≥0 vertices and E≥0E\ge0E≥0 edges. Each edge endpoint must lie in [0,V)[0,V)[0,V); integer weights are unrestricted and the three sequences may alias. A terminating kruskal.Call returns a fresh valid flag sequence describing a minimum spanning forest, preserves all previously allocated caller storage, and satisfies

runtime⁡(clocksafter)≤runtime⁡(clocksbefore)+B(V,E).\operatorname{runtime}(clocks_{after})\le\operatorname{runtime}(clocks_{before})+B(V,E).runtime(clocksafter​)≤runtime(clocksbefore​)+B(V,E).

The final heap, flags, selected-edge list and bound belong to the same call. call_contract restates this result directly in terms of clock increments for subsequent callers.

For VVV vertices and EEE edges, define

B(V,E)=2E(3⌊log⁡2(E+1)⌋+2)+V+2+E(10α(V)+14).B(V,E)=2E\bigl(3\lfloor\log_2(E+1)\rfloor+2\bigr)+V+2+E\bigl(10\alpha(V)+14\bigr).B(V,E)=2E(3⌊log2​(E+1)⌋+2)+V+2+E(10α(V)+14).

The eight unit-weight clocks are sort_steps, heap_comparisons, heap_swaps, uf_init, uf_find, uf_compress, uf_union, and kruskal. Their sum is KruskalAnalysis.runtime; the displayed expression is KruskalAnalysis.runtimeBound. Thus the finite inequality has scale O(V+Elog⁡(E+1)+Eα(V))O(V+E\log(E+1)+E\alpha(V))O(V+Elog(E+1)+Eα(V)). The inverse-Ackermann improvement is in the disjoint-set term; sorting still costs O(Elog⁡(E+1))O(E\log(E+1))O(Elog(E+1)).

Costs count the named source ticks over mathematical integers and total integer-indexed sequences. They are not wall-clock time, bit complexity, or a count of every arithmetic operation, access, allocation, or compiler transition. Counters may start at arbitrary values; bounds refer to the increments during this call.

Part of the Algo2Lean CLRS series: Introduction to Algorithms, fourth edition, Chapter 21, §21.2 (Kruskal), the safe-edge principle in Theorem 21.1 (§21.1), and the union-by-rank/path-compression bound in Theorem 19.14 (§19.4).

<!-- algo2lean-provenance-v0.1.4 -->

Algo2Lean provenance and reproduction. These definitions target Algo2Lean v0.1.4, using the compiler source at public commit 0f79d5e4242e6477531c9ee4410409f5b1c92f76, and Lean 4.33.1. The versioned compiler location is Algo2Lean v0.1.4. Compile all supplied .algo files and imports to reproduce the raw generated definitions. Release publication and exact website parity require their separately recorded checks; this metadata alone does not assert them. Prove2Me imports and declaration packaging are mechanically adapted, with original hashes, compiler-reported source ranges, and adaptations recorded in transplant provenance. Exact reproduction does not prove the compiler correct.


Superseded Kruskal implementation

The revised complete-algorithm contract composes sorting with the separately proved post-sorting scan.

Current entry points: KruskalAnalysis.total_call_contract.

This post is deprecated in favor of the revised implementation. Its original formal content and proofs remain available for existing imports. The linked results concern the revised implementation; their formal statements need not be identical to this one.

Preamble
import Definitions.Def_Algo_kruskal_Analysis
import Theorems.Thm_KruskalAnalysis_scan_contract
import Theorems.Thm_SortIndicesAnalysis_call
import Theorems.Thm_UnionFindAnalysis_constructor_summary
import Theorems.Thm_UnionFindAnalysis_constructor_rep
import Theorems.Thm_Algo2Lean_Kruskal_Scan_minimum_spanning_forest

set_option autoImplicit true
set_option backward.isDefEq.respectTransparency false
set_option backward.defeqAttrib.useBackward true

set_option autoImplicit false
Formal statement
theorem KruskalAnalysis.call_complete :
    ∀ (heap : Algo2LeanGenerated.Heap) (size count : Int)
      (sources targets weights : Algo2LeanGenerated.SeqRef),
      Algo.kruskal.kruskal.ValidInput heap
          (Prod.mk.{0, 0} size
            (Prod.mk.{0, 0} sources (Prod.mk.{0, 0} targets (Prod.mk.{0, 0} weights count)))) →
        KruskalInput.WellFormed size count (heap.read sources) (heap.read targets) →
          Exists.{1} fun final =>
            Exists.{1} fun chosen =>
              Exists.{1} fun selected =>
                Algo.kruskal.kruskal.Call heap
                    (Prod.mk.{0, 0} size
                      (Prod.mk.{0, 0} sources (Prod.mk.{0, 0} targets (Prod.mk.{0, 0} weights count))))
                    final chosen ∧
                  Algo2Lean.Kruskal.MinimumSpanningForest
                      (KruskalInput.edges (heap.read sources) (heap.read targets) (heap.read weights)
                        count)
                      selected ∧
                    KruskalAnalysis.Flags (final.read chosen) selected ∧
                      LE.le.{0} (KruskalAnalysis.runtime final.clocks)
                          (HAdd.hAdd.{0, 0, 0} (KruskalAnalysis.runtime heap.clocks)
                            (KruskalAnalysis.runtimeBound size.toNat count.toNat)) ∧
                        heap.Footprint final List.nil.{0} List.nil.{0} List.nil.{0} ∧
                          LE.le.{0} heap.nextRef chosen ∧ LT.lt.{0} chosen final.nextRef := by
  sorry
Source
https://github.com/lunjiah/algo2lean/blob/0f79d5e4242e6477531c9ee4410409f5b1c92f76/examples/kruskal/analysis/kruskal.lean#L98-L162

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me