Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Which set is it in? Almost instantly
Each edge is a union of the sets of its two vertices: find follows parent pointers to the root, with union by rank and path compression
Each edge is one union, in the order the edges were drawn. Switch an option off to see the trees grow taller.
Select an algorithm and generate steps to begin visualization
Union-find, also called disjoint set union (DSU), keeps a collection of disjoint sets and supports two operations: find, which tells which set an element is in, and union, which merges two sets. Every set is stored as a tree whose root represents it, and every element keeps a pointer to its parent. Bernard Galler and Michael Fischer introduced the structure in 1964, and Robert Tarjan proved in 1975 that with two simple optimizations it takes almost constant time per operation.
find(v) follows parent pointers from v up to the root of its tree. union(a, b) finds both roots and, if they differ, makes one the parent of the other. Union by rank puts the root of the shallower tree under the other, so no tree grows taller than log2(n). Path compression makes every vertex a find passes point straight at the root, which flattens the tree for later finds. With both, m operations on n elements cost O(m α(n)), where the inverse Ackermann function α(n) is at most 4 for any input that fits in a computer. In the visualizer each edge is one union, in the order the edges were drawn, parent pointers are drawn as arrows, and either optimization can be switched off to watch the trees grow tall.
Union-find is the heart of Kruskal's minimum spanning tree algorithm, where it rejects the edges that would close a cycle. It tracks connected components while edges arrive, groups pixels in image segmentation, merges equivalent types in compilers and duplicate accounts in identity resolution, and models percolation and maze generation. It is also one of the most frequent data structures in coding interviews and competitive programming.
Both operations take a few lines, and each optimization changes one of them: union by rank decides which root goes under which, and path compression rewrites the parent pointers on the way back from a find.
MakeSet(v):
parent[v] = v
rank[v] = 0
Find(v):
if parent[v] != v:
parent[v] = Find(parent[v]) // path compression
return parent[v]
Union(a, b):
ra = Find(a)
rb = Find(b)
if ra == rb: return // already one set
if rank[ra] < rank[rb]: swap(ra, rb) // union by rank
parent[rb] = ra
if rank[ra] == rank[rb]: rank[ra] = rank[ra] + 1The rank is an upper bound on the height of a root's tree. It grows only when two trees of equal rank are joined, and a tree of rank r has at least 2^r vertices, so no rank exceeds log2(n). Path compression can make the real height smaller than the rank, which is why the bound is called rank and not height.
Run the ten unions this page loads on the vertices A to H, first with both optimizations and then without them, to see what they buy.
Example graph: Edges, in order: B-A, C-B, D-C, E-D, F-E, G-H, A-G, A-F, H-F, B-E.
All eight vertices end in one set. The finds follow 10 parent pointers with the optimizations and 17 without them, and the gap grows quickly with the input: a chain of n vertices costs n steps per find, a tree of rank log2(n) only log2(n).
Time: O(m α(n)) · Space: O(n)
Union by rank alone keeps every tree at most log2(n) tall, so each find and union costs O(log n). Path compression alone also gives O(log n) amortized per operation. Together, Tarjan proved in 1975 that m operations on n elements take O(m α(n)) time, where α is the inverse of the Ackermann function, and Fredman and Saks showed in 1989 that no structure can do better. α(n) is at most 4 for every n below 2^65536, so in practice each operation takes constant time. The structure stores one parent and one rank per element, O(n) space.
Union-find answers one question, whether two elements are connected, and only while sets merge. Other structures cover the other cases.
| Alternative | Prefer it when | Cost |
|---|---|---|
| BFS or DFS | The graph is fixed and the components are needed once: one traversal labels every vertex. | O(V + E) |
| Quick-find (an array of set labels) | Finds must take O(1) and unions are rare: every union relabels a whole set. | O(1) find, O(n) union |
| Dynamic connectivity structures | Edges are deleted as well as added. Union-find cannot split a set. | polylogarithmic per update |
| Kruskal with union-find | Building a minimum spanning tree: sort the edges, then add each edge whose ends have different roots. | O(E log E) |
Related algorithms: Kruskal's MST Algorithm, Connected Components, Borůvka's Algorithm, Cycle Detection