Merge sets and shorten parent paths
The calculator creates n=5…100 singleton sets labelled 0…n−1. It performs fixed Union(0,1),Union(2,3),then your bridge Union(a,b) and Find(u). Rank ties choose the smaller root. This fixed sequence is not a general operation importer.[1]
Understand the calculation
- Start with a parent forest. Each set has a root pointing to itself. Find follows parent pointers to that root.
- Union calls Find on both elements. Only distinct roots cause a merge and decrease component count by one.
- Union by rank attaches the lower-rank root to the higher-rank one. Equal ranks increase only the new root rank by one. Our tie rule chooses the smaller root number.
- Path compression redirects visited parents to the found root. The partition stays unchanged; stored ranks need not equal current depths afterward.
Only an actual merge reduces component count[1]
Amortized work for union by rank with path compression[1]
Visualize the learning idea
Same sets, shorter parent paths
Free with a Quanta account. Your download stays ready after registration.
Two fully checked examples
Merge two trees and shorten a path
- Given
- n=5 elements 0…4. First Union(0,1),Union(2,3); bridge Union(1,3); finally Find(3). Rank ties choose the smaller root.
- Find
- Check parents, components and Find depth.
After the first two merges, roots are 0,2,4.
The bridge connects roots 0 and 2, both rank one.
Find(3) follows 3→2→0, then compresses directly to 0.
Result: Root of 3 is 0; two components remain.
Check: Sets are {0,1,2,3} and {4}. Find does not change them. Rank(0)=2 remains stored even though actual tree height is now only 1.
Do not count an existing connection twice
- Given
- n=8. Fixed initial unions (0,1),(2,3), then bridge (4,5), query u=5.
- Find
- Check components and a repeated merge.
Three actual merges reduce eight singleton sets to five.
The tie between 4 and 5 chooses root 4; Find(5) traverses one parent edge.
A subsequent Union(5,4) finds the same root.
Result: Roots are 0,2,4,6,7; repeating the connection does not change component count.
Check: Sets {0,1},{2,3},{4,5},{6},{7} contain all eight elements exactly once.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Interpret the quantities
Results are discrete counts or element labels without physical units. A root number does not measure set importance. Find steps count parent edges in the actual forest, not seconds or inverse Ackermann values.
Five solved exercises
Calculate · Foundation
n=7, initial unions (0,1),(2,3), bridge (1,3),Find(3): how many components remain?
Show hint
The bridge connects distinct roots.
Learning goal: Count actual merges.
Show solution and calculation
c=7−2−1=4.
Find(3) does not change it.
Rearrange · Application
After two fixed initial merges and one real bridge merge, seven components remain. What was n?
Show hint
There are three successful merges.
Learning goal: Rearrange component balance under clear conditions.
Show solution and calculation
7=n−3,so n=10.
This applies because all three unions connected distinct components.
Interpret · Foundation
Why can the root have rank 2 but height 1 after Find(3)?
Show hint
Compression changes parent pointers, not historical ranks.
Learning goal: Separate rank and actual depth.
Show solution and calculation
Rank is stored union history and remains a suitable bound.
It is not recomputed as current height after each compression.
Error analysis · Application
For n=8,initial unions (0,1),(2,3),bridge (0,1),someone reports c=5.
Show hint
0 and 1 are already connected.
Learning goal: Check whether union succeeds before subtracting.
Show solution and calculation
The bridge does not connect distinct sets.
Thus c=8−2=6,not 5.
Transfer · Challenge
How does union–find detect a cycle-closing edge in Kruskal?
Show hint
Compare endpoint roots.
Learning goal: Apply a partition structure to a graph algorithm.
Show solution and calculation
Equal roots mean the endpoints are already connected,so discard the edge.
Otherwise union can merge their components.
Check your understanding with the formula’s reference test.
8 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Limits and connections
In minimum spanning trees, Kruskal uses union–find to distinguish connections from cycle-closing edges. The data structure does not itself compute a cheapest path or store a complete graph route.
Sources for this explanation
Quanta · Updated November 9, 2026
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Expanded an explicit operation sequence with rank rules, component balance and actual path compression.
Cite this reference page
Quanta. Union–Find (disjoint-set union). Updated November 9, 2026. https://quanta-study.de/en/formula-sheet/union-find-disjunkte-mengen
When citing a specific scientific statement, also cite the specialist source linked to that statement.