|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Disjoint Set Union (Union-Find) data structure demonstration. More...
Go to the source code of this file.
Classes | |
| struct | Edge |
Functions | |
| void | demo_basic_operations () |
| void | demo_network_connectivity () |
| void | demo_kruskal_simulation () |
| void | demo_path_compression () |
| int | main () |
Disjoint Set Union (Union-Find) data structure demonstration.
This example demonstrates the Union-Find (also called Disjoint Set Union or DSU) data structure, one of the most elegant and efficient data structures in computer science. Despite its simplicity, it achieves near-constant-time operations through clever optimizations.
Union-Find maintains a collection of disjoint (non-overlapping) sets and supports two main operations:
It's perfect for tracking connectivity and equivalence relationships.
| Operation | Naive | Optimized |
|---|---|---|
| make_set | O(1) | O(1) |
| find | O(n) | O(α(n)) |
| union | O(n) | O(α(n)) |
Space: O(n) - one entry per element
Amortized complexity: O(α(n)) per operation, where α(n) is the inverse Ackermann function. For all practical values of n, α(n) ≤ 5, making it effectively constant time!
Union-Find is essential for Kruskal's MST algorithm:
This example has no command-line options; it runs all demos.
Definition in file union_find_example.C.
| void demo_basic_operations | ( | ) |
Definition at line 196 of file union_find_example.C.
References Fixed_Relation::are_connected(), Fixed_Relation::get_num_blocks(), Fixed_Relation::join(), and Aleph::maps().
Referenced by main().
| void demo_kruskal_simulation | ( | ) |
Definition at line 339 of file union_find_example.C.
References Fixed_Relation::are_connected(), StlAlephIterator< SetName >::begin(), StlAlephIterator< SetName >::end(), Fixed_Relation::join(), Aleph::maps(), Aleph::HTList::size(), and Aleph::sort().
Referenced by main().
| void demo_network_connectivity | ( | ) |
Definition at line 266 of file union_find_example.C.
References Aleph::maps(), and Aleph::HTList::size().
Referenced by main().
| void demo_path_compression | ( | ) |
Definition at line 406 of file union_find_example.C.
References Fixed_Relation::are_connected(), Fixed_Relation::get_num_blocks(), Fixed_Relation::join(), and Aleph::maps().
Referenced by main().
| int main | ( | ) |
Definition at line 445 of file union_find_example.C.
References demo_basic_operations(), demo_kruskal_simulation(), demo_network_connectivity(), demo_path_compression(), and Aleph::maps().