My goal is to compute an approximate value of the percolation threshold of a path from top to bottom in a matrix containing black and white cells. To do this, I randomly blacken cells and, after each step, I detect if there is a path made of black cells going from any top cell (first row) to any cell at the bottom (last row) of the matrix (without diagonal jumps). If so, the percolation will be reached. The percolation threshold is the proportion of black cells that it is probabilistically necessary to blacken to reach the percolation. This threshold is the same for any matrix that is not too small.
To represent a square matrix M of size N, I use a one-dimensional array T of size NxN : the cell in the row i and column j in M (with i and j between 0 and N-1) corresponds to the element of index i.N + j in T.
My problem is that my percolation threshold lies around 0.7, while it should be 0.6 normally. I tried to find the errors in my code but everything seems to be fine and I cannot find my mistake. Here is the code I created :
public class Percolation {
public static final int size = 10;
public static final int length = size * size;
public static boolean[] grid = new boolean[length];
public static void init() {
for (int i = 0; i < length; i++) {
grid[i] = false;
}
}
public static void print() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int index = i * size + j;
if (grid[index]) {
System.out.print("*");
} else {
System.out.print("-");
}
}
System.out.println();
}
}
public static int randomShadow() {
int index;
do {
index = (int) (Math.random() * length);
} while (grid[index]);
grid[index] = true;
return index;
}
public static boolean isNaivePercolation(int n) {
boolean[] seen = new boolean[length];
boolean pathToTop = detectPath(seen, n, true);
boolean pathToBottom = detectPath(seen, n, false);
// Percolation occurs if there's a path to both top and bottom rows
return pathToTop && pathToBottom;
}
private static boolean detectPath(boolean[] seen, int n, boolean up) {
if (n < 0 || n >= length || seen[n] || !grid[n]) {
return false; // Out of bounds, already visited cell, or white cell
}
if ((up && n < size) || (!up && n >= length - size)) {
return true; // Reached top or bottom row
}
seen[n] = true; // Mark cell as visited
int row = n / size;
int col = n % size;
// Check neighboring cells (up, down, left, right)
return detectPath(seen, n - size, up) ||
detectPath(seen, n + size, up) ||
(col > 0 && detectPath(seen, n - 1, up)) ||
(col < size - 1 && detectPath(seen, n + 1, up));
}
public static boolean isPercolation(int n) {
return isNaivePercolation(n);
}
public static double percolation() {
init(); // Initialize the matrix
int blackenedCells = 0;
int index;
boolean percolationDetected = false;
while (!percolationDetected) {
index = randomShadow(); // Blacken a random white cell
blackenedCells++;
percolationDetected = isPercolation(index);
}
print();
return (double) blackenedCells / length; // Return the proportion of blackened cells
}
public static void main(String[] args) {
init();
System.out.println("Matrix after initializing:");
print();
System.out.println();
int blackenedIndex = randomShadow();
System.out.println("Matrix after blackening a random cell:");
print();
System.out.println("Index of blackened cell: " + blackenedIndex);
System.out.println();
System.out.println("Testing percolation:");
double percolationThreshold = percolation();
System.out.println("Percolation threshold: " + percolationThreshold);
}
}
I have tried to debug multiple times, do examples by hand, copilot.
Perseus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.