Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added path compression to UnionFind #734

Merged
merged 5 commits into from
Sep 8, 2024

Conversation

soondubu137
Copy link
Contributor

Main Changes

The current implementation of the union find data structure claims its amortized time complexity is $O(\alpha(n))$, which is incorrect because its Find method has no optimization. I added path compression as the necessary optimization. Path splitting and path halving are also valid optimizations, but I think path compression is the most common one.

I changed union-by-size to union-by-rank, which complements path compression effectively.

In addition, I changed the receivers of Find and Union to pointers. Along with this, I removed the unnecessary return value in Union.

Other Changes

I fixed a small typo in the algorithm description and used more consistent variable names.

graph/kruskal.go uses UnionFind, so I also modified that file to conform to the updated Union method.

@codecov-commenter
Copy link

codecov-commenter commented Sep 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.68%. Comparing base (24c7f1f) to head (648e1b4).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #734      +/-   ##
==========================================
+ Coverage   87.59%   87.68%   +0.08%     
==========================================
  Files         208      209       +1     
  Lines        5524     5547      +23     
==========================================
+ Hits         4839     4864      +25     
+ Misses        543      542       -1     
+ Partials      142      141       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@raklaptudirm raklaptudirm merged commit 495cff8 into TheAlgorithms:master Sep 8, 2024
4 of 5 checks passed
@soondubu137 soondubu137 deleted the fix/unionfind branch September 9, 2024 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants