CSE3144 · Advanced Data Structures · Jul–Nov Semester 2026 · Lecture 31 of 36 · CO CSE3144.5

Disjoint Set Union (Union-Find)

One question, asked millions of times in a single algorithm: "are these two elements already in the same group?" The naive structure answers it in O(n) in the worst case. Two small, almost embarrassingly simple tweaks bring that down to essentially O(1) — and Lecture 3's battery is exactly the right lens for understanding why.

Dr. Manu ShrivastavaCourse Instructor · Consultation Fri 2–5 PM, LHC 308F
~154 minutesSession outcome: implement union by rank and path compression
L31 · 00 — Agenda ~154 min

The same 6 unions, three ways — and the difference is everything

Why disjoint sets?

00–08

Dynamic connectivity: Kruskal's algorithm, network reachability, connected components.

The Lecture 3 callback

08–14

The battery returns, one more time, for the structure that made it famous.

Definition & naive operations — concept

14–20

MAKE-SET, FIND, UNION — the simplest possible version, with no optimizations at all.

Animation: the naive structure degrades

20–38

6 unions build a straight chain; a single FIND then costs 5 hops out of 6 elements.

Union by rank — concept & animation

38–56

The SAME 6 unions, one rule added: attach shorter under taller. Height 5 becomes height 1.

Path compression — concept & animation

56–76

A single FIND flattens every node it visits — for free, as a side effect of just answering the query.

Combined complexity, honestly

76–86

Amortized O(α(n)) — inverse Ackermann, essentially O(1) for any n that could ever exist.

Kruskal’s algorithm — concept

86–94

Minimum spanning tree by a greedy rule whose every decision is a FIND.

Animation: building a minimum spanning tree

94–119

9 edges, every FIND, decision and UNION, with the parent and rank arrays.

Kruskal’s cost

119–125

Why the sort, not union-find, decides the running time.

Applications & recap

125–154

Connected components, percolation, friend circles — and what's next.

CSE3144 — Lecture 31
L31 · 01 — Why disjoint sets? ~8 min

"Are x and y already connected?" — asked over and over, cheaply

The problem

Maintain a collection of disjoint (non-overlapping) sets over a universe of elements, supporting: MAKE-SET(x) — create a new set containing just x; FIND(x) — report which set x currently belongs to (its "representative"); UNION(x,y) — merge the sets containing x and y into one.

Why this matters so much

Kruskal's minimum spanning tree algorithm processes graph edges in increasing weight order, and for EVERY edge asks "would adding this connect two components that are already connected?" — exactly a FIND, followed by a UNION if the answer is no. On a graph with millions of edges, this question is asked millions of times. How cheaply we can answer it determines whether Kruskal's algorithm is practical at all.

CSE3144 — Lecture 31 · Why disjoint sets
L31 · 02 — The Lecture 3 callback ~6 min

The battery, one more time — for the structure that made it famous

Recap — Lecture 3 in two lines

The potential method meters a data structure at the wall socket, not at the individual operation: amortized cost = actual work + change in battery charge (ΔΦ). Cheap operations quietly charge the battery; expensive ones spend it back down.

The battery, concretely, for union-find

Think of Φ informally as "how much unnecessary depth is sitting in the forest right now." Naive unions let this depth pile up unpaid — the battery charges and charges, and a FIND on a deep leaf is the bill finally coming due, in full, all at once. Path compression is the purest possible example of spending the battery: the pointer updates it performs during one FIND look like extra work, but they are prepaid by the very depth that FIND just discharged — every future FIND through that region gets cheaper as a direct result.

