-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolation.java
More file actions
242 lines (170 loc) · 7.94 KB
/
Copy pathPercolation.java
File metadata and controls
242 lines (170 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation {
private WeightedQuickUnionUF weightedQuickUnionUF;
private int virtualTopRoot;
private int virtualBottomRoot;
private int[][] grid;
private int totalNumberOfSides;
private int numberOfOpenSites = 0;
private int gridSize;
// creates n-by-n grid, with all sites initially blocked
public Percolation(int n) {
if (n <= 0)
throw new IllegalArgumentException("n cannot be less or equal to 0");
gridSize = n;
totalNumberOfSides = n * n;
grid = new int[n][n];
virtualTopRoot = totalNumberOfSides + 1;
virtualBottomRoot = totalNumberOfSides + 2;
weightedQuickUnionUF = new WeightedQuickUnionUF(totalNumberOfSides + 2);
for (int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
grid[i][j] = 0;
}
}
}
// opens the site (row, col) if it is not open already
public void open(int row, int col) {
validateRowAndColumnIndexes(row, col);
int realRowMatrixIndexNumber = row - 1;
int realColMatrixIndexNumber = col - 1;
if (isOpen(row, col)){
return;
}
int siteNumber = calculateSiteNumber(row, col);
// opening a new site
grid[realRowMatrixIndexNumber][realColMatrixIndexNumber] = 1;
numberOfOpenSites++;
if (realRowMatrixIndexNumber == 0 && realColMatrixIndexNumber == 0) {
weightedQuickUnionUF.union(siteNumber, virtualTopRoot - 1);
openElementsRightNeighborhood(row, col, siteNumber);
openElementsBottomNeighborhood(row, col, siteNumber);
return;
}
// is the number in the first row and 0 < col < N - 1
if (realRowMatrixIndexNumber == 0 &&
((realColMatrixIndexNumber > 0) && (realColMatrixIndexNumber < gridSize - 1))) {
weightedQuickUnionUF.union(siteNumber, virtualTopRoot - 1);
openElementsRightNeighborhood(row, col, siteNumber);
openElementsBottomNeighborhood(row, col, siteNumber);
openElementsLeftNeighborhood(row, col, siteNumber);
return;
}
// is the number in the first row and in the last column
if (realRowMatrixIndexNumber == 0 && (realColMatrixIndexNumber == gridSize - 1)) {
weightedQuickUnionUF.union(siteNumber, virtualTopRoot - 1);
openElementsLeftNeighborhood(row, col, siteNumber);
openElementsBottomNeighborhood(row, col, siteNumber);
return;
}
// is the number in the first col and 0 < row < N - 1
if (realColMatrixIndexNumber == 0 && (realRowMatrixIndexNumber > 0 &&
realRowMatrixIndexNumber < gridSize - 1)) {
openElementsTopNeighborhood(row, col, siteNumber);
openElementsRightNeighborhood(row, col, siteNumber);
openElementsBottomNeighborhood(row, col, siteNumber);
return;
}
// is the number 0 < row < N - 1 and 0 < col < N - 1
if ((realRowMatrixIndexNumber > 0 && realRowMatrixIndexNumber < gridSize - 1)
&& (realColMatrixIndexNumber > 0 && realColMatrixIndexNumber < gridSize - 1)) {
openElementsTopNeighborhood(row, col, siteNumber);
openElementsRightNeighborhood(row, col, siteNumber);
openElementsLeftNeighborhood(row, col, siteNumber);
openElementsBottomNeighborhood(row, col, siteNumber);
return;
}
// is the number in the last col and 0 < row < N - 1
if ((realRowMatrixIndexNumber > 0 && realRowMatrixIndexNumber < gridSize - 1)
&& (realColMatrixIndexNumber == gridSize - 1)) {
openElementsTopNeighborhood(row, col, siteNumber);
openElementsLeftNeighborhood(row, col, siteNumber);
openElementsBottomNeighborhood(row, col, siteNumber);
return;
}
// is the number in the fist col and last row
if (realColMatrixIndexNumber == 0 && (realRowMatrixIndexNumber == gridSize - 1)) {
weightedQuickUnionUF.union(siteNumber, virtualBottomRoot - 1);
openElementsTopNeighborhood(row, col, siteNumber);
openElementsRightNeighborhood(row, col, siteNumber);
return;
}
// is the number in the last row and 0 < col < N - 1
if ((realRowMatrixIndexNumber == gridSize - 1) &&
(realColMatrixIndexNumber > 0 && realColMatrixIndexNumber < gridSize - 1)) {
weightedQuickUnionUF.union(siteNumber, virtualBottomRoot - 1);
openElementsTopNeighborhood(row, col, siteNumber);
openElementsLeftNeighborhood(row, col, siteNumber);
openElementsRightNeighborhood(row, col, siteNumber);
return;
}
// is the number in the last row and last column
if ((realRowMatrixIndexNumber == gridSize - 1) &&
(realColMatrixIndexNumber == gridSize - 1)) {
weightedQuickUnionUF.union(siteNumber, virtualBottomRoot - 1);
openElementsTopNeighborhood(row, col, siteNumber);
openElementsLeftNeighborhood(row, col, siteNumber);
return;
}
}
// is the site (row, col) open?
public boolean isOpen(int row, int col) {
validateRowAndColumnIndexes(row, col);
int realRowMatrixIndexNumber = row - 1;
int realColMatrixIndexNumber = col - 1;
if (grid[realRowMatrixIndexNumber][realColMatrixIndexNumber] == 0) {
return false;
}
return true;
}
// is the site (row, col) full?
public boolean isFull(int row, int col) {
validateRowAndColumnIndexes(row, col);
return weightedQuickUnionUF.find(calculateSiteNumber(row, col)) == weightedQuickUnionUF.find(virtualTopRoot - 1);
}
// returns the number of open sites
public int numberOfOpenSites() {
return numberOfOpenSites;
}
// does the system percolate?
public boolean percolates() {
return weightedQuickUnionUF.find(virtualTopRoot - 1) == weightedQuickUnionUF.find(virtualBottomRoot - 1);
}
// this method considers row and col as starting with index 1
private int calculateSiteNumber(int row, int col) {
return ((row - 1) * gridSize) + col - 1;
}
// this method considers row and col as starting with index 1
private void validateRowAndColumnIndexes(int row, int col) {
if (row <= 0 || col <= 0)
throw new ArrayIndexOutOfBoundsException("Row/Col cannot be less or equal to 0");
}
// this method considers row and col as starting with index 1
private void openElementsTopNeighborhood(int row, int col, int siteNumber) {
if (isOpen(row - 1, col)) {
weightedQuickUnionUF.union(calculateSiteNumber(row - 1, col), siteNumber);
}
}
// this method considers row and col as starting with index 1
private void openElementsBottomNeighborhood(int row, int col, int siteNumber) {
if (isOpen(row + 1, col)) {
weightedQuickUnionUF.union(calculateSiteNumber(row + 1, col), siteNumber);
}
}
// this method considers row and col as starting with index 1
private void openElementsRightNeighborhood(int row, int col, int siteNumber) {
if (isOpen(row, col + 1)) {
weightedQuickUnionUF.union(calculateSiteNumber(row, col + 1), siteNumber);
}
}
// this method considers row and col as starting with index 1
private void openElementsLeftNeighborhood(int row, int col, int siteNumber) {
if (isOpen(row, col - 1)) {
weightedQuickUnionUF.union(calculateSiteNumber(row, col - 1), siteNumber);
}
}
// public static void main(String[] args) {
// Percolation percolation = new Percolation(5);
// percolation.open(1, 1);
// }
}