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

Skip to content

FIX restore UnionFind path compression in single-linkage clustering - #34873

Closed
ShamikOfficial wants to merge 1 commit into
scikit-learn:mainfrom
ShamikOfficial:fix-unionfind-path-compression-34626
Closed

FIX restore UnionFind path compression in single-linkage clustering#34873
ShamikOfficial wants to merge 1 commit into
scikit-learn:mainfrom
ShamikOfficial:fix-unionfind-path-compression-34626

Conversation

@ShamikOfficial

Copy link
Copy Markdown

What

UnionFind.fast_find in _hierarchical_fast.pyx was supposed to do path compression, but the Cython tuple assignment on the compression loop doesn't actually update self.parent[p] — it just rebinds a local variable. On adversarial MST inputs this degrades single-linkage to roughly O(n²) instead of the expected near-linear behavior.

This affects AgglomerativeClustering(linkage="single"), HDBSCAN, and anything else that goes through _single_linkage_label.

Fix

Replaced the broken loop with an explicit two-pass compression (find root, then walk back and point nodes directly at the root). Also removed @cython.wraparound(True) from fast_find since negative indexing isn't used there.

Tests

  • test_union_find_fast_find_compresses_path — verifies parent pointers get flattened after a find
  • test_union_find_fast_find_on_root_is_noop — calling find on a root shouldn't mutate the structure
  • Added an ASV benchmark with the adversarial MST from the issue

Fixes #34626

The fast_find method used a Cython tuple assignment that silently
disabled path compression, degrading single-linkage to O(n^2) on
adversarial inputs. Replace with an explicit two-pass compression
loop and add regression tests.

Fixes scikit-learn#34626
@github-actions

github-actions Bot commented Sep 2, 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.

UnionFind.fast_find's path compression never touches the queried node — quadratic linkage labelling

2 participants