On this page
Union-find, also called the disjoint-set data structure, answers one question quickly while groups keep merging: are x and y in the same group? Bernard Galler and Michael Fischer described the tree-based method in 1964, for any problem "in which it is necessary to identify equivalence classes" (Communications of the ACM).
What is the union-find data structure?
A disjoint-set structure splits n items into groups that never overlap and supports two operations:
find(x) returns the root that represents the group containing x.
union(x, y) merges the groups containing x and y.
Each item stores one number, its parent, and a root is its own parent. To find x, follow parents up to a root. Two items are connected exactly when they share a root.
How do union by rank and path compression work?
Union by rank. Each root carries a rank, an upper bound on its tree's height. The lower-rank root goes under the other; on a tie, pick one and raise its rank by 1. With this rule every node has rank at most ⌊log₂ n⌋ (Wikipedia's disjoint-set article, citing Cormen et al.).
Path compression. While find walks up to the root, it points every node it passes straight at the root, so later finds from those nodes take one step (Sedgewick and Wayne, Algorithms).
Worked example: union-find on 8 elements
Start with items 1 to 8, each its own root with rank 0. On a tie, the first root stays on top.
union(1, 2), union(3, 4), union(5, 6), union(7, 8): each is a tie, so 2, 4, 6 and 8 go under 1, 3, 5 and 7, which get rank 1.
union(2, 4): find(2) = 1 and find(4) = 3. Ranks tie at 1, so 3 goes under 1, which gets rank 2.
union(6, 8): likewise 7 goes under 5, which gets rank 2.
union(4, 8): find(4) walks 4 → 3 → 1, so compression points 4 at 1. find(8) walks 8 → 7 → 5, so 8 now points at 5. Ranks tie at 2, so 5 goes under 1, which gets rank 3.
Are 3 and 7 connected? find(3) = 1 in one step. find(7) walks 7 → 5 → 1, and 7 now points at 1. Same root, so yes.
The parents of items 1 to 8 end as [1, 1, 1, 1, 1, 5, 1, 5]. No node is more than two hops from the root, and the root's rank, 3, equals ⌊log₂ 8⌋, the most the rule allows.
How fast is union-find?
Robert Tarjan proved in 1975 that with both rules, any sequence of m operations on n items takes O(m α(n)) time, and that the bound is tight for this method (Journal of the ACM; summarised in Wikipedia). Here α is the inverse of the Ackermann function, which grows so fast that α(n) is less than 5 for any n that arises in practice (Sedgewick and Wayne).
In 1989 Michael Fredman and Michael Saks showed that no disjoint-set structure can beat Ω(α(n)) per operation, averaged over the sequence (Wikipedia).
Where is union-find used?
Kruskal's algorithm. Joseph Kruskal published his method for the shortest spanning tree of a graph in 1956 (Proceedings of the American Mathematical Society). In the form taught today, you sort the edges from lightest to heaviest and keep each edge unless its ends already share a tree. That test is find(u) ≠ find(v), keeping the edge is union(u, v), and because sorting dominates, the run takes O(E log E) time (Wikipedia). To count spanning trees instead, see Cayley's formula for trees on n labelled vertices.
Image segmentation. Pedro Felzenszwalb and Daniel Huttenlocher turned the same loop into a way of splitting a picture into regions (International Journal of Computer Vision, 2004; authors' copy). Each pixel is a node, and each edge between neighbouring pixels is weighted by how different they look. Edges are taken in order of weight, and two regions merge when the edge between them is small compared with the variation already inside each. The regions live in a disjoint-set forest with union by rank and path compression, so the merging steps take O(m α(m)) time for m edges.
The same bookkeeping tracks clusters as random edges arrive, which is one way to watch the giant component appear in an Erdős–Rényi random graph.
How to practise union-find in 15 minutes
Redo the steps above on paper, writing the parent array after each one.
Run them again with ties broken the other way. The groups match; the trees do not.
Code find with path compression in Python or C++ and test it on the same steps.
If the rank argument doesn't click, the Fermi Concept Library covers every concept in mathematics and computing, each with its own lesson, and the Fermi Problem Library has a step-by-step solver that explains the reasoning, so you can see where a trace went wrong.