The full formal potential function behind the amortized O(α(n)) bound is one of the most intricate proofs in this entire course — genuinely graduate-level, using a potential built from the Ackermann hierarchy itself. We will not re-derive it in full (Cormen, Leiserson, Rivest & Stein's Introduction to Algorithms, Ch. 21, has the complete argument) — but every tool the proof uses is one you already have from Lecture 3: define Φ, show cheap operations barely raise it, show the expensive operation is paid for by how much it lowers it.

CSE3144 — Lecture 31 · The Lecture 3 callback
L31 · 03 — Definition & naive operations — concept ~6 min

The simplest possible version — and its obvious weakness

Representation

Each set is a tree, stored via parent pointers only. The tree's ROOT is the set's representative. MAKE-SET(x): parent[x] = x (a singleton, its own root). FIND(x): follow parent pointers until reaching a node that is its own parent — that node is the representative.

Naive UNION(x,y)

Find both roots. If different, simply make one root point to the other — no rule at all for WHICH one attaches under which. This is correct, but nothing stops it from building a long, thin chain instead of a shallow tree — and a long chain means an expensive FIND.

CSE3144 — Lecture 31 · Naive operations concept
L31 · 04 — Animation: the naive structure degrades ~18 min

union(1,2), union(2,3), union(3,4), union(4,5), union(5,6)

Every union always attaches the first root under the second root's — no rule at all. Then FIND(1), traced hop by hop.

Interactive — naive union-find, worst case
CSE3144 — Lecture 31 · Naive union-find
L31 · 05 — Union by rank — concept ~6 min

Always attach the shorter tree under the taller one

Rank

Every root tracks a RANK — an upper bound on its subtree's height. MAKE-SET(x): rank[x] = 0.

UNION(x,y), by rank
  • Find both roots. If rank[rootX] < rank[rootY]: attach rootX under rootY (rootY stays taller, its rank is unchanged — it was already at least as tall).
  • If rank[rootX] > rank[rootY]: attach rootY under rootX, symmetrically.
  • If ranks are EQUAL: attach either one under the other (pick a convention), and increment the new root's rank by 1 — this is the only case where height can actually grow.

Because rank only grows when merging two EQUAL-rank trees, and doing so at least doubles the resulting tree's minimum size, a tree of rank r has at least 2r nodes. With n elements total, rank can never exceed log₂ n — height is O(log n), guaranteed, regardless of union order.

CSE3144 — Lecture 31 · Union by rank concept
L31 · 06 — Animation: union by rank ~18 min

The SAME 5 unions — this time, by rank

Identical input sequence to Section 04. Watch the shape it produces this time.

Interactive — union by rank, same sequence
CSE3144 — Lecture 31 · Union by rank
L31 · 07 — Path compression — concept ~6 min

Flatten every node you visit, as a free side effect of asking

The idea

FIND(x) already walks from x up to the root to answer the query. Path compression adds one more pass: once the root is known, go back through every node visited on the way up and point its parent DIRECTLY at the root — regardless of how many hops it used to take.

Why it's essentially free

The nodes were already being visited to answer THIS query — compression just rewrites pointers on nodes already in hand. The payoff is entirely for the FUTURE: every one of those nodes now answers its next FIND in a single hop, no matter how deep it used to be.

CSE3144 — Lecture 31 · Path compression concept
L31 · 08 — Animation: path compression ~20 min

FIND(A) on a genuinely deep (height-3) tree

Built independently via 7 rank-based unions engineered to reach height 3: {A,B}, {C,D}, {B,D}, {E,F}, {G,H}, {F,H}, {D,H}. Path from A to the root: A→B→D→H.

Interactive — FIND(A) with path compression
CSE3144 — Lecture 31 · Path compression
L31 · 09 — Combined complexity, honestly ~10 min

Amortized O(α(n)) — essentially O(1), for any n that could ever exist

The result (Tarjan, 1975)

With BOTH union by rank AND path compression, a sequence of m operations on n elements costs O(m · α(n)) total, where α is the inverse Ackermann function — it grows so slowly that α(n) ≤ 4 for any n you could ever actually write down (n would need to exceed the number of atoms in the observable universe, several times over, before α(n) reaches 5). In every practical sense: O(1) amortized per operation.

What each optimization contributes alone
CombinationAmortized cost per op
Neither (naive)O(n) worst case, as verified in Section 04
Union by rank onlyO(log n)
Path compression onlyO(log n)
Both togetherO(α(n)) — essentially O(1)
CSE3144 — Lecture 31 · Complexity
L31 · 10 — Kruskal’s algorithm — concept ~8 min

A greedy optimization algorithm whose every decision is a connectivity query

The problem: minimum spanning tree (MST)

Given a connected, weighted, undirected graph, find a subset of edges that connects every vertex, contains no cycle, and has the minimum possible total weight. On n vertices such a tree always has exactly n−1 edges. Example: connecting n substations with cables at the least total cost.

Kruskal’s greedy rule
  1. Sort all edges by weight, ascending.
  2. MAKE-SET for every vertex (each starts in its own component).
  3. For each edge (u, v, w) in sorted order: FIND(u) and FIND(v). Different roots → adding the edge cannot create a cycle: add it to the MST and UNION(u, v). Same root → it would create a cycle: skip it.
  4. Stop once the MST has n−1 edges.

The cycle test in step 3 is a FIND, and the merge is a UNION — the structure of this lecture, with union by rank (on equal ranks the root of u’s tree goes under the root of v’s, exactly as in Section 06) and path compression. Why picking the cheapest safe edge is never a mistake (the cut property) is proved in algorithms courses; here the focus is the data-structure side.

CSE3144 — Lecture 31 · Kruskal’s algorithm concept
L31 · 11 — Animation: building a minimum spanning tree ~25 min

6 vertices, 9 edges — every FIND, decision and UNION, with the parent and rank arrays

Edges (u, v, w): (1,3,1) (2,4,2) (2,3,3) (1,2,4) (3,4,5) (3,5,6) (4,5,7) (5,6,8) (4,6,9). For each edge, UNION(u, v) uses union by rank (equal ranks: root of u under root of v) and every FIND uses path compression.

Interactive — Kruskal’s MST on union-find
CSE3144 — Lecture 31 · Kruskal’s MST
L31 · 12 — Kruskal’s cost ~6 min

Where Kruskal’s real cost lives

StepCost
Sort all E edges by weightO(E log E)
MAKE-SET for all n verticesO(n)
E × (two FINDs + possibly a UNION)O(E · α(n)) — essentially O(E)
TotalO(E log E) — the sort dominates

Because union-find costs essentially O(1) per operation (Section 09), it almost disappears from the total: Kruskal’s real cost is the one-time sort. In the animation, 8 edges were examined (16 FINDs); only one FIND walked two hops and compressed the path — FIND(2) at edge (1,2,4) — and every other FIND took 0 or 1 hop.

CSE3144 — Lecture 31 · Kruskal’s cost
L31 · 13 — Applications & recap ~29 min

Anywhere "merge two groups, then ask if two things are in the same group" repeats

Kruskal's MST

The canonical use case, traced in Sections 10–12: process edges by increasing weight, FIND both endpoints’ components, UNION if different (adding the edge to the MST), skip if the same (it would form a cycle).

Network & graph connectivity

"Are these two computers on the same network segment, after these link failures/additions?" — dynamic connectivity queries as edges are added over time.

Image processing

Connected-component labeling: scan an image, union adjacent same-colored pixels, and the final representatives partition the image into its connected regions.

Percolation theory

Simulating whether a fluid can flow from top to bottom through a randomly-blocked grid — a classic application in physics and materials science, and a standard teaching example for this exact structure.

"Number of provinces" / friend circles

A very common interview-style problem: given pairwise friendships, how many separate friend groups exist? Union each pair, then count distinct roots.

Compiler & type-system unification

Type inference algorithms (e.g. Hindley-Milner) use union-find to track which type variables have been unified with each other during inference.

Lecture 32 — next

Applications of Specialized Data Structures

CO CSE3144.5 — a case-study lecture synthesizing Bloom filters, priority search trees, persistent structures, and union-find across optimization and connectivity problems.

Homework — bring to Lecture 32
  • Starting from Section 06's final union-by-rank tree (root 2, rank 1, children {1,3,4,5,6}), perform union(1,7) where 7 is a new singleton. Show the rank comparison and the result.
  • On Section 08's tree AFTER path compression, run FIND(C). How many hops does it take, and why didn't Section 08's FIND(A) also compress C's pointer?
  • Add a 7th vertex with edges (6,7,2) and (1,7,10) to Section 11’s graph. Re-run Kruskal’s algorithm on all 11 edges, showing every FIND, decision, and the parent and rank arrays.
  • In 3–4 sentences, using Lecture 3's language: why does path compression alone (without union by rank) still guarantee good amortized performance, even though a single FIND could still walk O(log n) or more nodes in the worst individual case?
CSE3144 — Lecture 31

Questions?

Dr. Manu Shrivastava — LHC 308F — Friday 2:00–5:00 PM

Next: Lecture 32 — Applications of Specialized Data Structures.