190using namespace Aleph;
199 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
200 cout <<
"║ EXAMPLE 1: Basic Union-Find Operations ║\n";
201 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
207 cout <<
"Created Union-Find with " << n <<
" elements (0 to 9)\n";
208 cout <<
"Initially, each element is in its own singleton set.\n\n";
211 cout <<
"Initial state: Each element is isolated in its own set.\n";
214 cout <<
"\n--- Performing unions ---\n\n";
217 cout <<
"join(0, 1): Merge sets containing 0 and 1\n";
220 cout <<
"join(2, 3): Merge sets containing 2 and 3\n";
223 cout <<
"join(4, 5): Merge sets containing 4 and 5\n";
226 cout <<
"join(0, 2): Merge sets {0,1} and {2,3} into {0,1,2,3}\n";
229 cout <<
"\n--- Checking connectivity ---\n\n";
232 auto check = [&uf](
size_t a,
size_t b) {
234 cout <<
" " << a <<
" and " << b <<
" are "
235 << (
same ?
"in the SAME set" :
"in DIFFERENT sets") <<
"\n";
238 cout <<
"Connectivity queries:\n";
244 cout <<
"\n--- Final state ---\n\n";
247 cout <<
"Elements in same set as 0: ";
248 for (
size_t i = 0; i < n; ++i)
253 cout <<
"Elements in same set as 4: ";
254 for (
size_t i = 0; i < n; ++i)
269 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
270 cout <<
"║ EXAMPLE 2: Network Connectivity Problem ║\n";
271 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
273 cout <<
"Problem: A network has 8 computers. Given a list of direct\n";
274 cout <<
"connections, determine which computers can communicate.\n\n";
282 {3, 4}, {4, 5}, {5, 3},
286 cout <<
"Network topology (direct connections):\n";
287 cout <<
" Cluster A: 0 — 1 — 2\n";
288 cout <<
" Cluster B: 3 — 4 — 5 (triangle)\n";
289 cout <<
" Cluster C: 6 — 7\n";
290 cout <<
" Isolated: (none)\n\n";
293 cout <<
"Adding connections:\n";
295 cout <<
" Connect " << a <<
" ↔ " << b <<
"\n";
299 cout <<
"\n--- Communication queries ---\n\n";
303 cout <<
" Computer " << a <<
" and " << b <<
": "
304 << (
can ?
"✓ CAN communicate" :
"✗ CANNOT communicate") <<
"\n";
307 cout <<
"Testing if computers can communicate:\n";
314 cout <<
"\n--- Adding a bridge connection ---\n\n";
316 cout <<
"Adding connection 2 ↔ 3 (bridges Cluster A and B)\n";
319 cout <<
"\nUpdated connectivity:\n";
342 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
343 cout <<
"║ EXAMPLE 3: Kruskal's MST Algorithm Simulation ║\n";
344 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
346 cout <<
"Kruskal's algorithm finds the Minimum Spanning Tree by:\n";
347 cout <<
" 1. Sort edges by weight\n";
348 cout <<
" 2. For each edge (u,v): add to MST if u,v in different sets\n";
349 cout <<
" 3. Union-Find tracks which vertices are connected\n\n";
356 {0, 1, 4.0}, {0, 2, 2.0}, {1, 2, 1.0}, {1, 3, 5.0},
357 {2, 3, 8.0}, {2, 4, 10.0}, {3, 4, 2.0}, {3, 5, 6.0}, {4, 5, 3.0}
360 cout <<
"Graph edges:\n";
361 for (
const auto& e :
edges)
362 cout <<
" " << e.u <<
" —(" << e.weight <<
")— " << e.v <<
"\n";
367 cout <<
"\nSorted edges by weight:\n";
368 for (
const auto& e :
edges)
369 cout <<
" " << e.u <<
" —(" << e.weight <<
")— " << e.v <<
"\n";
376 cout <<
"\n--- Running Kruskal's algorithm ---\n\n";
378 for (
const auto& e :
edges) {
380 cout <<
" ADD edge " << e.u <<
" —(" << e.weight <<
")— " << e.v
381 <<
" (connects different components)\n";
386 cout <<
" SKIP edge " << e.u <<
" —(" << e.weight <<
")— " << e.v
387 <<
" (would create cycle)\n";
395 cout <<
"\n--- Minimum Spanning Tree ---\n\n";
396 cout <<
"MST edges:\n";
397 for (
const auto& e :
mst)
398 cout <<
" " << e.u <<
" —(" << e.weight <<
")— " << e.v <<
"\n";
409 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
410 cout <<
"║ EXAMPLE 4: Path Compression Effect ║\n";
411 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
413 cout <<
"Path compression flattens the tree during find operations,\n";
414 cout <<
"making subsequent finds much faster.\n\n";
420 cout <<
"Creating connected components by sequential unions:\n";
421 for (
size_t i = 0; i < n - 1; ++i) {
422 cout <<
" join(" << i <<
", " << (i + 1) <<
")\n";
426 cout <<
"\nAfter all joins:\n";
427 cout <<
" All elements 0-" << (n-1) <<
" are now in the same set.\n";
430 cout <<
"\nVerifying all elements are connected:\n";
431 for (
size_t i = 1; i < n; ++i) {
433 cout <<
" 0 connected to " << i <<
": " << (
conn ?
"YES" :
"NO") <<
"\n";
436 cout <<
"\nPath compression happens automatically during are_connected().\n";
437 cout <<
"The internal tree structure is flattened, making future\n";
438 cout <<
"operations nearly O(1) - this is the key to Union-Find's speed!\n";
448 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
449 cout <<
"║ UNION-FIND (Disjoint Set Union) Data Structure Demo ║\n";
451 cout <<
"║ Aleph-w Library - https://github.com/lrleon/Aleph-w ║\n";
452 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n";
460 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
461 cout <<
"║ Summary ║\n";
462 cout <<
"╠══════════════════════════════════════════════════════════════════╣\n";
463 cout <<
"║ Union-Find is one of the most elegant data structures: ║\n";
465 cout <<
"║ • Nearly O(1) operations via union-by-rank & path compression ║\n";
466 cout <<
"║ • Essential for Kruskal's MST algorithm ║\n";
467 cout <<
"║ • Used in network connectivity, image processing, and more ║\n";
469 cout <<
"║ Aleph-w provides Fixed_Relation for integer elements and ║\n";
470 cout <<
"║ Relation for arbitrary types with hash-based element lookup. ║\n";
471 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
size_t size() const noexcept
Count the number of elements of the list.
Binary relation between a set of integers.
constexpr size_t get_num_blocks() const noexcept
Return the number of connected blocks, which is the number of equivalence classes.
void join(size_t i, size_t j)
Insert the pair '(i,j)' in the relation.
bool are_connected(size_t i, size_t j)
Return true if item i is related to item j.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Main namespace for Aleph-w library functions.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
DynList< T > maps(const C &c, Op op)
Classic map operation.
bool operator<(const Edge &other) const
Union-Find (Disjoint Set Union) data structure.
void demo_network_connectivity()
void demo_basic_operations()
void demo_kruskal_simulation()
void demo_path_compression()