|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Percolation Simulation using Union-Find. More...
#include <iostream>#include <iomanip>#include <vector>#include <random>#include <chrono>#include <tpl_union.H>#include <tclap/CmdLine.h>Go to the source code of this file.
Classes | |
| class | PercolationSystem |
| Percolation system using Union-Find. More... | |
Functions | |
| double | run_experiment (size_t n, mt19937 &rng) |
| Run a single percolation experiment. | |
| void | monte_carlo_simulation (size_t n, size_t trials, unsigned seed, bool verbose) |
| Monte Carlo estimation of percolation threshold. | |
| void | visual_demonstration (size_t n, unsigned seed) |
| Interactive demonstration with visualization. | |
| void | explain_union_find () |
| Explain Union-Find operations. | |
| int | main (int argc, char *argv[]) |
Percolation Simulation using Union-Find.
This example demonstrates the Union-Find (Disjoint Set Union) data structure through a classic application: percolation simulation. Percolation theory studies the behavior of connected clusters in random graphs and has applications in physics, materials science, and network analysis.
Given an n×n grid of sites:
Problem: Need to check if top connects to bottom efficiently
Solution with Union-Find:
find(top) == find(bottom)The percolation threshold p* is the critical probability at which the system transitions from non-percolating to percolating.
Properties:
| Lattice Type | Dimension | Threshold p* |
|---|---|---|
| Square | 2D | ≈ 0.593 |
| Square | 3D | ≈ 0.312 |
| Triangular | 2D | 0.5 (exact) |
| Hexagonal | 2D | 1 - 0.5 = 0.5 (exact) |
Through Monte Carlo simulation:
| Operation | Complexity | Notes |
|---|---|---|
| Open site | O(α(n²)) | Union-Find union |
| Check percolation | O(α(n²)) | Union-Find find |
| Full simulation | O(n² × α(n²)) | Open all sites |
With path compression and union by rank: α(n²) ≈ constant!
Definition in file percolation_example.C.
| void explain_union_find | ( | ) |
Explain Union-Find operations.
Definition at line 504 of file percolation_example.C.
References Aleph::maps().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 529 of file percolation_example.C.
References Aleph::all(), explain_union_find(), Aleph::maps(), min(), monte_carlo_simulation(), Aleph::verbose, and visual_demonstration().
| void monte_carlo_simulation | ( | size_t | n, |
| size_t | trials, | ||
| unsigned | seed, | ||
| bool | verbose | ||
| ) |
Monte Carlo estimation of percolation threshold.
Definition at line 388 of file percolation_example.C.
References abs(), Aleph::maps(), Aleph::mean(), rng, run_experiment(), sqrt(), Aleph::stddev(), Aleph::sum(), Aleph::variance(), and Aleph::verbose.
Referenced by main().
| double run_experiment | ( | size_t | n, |
| mt19937 & | rng | ||
| ) |
Run a single percolation experiment.
Opens random sites until system percolates, returns threshold
Definition at line 362 of file percolation_example.C.
References StlAlephIterator< SetName >::begin(), StlAlephIterator< SetName >::end(), Aleph::maps(), rng, and Aleph::shuffle().
Referenced by monte_carlo_simulation().
| void visual_demonstration | ( | size_t | n, |
| unsigned | seed | ||
| ) |
Interactive demonstration with visualization.
Definition at line 436 of file percolation_example.C.
References StlAlephIterator< SetName >::begin(), StlAlephIterator< SetName >::end(), Aleph::maps(), rng, Aleph::shuffle(), and Aleph::HTList::size().
Referenced by main().