Skip to content
Part IA Lent Term

The Disjoint-Set Abstract Data Type

Definition

A disjoint-set (or union-find) data structure maintains a partition of a set of nn elements into disjoint subsets. Each subset has a canonical representative element (typically the root in tree-based implementations).

Operations

OperationDescription
MakeSet(x)Create a singleton set {x}\{x\} containing only element xx.
Find(x)Return the representative of the set containing xx.
Union(x, y)Merge the sets containing xx and yy into a single set. The representative of the result is typically the representative of one of the input sets.

In some formulations, Find is called In-Same-Set(x, y) and returns a boolean; this is equivalent to Find(x) == Find(y).

Invariant

At all times, the sets are pairwise disjoint. Every element belongs to exactly one set. After nn MakeSet operations, there are nn singleton sets; after n1n-1 Union operations, there is one set containing all elements.

Applications

Kruskal’s Minimum Spanning Tree

The canonical application. Kruskal’s algorithm sorts edges by weight, then iteratively adds the lightest edge that does not create a cycle. Disjoint sets track connected components:

for each vertex v: MakeSet(v)
edges = sort(edges by weight)
for (u, v, weight) in edges:
    if Find(u) != Find(v):
        add (u, v) to tree
        Union(u, v)

With an optimal disjoint-set implementation, Kruskal runs in O(ElogV)O(|E| \log |V|) time (dominated by sorting), where union-find operations contribute near-constant time.

Other Applications

  • Connected components in an undirected graph: iterate edges, Union the endpoints; each set is a component.
  • Equivalence relations: maintain equivalence classes under Union.
  • Dynamic connectivity: answer whether two vertices are connected after a sequence of edge additions.
  • Percolation theory: grid-based simulations of fluid flow; Union adjacent open cells.

Goal: Nearly Constant Time

The naive approaches (linked lists, hash tables) give O(n)O(n) worst-case for at least one operation. With union by rank and path compression, mm operations on nn elements take O(mα(n))O(m \cdot \alpha(n)) time, where α(n)\alpha(n) is the inverse Ackermann function — effectively constant for any practical nn (see The Inverse Ackermann Bound).

Worked Example: Kruskal Trace

Consider the graph:

Vertices: A, B, C, D, E
Edges (weight): AB(1), CD(2), AC(3), BE(4), DE(5), BC(6), AE(7)
StepEdgeFindActionSets after
1AB(1)A≠BUnion{A,B}, {C}, {D}, {E}
2CD(2)C≠DUnion{A,B}, {C,D}, {E}
3AC(3)A≠CUnion{A,B,C,D}, {E}
4BE(4)B≠EUnion{A,B,C,D,E}
5DE(5)D=EReject
6BC(6)B=CReject
7AE(7)A=EReject

MST edges: AB, CD, AC, BE. Total weight: 1+2+3+4=101+2+3+4 = 10.

Summary

PropertyValue
ADT operationsMakeSet, Find, Union
Key invariantSets are disjoint; union reduces count by 1
Naive linked listFind O(1)O(1), Union O(n)O(n)
Naive hash tableFind O(1)O(1), Union O(n)O(n)
Optimal (forest + heuristics)Both O(α(n))\sim O(\alpha(n)) amortised
Primary applicationKruskal’s MST algorithm
Exam contextTripos Paper 1 Q9-10; trace on small examples and explain heuristics