DSA Notes
Master DSU with path compression and union by rank for efficient operations.
What is Disjoint Set Union?
Disjoint Set Union (DSU), also called Union-Find, is a data structure that tracks a collection of non-overlapping (disjoint) sets. It supports two primary operations extremely efficiently: finding which set an element belongs to, and merging two sets into one. Think of it like managing groups of friends — you can quickly check if two people are in the same friend group, and you can merge two friend groups together.
The beauty of DSU is that with two simple optimizations (path compression and union by rank), both operations run in nearly O(1) time — technically O(α(n)) where α is the inverse Ackermann function, which is effectively constant for any practical input size.
Naive Implementation
The simplest approach uses a parent array where each element points to its parent:
Problem: Without optimizations, the tree can become a long chain, making Find operation O(n) in the worst case.
Optimized Implementation with Path Compression and Union by Rank
How Path Compression Works
Path compression flattens the tree structure during Find operations. When you find the root of an element, you make every node along the path point directly to the root:
Before path compression (finding root of 4)
0
|
1
|
2
|
3
|
4
After find(4) with path compression
0
/ | | \
1 2 3 4
Now future Find operations on any of these elements take O(1) since they point directly to the root.
How Union by Rank Works
Union by rank ensures we always attach the shorter tree under the root of the taller tree. This prevents the tree from growing unnecessarily tall:
Java Implementation
Complexity Analysis
| Operation | Naive | With Union by Rank | With Both Optimizations |
|---|---|---|---|
| Find | O(n) | O(log n) | O(α(n)) ≈ O(1) |
| Union | O(n) | O(log n) | O(α(n)) ≈ O(1) |
| Space | O(n) | O(n) | O(n) |
α(n) is the inverse Ackermann function — it grows so slowly that for any practical input (even 10^80 elements), α(n) ≤ 4. Effectively constant time.
Classic Applications
1. Kruskal's Minimum Spanning Tree
2. Detecting Cycles in Undirected Graphs
def has_cycle(edges, n):
dsu = DSU(n)
for u, v in edges:
if dsu.connected(u, v):
return True # Edge connects two nodes already in same component
dsu.union(u, v)
return False3. Number of Connected Components
def count_components(edges, n):
dsu = DSU(n)
for u, v in edges:
dsu.union(u, v)
return dsu.num_componentsReal-World Applications
- Social networks — finding friend groups, suggesting connections
- Image processing — connected component labeling in pixel grids
- Network connectivity — determining if all nodes can reach each other
- Percolation theory — modeling fluid flow through porous materials
- Game development — merging regions in procedural map generation
- Database systems — equivalence class determination in query optimization
Interview Questions
Q1: What is the time complexity of DSU with both optimizations? A: O(α(n)) per operation, where α is the inverse Ackermann function. For all practical purposes, this is O(1).
Q2: Can you undo union operations in DSU? A: Standard DSU does not support undo. However, a "DSU with rollback" variant stores history on a stack and can reverse unions. This is used in offline divide-and-conquer problems.
Q3: How would you find the number of connected components? A: Maintain a counter initialized to n (number of elements). Decrease by 1 on each successful union. The counter always holds the current component count.
Q4: How is DSU used in Kruskal's algorithm? A: Sort edges by weight. For each edge, check if its endpoints are in the same component (Find). If not, add the edge to MST and merge components (Union). This greedily builds the MST.
Q5: What is the difference between union by rank and union by size? A: Union by rank attaches the shorter tree under the taller one (based on tree height). Union by size attaches the smaller set under the larger one (based on element count). Both achieve O(log n) height. Combined with path compression, both give O(α(n)).
Key Takeaways
- DSU is the go-to data structure for dynamic connectivity problems
- Path compression and union by rank together give nearly O(1) per operation
- It is essential for Kruskal's MST, cycle detection, and connected components
- The implementation is short (under 30 lines) but incredibly powerful
- Learn to recognize problems that ask "are these connected?" — they usually need DSU
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Disjoint Set Union (DSU) - Union Find.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, advanced, topics, disjoint
Related Data Structures & Algorithms Topics