Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Union-find: exact mutable find and full-compression contract

Proved
UnionFindAnalysis.find_contract

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

algo2leanalgorithmsclrsdata-structuresrunning-timetextbook

For a represented object and an in-range vertex vvv, a terminating UnionFind.find.Call returns the initial model's representative of vvv and rewrites every parent pointer on the visited path to that root. The theorem provides a finite path witness of length ppp, the matching compressed model, and the same final heap's representation and storage effect. Only the parent array may change.

Its exact increment function findCosts p is p+1p+1p+1 on uf_find, ppp on uf_compress and zero on every other clock. This is the operational foundation for the inverse-Ackermann bound, not a supplied runtime assumption.

The precondition Rep heap layout model connects an allocated object and distinct allocated parent/rank arrays to a valid forest on Fin n. Validity includes representative consistency, strictly increasing rank along nonroot parent links, and the root-mass rank bound. It does not assume a particular operation history. Vertex arguments belong to Fin n; no such argument exists when n=0n=0n=0.

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

Preamble
import Definitions.Def_Algo_union_find_Analysis
import Theorems.Thm_Algo2Lean_UnionFind_compress_valid

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

set_option autoImplicit false
Formal statement
theorem UnionFindAnalysis.find_contract :
    ∀ {n : Nat} (heap : Algo2LeanGenerated.Heap) (layout : UnionFindAnalysis.Layout)
      (model : Algo2Lean.UnionFind.State n),
      UnionFindAnalysis.Rep heap layout model →
        ∀ (vertex : Fin n),
          Exists.{1} fun length =>
            Exists.{1} fun after =>
              Algo2Lean.UnionFind.Find model vertex (model.representative vertex) length ∧
                Algo.union_find.UnionFind.find.Call heap
                    (Prod.mk.{0, 0} layout.object (↑(↑vertex : Nat) : Int)) after
                    (↑(↑(model.representative vertex) : Nat) : Int) ∧
                  UnionFindAnalysis.Rep after layout
                      (UnionFindAnalysis.findResult model vertex length) ∧
                    UnionFindAnalysis.Storage heap after (List.cons.{0} layout.parent List.nil.{0}) ∧
                      heap.Increments after (UnionFindAnalysis.findCosts length) := by
  sorry
Source
https://github.com/lunjiah/algo2lean/blob/0f79d5e4242e6477531c9ee4410409f5b1c92f76/examples/kruskal/analysis/union_find.lean#L518-L536

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