Tracks a partition of elements into disjoint sets. Union merges two sets by rank and Find locates a set's root with path compression, giving near-constant amortized time.
Tracks a partition of elements into disjoint sets. Union merges two sets by rank and Find locates a set's root with path compression, giving near-constant amortized time.
Time complexity: O(α(n)). Space complexity: O(n).
Use the interactive visualizer above to run Union-Find (Disjoint Set) on your own input and watch every comparison, swap, and operation animate step by step — pause, scrub, or replay at any speed.