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