Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Percolation{
- private int arrDim;
- private char[][] state;
- private MyWeightedQuickUnionUF qu;
- private int top = 0;
- private int bottom;
- private double count;
- // final long constructionTime;
- Percolation(int N){
- arrDim = N;
- qu = new MyWeightedQuickUnionUF((N*N)+2); // +2 is for the virtual top n bottom site
- state = new char[N][N];
- // System.out.print("\n state : " + state.length + " elements : ");
- for(int i = 0; i< N; i++){
- for(int j = 0; j < N ; j++){
- // 'b' = blocked
- // 'o' = open
- // 'f' = full
- state[i][j]='b';
- // System.out.print(" " + state[i][j]);
- }
- }
- bottom = N*N + 1; // (N*N + 2) - 1;
- for(int i = 1; i<= N; i++){
- qu.union(top, i); // creating virual connection of top row to Top site
- qu.union(bottom,bottom - i); // connecting bottom row sites to Bottom
- }
- // System.out.println("done constructing\n\n\n");
- // constructionTime = System.nanoTime();
- }
- public int convertTo1D(int row, int col){
- return ((row*arrDim + col) + 1); // the conversion is needed as union find has 1D array, and return value will give the index of that array
- // but, since the initial(0) and final(N*N+1) positions of the (N*N + 2) array are reserved for TOP n BOTTOM virtual sites
- // the array index range for use is from 1 to N*N
- }
- public void open(int row,int col){
- // System.out.println("in open(): passed value: " + row+ " " + col + " state: " + state[row-1][col-1]);
- if (row < 1 || row > arrDim || col < 1 || col > arrDim)
- throw new IndexOutOfBoundsException("Illegal parameter value.");
- // passed values are in 1:N convetion , here, sice 2D to 1D conversion is involded, row convention changed to 0:(N-1)
- row = row - 1;
- col = col - 1;
- if(calledBefore(row,col)){
- // if the site is already opened, do nothing
- // System.out.println("called before");
- }
- else{
- count++;
- // System.out.print("count: " + count);
- int myIndex = convertTo1D(row,col);
- // System.out.println(" myIndex : " + myIndex);
- if(row == 0){
- state[row][col] = 'f';
- // System.out.println(" state: " + state[row][col]);
- }
- else if(row == (arrDim - 1)){
- state[row][col] = 'o';
- // System.out.println(" state: " + state[row][col]);
- }
- else{
- // if neither in top or bottom row just mark state as open n check for open neigbors, and connect them.
- state[row][col] = 'o';
- // System.out.println(" state: " + state[row][col]);
- }
- // opening a site means connecting the newly opened site with its neighboring sites
- if(row < (arrDim - 1)){
- // do row + 1
- int neighborIndex = convertTo1D(row+1,col);
- if(isOpen(row+1,col)){ // ||
- qu.union(myIndex, neighborIndex);
- // System.out.print(" open connect row + 1 ");
- state[row + 1][col] = state[row][col]; // isOpen returns true, so state[row + 1][col] is open. thus it can only change to full if state[row][col] = 'f'
- // state[row][col] is either full or open, so state[row + 1][col] has nothing to lose, only it can gain.
- }else if(isFull(row+1,col)){
- qu.union(myIndex, neighborIndex);
- // System.out.print(" full connect row + 1 ");
- state[row][col] = 'f';
- }
- }
- if(row > 0){
- // do row - 1
- int neighborIndex = convertTo1D(row-1,col);
- if(isOpen(row-1,col)){// ||
- qu.union(myIndex, neighborIndex);
- // System.out.print(" open connect row - 1");
- state[row - 1][col] = state[row][col];
- }else if(isFull(row-1,col)){
- qu.union(myIndex, neighborIndex);
- // System.out.print(" full connect row - 1 ");
- state[row][col] = 'f';
- }
- }
- if(col < (arrDim - 1)){
- // do col + 1
- int neighborIndex = convertTo1D(row,col+1);
- if(isOpen(row,col + 1)){ // ||
- qu.union(myIndex, neighborIndex);
- // System.out.print(" open connect col + 1");
- state[row][col + 1] = state[row][col];
- }else if(isFull(row,col + 1)){
- qu.union(myIndex, neighborIndex);
- // System.out.print(" full connect col + 1 ");
- state[row][col] = 'f';
- }
- }
- if(col > 0){
- // do col - 1
- int neighborIndex = convertTo1D(row,col-1);
- if(isOpen(row,col - 1)){ //
- qu.union(myIndex, neighborIndex);
- // System.out.print(" open connect col - 1\n");
- state[row][col - 1] = state[row][col];
- }else if(isFull(row,col - 1)){
- qu.union(myIndex, neighborIndex);
- // System.out.print(" full connect col - 1 ");
- state[row][col] = 'f';
- }
- }
- }
- }
- public boolean isOpen(int row, int col){
- return (state[row][col] == 'o');
- }
- public boolean isFull(int row, int col){
- return (state[row][col] == 'f');
- //return (qu.rootOf(convertTo1D(row,col)) == 0);
- }
- public boolean calledBefore(int row, int col){
- return (state[row][col] == 'o' || state[row][col] == 'f');
- }
- public boolean percolates(){
- // System.out.println("in percolates: returning: ");
- if(qu.connected(top, bottom)){
- System.out.println("\n\n percolates: true");
- return true;
- }
- else{
- // System.out.println("false");
- return false;
- }
- }
- public static void main(String[] args){
- final long startTime = System.nanoTime();
- int numOfRuns = 100;
- double[] fraction = new double[numOfRuns];
- for(int runCount = 0; runCount < numOfRuns ; runCount++){
- //
- System.out.println("enter size: ");
- // int size = StdIn.readInt() ;
- int size=200;
- int arraySize= size*size;
- Percolation perC = new Percolation(size);
- // System.out.println("enter number of iterations: ");
- // int numOfRuns = StdIn.readInt();
- for(;;){
- int row = StdRandom.uniform(size) + 1;
- int column = StdRandom.uniform(size) + 1;
- // System.out.format("\n row n column: %d %d",row,column);
- // int row = StdIn.readInt();
- // int column = StdIn.readInt();
- if((row<1 || row>size) || (column < 1 || column > size )) throw new java.lang.IndexOutOfBoundsException("entered value is not valid \n");
- perC.open(row,column);
- if(perC.percolates()){
- System.out.println("percolates with: " + perC.count + "counts");
- fraction[runCount] = perC.count/arraySize;
- break;
- }
- }
- }
- double mu = StdStats.mean(fraction);
- double sigma = StdStats.stddev(fraction);
- double confHi = mu + (1.96*sigma/Math.sqrt((double)numOfRuns));
- double confLow = mu - (1.96*sigma/Math.sqrt((double)numOfRuns));
- System.out.println("\n mean: " + mu);
- System.out.println(" stddev: " + sigma);
- System.out.println(" confHi: " + confHi);
- System.out.println(" confLow: " + confLow);
- final long duration = System.nanoTime() - startTime;
- System.out.println("duration: " + duration);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement