Fix UnionFind.fast_find path compression bug (#34626) - #34881
Closed
KaiOnCode wants to merge 1 commit into
Closed
Conversation
The path compression in UnionFind.fast_find was broken due to a Cython tuple assignment issue. The original code: evaluates the right-hand side as a tuple first, then assigns left to right. This means is updated before is assigned, so the path compression doesn't work correctly. The fix walks to the root first, then compresses every node on the path in a second pass: This restores O(α(n)) amortized time complexity for repeated fast_find calls, where α is the inverse Ackermann function. Without this fix, single-linkage clustering was O(n²) instead of O(n·α(n)). Fixes scikit-learn#34626
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
Contributor
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.
This PR fixes issue #34626 - a path compression bug in UnionFind.fast_find that caused O(n²) performance in single-linkage clustering.
Problem
The path compression in UnionFind.fast_find was broken due to a Cython tuple assignment issue:
This evaluates the right-hand side as a tuple first, then assigns left to right. This means is updated before is assigned, so the path compression doesn't work correctly.
Solution
The fix walks to the root first, then compresses every node on the path in a second pass:
Impact
Testing
This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.