Disjoint-Set Union and Path Compression
Maintain connectivity under insertions and explain how size-based linking and path compression share the cost of finding representatives.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Store each component as a rooted tree
Every element begins as a singleton. To join two sets, first find their roots and attach a smaller rooted tree to the larger, updating the size at the surviving root. With linking by size alone, whenever an element’s depth grows through a root attachment, its component size at least doubles. Depth is thus at most \(\lfloor\log_2 n\rfloor\). Path compression additionally redirects visited nodes to the root during a find, reducing future traversals without changing set membership.
Use the representation for connectivity, not paths
Joining endpoints of processed edges means two vertices share a root exactly when a path connects them through processed edges. With compression and size or rank linking, the standard amortized bound over a sequence is inverse-Ackermann per operation under the usual assumptions. The parent pointers need not describe input graph edges. Kruskal’s algorithm uses the equivalence information to avoid cycles. Rollback union-find opts for easier undo when connectivity must be queried at several times.[1][2]