Chapter 16: Sets and Union-Find

Data Structures and Algorithms with C
Disjoint setsUnion and findThe parent arrayWorst case O(n²)Weighting ruleCollapsing ruleInverse AckermannComponents · Kruskal
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Model

Represent pairwise disjoint sets as trees whose links point from child to parent.

Implement

Write find and union with a single parent array.

Find the worst case

Show which sequence of operations degenerates the tree and costs O(n²).

Improve

Apply the weighting rule and the collapsing rule; interpret the lemmas.

Analyze

Explain the Tarjan–van Leeuwen result and the inverse Ackermann function.

Use

Apply union-find to counting connected components and to Kruskal's algorithm.

Disjoint Sets

Union and find on sets

The universe is {0, 1, …, n−1} and the sets are pairwise disjoint: S₁ ∩ S₂ = S₂ ∩ S₃ = S₁ ∩ S₃ = ∅. For n = 10:

0123456789S₁ = {0, 6, 7, 8}S₂ = {1, 4, 9}S₃ = {2, 3, 5}

Each set is a tree; links point from children to parents (arrows go up).

From the lecture“A very, very basic data structure, yet we use it in many problems. You have to learn it to be able to use it.”

Disjoint Sets

Two operations, and two ways to unite

1 · Disjoint set union

Si ∪ Sj = {x : x ∈ Si ∨ x ∈ Sj}. After a union the two sets no longer exist separately: S₁ ∪ S₂ = {0, 6, 7, 8, 1, 4, 9}.

2 · find(i)

