Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix UnionFind.fast_find path compression bug (#34626) - #34881

Closed
KaiOnCode wants to merge 1 commit into
scikit-learn:mainfrom
KaiOnCode:fix-union-find-path-compression
Closed

Fix UnionFind.fast_find path compression bug (#34626)#34881
KaiOnCode wants to merge 1 commit into
scikit-learn:mainfrom
KaiOnCode:fix-union-find-path-compression

Conversation

@KaiOnCode

Copy link
Copy Markdown

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

  • Restores O(α(n)) amortized time complexity for repeated fast_find calls
  • Without this fix, single-linkage clustering was O(n²) instead of O(n·α(n))
  • Results are unaffected - the fix only improves performance

Testing

  • Added unit test to verify path compression works correctly
  • Verified that the fix doesn't break existing functionality

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

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
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

@cakedev0

cakedev0 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Closing in favor of #34872 which was opened before and respects the PR template.

Feel free to review / make suggestion on #34872 if you want to contribute.

@cakedev0 cakedev0 closed this Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants