Skip to content

Commit

Permalink
Change commenting style.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakk.Wong@gmail.com authored and Jakk.Wong@gmail.com committed Sep 4, 2012
1 parent c0fd1d9 commit 8f57108
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
39 changes: 16 additions & 23 deletions HW1/Percolation.java
Expand Up @@ -7,25 +7,24 @@ public class Percolation {
private WeightedQuickUnionUF ufTwo;

public Percolation(int N) {
// First site in N*N matrix starts at index 0
// 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 opened/closed sites using 1's (opened) and 0's (closed)
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 opened/closed sites using 1's (opened) and 0's (closed).

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
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
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 */

// Opens a given site at i, j coordinates.
public void open(int i, int j) {
validateIndices(i, j);
int newSite1D = xyTo1D(i, j);
Expand All @@ -36,16 +35,14 @@ public void open(int i, int j) {
connectNeighborOf(newSite1D, i, j-1); // left
}

/* Converts i, j coordinates to 1D index */

// 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 */

// 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)) {
Expand All @@ -56,17 +53,15 @@ private void connectNeighborOf(int site, int i, int j) {
}
}

/* Checks whether a site at i, j is opened */

// 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 */

// 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);
Expand All @@ -76,17 +71,15 @@ public boolean isFull(int i, int j) {
return false;
}

/* Checks if any cell in bottom row is connected to another cell in top row through virtual sites */

// 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 */

// 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");
Expand Down
24 changes: 11 additions & 13 deletions HW1/PercolationStats.java
Expand Up @@ -2,34 +2,34 @@ public class PercolationStats {
private Percolation pc;
private double[] thresholdList;

/* N is N in N*N matrix. T is number of experiments ran */

// 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
thresholdList = new double[T]; // Array used to keep track of thresholds generated in all T experiments.

// Performs Monte Carlo Simulation.
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
thresholdList[i] = threshold; // Each threshold produced from an experiment is added to the array.
}
}

// Main method used in the simulations.
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 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
// Threshold is calculated as (# of opened sites required to percolate)/# of sites in matrix.
return count/(N*N);
}

Expand All @@ -44,13 +44,12 @@ public double stddev() {
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);

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
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();
Expand All @@ -59,5 +58,4 @@ public static void main(String[] args) {
StdOut.println("95% confidence interval = " + ps.confidenceInterval(mean, stddev, T));
}


}

0 comments on commit 8f57108

Please sign in to comment.