Permalink
Browse files

Init git and add hw1

  • Loading branch information...
Jakk.Wong@gmail.com Jakk.Wong@gmail.com
Jakk.Wong@gmail.com authored and Jakk.Wong@gmail.com committed Aug 31, 2012
0 parents commit db703853ba44c378ee02b7448bbe5949988b24a7
Showing with 162 additions and 0 deletions.
  1. +99 −0 HW1/Percolation.java
  2. +63 −0 HW1/PercolationStats.java
@@ -0,0 +1,99 @@
+public class Percolation {
+ private int gridSize;
+ private int virtualTop;
+ private int virtualBottom;
+ private int[] openSites;
+ private WeightedQuickUnionUF ufOne;
+ private WeightedQuickUnionUF ufTwo;
+
+ public Percolation(int N) {
+ // First site in N*N matrix starts at index 0
+
+ gridSize = N;
+ virtualTop = N*N; // For 4x4 matrix, the virtualTop will be at index 16
+ virtualBottom = (N*N)+1; // For 4x4 matrix, the virtualBottom will be at index 17
+ openSites = new int[N*N]; // Matrix used to keep track of open sites using 0's (closed) and 1's (opened)
+
+ for (int i = 0; i < (N*N); i++) {
+ openSites[i] = 0; // all sites are blocked initially
+ }
+
+ ufOne = new WeightedQuickUnionUF((N*N)+1); // Obj used in checking for isFull - no virtual bottom
+ ufTwo = new WeightedQuickUnionUF((N*N)+2); // Obj used in checking for percolation
+
+ for (int i = 0; i < N; i++) {
+ ufOne.union(i, virtualTop); // establish virtual top for uf object 1
+ ufTwo.union(i, virtualTop); // establish virtual top for uf object 2
+ ufTwo.union((N*N)-(i+1), virtualBottom); // establish virtual bottom for uf object 2
+ }
+ }
+
+ /* Open a given site at i, j coordinates */
+
+ public void open(int i, int j) {
+ validateIndices(i, j);
+ int newSite1D = xyTo1D(i, j);
+ openSites[newSite1D] = 1;
+ connectNeighborOf(newSite1D, i-1, j); // top
+ connectNeighborOf(newSite1D, i, j+1); // right
+ connectNeighborOf(newSite1D, i+1, j); // bottom
+ connectNeighborOf(newSite1D, i, j-1); // left
+ }
+
+ /* Converts i, j coordinates to 1D index */
+
+ private int xyTo1D(int i, int j) {
+ int a = i-1;
+ int b = j-1;
+ return (gridSize*a)+b;
+ }
+
+ /* Connects a site to its neighbor if it is opened */
+
+ private void connectNeighborOf(int site, int i, int j) {
+ if ((i > 0 && i <= gridSize) && (j > 0 && j <= gridSize)) {
+ if (isOpen(i, j)) {
+ int neighborOfSite = xyTo1D(i, j);
+ ufOne.union(site, neighborOfSite);
+ ufTwo.union(site, neighborOfSite);
+ }
+ }
+ }
+
+ /* Checks whether a site at i, j is opened */
+
+ public boolean isOpen(int i, int j) {
+ // an open site is not blocked
+ validateIndices(i, j);
+ int siteToCheck = xyTo1D(i, j);
+ return openSites[siteToCheck] == 1;
+ }
+
+ /* Checks whether a site at i, j is opened and connected to another site in top row */
+
+ public boolean isFull(int i, int j) {
+ validateIndices(i, j);
+ int siteToCheck = xyTo1D(i, j);
+ if (isOpen(i, j) && ufOne.connected(siteToCheck, virtualTop)) {
+ return true;
+ }
+ return false;
+ }
+
+ /* Checks if any cell in bottom row is connected to another cell in top row through virtual sites */
+
+ public boolean percolates() {
+ if (gridSize == 1 && !isOpen(1, 1)) {
+ return false;
+ }
+ return ufTwo.connected(virtualTop, virtualBottom);
+ }
+
+ /* Throws exception when given i and j coordinates are out of bounds */
+
+ private void validateIndices(int i, int j) {
+ if (i <= 0 || i > gridSize) throw new IndexOutOfBoundsException("row index i out of bounds");
+ if (j <= 0 || j > gridSize) throw new IndexOutOfBoundsException("row index j out of bounds");
+ }
+
+}
@@ -0,0 +1,63 @@
+public class PercolationStats {
+ private Percolation pc;
+ private double[] thresholdList;
+
+ /* N is N in N*N matrix. T is number of experiments ran */
+
+ public PercolationStats(int N, int T) {
+ if (N <= 0 || T <= 0) {
+ throw new IllegalArgumentException("N and T cannot be less than or equal to 0.");
+ }
+
+ thresholdList = new double[T]; // Array used to keep track of thresholds generated in all T experiments
+
+ for (int i = 0; i < T; i++) {
+ double threshold = calculateThreshold(N);
+ thresholdList[i] = threshold; // Each threshold produced from an experiment is added to the array
+ }
+ }
+
+ private double calculateThreshold(int N) {
+ pc = new Percolation(N);
+ double count = 0;
+ while (!pc.percolates()) {
+ int i = StdRandom.uniform(1, N+1); // generates a random number from 1 to N+1
+ int j = StdRandom.uniform(1, N+1);
+ if (!pc.isOpen(i, j)) {
+ pc.open(i, j);
+ count += 1;
+ }
+
+ }
+ // threshold is calculated as (# of opened sites required to percolate)/# of sites in matrix
+ return count/(N*N);
+ }
+
+ public double mean() {
+ return StdStats.mean(thresholdList);
+ }
+
+ public double stddev() {
+ return StdStats.stddev(thresholdList);
+ }
+
+ private String confidenceInterval(double mean, double stddev, int T) {
+ double lessThan = mean - ((1.96*stddev)/Math.sqrt(T));
+ double greaterThan = mean + ((1.96*stddev)/Math.sqrt(T));
+ return String.format("%f, %f", lessThan, greaterThan);
+
+ }
+
+ public static void main(String[] args) {
+ int N = Integer.parseInt(args[0]); // N is provided in command line as argument 1
+ int T = Integer.parseInt(args[1]); // T is provided in command line as argument 2
+ PercolationStats ps = new PercolationStats(N, T);
+ double mean = ps.mean();
+ double stddev = ps.stddev();
+ StdOut.println("mean = " + mean);
+ StdOut.println("stddev = " + stddev);
+ StdOut.println("95% confidence interval = " + ps.confidenceInterval(mean, stddev, T));
+ }
+
+
+}

0 comments on commit db70385

Please sign in to comment.