-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abwas submitting percolation assignment #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,161 @@ | ||
import edu.princeton.cs.algs4.StdOut; | ||
import edu.princeton.cs.algs4.StdRandom; | ||
import edu.princeton.cs.algs4.StdStats; | ||
import edu.princeton.cs.algs4.WeightedQuickUnionUF; | ||
|
||
public class Percolation { | ||
public Percolation(int n) // create n-by-n grid, with all sites blocked | ||
public void open(int row, int col) // open site (row, col) if it is not open already | ||
public boolean isOpen(int row, int col) // is site (row, col) open? | ||
public boolean isFull(int row, int col) // is site (row, col) full? | ||
public int numberOfOpenSites() // number of open sites | ||
public boolean percolates() // does the system percolate? | ||
|
||
// gridLength is the length of the square grid. | ||
// There are gridLength^2 non-virtual sites in the grid | ||
private int gridLength; | ||
|
||
// two sites can percolate to one another if they are both open and connected: | ||
// isOpen monitors open/closed state of each site. | ||
private boolean[] isOpen; | ||
|
||
// percolation represents connectivity between sites. connected, open sites percolate to one another. | ||
private WeightedQuickUnionUF percolation; | ||
|
||
// quick union structure for tracking fullness without backwash. | ||
// similar to percolation above, but without bottom virtual site | ||
private WeightedQuickUnionUF fullness; | ||
|
||
// index of virtual site that is connected to entire top row, initializes to 0. | ||
private int virtualTopIndex; | ||
|
||
// index of virtual site that is connected to entire bottom row, initializes to (n^2)+1 | ||
private int virtualBottomIndex; | ||
|
||
// converts between two dimensional coordinate system and site array index. | ||
// throws exceptions on invalid bounds. valid indices are 1 : n^2 | ||
// a is the row; b is the column | ||
private int siteIndex(int a, int b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. naming your variables row and column instead of a and b will make your code much more readable. Please could you update this code to use row and column instead of a and b? |
||
{ | ||
checkBounds(a, b); | ||
int x = b; | ||
int y = a; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need these variables. Why don't you just name method parameters a and b to x and y? |
||
return (y - 1) * gridLength + (x); | ||
} | ||
|
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please javadoc method level comment should be formatted
|
||
* By convention, the indices a and b are integers between 1 and n, where (1, 1) is the upper-left site: | ||
* Throw a java.lang.IndexOutOfBoundsException if either a or b is outside this range. | ||
*/ | ||
private void checkBounds(int a, int b) | ||
{ | ||
if (a > gridLength || a < 1 ) | ||
{ | ||
throw new IndexOutOfBoundsException("row index a out of bounds"); | ||
} | ||
if (b > gridLength || b < 1) | ||
{ | ||
throw new IndexOutOfBoundsException("column index b out of bounds"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code could be better formatted. |
||
} | ||
|
||
// create n-by-n grid, with all sites blocked | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Javadoc single line comments should be formatted as: Please do update your method comments... |
||
public Percolation(int n) | ||
{ | ||
if (n < 1) { | ||
throw new IllegalArgumentException(); | ||
} | ||
gridLength = n; | ||
int arraySize = n * n + 2; | ||
isOpen = new boolean[arraySize]; | ||
|
||
virtualTopIndex = 0; | ||
virtualBottomIndex = (n * n) + 1; | ||
|
||
isOpen[virtualTopIndex] = true; /// open virtual top site | ||
isOpen[virtualBottomIndex] = true; /// open virtual bottom site | ||
|
||
percolation = new WeightedQuickUnionUF(arraySize); | ||
fullness = new WeightedQuickUnionUF(arraySize); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fullness doesn't contain bottom virtual site, then could be represented with arraySiye -1 elements. |
||
for (int b = 1; b <= n; b++) | ||
{ | ||
/// connect all top row sites to virtual top site | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Java comments ar two |
||
int a = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need this variable. |
||
int topSiteIndex = siteIndex(a, b); | ||
percolation.union(virtualTopIndex, topSiteIndex); | ||
fullness.union(virtualTopIndex, topSiteIndex); | ||
|
||
/// connect all bottom row sites to virtual bottom site | ||
a = n; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this variable |
||
int bottomSiteIndex = siteIndex(a, b); | ||
percolation.union(virtualBottomIndex, bottomSiteIndex); | ||
|
||
} | ||
}; | ||
|
||
// open site (row a, column b) if it is not already | ||
public void open(int a, int b) | ||
{ | ||
int siteIndex = siteIndex(a,b); | ||
if (!isOpen[siteIndex]) | ||
{ | ||
/// to open a site, change boolean value, and union with any adjacent open sites | ||
isOpen[siteIndex] = true; | ||
|
||
public static void main(String[] args) // test client (optional) | ||
// before connecting to a neighbor, first check that site is not on an edge, and is open | ||
if (b > 1 && isOpen(a, b - 1)) | ||
{ | ||
int indexToLeft = siteIndex(a, b - 1); | ||
percolation.union(siteIndex, indexToLeft); | ||
fullness.union(siteIndex, indexToLeft); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're repeating this pattern 4 times. You could instead create a "private void connect(int siteIndex, int row, int col)" method where you could first check row and column are valids and that the cell is open, before creating the connection... |
||
|
||
if (b < gridLength && isOpen(a, b + 1)) | ||
{ | ||
int indexToRight = siteIndex(a, b + 1); | ||
percolation.union(siteIndex, indexToRight); | ||
fullness.union(siteIndex,indexToRight); | ||
} | ||
|
||
if (a > 1 && isOpen(a - 1, b)) // site is not top edge | ||
{ | ||
int indexToTop = siteIndex(a - 1, b); | ||
percolation.union(siteIndex, indexToTop); | ||
fullness.union(siteIndex,indexToTop); | ||
} | ||
|
||
if (a < gridLength && isOpen(a + 1, b)) /// site is not on bottom edge | ||
{ | ||
int indexToBottom = siteIndex(a + 1, b); | ||
percolation.union(siteIndex, indexToBottom); | ||
fullness.union(siteIndex,indexToBottom); | ||
} | ||
} | ||
}; | ||
|
||
// is site (row a, column b) open? | ||
//// openness represented by boolean value in isOpen array | ||
public boolean isOpen(int a, int b) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You didn't check row and column are inside the prescribed range. |
||
int siteIndex = siteIndex(a, b); | ||
return isOpen[siteIndex]; | ||
} | ||
|
||
// is site (row a, column b) full? | ||
/// fullness represented by union with virtual top node | ||
public boolean isFull(int a, int b) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You didn't check row and column are inside the prescribed range. |
||
int siteIndex = siteIndex(a, b); | ||
//return (percolation.connected(virtualTopIndex,siteIndex) && isOpen[siteIndex]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please could you remove this comment, it's not needed at all... |
||
return (fullness.connected(virtualTopIndex,siteIndex) && isOpen[siteIndex]); | ||
} | ||
|
||
// does the system percolate? | ||
public boolean percolates() { | ||
if (gridLength > 1) { | ||
return percolation.connected(virtualTopIndex,virtualBottomIndex); | ||
} | ||
else { | ||
return isOpen[siteIndex(1,1)]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could have used the ternary operator here instead of the if else... |
||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,72 @@ | ||
import edu.princeton.cs.algs4.StdOut; | ||
import edu.princeton.cs.algs4.StdRandom; | ||
import edu.princeton.cs.algs4.StdStats; | ||
import edu.princeton.cs.algs4.WeightedQuickUnionUF; | ||
|
||
public class PercolationStats { | ||
public PercolationStats(int n, int trials) // perform trials independent experiments on an n-by-n grid | ||
public double mean() // sample mean of percolation threshold | ||
public double stddev() // sample standard deviation of percolation threshold | ||
public double confidenceLo() // low endpoint of 95% confidence interval | ||
public double confidenceHi() // high endpoint of 95% confidence interval | ||
|
||
public static void main(String[] args) // test client (described below) | ||
|
||
// holds each experiment's percolation threshold result | ||
private double[] thresholdResults; | ||
|
||
private int T; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non final static fields in java shouldn't be capitabilized. Please could you call this one trial instead? |
||
// perform T independent computational experiments on an n-by-n grid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a well formatted javadoc comment and please could you place it closer to PercolationStats |
||
|
||
public PercolationStats(int n, int T) | ||
{ | ||
///The constructor should throw a java.lang.IllegalArgumentException if either n <= 0 or T >= 0. | ||
if (n < 1 || T < 1) | ||
{ | ||
throw new IllegalArgumentException("both arguments n and T must be greater than 1"); | ||
} | ||
|
||
this.T = T; | ||
thresholdResults = new double[T]; | ||
for (int t = 0; t < T; t++) | ||
{ | ||
Percolation percolation = new Percolation(n); | ||
int openSites = 0; | ||
while (!percolation.percolates()) | ||
{ | ||
int a = StdRandom.uniform(1, n+1); | ||
int b = StdRandom.uniform(1, n+1); | ||
|
||
if (!percolation.isOpen(a, b)) | ||
{ | ||
percolation.open(a, b); | ||
openSites += 1; | ||
} | ||
} | ||
double threshold = (double)openSites/(double)(n * n); | ||
thresholdResults[t] = threshold; | ||
} | ||
} | ||
|
||
// sample mean of percolation threshold | ||
public double mean() | ||
{ | ||
return StdStats.mean(thresholdResults); | ||
} | ||
|
||
// sample standard deviation of percolation threshold | ||
public double stddev() | ||
{ | ||
return StdStats.stddev(thresholdResults); | ||
} | ||
|
||
// returns lower bound of the 95% confidence interval | ||
public double confidenceLo() | ||
{ | ||
return mean() - (1.96 * stddev() / Math.sqrt(T)); | ||
} | ||
|
||
// returns upper bound of the 95% confidence interval | ||
public double confidenceHi() | ||
{ | ||
return mean() + (1.96 * stddev() / Math.sqrt(T)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't implement this one... Please could you implement it?