- Home
- /
- Tutorials
- /
- DSA Tutorial
- /
- Union-Find (Disjoint Sets)
Union-Find
Union-Find (Disjoint Sets)
Union-Find is the structure for questions about grouping. It cannot tell you the path between two nodes - only whether one exists - and that restriction is what makes it almost free.
What it does
Two operations, on a collection of disjoint groups:
- find(x) - which group is x in?
- union(a, b) - merge the two groups.
With both optimisations applied, each is effectively O(1) - technically inverse Ackermann, which is under 5 for any input that will ever exist.
Union-Find
javascript
