Percolation/Mean cluster density: Difference between revisions

New post.
m (syntax highlighting fixup automation)
(New post.)
Line 855:
16 16 16 0 17 17 17 0 0 15 0 15 0 0 0
16 16 16 0 0 0 17 17 0 15 15 0 0 18 0</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
 
public final class PercolationMeanCluster {
 
public static void main(String[] aArgs) {
final int size = 15;
final double probability = 0.5;
final int testCount = 5;
Grid grid = new Grid(size, probability);
System.out.println("This " + size + " by " + size + " grid contains " + grid.clusterCount() + " clusters:");
grid.display();
System.out.println(System.lineSeparator() + " p = 0.5, iterations = " + testCount);
List<Integer> gridSizes = List.of( 10, 100, 1_000, 10_000 );
for ( int gridSize : gridSizes ) {
double sumDensity = 0.0;
for ( int test = 0; test < testCount; test++ ) {
grid = new Grid(gridSize, probability);
sumDensity += grid.clusterDensity();
}
double result = sumDensity / testCount;
System.out.println(String.format("%s%5d%s%.6f", " n = ", gridSize, ", simulation K = ", result));
}
}
}
final class Grid {
public Grid(int aSize, double aProbability) {
createGrid(aSize, aProbability);
countClusters();
}
public int clusterCount() {
return clusterCount;
}
public double clusterDensity() {
return (double) clusterCount / ( grid.length * grid.length );
}
public void display() {
for ( int row = 0; row < grid.length; row++ ) {
for ( int col = 0; col < grid.length; col++ ) {
int value = grid[row][col];
char ch = ( value < GRID_CHARACTERS.length() ) ? GRID_CHARACTERS.charAt(value) : '?';
System.out.print(" " + ch);
}
System.out.println();
}
}
private void identifyCluster(int aRow, int aCol, int aCount) {
grid[aRow][aCol] = aCount;
if ( aRow < grid.length - 1 && grid[aRow + 1][aCol] == CLUSTERED ) {
identifyCluster(aRow + 1, aCol, aCount);
}
if ( aCol < grid[0].length - 1 && grid[aRow][aCol + 1] == CLUSTERED ) {
identifyCluster(aRow, aCol + 1, aCount);
}
if ( aCol > 0 && grid[aRow][aCol - 1] == CLUSTERED ) {
identifyCluster(aRow, aCol - 1, aCount);
}
if ( aRow > 0 && grid[aRow - 1][aCol] == CLUSTERED ) {
identifyCluster(aRow - 1, aCol, aCount);
}
}
 
private int countClusters() {
clusterCount = 0;
for ( int row = 0; row < grid.length; row++ ) {
for ( int col = 0; col < grid.length; col++ ) {
if ( grid[row][col] == CLUSTERED ) {
clusterCount += 1;
identifyCluster(row, col, clusterCount);
}
}
}
return clusterCount;
}
private void createGrid(int aGridSize, double aProbability) {
grid = new int[aGridSize][aGridSize];
for ( int row = 0; row < aGridSize; row++ ) {
for ( int col = 0; col < aGridSize; col++ ) {
if ( random.nextDouble(1.0) < aProbability ) {
grid[row][col] = CLUSTERED;
}
}
}
}
private int[][] grid;
private int clusterCount;
private static ThreadLocalRandom random = ThreadLocalRandom.current();
 
private static final int CLUSTERED = -1;
private static final String GRID_CHARACTERS = ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}
</syntaxhighlight>
{{ out }}
<pre>
This 15 by 15 grid contains 21 clusters:
A . . B B B . . C . D D . E .
. . F . B . G . C . . . E E E
. F F . B . . . C C . H . E .
F F . B B B . I . C C . . E .
. . . . . . . I . . . J . . K
. L . . M M . I . . N . . . K
O . . O . M . I I . . . . K K
O O O O O . I I . K K . . . K
. O . O . P . I . K . K K K K
. . Q . . . I I . K K K K K K
Q Q Q . . I I I I . . . . . K
Q . Q Q . . I . . . . R R . .
. . Q . I I I . . . . R . R R
. . Q . . I I . . . . R R R .
S S . T . . I I I . R R R . U
 
p = 0.5, iterations = 5
n = 10, simulation K = 0.094000
n = 100, simulation K = 0.070420
n = 1000, simulation K = 0.066056
n = 10000, simulation K = 0.065780
</pre>
 
=={{header|Julia}}==
871

edits