Returns the set that contains i (the set's name = the root of its tree).

0123456789

The roots are 0, 4 and 2. To unite two sets, make one root's parent link point to the other root.

0123456789

S₁ ∪ S₂: 4's parent becomes 0; the tree of S₂ becomes a subtree of 0.

0123456789

S₂ ∪ S₁: this time 0's parent becomes 4. Both are correct; the choice affects the cost.

Disjoint Sets

A set is named by its root

Set namelink (root)
S₁→ 0
S₂→ 4
S₃→ 2

If the names are needed as text, a separate table keeps a link to each root.

For union and find we identify each set with the root of its tree: instead of “set S₁” we say “set 0”.

So find(8) == find(6) asks “are 8 and 6 in the same set?”

From the lecture“First we approach the problem like a mathematician, like someone who cannot program; before anything else we must state it correctly in the language of graph theory.”

Array Implementation

How should we implement it?

MethodUnionFindProblem
Linked listO(1)O(n)slow search
Keep it sorted——sorting is O(n lg n)
parent array (tree)O(1)tree depthdepth must stay small
From the lecture“Is there a way to do it with the array, the most reliable, most robust and fastest data structure we have?”
The nodes are numbered 0 to n−1, so we use the number as an index: each node needs just one field, its parent.

Array Implementation

The parent array: one link per index

01234567890-1142-1324-15260708094parentparent[i] = parent of i; −1 at a root
  • The parent of 1 and 9 is 4; of 3 and 5, 2; of 6, 7 and 8, 0.
  • 0, 2 and 4 are roots: parent[root] = −1 (negative = root).
#define N 10
int parent[N] = { -1, 4, -1, 2, -1, 2, 0, 0, 0, 4 };

Array Implementation

First version: find1 and union1

int find1(int i) {
  for (; parent[i] >= 0; i = parent[i])
    ;                /* climb to a root */
  return i;
}
void union1(int i, int j) {
  parent[i] = j;     /* put tree i under j */
}
find(i)

Follows parent links until it reaches a negative entry (the root) and returns the root.

union(i, j)

Given two roots, makes one tree a subtree of the other: O(1).

Precondition

i and j must be roots; otherwise call find1 first.

Array implementation · step through the code

Code trace: find1(9), union1(4, 0)

0123456789i0-1142-1324-15260708094parentfind1(9)

find1(9): which set is 9 in?

0123456789i0-1142-1324-15260708094parentfind1(9)

parent[9] = 4 ≥ 0 → i = parent[i] = 4.

0123456789i0-1142-1324-15260708094parentfind1(9)

parent[4] = −1 < 0: a root. return 4 → 9 is in set 4.

01234567890-1142-1324-15260708094parentunion1(4, 0)

union1(4, 0): two roots are passed.

01234567890-1142-132405260708094parentunion1(4, 0)

parent[4] = 0: tree 4 now hangs under 0. One assignment: O(1).

0123456789i0-1142-132405260708094parentfind1(9)

find1(9) again: the path is one node longer.

0123456789i0-1142-132405260708094parentfind1(9)

parent[9] = 4 ≥ 0 → i = 4.

0123456789i0-1142-132405260708094parentfind1(9)

parent[4] = 0 ≥ 0 → i = 0.

0123456789i0-1142-132405260708094parentfind1(9)

return 0: 9 is now in set 0. Cost = depth of the node.

int find1(int i) {
  for (; parent[i] >= 0; i = parent[i])
    ;                /* climb to a root */
  return i;
}
void union1(int i, int j) {
  parent[i] = j;     /* put tree i under j */
}

Analysis · step through the code

Code trace: union1(0,1) … union1(3,4), find1(0)

012340-11-12-13-14-1parentinitial

n = 5 singleton sets Si = {i}, all with parent[i] = −1.

01234011-12-13-14-1parentunion1(0, 1)

union1(0, 1): parent[0] = 1. Each call is O(1).

0123401122-13-14-1parentunion1(1, 2)

union1(1, 2): parent[1] = 2. Each call is O(1).

012340112233-14-1parentunion1(2, 3)

union1(2, 3): parent[2] = 3. Each call is O(1).

01234011223344-1parentunion1(3, 4)

union1(3, 4): parent[3] = 4. Each call is O(1).

01234i011223344-1parentfind1(0)

The tree has become a chain of depth n. Now find1(0).

01234i011223344-1parentfind1(0)

i = parent[0] = 1 (step 1).

01234i011223344-1parentfind1(0)

i = parent[1] = 2 (step 2).

01234i011223344-1parentfind1(0)

i = parent[2] = 3 (step 3).

01234i011223344-1parentfind1(0)

i = parent[3] = 4 (step 4).

01234011223344-1parentfind1(0)

return 4 after n−1 = 4 steps. find1(1), (2), … take n−2, n−3, … steps: in total Σ = O(n²).

int find1(int i) {
  for (; parent[i] >= 0; i = parent[i])
    ;                /* climb to a root */
  return i;
}
void union1(int i, int j) {
  parent[i] = j;     /* put tree i under j */
}

Analysis

Analysis of find1 and union1

/* start with n singleton sets */
union1(0, 1);
union1(1, 2);
...
union1(n-2, n-1);
find1(0);
find1(1);
...
find1(n-1);
  • n−1 unions, each constant → O(n) in total; no problem there.
  • The tree degenerates into a chain of depth n.
  • For the element i at level n−i, find1 takes O(n−i): n−1 steps for 0, n−2 for 1, …
  • One find1 can cost O(n); n of them cost Σ i = O(n²).
This sequence ruins the tree. The culprit is not the union itself but which tree is attached to which. The fix: the weighting rule.

Analysis

An aside: testing the worst case

Worst-case input

For worst-case analysis, code should be tested on the data that crashes it or makes it run longest. Finding that data is hard.

Here

The chain-building union order is exactly such an input for find1; random tests easily miss it.

From the lecture“What does ‘beta version’ mean? ‘We may have missed some bugs. Forgive us, and if you find one, tell us so we can fix it.’ Software testing is a field of its own; read papers on it too.”

Weighting Rule

The weighting rule

Definition: if tree i has fewer nodes than tree j, make j the parent of i; otherwise make i the parent of j. (The tree with more nodes becomes the parent.)

0123456Smaller under larger: depth 30123456Larger under smaller: depth 4
From the lecture“Question: do we need a separate array for the number of nodes in each tree? Answer: every non-root node already stores a nonnegative value in the parent array. So the node count of a root can be stored there too, as a negative number.”

Weighting Rule

union2: the root stores minus the node count

void union2(int i, int j) {
  /* weighting rule; i, j are roots */
  int p = parent[i] + parent[j]; /* -(size) */
  if (parent[i] > parent[j]) {  /* i smaller */
    parent[i] = j;      /* i under j */
    parent[j] = p;      /* j's new size */
  } else {              /* j not larger */
    parent[j] = i;
    parent[i] = p;
  }
}
−count at the root

A root holds −(number of nodes): −1 for a singleton, −8 for an 8-element set.

Why >?

The values are negative: parent[i] > parent[j] means i has fewer nodes (−2 > −4).

Cost

Still O(1), with no extra array.

Weighting rule · step through the code

Code trace: union2 (0,1) (2,3) (4,5) (6,7)

012345670-11-12-13-14-15-16-17-1parentunion2(0, 1)

union2(0, 1): set 0 has 1 node, set 1 has 1 node.

012345670-11-12-13-14-15-16-17-1parentunion2(0, 1)

p = -1 + (-1) = -2: the merged set has 2 nodes.

012345670-11-12-13-14-15-16-17-1parentunion2(0, 1)

-1 > -1 is false (j is not larger) → else: j goes under i.

012345670-2102-13-14-15-16-17-1parentunion2(0, 1)

parent[1] = 0, parent[0] = -2. The deepest node is on level 2.

012345670-2102-13-14-15-16-17-1parentunion2(2, 3)

union2(2, 3): set 2 has 1 node, set 3 has 1 node.

012345670-2102-13-14-15-16-17-1parentunion2(2, 3)

p = -1 + (-1) = -2: the merged set has 2 nodes.

012345670-2102-13-14-15-16-17-1parentunion2(2, 3)

-1 > -1 is false (j is not larger) → else: j goes under i.

012345670-2102-2324-15-16-17-1parentunion2(2, 3)

parent[3] = 2, parent[2] = -2. The deepest node is on level 2.

012345670-2102-2324-15-16-17-1parentunion2(4, 5)

union2(4, 5): set 4 has 1 node, set 5 has 1 node.

012345670-2102-2324-15-16-17-1parentunion2(4, 5)

p = -1 + (-1) = -2: the merged set has 2 nodes.

012345670-2102-2324-15-16-17-1parentunion2(4, 5)

-1 > -1 is false (j is not larger) → else: j goes under i.

012345670-2102-2324-2546-17-1parentunion2(4, 5)

parent[5] = 4, parent[4] = -2. The deepest node is on level 2.

012345670-2102-2324-2546-17-1parentunion2(6, 7)

union2(6, 7): set 6 has 1 node, set 7 has 1 node.

012345670-2102-2324-2546-17-1parentunion2(6, 7)

p = -1 + (-1) = -2: the merged set has 2 nodes.

012345670-2102-2324-2546-17-1parentunion2(6, 7)

-1 > -1 is false (j is not larger) → else: j goes under i.

012345670-2102-2324-2546-276parentunion2(6, 7)

parent[7] = 6, parent[6] = -2. The deepest node is on level 2.

void union2(int i, int j) {
  /* weighting rule; i, j are roots */
  int p = parent[i] + parent[j]; /* -(size) */
  if (parent[i] > parent[j]) {  /* i smaller */
    parent[i] = j;      /* i under j */
    parent[j] = p;      /* j's new size */
  } else {              /* j not larger */
    parent[j] = i;
    parent[i] = p;
  }
}

Weighting rule · step through the code

Code trace: union2 (0,2) (4,6) (0,4)

012345670-2102-2324-2546-276parentunion2(0, 2)

union2(0, 2): set 0 has 2 nodes, set 2 has 2 nodes.

012345670-2102-2324-2546-276parentunion2(0, 2)

p = -2 + (-2) = -4: the merged set has 4 nodes.

012345670-2102-2324-2546-276parentunion2(0, 2)

-2 > -2 is false (j is not larger) → else: j goes under i.

012345670-41020324-2546-276parentunion2(0, 2)

parent[2] = 0, parent[0] = -4. The deepest node is on level 3.

012345670-41020324-2546-276parentunion2(4, 6)

union2(4, 6): set 4 has 2 nodes, set 6 has 2 nodes.

012345670-41020324-2546-276parentunion2(4, 6)

p = -2 + (-2) = -4: the merged set has 4 nodes.

012345670-41020324-2546-276parentunion2(4, 6)

-2 > -2 is false (j is not larger) → else: j goes under i.

012345670-41020324-4546476parentunion2(4, 6)

parent[6] = 4, parent[4] = -4. The deepest node is on level 3.

012345670-41020324-4546476parentunion2(0, 4)

union2(0, 4): set 0 has 4 nodes, set 4 has 4 nodes.

012345670-41020324-4546476parentunion2(0, 4)

p = -4 + (-4) = -8: the merged set has 8 nodes.

012345670-41020324-4546476parentunion2(0, 4)

-4 > -4 is false (j is not larger) → else: j goes under i.

012345670-810203240546476parentunion2(0, 4)

parent[4] = 0, parent[0] = -8. The deepest node is on level 4.

void union2(int i, int j) {
  /* weighting rule; i, j are roots */
  int p = parent[i] + parent[j]; /* -(size) */
  if (parent[i] > parent[j]) {  /* i smaller */
    parent[i] = j;      /* i under j */
    parent[j] = p;      /* j's new size */
  } else {              /* j not larger */
    parent[j] = i;
    parent[i] = p;
  }
}

Weighting Rule

Question: how badly can the tree degenerate?

From the lecture“The algorithm must be correct, efficient and well engineered. But that is not the end: we ask which data hurts it most. Whatever happens behind the scenes, how long can my code run at most? That is what computer science and computer engineering are about!”
From the lecture“‘Do you all know C, Java and so on?’ Yes, they do. ‘Fine, you are all hired, same salary!’ The computer engineer says: ‘Wait a minute! I am a computer engineer. How can it be the same salary? Ask me a question and watch how I solve it!’”
The worst case for the weighting rule is uniting two trees of equal size every time (as in the union2 traces). Each union deepens every node of the smaller tree by 1, but the tree at least doubles.

Weighting Rule

Lemma: weighted union keeps the depth logarithmic

Lemma (Horowitz)If T is a tree with m nodes built by a sequence of union2 operations, then the depth (number of levels) of T is at most ⌊lg m⌋ + 1.
  • Proof idea (induction): a node gets deeper by 1 only when its tree is attached to a tree that is no smaller.
  • Then the tree containing the node at least doubles.
  • A size can double at most lg m times before reaching m → depth ≤ ⌊lg m⌋ + 1.
01234567m = 8 → 4 levels = ⌊lg 8⌋ + 1
From the lecture“In a mathematics department the question would go on: ‘Prove it.’ […] We brought the cost down from n to ⌊lg n⌋+1. That is algorithm design! But we do not stop here, on we go…”

Collapsing Rule

The collapsing rule

In the final tree, find(7) reaches the root in 3 steps. If a program calls find(7) over and over, the cost piles up. Even when the smaller tree goes under the larger one the depth can grow; since we cannot control the depth, we go one step further:

DefinitionIf j is a node on the path from i to its root (i not the root), then j is linked directly to the root.
01234567every node on the path 7 → 6 → 4 → 0 is linked to the root
From the lecture“In graph theory this corresponds to a ‘closure’. You will need it in one of the graph theory assignments; I won't say which one. The problem looks different, but this is what lies underneath.”

Collapsing Rule

find2: find the root, then link the path to it

int find2(int i) {
  int r, s;
  for (r = i; parent[r] >= 0; r = parent[r])
    ;                   /* find the root */
  while (i != r) {      /* collapse path */
    s = parent[i];      /* save the parent */
    parent[i] = r;      /* link i to root */
    i = s;              /* move up */
  }
  return r;
}
Pass 1 (for)

r finds the root (the negative entry).

Pass 2 (while)

Every node on the path is linked straight to r. It also runs once for a node already linked to the root (useless but harmless).

Payoff

Later finds on the same path reach the root in one step.

Collapsing rule · step through the code

Code trace: find2(7)

01234567i0-810203240546476parentfind2(7)

find2(7).

01234567ir0-810203240546476parentfind2(7)

parent[7] = 6 ≥ 0 → r = 6.

01234567ir0-810203240546476parentfind2(7)

parent[6] = 4 ≥ 0 → r = 4.

01234567ir0-810203240546476parentfind2(7)

parent[4] = 0 ≥ 0 → r = 0.

01234567ir0-810203240546476parentfind2(7)

parent[0] = -8 < 0: root r = 0.

01234567ir0-810203240546476parentfind2(7)

i = 7 ≠ r: save s = parent[7] = 6.

01234567ir0-810203240546470parentfind2(7)

parent[7] = 0: 7 now points to the root; i = s = 6.

01234567ir0-810203240546470parentfind2(7)

i = 6 ≠ r: save s = parent[6] = 4.

01234567ir0-810203240546070parentfind2(7)

parent[6] = 0: 6 now points to the root; i = s = 4.

01234567ir0-810203240546070parentfind2(7)

i = 4 ≠ r: save s = parent[4] = 0.

01234567r0-810203240546070parentfind2(7)

parent[4] = 0 (already linked to the root: one extra round); i = 0.

01234567i r0-810203240546070parentfind2(7)

i == r: the loop ends, return 0. parent = [-8, 0, 0, 2, 0, 4, 0, 0].

int find2(int i) {
  int r, s;
  for (r = i; parent[r] >= 0; r = parent[r])
    ;                   /* find the root */
  while (i != r) {      /* collapse path */
    s = parent[i];      /* save the parent */
    parent[i] = r;      /* link i to root */
    i = s;              /* move up */
  }
  return r;
}

Collapsing rule · step through the code

Code trace: find2(7) again, then find2(5)

01234567i0-810203240546070parentfind2(7)

find2(7).

01234567ir0-810203240546070parentfind2(7)

parent[7] = 0 ≥ 0 → r = 0.

01234567ir0-810203240546070parentfind2(7)

parent[0] = -8 < 0: root r = 0.

01234567ir0-810203240546070parentfind2(7)

i = 7 ≠ r: save s = parent[7] = 0.

01234567r0-810203240546070parentfind2(7)

parent[7] = 0 (already linked to the root: one extra round); i = 0.

01234567i r0-810203240546070parentfind2(7)

i == r: the loop ends, return 0. parent = [-8, 0, 0, 2, 0, 4, 0, 0].

01234567i0-810203240546070parentfind2(5)

find2(5).

01234567ir0-810203240546070parentfind2(5)

parent[5] = 4 ≥ 0 → r = 4.

01234567ir0-810203240546070parentfind2(5)

parent[4] = 0 ≥ 0 → r = 0.

01234567ir0-810203240546070parentfind2(5)

parent[0] = -8 < 0: root r = 0.

01234567ir0-810203240546070parentfind2(5)

i = 5 ≠ r: save s = parent[5] = 4.

01234567ir0-810203240506070parentfind2(5)

parent[5] = 0: 5 now points to the root; i = s = 4.

01234567ir0-810203240506070parentfind2(5)

i = 4 ≠ r: save s = parent[4] = 0.

01234567r0-810203240506070parentfind2(5)

parent[4] = 0 (already linked to the root: one extra round); i = 0.

01234567i r0-810203240506070parentfind2(5)

i == r: the loop ends, return 0. parent = [-8, 0, 0, 2, 0, 0, 0, 0].

int find2(int i) {
  int r, s;
  for (r = i; parent[r] >= 0; r = parent[r])
    ;                   /* find the root */
  while (i != r) {      /* collapse path */
    s = parent[i];      /* save the parent */
    parent[i] = r;      /* link i to root */
    i = s;              /* move up */
  }
  return r;
}

Collapsing Rule

“We had a for loop, and now we added a while!”

From the lecture“‘We had a for loop and now we added a while loop too. How is that an improvement?’ I don't get it either, let's see. On the second and every later call, find2(7) finds r = 0 in a single step.”
Callfind1 (steps)find2 (steps + relinks)
1st find(7)33 + 3
2nd find(7)31 + 1
find(7) 10 times306 + 9·2 = 24
find(5) (afterwards)22 + 2, then 1 + 1
The first call costs about twice as much, but it shortens the path for good. Amortized over many operations, the cost drops to almost a constant.

Collapsing Rule

Lemma (Tarjan and van Leeuwen): almost constant

LemmaStart with singleton sets and let T(f, u) be the maximum time for f finds and u unions (weighting rule + collapsing rule) in any interleaved order. For u ≥ n/2:

k₁·(n + f·α(f+n, n)) ≤ T(f, u) ≤ k₂·(n + f·α(f+n, n))

  • α is the inverse Ackermann function; it grows extremely slowly.
  • Even for as many elements as there are atoms in the universe, α ≤ 4 in practice.
  • Divided by the number of operations, the cost is almost O(1).
/* Ackermann: grows very fast */
A(m, n) = n + 1                  m = 0
        = A(m-1, 1)              m > 0, n = 0
        = A(m-1, A(m, n-1))      m > 0, n > 0
From the lecture“There are a million elements; one finds them in O(1), the other is still searching a list… As we said, this is the difference between the mathematician and the engineer applying for the same job!”

Collapsing Rule

The Ackermann function and its inverse

n = 0n = 1n = 2n = 3n = 4
m = 012345
m = 123456
m = 2357911
m = 35132961125
m = 41365533265536−3……
  • A(5, 1) = 22…2 − 3 (a tower of 65536 twos): unimaginably large.
  • Inverse: α(m, n) = min{ i ≥ 1 : A(i, ⌊m/n⌋) ≥ lg n }. Since A grows so fast, α grows very slowly.

The Ackermann function has several definitions. With the one above, A(3,4) = 125; with Horowitz's definition (A(1,j) = 2j …), A(3,1) = 16. The conclusion is the same either way: in practice α ≤ 4.

Collapsing Rule

Exam question: write the Ackermann function recursively

Exam questionWrite the Ackermann function recursively. Then show the call stack of A(1, 2) step by step.
Solution
unsigned long ackermann(unsigned long m,
                        unsigned long n) {
  if (m == 0) return n + 1;
  if (n == 0) return ackermann(m - 1, 1);
  return ackermann(m - 1, ackermann(m, n - 1));
}

A(1,2) = A(0, A(1,1)) = A(0, A(0, A(1,0))) = A(0, A(0, A(0,1))) = A(0, A(0, 2)) = A(0, 3) = 4. Even A(4, 2) never finishes on any computer; the stack overflows.

Recursion · step through the code

Code trace: ackermann(1, 2)

A(1,2)active calls (deeper to the right)

ackermann(1, 2) is called.

A(1,2)active calls (deeper to the right)

A(1,2): the inner A(1,1) must be computed first.

A(1,2)A(1,1)active calls (deeper to the right)

A(1,1): the inner A(1,0) must be computed first.

A(1,2)A(1,1)A(1,0)active calls (deeper to the right)

A(1,0): n = 0 → call A(0, 1).

A(1,2)A(1,1)A(1,0)A(0,1)active calls (deeper to the right)A(0,1) = 2

A(0,1): m = 0 → return 2.

A(1,2)A(1,1)A(1,0)active calls (deeper to the right)A(1,0) = 2

A(1,0) = A(0,1) = 2; return.

A(1,2)A(1,1)active calls (deeper to the right)

The inner call gave 2 → outer call A(0,2).

A(1,2)A(1,1)A(0,2)active calls (deeper to the right)A(0,2) = 3

A(0,2): m = 0 → return 3.

A(1,2)A(1,1)active calls (deeper to the right)A(1,1) = 3

A(1,1) = 3.

A(1,2)active calls (deeper to the right)

The inner call gave 3 → outer call A(0,3).

A(1,2)A(0,3)active calls (deeper to the right)A(0,3) = 4

A(0,3): m = 0 → return 4.

A(1,2)active calls (deeper to the right)A(1,2) = 4

A(1,2) = 4.

unsigned long ackermann(unsigned long m,
                        unsigned long n) {
  if (m == 0) return n + 1;
  if (n == 0) return ackermann(m - 1, 1);
  return ackermann(m - 1, ackermann(m, n - 1));
}

Collapsing Rule

An aside: where do these lemmas come from?

From the lecture“Tarjan is a famous graph theorist with many results to his name. This lemma came out of his joint work with Jan van Leeuwen (1984).”
From the lecture“An undergraduate degree takes four years and gives you a profession; in a PhD you spend four years on a single problem, one nobody in the world has solved […] That is why working in a group means learning all the time.”
From the lecture“You cannot finish these topics in a week, and you never will if you don't study. Study 2–3 hours every day, and review each week's topic that same week.”

Application · step through the code

Code trace: counting connected components

014235graph: 6 nodes, 4 edges0123450-11-12-13-14-15-1parent

Edges u–v: (0,1), (2,3), (1,4), (3,2). How many connected components are there?

014235graph: 6 nodes, 4 edges0123450-11-12-13-14-15-1parentcount = 6

Every node is its own set: parent = −1, count = 6.

014235graph: 6 nodes, 4 edges0123450-11-12-13-14-15-1parentcount = 6

Edge (0, 1): find2(0) = 0, find2(1) = 1.

014235graph: 6 nodes, 4 edges0123450-2102-13-14-15-1parentcount = 5

0 ≠ 1: different components → union2(0, 1), count = 5.

014235graph: 6 nodes, 4 edges0123450-2102-13-14-15-1parentcount = 5

Edge (2, 3): find2(2) = 2, find2(3) = 3.

014235graph: 6 nodes, 4 edges0123450-2102-2324-15-1parentcount = 4

2 ≠ 3: different components → union2(2, 3), count = 4.

014235graph: 6 nodes, 4 edges0123450-2102-2324-15-1parentcount = 4

Edge (1, 4): find2(1) = 0, find2(4) = 4.

014235graph: 6 nodes, 4 edges0123450-3102-232405-1parentcount = 3

0 ≠ 4: different components → union2(0, 4), count = 3.

014235graph: 6 nodes, 4 edges0123450-3102-232405-1parentcount = 3

Edge (3, 2): find2(3) = 2, find2(2) = 2.

014235graph: 6 nodes, 4 edges0123450-3102-232405-1parentcount = 3

a == b = 2: 3 and 2 are already in one component; the edge closes a cycle and the count stays.

014235graph: 6 nodes, 4 edges0123450-3102-232405-1parentcount = 3

return 3: the components are {0,1,4}, {2,3}, {5}. Total O(n + m·α).

int components(int n, int m, int u[], int v[]) {
  for (int i = 0; i < n; i++) parent[i] = -1;
  int count = n;              /* all separate */
  for (int e = 0; e < m; e++) {
    int a = find2(u[e]), b = find2(v[e]);
    if (a != b) {             /* new component */
      union2(a, b);
      count--;
    }
  }
  return count;
}

Applications

Where else is union-find used?

Equivalence classes

Derive the classes from statements i ≡ j (Horowitz's equivalence problem).

Network connectivity

Answer “are these two computers connected?” online (Sedgewick's opening example).

Image processing

Group adjacent pixels of the same color into regions (connected-component labeling).

Percolation

Is there an open path from top to bottom of a grid? The core of Monte Carlo experiments.

Compilers

Unification in type inference; alias analysis.

Offline LCA

Tarjan's lowest common ancestor algorithm.

Kruskal's algorithm: sort the edges by weight; if find gives different roots for the ends of an edge, take the edge and unite the two sets; if it gives the same root, the edge closes a cycle and is rejected. Cost: sorting O(m lg m) + union-find ≈ O(m·α).

Lab · C code step by step

Lab: Counting Components with Union-Find

Summary

Exam question: contents of the parent array

Exam questionGiven union2 and find2, and the sets S₁ = {0, 6, 7, 8}, S₂ = {1, 4, 9}, S₃ = {2, 3, 5} (the trees in the figure):
(i) What does the parent array contain initially?
(ii) What does parent contain after union2(4, 2); find2(5); union2(0, 4); find2(7); find2(3);? Note: if (i) is wrong, (ii) is not graded.
0123456789
Solution
  1. parent = [-4, 4, -3, 2, -3, 2, 0, 0, 0, 4]: each root holds minus its node count (−4, −3, −3).
  2. union2(4,2): equal (−3, −3) → 2 goes under 4, parent[4] = −6. find2(5): 5 → 2 → 4, so 5 is linked straight to 4. union2(0,4): −4 > −6 → 0 goes under 4, parent[4] = −10. find2(7): 7 → 0 → 4, so 7 is linked to the root. find2(3): 3 → 2 → 4, so 3 is linked to the root. Result: [4, 4, 4, 4, -10, 4, 0, 4, 0, 4].

Summary

Complexity summary

VersionUnionFind (worst)n−1 unions + m finds
union1 + find1O(1)O(n)O(n + m·n)
union2 (weighted)O(1)O(lg n)O(n + m lg n)
union2 + find2O(1)O(lg n) per callO(n + m·α(m+n, n))

Memory: just the parent array of n integers. Since α ≤ 4, the last row is linear in practice.

A union alone is always O(1); the cost is set by the tree depth, that is, by find. The weighting rule bounds the depth; the collapsing rule shortens the paths.

Summary

Common mistakes

1Passing an arbitrary node instead of a root to union: parent[i] = j cuts off i's subtree.
2Uniting a root with itself (i == j): the root points to itself, an infinite loop.
3Misreading the comparison of negative counts in the weighting rule (−2 > −4 means “fewer nodes”).
4Forgetting to update the node count (p) at the root after a union.
5Relinking in find2 without saving the parent (s) first: the rest of the path is lost.
6Not filling the parent array with −1 at the start.

Summary

Exercises

  1. n = 6, parent all −1. What is parent after union2(0,1); union2(2,3); union2(0,2); union2(4,5)?
  2. With the weighting rule, what is the maximum depth of a 16-node tree?
  3. Write a union rule that stores the height (rank) instead of the weight. What changes?
  4. Write find2 recursively (one-line path compression).
  5. Why does rejecting an edge in Kruskal's algorithm mean a cycle?
  6. What does it cost to count the components of a graph with n nodes and m edges?
Show answers
  1. [-4, 0, 0, 2, -2, 4] (root 0 has 4 nodes; root 4 has 2).
  2. ⌊lg 16⌋ + 1 = 5 levels.
  3. The root stores −(height); on a tie one root goes under the other and the height grows by 1. The depth is still O(lg n); with collapsing, rank becomes an upper bound.
  4. int find(int i) { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); }
  5. The ends are already in the same set, so a path of chosen edges joins them; the new edge would close that path into a cycle.
  6. O(n + m·α(m+n, n)), linear in practice.

Summary

Chapter summary

Model

Disjoint sets = trees with child-to-parent links, named by their roots.

Representation

A single parent array; a negative value marks a root.

Worst case

Linking without a rule can build a chain: n finds cost O(n²).

Weighting rule

Smaller tree under larger: depth ≤ ⌊lg m⌋ + 1.

Collapsing rule

The path is linked to the root; later finds take one step.

Result

Together: α ≤ 4 per operation, almost O(1).

Summary

References and next steps

  • E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, Section 5.10 (Set Representation).
  • T. H. Cormen et al., Introduction to Algorithms (CLRS), chapter “Data Structures for Disjoint Sets”.
  • R. Sedgewick, Algorithms in C, Chapter 1 (connectivity with union-find).
  • R. E. Tarjan, J. van Leeuwen, “Worst-case analysis of set union algorithms”, J. ACM 31(2), 1984.

The End

Chapter 16: Sets and Union-Find
Next chapter: Chapter 17: Minimum Spanning Trees · Course page
1 / 1 Sercan KÜLCÜ. All rights reserved.