(adapted from Sedgewick & Wayne, Stanford)

We will model the system by an N-by-N grid of *sites*. Each
site is either *blocked* or *open*. Open sites can be
*full* or *empty*; blocked sites are always empty.
Initially all sites are empty. The process starts by filling the open
sites in the top row of cells. We want to model the fluid as it moves
through the grid. We say that a system *percolates* if, at the
end of the movement, there is a *full* site on the bottom row. If
all bottom cells are empty, the system does not percolate.

Let's talk about modeling the fluid moving through the grid. The
first version that we'll consider is *vertical percolation*.
Imagine the fluid moving straight down (not sideways, and not up). So
an open cell gets filled if it can be connected to a full cell in the
top row via a chain of neighboring up cells. You can model this
process as a row-by-row traversal of the grid, where each open cell on
the row gets filled if the cell immediately above it is full.

**General percolation** With general percolation, we allow
*any* path starting at the top and ending at the bottom (not just
a vertical one). From any cell, the water can flow down, left, right,
but also *up*. To compute all such paths, a row-by-row traversal like
above is not enough; you will need to use recursion.

So, given a grid with open and blocked cells, you want to compute whether it percolates, either with vertical or with general percolation. You also want to visualize the grid after you let the fluid go through. This is only one part.

The other part is to help researchers study when a system
percolates and when it does not. Researchers are interested in the
following question: if the sites are independently set to be open with
probability *p*, and therefore blocked with probability
*1-p*, what is the probability that the system percolates?

For this, you'll write code to set up a set of random grids (as
specified above), compute whether they percolate, keep track of how
many of them percolate, and display the probability. The probability
*p*, the size of the grid N, and the number of trials should be
specified by the user on the command line.

Class Visualize will generate T random grids of size N-by-N with vacancy probability p, and will visualize each grid (after you let the fluid go through). For example,

java Visualize 20 .8 1 vwill generate 1 grid of size 20-by-20 with vacancy probability .8, will simulate vertical percolation and will render the resulting grid. This is the tool used to view the percolation process.

Class Estimate will generate T random grids of size N-by-N with vacancy probability p, and will estimate the percolation probability. For example,

java Estimate 20 .8 100 vwill generate 100 grid of size 20-by-20 with vacancy probability .8, will simulate vertical percolation and will print the probability that a grid percolates. This is the tool used to compute the percolation probability.

In addition to these two classes, you will have a class Percolation to handle percolation.

Class Percolation should have the following:

- a method
`boolean[][] readOpenFromFile(String filename)`that reads an open grid from a file and creates and returns the grid. - a method
`void show(boolean[][] grid, boolean which )`that renders a grid. The second parameter is used to determine which values of the grid to draw: if`which is true`, the`true`values of the grid are drawn in blue. if`which is false`, the`false`values of the grid are drawn in black. Note that two different calls to show() with appropriate parameters will render the grid with blocked cells shown in black, and filled cells shown in blue. - a method
`boolean[][] flowVertical(boolean[][] open)`that simulates vertical percolation. This method should take as parameter an open grid, and return a boolean[][] representing the filled grid after the fluid moves through. - a similar method for general percolation.
- a method
`boolean percolates(boolean[][] open, char type)`that takes an open grid, calls flow on it (vertical or general, as specifed by the type paramter), and then checks whether any cell on the bottom row is full. If yes, it returns true, otherwise it returns false. - a main method that takes as argument a filename (storing a
boolean open grid) and another argument representing vertical or
general percolation; the method should print the result of calling
flow, render it (after the fluid moves) and print whether it
percolates or not. We'll use this method to test your program.
The format of a test file should be:
5 5 0 1 1 0 1 0 0 1 1 1 1 1 0 1 1 1 0 0 0 1 0 1 1 1 1

java Percolates v testGrid.txt 5 5 0 1 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 percolates

with the filled grid renderd.

File f = new File(fileName); Scanner sc = null; try { sc = new Scanner(f); } catch (FileNotFoundException e) { System.err.println("File not found!"); System.exit(0); } //here is how you would read a file of integers while (sc.hasNextInt()) { int nextOne = sc.nextInt(); }Enjoy!

Last modified: Fri Nov 13 18:00:53 EST 2009