Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
PercolationSystem Class Reference

Percolation system using Union-Find. More...

Collaboration diagram for PercolationSystem:
[legend]

Public Member Functions

 PercolationSystem (size_t grid_size)
 Create an n×n percolation system with all sites blocked.
 
void open (size_t row, size_t col)
 Open a site at (row, col) and connect to adjacent open sites.
 
bool is_open (size_t row, size_t col) const
 Check if site at (row, col) is open.
 
bool is_full (size_t row, size_t col)
 Check if site at (row, col) is connected to top (is "full")
 
bool percolates ()
 Check if system percolates (top connected to bottom)
 
size_t number_of_open_sites () const
 Get number of open sites.
 
size_t size () const
 Get grid size.
 
double open_fraction () const
 Get fraction of open sites.
 
void print () const
 Print the grid (for small grids)
 

Private Member Functions

size_t virtual_top () const
 
size_t virtual_bottom () const
 
size_t index (size_t row, size_t col) const
 
bool valid (size_t row, size_t col) const
 

Private Attributes

size_t n
 
vector< boolopen_sites
 
Fixed_Relation uf
 
size_t num_open
 

Detailed Description

Percolation system using Union-Find.

Models an n×n grid where each site can be open or blocked. Uses virtual top and bottom nodes for efficient percolation testing.

Definition at line 221 of file percolation_example.C.

Constructor & Destructor Documentation

◆ PercolationSystem()

PercolationSystem::PercolationSystem ( size_t  grid_size)
inline

Create an n×n percolation system with all sites blocked.

Definition at line 249 of file percolation_example.C.

Member Function Documentation

◆ index()

size_t PercolationSystem::index ( size_t  row,
size_t  col 
) const
inlineprivate

Definition at line 234 of file percolation_example.C.

References Aleph::maps(), and n.

Referenced by is_full(), is_open(), and open().

◆ is_full()

bool PercolationSystem::is_full ( size_t  row,
size_t  col 
)
inline

Check if site at (row, col) is connected to top (is "full")

Definition at line 301 of file percolation_example.C.

References Fixed_Relation::are_connected(), index(), is_open(), Aleph::maps(), uf, and virtual_top().

Referenced by print().

◆ is_open()

bool PercolationSystem::is_open ( size_t  row,
size_t  col 
) const
inline

Check if site at (row, col) is open.

Definition at line 293 of file percolation_example.C.

References index(), Aleph::maps(), open_sites, and valid().

Referenced by is_full(), open(), and print().

◆ number_of_open_sites()

size_t PercolationSystem::number_of_open_sites ( ) const
inline

Get number of open sites.

Definition at line 319 of file percolation_example.C.

References num_open.

◆ open()

void PercolationSystem::open ( size_t  row,
size_t  col 
)
inline

Open a site at (row, col) and connect to adjacent open sites.

Definition at line 260 of file percolation_example.C.

References index(), is_open(), Fixed_Relation::join(), Aleph::maps(), n, num_open, open_sites, uf, valid(), virtual_bottom(), and virtual_top().

◆ open_fraction()

double PercolationSystem::open_fraction ( ) const
inline

Get fraction of open sites.

Definition at line 329 of file percolation_example.C.

References n, and num_open.

◆ percolates()

bool PercolationSystem::percolates ( )
inline

Check if system percolates (top connected to bottom)

Definition at line 311 of file percolation_example.C.

References Fixed_Relation::are_connected(), uf, virtual_bottom(), and virtual_top().

◆ print()

void PercolationSystem::print ( ) const
inline

Print the grid (for small grids)

Legend: '#' = blocked, '.' = open but not full, 'O' = full (connected to top)

Definition at line 339 of file percolation_example.C.

References is_full(), is_open(), Aleph::maps(), and n.

◆ size()

size_t PercolationSystem::size ( ) const
inline

Get grid size.

Definition at line 324 of file percolation_example.C.

References n.

◆ valid()

bool PercolationSystem::valid ( size_t  row,
size_t  col 
) const
inlineprivate

Definition at line 240 of file percolation_example.C.

References Aleph::maps(), and n.

Referenced by is_open(), and open().

◆ virtual_bottom()

size_t PercolationSystem::virtual_bottom ( ) const
inlineprivate

Definition at line 231 of file percolation_example.C.

References n.

Referenced by open(), and percolates().

◆ virtual_top()

size_t PercolationSystem::virtual_top ( ) const
inlineprivate

Definition at line 230 of file percolation_example.C.

References n.

Referenced by is_full(), open(), and percolates().

Member Data Documentation

◆ n

size_t PercolationSystem::n
private

◆ num_open

size_t PercolationSystem::num_open
private

Definition at line 227 of file percolation_example.C.

Referenced by number_of_open_sites(), open(), and open_fraction().

◆ open_sites

vector<bool> PercolationSystem::open_sites
private

Definition at line 225 of file percolation_example.C.

Referenced by is_open(), and open().

◆ uf

Fixed_Relation PercolationSystem::uf
private

Definition at line 226 of file percolation_example.C.

Referenced by is_full(), open(), and percolates().


The documentation for this class was generated from the following file: