fixed_disjoint_set¶
Union-Find data structure with path compression and union by rank. All N elements are pre-allocated.
Synopsis¶
Template Parameters¶
| Parameter | Description |
|---|---|
N |
Total number of elements (0 to N-1) |
Member Functions¶
find(x)Find root of element x (with path compression)unite(x, y)Merge sets containing x and y; returns true if they were separateconnected(x, y)True if x and y are in the same setcount_sets()Number of disjoint componentselement_count()Returns Nreset()Reset all elements to individual sets
Complexity¶
| Operation | Amortized |
|---|---|
| find | O(α(N)) ≈ O(1) |
| unite | O(α(N)) ≈ O(1) |
| connected | O(α(N)) ≈ O(1) |
Where α is the inverse Ackermann function (effectively constant for all practical inputs).
Example¶
prototype::fixed_disjoint_set<100> ds;
// Connect nodes in a network
ds.unite(0, 1);
ds.unite(1, 2);
ds.unite(5, 6);
// Query connectivity
ds.connected(0, 2); // true (transitive)
ds.connected(0, 5); // false
// Count components
ds.count_sets(); // 100 - 3 = 97 (merged 3 pairs, but {0,1,2} is one merge)
Use Cases¶
- Network connectivity checking
- Kruskal's MST algorithm
- Image segmentation (connected components)
- Equivalence class tracking