The percolation threshold of my function creating a path from bottom to top is 0.1 too high

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.

New contributor

Perseus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật