210#include <tclap/CmdLine.h>
213using namespace Aleph;
236 return row *
n + col;
242 return row <
n && col <
n;
270 const int dr[] = {-1, 1, 0, 0};
271 const int dc[] = {0, 0, -1, 1};
273 for (
int i = 0; i < 4; i++)
276 size_t nc = col +
dc[i];
331 return static_cast<double>(
num_open) / (
n *
n);
343 for (
size_t col = 0; col <
n; col++)
368 for (
size_t i = 0; i < n; i++)
369 for (
size_t j = 0; j < n; j++)
370 sites.emplace_back(i, j);
375 for (
const auto& [
row, col] :
sites)
378 if (
perc.percolates())
382 return perc.open_fraction();
390 cout <<
"\n=== Monte Carlo Percolation Threshold Estimation ===" <<
endl;
391 cout <<
"Grid size: " << n <<
"×" << n <<
endl;
400 auto start = chrono::high_resolution_clock::now();
402 for (
size_t t = 0; t <
trials; t++)
410 cout <<
" Completed " << (t + 1) <<
"/" <<
trials <<
" trials..." <<
endl;
413 auto end = chrono::high_resolution_clock::now();
414 double ms = chrono::duration<double, milli>(end - start).count();
422 cout <<
"\n--- Results ---" <<
endl;
425 cout <<
"95% confidence interval: ["
428 cout <<
"\nTheoretical p* ≈ 0.592746" <<
endl;
438 cout <<
"\n=== Visual Percolation Demo ===" <<
endl;
439 cout <<
"Grid size: " << n <<
"×" << n <<
endl;
446 for (
size_t i = 0; i < n; i++)
447 for (
size_t j = 0; j < n; j++)
448 sites.emplace_back(i, j);
451 cout <<
"\nLegend: # = blocked, . = open (not full), O = full (connected to top)" <<
endl;
454 vector<double>
checkpoints = {0.2, 0.4, 0.5, 0.55, 0.6, 0.65, 0.7};
469 <<
"% sites open (" <<
perc.number_of_open_sites() <<
"/" <<
total_sites
473 if (
perc.percolates())
475 cout <<
"\n*** SYSTEM PERCOLATES at p = "
481 cout <<
"(Does not percolate yet)" <<
endl;
486 if (!
perc.percolates())
494 cout <<
"\n--- Percolation achieved ---" <<
endl;
496 cout <<
"\n*** SYSTEM PERCOLATES at p = "
506 cout <<
"\n=== Union-Find Data Structure ===" <<
endl;
508 cout <<
"\nThe Union-Find (Disjoint Set Union) structure supports:" <<
endl;
509 cout <<
" - make_set(x): Create a new set containing only x" <<
endl;
510 cout <<
" - find(x): Return the representative of x's set" <<
endl;
511 cout <<
" - union(x, y): Merge the sets containing x and y" <<
endl;
514 cout <<
" - Path compression: During find(), make nodes point directly to root" <<
endl;
515 cout <<
" - Union by rank: Attach smaller tree under larger tree's root" <<
endl;
517 cout <<
"\nComplexity (with both optimizations):" <<
endl;
518 cout <<
" - Nearly O(1) per operation (amortized)" <<
endl;
519 cout <<
" - Formally: O(α(n)) where α is inverse Ackermann function" <<
endl;
520 cout <<
" - For all practical n: α(n) ≤ 4" <<
endl;
522 cout <<
"\nApplication in Percolation:" <<
endl;
523 cout <<
" - Each grid cell is an element" <<
endl;
524 cout <<
" - Opening a cell: union with adjacent open cells" <<
endl;
525 cout <<
" - Percolation test: are top and bottom connected?" <<
endl;
526 cout <<
" - Virtual nodes simplify: virtual_top connected to all top row sites" <<
endl;
533 TCLAP::CmdLine
cmd(
"Percolation Example with Union-Find",
' ',
"1.0");
535 TCLAP::ValueArg<size_t>
sizeArg(
"n",
"size",
536 "Grid size",
false, 20,
"size_t");
537 TCLAP::ValueArg<size_t>
trialsArg(
"t",
"trials",
538 "Number of Monte Carlo trials",
false, 100,
"size_t");
539 TCLAP::ValueArg<unsigned>
seedArg(
"s",
"seed",
540 "Random seed",
false, 42,
"unsigned");
541 TCLAP::SwitchArg
visualArg(
"i",
"visual",
542 "Show visual demonstration",
false);
543 TCLAP::SwitchArg
monteArg(
"m",
"monte-carlo",
544 "Run Monte Carlo simulation",
false);
546 "Explain Union-Find",
false);
547 TCLAP::SwitchArg
allArg(
"a",
"all",
548 "Run all demos",
false);
550 "Show detailed output",
false);
565 unsigned seed =
seedArg.getValue();
576 cout <<
"=== Percolation: A Union-Find Application ===" <<
endl;
591 cout <<
"\n=== Summary ===" <<
endl;
592 cout <<
"Percolation threshold for 2D square lattice:" <<
endl;
593 cout <<
" Theoretical: p* ≈ 0.592746" <<
endl;
594 cout <<
" (Critical probability at which infinite cluster appears)" <<
endl;
595 cout <<
"\nUnion-Find enables efficient connectivity queries:" <<
endl;
596 cout <<
" O(α(n)) per operation ≈ O(1) in practice" <<
endl;
600 catch (TCLAP::ArgException& e)
602 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
size_t size() const noexcept
Count the number of elements of the list.
Binary relation between a set of integers.
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.
Percolation system using Union-Find.
PercolationSystem(size_t grid_size)
Create an n×n percolation system with all sites blocked.
size_t index(size_t row, size_t col) const
double open_fraction() const
Get fraction of open sites.
bool is_open(size_t row, size_t col) const
Check if site at (row, col) is open.
void open(size_t row, size_t col)
Open a site at (row, col) and connect to adjacent open sites.
void print() const
Print the grid (for small grids)
bool is_full(size_t row, size_t col)
Check if site at (row, col) is connected to top (is "full")
vector< bool > open_sites
size_t virtual_bottom() const
size_t size() const
Get grid size.
size_t number_of_open_sites() const
Get number of open sites.
size_t virtual_top() const
bool valid(size_t row, size_t col) const
bool percolates()
Check if system percolates (top connected to bottom)
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
auto variance(const Container &data, bool population=false) -> std::decay_t< decltype(*std::begin(data))>
Compute variance using Welford's numerically stable algorithm.
auto stddev(const Container &data, bool population=false) -> std::decay_t< decltype(*std::begin(data))>
Compute standard deviation.
auto mean(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the arithmetic mean.
auto shuffle(const C< T > &c)
Randomly shuffle a sequence.
bool verbose
Flag enabling verbose output.
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
void monte_carlo_simulation(size_t n, size_t trials, unsigned seed, bool verbose)
Monte Carlo estimation of percolation threshold.
double run_experiment(size_t n, mt19937 &rng)
Run a single percolation experiment.
void explain_union_find()
Explain Union-Find operations.
void visual_demonstration(size_t n, unsigned seed)
Interactive demonstration with visualization.
Union-Find (Disjoint Set Union) data structure.