-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolation.java
180 lines (144 loc) · 4.61 KB
/
Percolation.java
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
public class Percolation {
private int[] ids;
private int[] weights;
private int n;
private int numberOfOpenSites = 0;
// creates n-by-n grid, with all ids initially blocked
public Percolation(int n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive.");
}
this.n = n;
ids = new int[n * n + 2];
weights = new int[n * n + 2];
ids[0] = 0;
ids[n * n + 1] = n * n + 1;
for (int i = 1; i <= n * n; i++) {
ids[i] = -1;
weights[i] = 1;
}
weights[0] = 1;
weights[n * n + 1] = 1;
}
private int[] adjacentSites(int row, int col) {
validateRolCol(row, col);
int[] out = {-1, -1, -1, -1};
if ((row == 0) || (row == n + 1)) {
return out; // NOT QUITE "TRUE" BUT IT WORKS
}
if (col > 1) {
out[0] = (row - 1) * n + (col - 1);
}
if (col < n) {
out[2] = (row - 1) * n + col + 1;
}
if (row == 1) {
out[1] = 0;
} else {
out[1] = (row - 2) * n + col;
}
if (row == n) {
out[3] = n * n + 1;
} else {
out[3] = row * n + col;
}
// System.out.printf("[adjacentSites] adjacentSites(%d, %d) is %s\n",
// row, col, Arrays.toString(out));
return out;
}
// opens the site (row, col) if it is not open already
public void open(int row, int col) {
if (isOpen(row, col)) {
return;
}
numberOfOpenSites++;
int i = (row - 1) * n + col;
ids[i] = i; // initial setting
for (int adj: adjacentSites(row, col)) {
if (adj == -1) continue;
int adjRow = (adj - 1) / n + 1, adjCol = adj - (adjRow - 1) * n;
if (isOpen(adjRow, adjCol)) {
union(i, adj);
}
}
// System.out.printf("\t UPDATED ids: %s\n", Arrays.toString(ids));
}
private void union(int p, int q) {
int rootP = root(p);
int rootQ = root(q);
if (rootP == rootQ) return;
if (weights[rootP] == weights[rootQ]) {
int rootRight = rootP == 0 || rootP == n * n + 1 ? rootP : rootQ;
int rootLeft = rootRight == rootP ? rootQ : rootP;
ids[rootLeft] = rootRight;
weights[rootRight] += weights[rootLeft];
weights[rootLeft] = 0; // OPTIONAL
} else if (weights[rootP] > weights[rootQ]) {
ids[rootQ] = rootP;
weights[rootP] += weights[rootQ];
weights[rootQ] = 0; // OPTIONAL
} else {
ids[rootP] = rootQ;
weights[rootQ] += weights[rootP];
weights[rootP] = 0; // OPTIONAL
}
// System.out.printf("\n\n[union] (%d, %d) are CONNECTED!\n", p, q);
}
private int root(int i) {
while (i != ids[i]) {
i = ids[i];
}
return i;
}
private void validateRolCol(int row, int col) {
if (row < 0 || row > (n + 1) || col < 0 || col > n) {
throw new IllegalArgumentException("Either row or col or both is"
+ " not valid");
}
}
// is the site (row, col) open?
public boolean isOpen(int row, int col) {
validateRolCol(row, col);
if ((row == 0) || (row == n + 1)) {
return true;
}
return ids[(row - 1) * n + col] != -1;
}
// is the site (row, col) full?
public boolean isFull(int row, int col) {
return connected((row - 1) * n + col, 0);
}
// returns the number of open ids
public int numberOfOpenSites() {
return numberOfOpenSites;
}
private boolean connected(int p, int q) {
boolean out = false;
int pRow = (p - 1) / n + 1;
int pCol = p - (pRow - 1) * n;
int qRow = (q - 1) / n + 1;
int qCol = q - (qRow - 1) * n;
if (isOpen(pRow, pCol) && isOpen(qRow, qCol)) {
out = root(p) == root(q);
}
return out;
}
// does the system percolate?
public boolean percolates() {
return connected(n * n + 1, 0);
}
private void openSites() {
// System.out.printf("[openSites] Open site(s): 0, ");
for (int i = 1; i < n * n + 1; i++) {
if (ids[i] != -1) {
System.out.printf("%d, ", i);
}
}
System.out.printf("%d\n", n * n + 1);
}
// test client (optional)
public static void main(String[] args) {
Percolation perc = new Percolation(3);
perc.openSites();
}
}