hw/hw2/hw2/Percolation.java
private void Connect(int p, int q) {
        int curr = matrix2vector(p, q);
        List<Integer> neighbors = new ArrayList<>();

        // first row in the grid, connect to the head
        if (p == 0) WQU.union(head, curr);

        // bottom row in the grid, connect to the tail
        if (p == grid.length - 1) WQU.union(tail, curr);

        // Check current site's neighbours is opened or not
        if (p != 0 && isOpen(p - 1, q)) neighbors.add(matrix2vector(p - 1, q));
        if (p != (grid.length - 1) && isOpen(p + 1, q)) neighbors.add(matrix2vector(p + 1, q));
        if (q != 0 && isOpen(p, q - 1)) neighbors.add(matrix2vector(p, q - 1));
        if (q != (grid.length - 1) && isOpen(p, q + 1)) neighbors.add(matrix2vector(p, q + 1));

        // Union the opened sites
        for (int i : neighbors) {
            WQU.union(curr, i);
        }


    }
/**
     * private helper method
     *
     * connect the two components
     */
private helper method
connect the two components
private helper method connect the two components
private void Connect(int p, int q) {
        int curr = matrix2vector(p, q);
        List<Integer> neighbors = new ArrayList<>();
        if (p == 0) WQU.union(head, curr);
        if (p == grid.length - 1) WQU.union(tail, curr);
        if (p != 0 && isOpen(p - 1, q)) neighbors.add(matrix2vector(p - 1, q));
        if (p != (grid.length - 1) && isOpen(p + 1, q)) neighbors.add(matrix2vector(p + 1, q));
        if (q != 0 && isOpen(p, q - 1)) neighbors.add(matrix2vector(p, q - 1));
        if (q != (grid.length - 1) && isOpen(p, q + 1)) neighbors.add(matrix2vector(p, q + 1));
        for (int i : neighbors) {
            WQU.union(curr, i);
        }
    }
private void Connect ( int p , int q ) { int curr = matrix2vector ( p , q ) ; List < Integer > neighbors = new ArrayList < > ( ) ; if ( p == 0 ) WQU . union ( head , curr ) ; if ( p == grid . length - 1 ) WQU . union ( tail , curr ) ; if ( p != 0 && isOpen ( p - 1 , q ) ) neighbors . add ( matrix2vector ( p - 1 , q ) ) ; if ( p != ( grid . length - 1 ) && isOpen ( p + 1 , q ) ) neighbors . add ( matrix2vector ( p + 1 , q ) ) ; if ( q != 0 && isOpen ( p , q - 1 ) ) neighbors . add ( matrix2vector ( p , q - 1 ) ) ; if ( q != ( grid . length - 1 ) && isOpen ( p , q + 1 ) ) neighbors . add ( matrix2vector ( p , q + 1 ) ) ; for ( int i : neighbors ) { WQU . union ( curr , i ) ; } }
private helper method
 connect the two components
private helper method connect the two components
// first row in the grid, connect to the head // bottom row in the grid, connect to the tail // Check current site's neighbours is opened or not // Union the opened sites