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

Skip to content

FIX Restore path compression in UnionFind.fast_find - #34872

Open
cboseak wants to merge 2 commits into
scikit-learn:mainfrom
cboseak:fast-find-fix
Open

FIX Restore path compression in UnionFind.fast_find#34872
cboseak wants to merge 2 commits into
scikit-learn:mainfrom
cboseak:fast-find-fix

Conversation

@cboseak

@cboseak cboseak commented Sep 2, 2026

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #34626.

What does this implement/fix? Explain your changes.

UnionFind.fast_find finds the correct root, but its existing path-compression loop does not repoint the queried node. The chained assignment advances the traversal variable before using it as the parent-array index. A lookup that starts at a root can also write through parent[-1] because the method enables Cython wraparound. The returned root generally remains correct, so the defect is visible primarily as quadratic linkage-labeling time on adversarial trees rather than incorrect clustering output.

This changes fast_find to use two explicit passes: first locate the root, then walk the original path while saving each next parent and repointing the current node to that root. It also removes @cython.wraparound(True) because negative indexing is no longer needed.

Performance

Direct local timings of the workload used by SingleLinkageLabelBenchmark produced:

n_samples Baseline median Fixed median Speedup
2,000 2.476 ms 0.0620 ms 39.9x
16,000 166.372 ms 0.3943 ms 421.9x

The measured revisions were 8a6f766c6d (baseline) and 0bec25b5d9 (fix).

These are direct local timings rather than an asv continuous result. The loop counts
per repeat were 100 and 2 for the 2,000- and 16,000-sample baseline cases, and 5,000
and 500 for the corresponding fixed cases.

Introduce yourself

AI usage disclosure

I used AI assistance for:

  • Some code generation
    • some AI generated code for the initial version of the code but tweaked and completed by human
  • Test and benchmark generation
  • Research and understanding

@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 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

We don't really maintain the ASV benchmarks anymore but your addition is small, so I'm not against keeping it.

@cakedev0

cakedev0 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR, you can mark it ready if you want (this might requires you to draft/close other PRs, I think this is some kind of new rule on this repo).

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Sep 4, 2026
@cboseak

cboseak commented Sep 4, 2026

Copy link
Copy Markdown
Author

Thanks for the PR, you can mark it ready if you want (this might requires you to draft/close other PRs, I think this is some kind of new rule on this repo).

Thanks, I left as draft so I could add the changelog entry. Doing that now and will mark as ready after.

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Sep 4, 2026
@cboseak
cboseak marked this pull request as ready for review September 4, 2026 12:40
@cakedev0 cakedev0 added the Waiting for Second Reviewer First reviewer is done, need a second one! label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cython module:cluster Waiting for Second Reviewer First reviewer is done, need a second one!

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