Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Kruskal: inverse-Ackermann running time of every returned execution

Proved
KruskalAnalysis.running_time

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

algo2leanalgorithmsclrsdata-structuresrunning-timetextbook

For well-formed finite endpoint data, every kruskal.ReturnsWithCost witness with clock increment function costs satisfies

KruskalAnalysis.runtime⁡(costs)≤B(V,E).\operatorname{KruskalAnalysis.runtime}(costs)\le B(V,E).KruskalAnalysis.runtime(costs)≤B(V,E).

Here VVV and EEE are the nonnegative vertex and edge counts. The weights may be arbitrary integers. This is a finite inequality for any completed execution; existence and the reusable heap/storage contract are established by call_contract.

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)).

Here nnn is the fixed number of elements, α(n)\alpha(n)α(n) is Algo2Lean.Ackermann.alpha, and Φ\PhiΦ is Algo2Lean.UnionFind.potential. Their definitions are in the separate Ackermann and union-find amortization definition bundles. The inverse is the least positive kkk with Ak(1)>nA_k(1)>nAk​(1)>n, where A0(r)=r+1A_0(r)=r+1A0​(r)=r+1 and Ak+1(r)A_{k+1}(r)Ak+1​(r) iterates AkA_kAk​ exactly r+1r+1r+1 times starting at rrr.

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

This returned-execution bound concerns the earlier implementation. The revised implementation has a reusable call contract and a total-runtime theorem including sorting.

Current entry points: KruskalAnalysis.total_call_contract; KruskalAnalysis.correctness_and_running_time.

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_call_complete

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

set_option autoImplicit false
Formal statement
theorem KruskalAnalysis.running_time :
    ∀ {size count : Int} {sources targets weights chosen : Algo2LeanGenerated.Seq}
      {costs : Algo2LeanGenerated.Clocks},
      KruskalInput.WellFormed size count sources targets →
        Algo.kruskal.kruskal.ReturnsWithCost
            (Prod.mk.{0, 0} size
              (Prod.mk.{0, 0} sources (Prod.mk.{0, 0} targets (Prod.mk.{0, 0} weights count))))
            chosen costs →
          LE.le.{0} (KruskalAnalysis.runtime costs)
            (KruskalAnalysis.runtimeBound size.toNat count.toNat) := by
  sorry
Source
https://github.com/lunjiah/algo2lean/blob/0f79d5e4242e6477531c9ee4410409f5b1c92f76/examples/kruskal/analysis/kruskal.lean#L264-L271

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