ENع
←Union-find
Lesson 24 · Graph algorithms

Union-find: merging groups fast

14 min10 steps5 questionsIntermediate
About this lesson

Union-find (also called a disjoint-set structure) keeps track of groups that only ever merge, and answers "are these two in the same group?" almost instantly. Each element stores one number, its parent, so every group is a tree whose root names the group. Follow six elements through four unions, watch find climb the parent arrows, and see union by size and path compression flatten the trees until every element points straight at one root. The lesson "Connected components and grid flood fill" finds the same groups by walking the graph; the Software lessons on graphs and on BFS and DFS cover the graphs themselves.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 24 · Interactive
01 / 10

Groups that only merge

Six people, numbered 0 to 5. Friendships arrive one at a time, and we keep asking: are a and b in the same group? Groups only ever merge. Union-find answers this quickly, without searching the whole graph again.

no structure yet: just six separate elements

parent = [0, 1, 2, 3, 4, 5]
size   = [1, 1, 1, 1, 1, 1]
Loading figure…
or ← →
Read the lesson step by step
  1. Groups that only merge

    Six people, numbered 0 to 5. Friendships arrive one at a time, and we keep asking: are a and b in the same group? Groups only ever merge. Union-find answers this quickly, without searching the whole graph again.

    no structure yet: just six separate elements
    
    parent = [0, 1, 2, 3, 4, 5]
    size   = [1, 1, 1, 1, 1, 1]
  2. Everyone starts alone

    Each element stores one number: its parent. At the start parent[i] = i, so every element points at itself. An element that is its own parent is a root, and the root names its group. Six roots: six groups.

    nothing new runs
    
    parent = [0, 1, 2, 3, 4, 5]
    size   = [1, 1, 1, 1, 1, 1]
  3. union(0, 1) links two roots

    union(0, 1) first finds each root: 0 and 1, both alone. They differ, so root 1 becomes a child of root 0: parent[1] = 0. One write merges two groups, and size[0] becomes 2.

    runs: union(0, 1)
    
    parent = [0, 0, 2, 3, 4, 5]
    size   = [2, 1, 1, 1, 1, 1]
  4. Two more unions

    union(2, 3) and union(4, 5) work the same way: parent[3] = 2 and parent[5] = 4. Now there are three groups of two. Each group is a small tree, and every arrow points from a child up to its parent.

    runs: union(2, 3), union(4, 5)
    
    parent = [0, 0, 2, 2, 4, 4]
    size   = [2, 1, 2, 1, 2, 1]
  5. Merging two trees

    union(1, 3): find(1) climbs to root 0 and find(3) climbs to root 2. Both trees have size 2, a tie, so root 2 goes under root 0: parent[2] = 0. Element 3 comes along; its parent is still 2.

    runs: union(1, 3)
    
    parent = [0, 0, 0, 2, 4, 4]
    size   = [4, 1, 2, 1, 2, 1]
  6. find(3) climbs to the root

    find(x) follows parent arrows until it reaches an element that points at itself. find(3) goes 3 → 2 → 0: two hops, and 0 is the root. The deeper an element sits, the longer its find takes.

    nothing new runs
    find(3): 3 → 2 → 0  (2 hops)
    
    parent = [0, 0, 0, 2, 4, 4]
    size   = [4, 1, 2, 1, 2, 1]
  7. Path compression

    On its way back, find(3) points 3 straight at the root: parent[3] = 0. The answer is the same, but the next find(3) takes one hop. This is path compression: every find flattens the path it walked.

    runs: find(3)
    find(3): 3 → 2 → 0  (2 hops)
    
    parent = [0, 0, 0, 0, 4, 4]
    size   = [4, 1, 2, 1, 2, 1]
  8. Union by size

    union(5, 3): find(5) gives root 4, size 2; find(3) gives root 0, size 4. The smaller tree hangs under the bigger root: parent[4] = 0 and size[0] = 6. Only a root’s size is ever read. No element ends up deeper than log₂ n.

    runs: union(5, 3)
    
    parent = [0, 0, 0, 0, 0, 4]
    size   = [6, 1, 2, 1, 2, 1]
  9. Are 5 and 1 connected?

    Same group? Compare roots. find(5) climbs 5 → 4 → 0, then compresses: parent[5] = 0. find(1) reaches 0 in one hop. Both roots are 0, so the answer is yes. Now every element points directly at 0.

    runs: find(5), find(1)
    find(5): 5 → 4 → 0  (2 hops)
    find(1): 1 → 0  (1 hop)
    
    parent = [0, 0, 0, 0, 0, 0]
    size   = [6, 1, 2, 1, 2, 1]
  10. Nearly constant time

    With both tricks, any m operations on n elements take O(m α(n)) time. α, the inverse Ackermann function, is at most 4 for any n you could ever store. Kruskal’s spanning tree algorithm, later in this course, relies on it.

    nothing new runs
    
    parent = [0, 0, 0, 0, 0, 0]
    size   = [6, 1, 2, 1, 2, 1]

Quiz Unlocks after the last step.