Advertisement
defineSN

percolation

Feb 25th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.41 KB | None | 0 0
  1.  
  2. public class Percolation{
  3.    
  4.     private int arrDim;
  5.     private char[][] state;
  6.     private MyWeightedQuickUnionUF qu;
  7.     private int top = 0;
  8.     private int bottom;
  9.     final long constructionTime;
  10.    
  11.     Percolation(int N){
  12.         arrDim = N;
  13.         qu = new MyWeightedQuickUnionUF((N*N)+2); // +2 is for the virtual top n bottom site
  14.         state = new char[N][N];
  15. //      System.out.print("\n state : " + state.length + " elements : ");
  16.         for(int i = 0; i< N; i++){
  17.             for(int j = 0; j < N ; j++){
  18. //              'b' = blocked
  19. //              'o' = open
  20. //              'f' = full
  21.                 state[i][j]='b';
  22. //              System.out.print(" " + state[i][j]);
  23.             }
  24.         }
  25.         bottom = N*N + 1; // (N*N + 2) - 1;
  26.         for(int i = 1; i<= N; i++){
  27.             qu.union(top, i); // creating virual connection of top row to Top site
  28.             qu.union(bottom,bottom - i); // connecting bottom row sites to Bottom
  29.         }
  30. //      System.out.println("done constructing\n\n\n");
  31.         constructionTime = System.nanoTime();
  32.            
  33.     }
  34.    
  35.     public int convertTo1D(int row, int col){
  36.         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
  37.                                         // 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
  38.                                         // the array index range for use is from 1 to N*N
  39.     }
  40.     public void open(int row,int col){
  41. //      System.out.print("in open(): passed value: " + row+ " " + col);
  42.         if (row < 1 || row > arrDim || col < 1 || col > arrDim)  
  43.             throw new IndexOutOfBoundsException("Illegal parameter value.");  
  44.        
  45.         // passed values are in 1:N convetion , here, sice 2D to 1D conversion is involded, row convention changed to 0:(N-1)
  46.         row = row - 1;
  47.         col = col - 1;
  48.        
  49.         if(calledBefore(row,col)){
  50.             // if the site is already  opened, do nothing
  51.             System.out.println("called before");
  52.         }
  53.         else{
  54.                        
  55.             int myIndex = convertTo1D(row,col);
  56.            
  57. //          System.out.println(" myIndex : " + myIndex);
  58.            
  59.             if(row == 0){
  60.                 state[row][col] = 'f';
  61.             }
  62.             else if(row == (arrDim - 1)){
  63.                 state[row][col] = 'o';
  64.             }
  65.             else{
  66.                 // if neither in top or bottom row just mark state as open n check for open neigbors, and connect them.
  67.                 state[row][col] = 'o';
  68.             }
  69.             // opening a site means connecting the newly opened site with its neighboring sites
  70.             if(row < (arrDim - 1)){
  71.                 // do row + 1
  72.                 int neighborIndex = convertTo1D(row+1,col);
  73.                 if(isOpen(row+1,col)){ // ||
  74.                     qu.union(myIndex, neighborIndex);
  75. //                  System.out.print(" open connect row + 1 ");
  76.                     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'
  77.                                                             // state[row][col] is either full or open, so state[row + 1][col] has nothing to lose, only it can gain.
  78.                    
  79.                 }else if(isFull(row+1,col)){
  80.                     qu.union(myIndex, neighborIndex);
  81. //                  System.out.print(" full connect row + 1 ");
  82.                     state[row][col] = 'f';
  83.                 }
  84.             }
  85.             if(row > 0){
  86.                 // do row - 1
  87.                
  88.                 int neighborIndex = convertTo1D(row-1,col);
  89.                 if(isOpen(row-1,col)){// ||
  90.                     qu.union(myIndex, neighborIndex);
  91. //                  System.out.print(" open connect row - 1");
  92.                     state[row - 1][col] = state[row][col];
  93.                    
  94.                 }else if(isFull(row-1,col)){
  95.                     qu.union(myIndex, neighborIndex);
  96. //                  System.out.print(" full connect row - 1 ");
  97.                     state[row][col] = 'f';                 
  98.                 }
  99.             }
  100.             if(col < (arrDim - 1)){
  101.                 // do col + 1
  102.                
  103.                 int neighborIndex = convertTo1D(row,col+1);
  104.                 if(isOpen(row,col + 1)){ // ||
  105.                     qu.union(myIndex, neighborIndex);
  106. //                  System.out.print(" open connect col + 1");
  107.                     state[row][col + 1] = state[row][col];
  108.                    
  109.                 }else if(isFull(row,col + 1)){
  110.                     qu.union(myIndex, neighborIndex);
  111. //                  System.out.print(" full connect col + 1 ");
  112.                     state[row][col] = 'f';
  113.                 }
  114.             }
  115.             if(col > 0){
  116.                 // do col - 1
  117.                
  118.                 int neighborIndex = convertTo1D(row,col-1);
  119.                 if(isOpen(row,col - 1)){ //
  120.                     qu.union(myIndex, neighborIndex);
  121. //                  System.out.print(" open connect col - 1\n");
  122.                     state[row][col - 1] = state[row][col];
  123.                    
  124.                 }else if(isFull(row,col - 1)){
  125.                     qu.union(myIndex, neighborIndex);
  126. //                  System.out.print(" full connect col - 1 ");
  127.                     state[row][col] = 'f';
  128.                 }
  129.             }
  130.         }
  131.  
  132.        
  133.     }
  134.     public boolean isOpen(int row, int col){
  135.         return (state[row][col] == 'o');
  136.     }
  137.     public boolean isFull(int row, int col){
  138.         return (state[row][col] == 'f');
  139.         //return (qu.rootOf(convertTo1D(row,col)) == 0);
  140.     }
  141.     public boolean calledBefore(int row, int col){
  142.         return (state[row][col] == '0' || state[row][col] == 'f');
  143.     }
  144.     public boolean percolates(){
  145. //      System.out.println("in percolates: returning: ");
  146.         if(qu.connected(top, bottom)){
  147.             System.out.println("\n\n percolates: true");
  148.             return true;
  149.         }
  150.         else{
  151. //          System.out.println("false");
  152.             return false;
  153.         }
  154.     }
  155.    
  156.     public static void main(String[] args){
  157.         final long startTime = System.nanoTime();
  158. //      System.out.println("enter size: ");
  159. //      int size = StdIn.readInt() ;
  160.         int size=200;
  161.         Percolation perC = new Percolation(size);
  162. //      System.out.println("enter number of iterations: ");
  163. //      int numOfRuns = StdIn.readInt();
  164.                
  165.         for(;;){
  166.             int row = (int)(Math.random()*size + 1);
  167.             int column = (int)(Math.random()*size + 1);
  168. //          System.out.format("\n row n column: %d %d",row,column);
  169. //          int row = StdIn.readInt();
  170. //          int column = StdIn.readInt();
  171.             if((row<1 || row>size) || (column < 1 || column > size )) throw new java.lang.IndexOutOfBoundsException("entered value is not valid \n");
  172.            
  173.             perC.open(row,column);
  174.             if(perC.percolates()){
  175.                 System.out.println("percolates");
  176.                 break;
  177.             }
  178.            
  179.         }
  180.        
  181.         final long endTime = System.nanoTime();
  182.         final long constructionDuration = perC.constructionTime - startTime;
  183.         final long runningDuration = endTime - perC.constructionTime;
  184.         final long totalDuration = System.nanoTime() - startTime;
  185.         System.out.println("\n\n construction time: " + constructionDuration + "\n running duration: " + runningDuration);
  186.         System.out.println(" total running time: " + totalDuration);
  187.     }
  188.    
  189.    
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement