Skip to content
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

my First assignment on algorithm percolation1 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 74 additions & 12 deletions Percolation.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,76 @@

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?

public static void main(String[] args) // test client (optional)
}
import princeton.cs.algs4.WeightedQuickUnionUF;


// given an n-by-n matrix of open sites, return an n-by-n matrix
// of sites reachable from the top
public boolean isOpen(int row, int col){
//public static boolean[n][n] flow(boolean[][] isOpen) {
string n =is0pen.length();
boolean[][] isFull = new boolean[n][n];
for (int j = 0; j < n; j++) {
flow(isOpen, isFull, 0, j);
}
return isFull;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not isOpen is supposed to do, it should just check the site is open.

}
// determine set of full sites using depth first search
public static void flow(boolean[][] isOpen, boolean[][] isFull, int i, int j) {
int n = isOpen.length;

// base cases
if (i < 0 || i >= n) return; // invalid row
if (j < 0 || j >= n) return; // invalid column
if (!isOpen[i][j]) return; // not an open site
if (isFull[i][j]) return; // already marked as full

// mark i-j as full
isFull[i][j] = true;

flow(isOpen, isFull, i+1, j); // down
flow(isOpen, isFull, i, j+1); // right
flow(isOpen, isFull, i, j-1); // left
flow(isOpen, isFull, i-1, j); // up
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method doesn't determine full site, but just mark as full any site that is open, and this is not the definition of a full site, which is: A full site is an open site that can be connected to an open site in the top row via a chain of neighboring (left, right, up, down) open sites.



// does the system percolate?
public static boolean percolates(boolean[][] isOpen) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

percolates is not supposed to take any parameter.

int n = isOpen.length;
boolean[][] isFull = flow(isOpen);
for (int j = 0; j < n; j++) {
if (isFull[n-1][j]) return true;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the isFull was actually returning full sites, then your method would be working. However, it would have not respected performance requirements, as this check is O(n) not constant and flow is not constant also.

return false;
}

// draw the n-by-n boolean matrix to standard draw
public static void show(boolean[][] a, boolean which) {
int n = a.length;
StdDraw.setXscale(-1, n);
StdDraw.setYscale(-1, n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j] == which)
StdDraw.filledSquare(j, n-i-1, 0.5);
}

// return a random n-by-n boolean matrix, where each entry is
// true with probability p
public static boolean[][] random(int n, double p) {
boolean[][] a = new boolean[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = StdRandom.bernoulli(p);
return a;
}

// test client
public static void main(String[] args) {
boolean[][] isOpen = StdArrayIO.readBoolean2D();
StdArrayIO.print(flow(isOpen));
StdOut.println(percolates(isOpen));
}
}