FIX Restore path compression in UnionFind.fast_find - #34872
Conversation
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
cakedev0
left a comment
There was a problem hiding this comment.
LGTM.
We don't really maintain the ASV benchmarks anymore but your addition is small, so I'm not against keeping it.
|
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. |
babcfce to
deab893
Compare
Reference Issues/PRs
Fixes #34626.
What does this implement/fix? Explain your changes.
UnionFind.fast_findfinds 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 throughparent[-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_findto 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
SingleLinkageLabelBenchmarkproduced:n_samplesThe measured revisions were
8a6f766c6d(baseline) and0bec25b5d9(fix).These are direct local timings rather than an
asv continuousresult. The loop countsper 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
UnionFind.fast_find's path compression never touches the queried node — quadratic linkage labelling #34626AI usage disclosure
I used AI assistance for: