Today, I began to learn the open coursera algorithmic class. I feel that the difficulty of the first week's homework is not lower than 23333.
Operational requirements: http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
The main content is to use weighted QuickUnion method to calculate the threshold of Monte Carlo model. Several modifications have been made in the middle, adding final attributes to some variables in the class for initialization according to warning
Some computations (such as mean and standard deviation) are stored in the constructor to reduce method calls, increase computational speed and solve the Timing problem.
Test13 1415 is only about isFull because the last layer of the table will be connected through the bottom virtual nodes, which leads to the problem of isFull method in the last layer. All the methods on the Internet are to build a new Weighted Quick Union UF which does not include the bottom nodes. I don't think it's very good. Then think about the solution. Final Achievement 95
Percolation.java
import edu.princeton.cs.algs4.WeightedQuickUnionUF; public class Percolation { private boolean[][] table; private final WeightedQuickUnionUF wq; private final int nval; private int op; public Percolation(int n) { if (n <= 0) throw new IllegalArgumentException(); //Throw an exception when n is less than or equal to zero nval = n; wq = new WeightedQuickUnionUF(n * n + 2); //Define a weighted QuickUnion and add a virtual node at the top and bottom levels, respectively. table = new boolean[n][n]; for (int i = 0; i <= n - 1; i++) { for (int j = 0; j < n - 1; j++) { table[i][j] = false; //Initialize the nxn table } } op = 0; //Zero the counting value of a lattice with open state } private int map2Dto1D(int row, int col) { return row * nval + col; //Expansion of row and column coordinates into one-dimensional sequence values } public void open(int row, int col) { if (row < 1 || row > nval || col < 1 || col > nval) throw new IllegalArgumentException(); int trow = row - 1; //The actual index starts at 0 int tcol = col - 1; int in = map2Dto1D(trow, tcol); //Save the open grid location first, and then use it many times. if (table[trow][tcol] == false) { op++; //The number of lattices with open state plus one table[trow][tcol] = true; //true is open if (row == 1) { wq.union(tcol, nval * nval); //The first line is connected to the top virtual node } if (row == nval) { //The last line is connected to the bottom virtual node wq.union(map2Dto1D(trow, tcol), nval * nval + 1); } if (row > 1 && table[trow - 1][tcol] == true) { //Connect to the surrounding nodes wq.union(map2Dto1D(trow - 1, tcol), in); } if (col > 1 && table[trow][tcol - 1] == true) { wq.union(map2Dto1D(trow, tcol - 1), in); } if (col < nval && table[trow][tcol + 1] == true) { wq.union(map2Dto1D(trow, tcol + 1), in); } if (row < nval && table[trow + 1][tcol] == true) { wq.union(map2Dto1D(trow + 1, tcol), in); } } } public boolean isOpen(int row, int col) { if (row < 1 || row > nval || col < 1 || col > nval) throw new IllegalArgumentException(); return table[row - 1][col - 1]; } public boolean isFull(int row, int col) { if (row < 1 || row > nval || col < 1 || col > nval) throw new IllegalArgumentException(); return wq.connected(map2Dto1D(row - 1, col - 1), nval * nval); } public int numberOfOpenSites() { return op; } public boolean percolates() { //Judging whether to infiltrate return wq.connected(nval * nval, nval * nval + 1); } }
PercolationStats.java
import edu.princeton.cs.algs4.StdIn; import edu.princeton.cs.algs4.StdRandom; import edu.princeton.cs.algs4.StdStats; public class PercolationStats { private final double threshold[]; private final int trials; private final double shu = 1.96; private double meanVal; private double stddevVal; public PercolationStats(int n, int trials) { if (n <= 0 || trials <= 0) throw new IllegalArgumentException(); this.trials = trials; threshold = new double[trials]; for (int i = 1; i <= trials; i++) { //Cycle of test times Percolation per = new Percolation(n); while (per.percolates() == false) { //Cycling until osmosis int r = StdRandom.uniform(n) + 1; //Generating Random Coordinates int c = StdRandom.uniform(n) + 1; per.open(r, c); //open Selected Lattice } threshold[i - 1] = (double) per.numberOfOpenSites() / (n * n); //Save Threshold after Cycle Ends } meanVal = StdStats.mean(threshold); stddevVal = StdStats.stddev(threshold); } public double mean() { //Calculating the Mean Value of the Threshold of Each Test return meanVal; } public double stddev() { //Calculating standard deviation return stddevVal; } public double confidenceLo() { return meanVal - shu * stddevVal / Math.sqrt(trials); } public double confidenceHi() { return meanVal + shu * stddevVal / Math.sqrt(trials); } public static void main(String[] args) { //main program int n = StdIn.readInt(); //Enter an n and test times T int T = StdIn.readInt(); PercolationStats m = new PercolationStats(n, T); System.out.println(m.mean()); System.out.println(m.stddev()); System.out.println(m.confidenceLo()); System.out.println(m.confidenceHi()); } }