feat(UnionFind): union by rank with verified O(log n) find#695
Open
barni120400 wants to merge 1 commit into
Open
feat(UnionFind): union by rank with verified O(log n) find#695barni120400 wants to merge 1 commit into
barni120400 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 toMergeSortand reusing theTimeMcost monad the same way.Main results, all
sorry-free (lake build+lake testpass):rank_le_log: every rank is at most⌊log₂ n⌋.findDepth_le_log:finddereferences 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 timedfindinTimeMcosts 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.