DisjointSet Module
Disjoint Set Union (Union-Find) data structure for efficient set operations.
The disjoint-set data structure maintains a partition of elements into disjoint (non-overlapping) sets. It provides near-constant time operations to add elements, find which set an element belongs to, and merge two sets together.
Key Operations
Operation |
Function |
Complexity |
|---|---|---|
Make Set |
|
O(1) |
Find |
|
O(α(n)) amortized |
Union |
|
O(α(n)) amortized |
Where α(n) is the inverse Ackermann function, which grows so slowly that it is effectively a small constant (≤ 4) for all practical inputs.
Optimizations
This implementation uses two key optimizations: - Path Compression: Flattens the tree structure during find operations, making future queries faster - Union by Rank: Attaches the shorter tree under the taller tree to minimize tree height
Use Cases
- Kruskal's MST algorithm - detecting cycles
- Connected components in dynamic graphs
- Equivalence relations and partitioning
- Percolation theory and network reliability
References
Types
| Type | Description |
|
Disjoint Set Union (Union-Find) data structure. Efficiently tracks a partition of elements into disjoint sets. Uses path compression and union by rank for near-constant time operations. Time Complexity: O(α(n)) amortized per operation, where α is the inverse Ackermann function (effectively constant for all practical purposes, less than 5 for 2^65536 elements). |
Functions and values
| Function or value |
Description
|
||
|
Adds a new element to the disjoint set. The element starts in its own singleton set. If the element already exists, the structure is returned unchanged. Time Complexity: O(log n) without path compression (amortized O(α(n)) with find)
Example
val dsu: obj
|
||
Full Usage:
connected x y dsu
Parameters:
'a
y : 'a
dsu : DisjointSet<'a>
Returns: DisjointSet<'a> * bool
|
Checks if two elements are in the same set (connected). Returns the updated disjoint set (due to path compression) and a boolean result. Time Complexity: O(α(n)) amortized
Example
Use Cases
val dsu: obj
val dsu2: obj
val result1: obj
val dsu3: obj
val result2: obj
|
||
|
Returns the number of disjoint sets. Counts the distinct sets by finding the unique roots. Time Complexity: O(n × α(n)) where n is the number of elements
Example
Use Cases
val dsu: obj
val count: obj
|
||
|
Creates a new empty disjoint set structure.
|
||
Full Usage:
find element dsu
Parameters:
'a
dsu : DisjointSet<'a>
Returns: DisjointSet<'a> * 'a
|
Finds the representative (root) of the set containing the element. Uses path compression to flatten the tree structure for future queries. If the element doesn't exist, it's automatically added first. Returns a tuple of Time Complexity: O(α(n)) amortized (inverse Ackermann)
Example
val dsu: obj
val dsu2: obj
val root: obj
|
||
|
Creates a disjoint set from a sequence of pairs to union. This is a convenience function for building a disjoint set from edge lists or connection pairs. Perfect for graph problems, AoC, and competitive programming. Time Complexity: O(k × α(n)) where k is the number of pairs
Example
Use Cases
val dsu: obj
|
||
|
Returns the total number of elements in the structure. Time Complexity: O(1)
Example
val dsu: obj
val n: obj
|
||
|
Returns all disjoint sets as a list of lists. Each inner list contains all members of one set. The order of sets and elements within sets is unspecified. Note: This operation doesn't perform path compression, so the structure is not modified. Time Complexity: O(n × α(n))
Example
Use Cases
val dsu: obj
val sets: obj
|
||
|
Merges the sets containing the two elements. Uses union by rank to keep the tree balanced. If the elements are already in the same set, returns unchanged. Time Complexity: O(α(n)) amortized
Example
val dsu: obj
val dsu2: obj
val dsu3: obj
|