public class Percolation
extends java.lang.Object
Requirement: Class must be in unionfind package and exercises.* and java.lang.* must be imported.
Javadoc: javadoc -author -version -private -classpath .;algs4_exercises.jar; -d .\javadoc Percolation.java
Compilation: javac -cp .;algs4_exercises.jar; Percolation.java
Execution 1: java -cp ../;.;algs4_exercises.jar; unionfind.Percolation <input5.txt
Execution 2: java -cp ../;.;algs4_exercises.jar; unionfind.Percolation <input10.txt
Execution 3: java -cp ../;.;algs4_exercises.jar; unionfind.Percolation <jerry47.txt
Execution 4: java -cp ../;.;algs4_exercises.jar; unionfind.Percolation <sedgewick60.txt
Execution 5: java -cp ../;.;algs4_exercises.jar; unionfind.Percolation <wayne98.txt
Summary: The Percolation Program reads a data file, into a 2D array. The first number in the file is the dimensions of the 2D array. The dimension is passed into the percolation constructor. In order to workaround accepting input with a col and row starting at (1,1), a 1 is added to the dimension, to produce an extra row and column, with nothing in the 0 row or 0 column. The printBlockedGrid() method is called to set the 2-D coordinates to BLOCKED and draw them. The open() method then reads the row, col combination from the input file. For each set of coordinates read, they must be assigned to instance members this.col and this.row and the this.status variable (grid[col][row] must be assigned the value BLOCKED before calling the nodeChangerRowTracker() method. The open() method (should use Weighted API) flips the row coordinates to map them to the StdDraw coordinates. For example, to map the top left coords. of StdDraw to (1,1) file coords, the input row has to be flipped, to map the StdDraw coord map (0, 0) to the bottom left corner, instead of the top. Since the 0 column is not used, the column values must be shifted to the right. This requires an extra column at the end of the grid to shift the last column over one.
Methods:Files: The first element in the file is the dimension, the other numbers are rows and cols in sequence.
Modifier and Type | Field and Description |
---|---|
private int |
BLOCKED
The lowest status that can be entered into an array index (Blocked - this.squaredDimensionHigh).
|
private int |
col |
private int |
count
Tracks the number of inputs read from the external file, it is incremented by 2 because the coordinates come in pairs (col, row).
|
private int |
dimensionHigh
The dimension of the N by N grid, +1 to get rid of 0 offset
|
private static int |
dimensionLow
The lowest index of the N by N grid, starts at 0
|
private double |
doubleThreshold |
private boolean |
endOfRowReached
Flag that is set to true when the end of a row has been reached.
|
private int |
FILLED
The highest status that can be entered into an array index (Filled - this.squaredDimensionHigh+2).
|
private int |
filledRowCounter
Keeps track of the total number of Filled squares.
|
private int |
fillRow
Keeps track of the number of squares Filled in the row (starting at 1).
|
private int[][] |
grid
The N by N grid array.
|
private static int[] |
inputArray
The linear input array.
|
private int |
OPENED
The middle status that can be entered into an array index (Opened - this.squaredDimensionHigh+1).
|
private int |
openedSquareCounter
The total number of Opened squares
|
private int |
openNodeSearchCol
The starting value (1) used to search for other opened squares in the same row, when an opened square is found.
|
private boolean |
percolates
Boolean value that returns true if the grid perculates (Filled value reaches the bottom of the grid).
|
private exercises.WeightedQuickUnionUF |
qf |
private int |
row |
private int |
squaredDimensionHigh
The total number of squares in the grid.
|
private int |
status
The array index value of the grid, where squaredDimensionHigh = Blocked, squaredDimensionHigh+1= Opened, squaredDimensionHigh+2 = Filled.
|
private static int |
statusHigh
The highest status that can be entered into an array index (Filled - this.squaredDimensionHigh+2).
|
private static int |
statusLow
The highest status that can be entered into an array index (Blocked - this.squaredDimensionHigh).
|
private int |
totalFilledSquares
The total number of Filled squares.
|
Constructor and Description |
---|
Percolation(int dimensionHigh)
Percolation constructor intializes all the grid values based on the dimensions.
|
Modifier and Type | Method and Description |
---|---|
private void |
checkBound(int n) |
private void |
checkBounds(int row,
int col) |
private void |
checkPerculation()
Checks the entire grid, starting at col 1(opentNodeSearchCtr), to determine if the grid perculates (filled square has reached the bottom of the grid).
|
private int |
convertGridCoordToLinearNode(int col,
int row)
Method maps the random coordinates from the createNode method to the Linear Array in UF
|
private void |
convertLinearArrayNodeToGridCoord(int index)
Converts a linear array index to an N by N grid coordinate and stores the row and col instance.
|
private void |
createNode()
Creates random linear index from the dimensionLow and the squaredDimensionHigh instance variables.
|
private void |
drawSquare(int col,
int row,
int status)
Draws the background color of the squares, based on its status
|
private void |
drawText(java.lang.String text) |
private void |
flipBlockedNodeToOpened(int col,
int row)
Method Flips Blocked Nodes to Opened, if surrounding squares are Filled.
|
private void |
flipOpenNodeToFilled(int col,
int row)
Method changes the value in the square associated with the (col, row) coordinate in the N by N grid, from Opened to Filled.
|
private void |
flipSurroundingOpenSquaresToFilled(int col,
int row)
Method checks surrounding squares and changes colors as necessary
Method is recursively called to flip squares from Opened (White) to Filled (Green) as necessary.
|
private int |
getRandomNum(int low,
int high)
Returns a random integer uniformly in [a, b).
|
private boolean |
isBlockedNodeReadyToBeOpened(int col,
int row)
Method checks if target square is Blocked, and instance var status is set to Opened.
|
boolean |
isFilled(int row,
int col) |
boolean |
isOpen(int row,
int col) |
private boolean |
isOpenNodeReadyToBeFilled(int col,
int row)
Method checks if target node is Opened and instance var status is set to Filled.
|
private boolean |
isSurroundingSquareFilled(int col,
int row)
Method checks to the Left, Above, Right, and Below the current sqaure, for filled squares.
|
static void |
main(java.lang.String[] args) |
private void |
nodeChangerRowTracker()
Method changes the colors from Blocked to Opened, and from Opened to Filled.
|
int |
numberOfOpenSites() |
void |
open(int row,
int col)
Sets the square at (col, row) to Opened, and flips the row , so coordinates (0,0) is at the top of the coordinate system, instead of at lower left.
|
private void |
openSquare(int row,
int col)
Method changes the N by N square coordinates(row, col) from blocked to opened
|
boolean |
percolates()
Determines if the grid percolates (Filled square reaches the bottom grid).
|
private void |
printBlockedGrid(int source)
Prints the grid with initial Blocked values
|
private void |
searchForOpenedNodeByRow(int col,
int row)
When an opened node is found in the grid, the method searches the row of the Opened square of other Opened squares, to be Filled (starting at col 1, to take care of the 1 col offset).
|
private java.lang.String |
threshold() |
private final int dimensionHigh
private static final int dimensionLow
private exercises.WeightedQuickUnionUF qf
private int squaredDimensionHigh
private static int statusHigh
private static int statusLow
private int status
private int[][] grid
private int row
private int col
private int count
private static int[] inputArray
private int fillRow
private int filledRowCounter
private int openedSquareCounter
private int totalFilledSquares
private int openNodeSearchCol
private double doubleThreshold
private boolean percolates
private boolean endOfRowReached
private int BLOCKED
private int OPENED
private int FILLED
public Percolation(int dimensionHigh)
dimensionHigh
- the integer used to establish the dimensions of the N by N grid.private void createNode()
private void searchForOpenedNodeByRow(int col, int row)
private int convertGridCoordToLinearNode(int col, int row)
private void convertLinearArrayNodeToGridCoord(int index)
index
- The linear array index to be created to an N by N grid coordinate.private void openSquare(int row, int col)
public void open(int row, int col)
row
- the row coordinate in the N by N grid.col
- the col coordinate in the N by N grid.private int getRandomNum(int low, int high)
low
- excluded integer value at the low end of the range.high
- included integer value at the high end of the range.private void printBlockedGrid(int source)
private void drawSquare(int col, int row, int status)
col
- the col of the N by N grid.row
- the row of the N by N grid.status
- The grid coordinate value, which indicates the status of the grid[Blocked , Opened , and Filled squares]private void flipOpenNodeToFilled(int col, int row)
col
- the col of the N by N grid.row
- the row of the N by N grid.private boolean isSurroundingSquareFilled(int col, int row)
private void checkBounds(int row, int col)
private void checkBound(int n)
public boolean isOpen(int row, int col)
public boolean isFilled(int row, int col)
public boolean percolates()
private void nodeChangerRowTracker()
private boolean isOpenNodeReadyToBeFilled(int col, int row)
private boolean isBlockedNodeReadyToBeOpened(int col, int row)
private void flipBlockedNodeToOpened(int col, int row)
public int numberOfOpenSites()
private void flipSurroundingOpenSquaresToFilled(int col, int row)
private java.lang.String threshold()
private void drawText(java.lang.String text)
private void checkPerculation()
public static void main(java.lang.String[] args)