AFTD Auto-Formalizing Theoretical Domains

Kruskal.merge_swap_partition

theoremverifiedimported Lean

Merge order is irrelevant to the induced partition. Let $n$ be a natural number, let $\mathit{uf}\colon \mathrm{Fin}\,n \to \bbn$ be a union-find state, and let $a$ and $b$ be weighted edges over $n$ vertices, with endpoints $a_u, a_v$ and $b_u, b_v$ respectively. Form one state by merging the components of $a_u$ and $a_v$ and then merging the components of $b_u$ and $b_v$, and a second state by performing these two merges in the opposite order. Then the two resulting states induce the same partition of the nodes: for all $i, j : \mathrm{Fin}\,n$, the first state assigns $i$ and $j$ the same representative if and only if the second state does.

Statement

set_option relaxedAutoImplicit false in
set_option autoImplicit false in
lemma Kruskal.merge_swap_partition {n : ℕ} (uf : UF n) (a b : WEdge n) :
    UF.SamePartition
      ((uf.merge a.u a.v).merge b.u b.v)
      ((uf.merge b.u b.v).merge a.u a.v)
Source
TCSlib, `Kruskal.merge_swap_partition`
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
Classical.choiceQuot.soundpropext
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_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.SamePartition$\mathtt{SamePartition}\,\mathit{uf}_1\,\mathit{uf}_2$ holds when two union-find states induce the same equivalence relation on nodes: for…
  • 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 1
  • Kruskal.mergeAll_permPermutation invariance of edge merging. Fix $n$ and an initial union-find state $\mathit{uf} : \mathrm{Fin}\,n \to \bbn$. If two lists of…

Lean source view module on GitHub

set_option relaxedAutoImplicit false inset_option autoImplicit false inlemma Kruskal.merge_swap_partition {n : ℕ} (uf : UF n) (a b : WEdge n) :    UF.SamePartition      ((uf.merge a.u a.v).merge b.u b.v)      ((uf.merge b.u b.v).merge a.u a.v) := by  intros i j  by_cases hi : uf i = uf a.u <;> by_cases hj : uf j = uf a.u <;>    simp +decide [*, UF.merge] <;> grind
See it in the graphDiscuss this resultChallenge it