Skip to content

feat(UnionFind): union by rank with verified O(log n) find#695

Open
barni120400 wants to merge 1 commit into
leanprover:mainfrom
barni120400:unionfind/full
Open

feat(UnionFind): union by rank with verified O(log n) find#695
barni120400 wants to merge 1 commit into
leanprover:mainfrom
barni120400:unionfind/full

Conversation

@barni120400

Copy link
Copy Markdown

Formalizes union-find (union by rank) in Cslib/Algorithms/Lean/UnionFind/, the "Union find" item under Verified data structures with time complexity (Batteries + Time Monad) in CONTRIBUTING. Placed next to MergeSort and reusing the TimeM cost monad the same way.

Main results, all sorry-free (lake build + lake test pass):

  • rank_le_log: every rank is at most ⌊log₂ n⌋.
  • findDepth_le_log: find dereferences at most ⌊log₂ n⌋ parent pointers.
  • link_wellFormed: union by rank preserves the well-formedness invariant, so the bound holds for any forest built from singletons by unions.
  • find_time_le_log / find_ret: a timed find in TimeM costs at most ⌊log₂ n⌋ ticks and returns the correct root.

No path compression yet (worst-case union-by-rank bound; the inverse-Ackermann amortized result would be a larger follow-up).

This is a single PR for the whole feature (I had first drafted it as a stack; consolidated here). Supersedes #692.

AI usage: developed with a mix of Claude Code and manual proving/review; the proofs were iterated against the Lean compiler.

Formalize a union-by-rank union-find over `Fin n` in
`Cslib/Algorithms/Lean/UnionFind/`, reusing the `TimeM` cost monad like `MergeSort`.

Main results (all sorry-free):
* `rank_le_log`: every rank is at most log2 n.
* `findDepth_le_log`: `find` dereferences at most log2 n parent pointers.
* `link_wellFormed`: union by rank preserves the well-formedness invariant, so the bound
  holds for any forest built from singletons by unions.
* `find_time_le_log` / `find_ret`: a timed `find` in `TimeM` costs at most log2 n ticks
  and returns the correct root.

No path compression yet (worst-case union-by-rank bound).

AI usage: developed with a mix of Claude Code and manual proving/review; proofs iterated
against the Lean compiler.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants