Union-Find Disjoint Set / UFDS
Operations
findunion
When to use UFDS?
- If you want to know whether two items are in the same set (e.g. whether 2 nodes are in the same component)
Question
Can you think of other scenarios?
Implementations
| Implementation | Find | Union |
|---|---|---|
| QuickFind | \(O(1)\) | \(O(n)\) |
| QuickUnion | \(O(n)\) | \(O(n)\) |
| Union by Rank | \(O(\log n)\) | \(O(\log n)\) |
| Path Compression | \(O(\log n)\) (Amortized) | \(O(\log n)\) (Amortized) |
| Union by Rank + Path Compression | \(O(\alpha(n))\) (Amortized) | \(O(\alpha(n))\) (Amortized) |
Info
From this paper (just look at the table at the second last page), The time complexity of Union by Rank + Path Compression is given by:
\[
T(m, n) = \begin{cases}
\Theta(m \alpha(m, n)) & \text{if } m \geq n \\
\Theta(n + m \alpha(n,n)) & \text{otherwise}
\end{cases}
\]
for a sequence of \(m\) union/find operations,
with \(n\) items in the UFDS.
The proof can be found in CLRS, under the chapter "Data Structures for Disjoint Sets".
Proof: Why Union by Rank takes logarithmic time for find and union?
It takes \(O(\log n)\) time for find and union because that's the max height of the UFDS tree.
You can prove it yourself :>
You may want to refer to how the AVL tree height is derived.