Content deleted Content added
No edit summary |
No edit summary |
||
Line 5:
== Percolation Theory ==
<div style="text-align:justify">
Percolation theory is the study of the behavior and statistics of clusters on lattices. Suppose we have a large square lattice where each cell can be occupied with the probability p and empty with probability 1 – p. Each group of neighboring occupied cells forms a cluster. Neighbors are defined as cells having a common side but not those sharing only a corner i.e. we consider 4x4 neighborhood. (top, bottom, left, right). Each occupied cell is occupied independently of the status of its neighborhood. The number of clusters, size of each cluster and their distribution are important topics in percolation theory.▼
</div>
▲Each group of neighboring occupied cells forms a cluster. Neighbors are defined as cells having a common side but not those sharing only a corner i.e. we consider 4x4 neighborhood. (top, bottom, left, right). Each occupied cell is occupied independently of the status of its neighborhood. The number of clusters, size of each cluster and their distribution are important topics in percolation theory.
== Union-Find Algorithm ==
Line 21:
largest_label = 0;
for x in 0 to n_columns {
for y in 0 to n_rows {▼
if occupied[x,y] then▼
left = occupied[x-1,y];▼
above = occupied[x,y-1];▼
▲for y in 0 to n_rows
largest_label = largest_label + 1; /* Make a new, as-yet-unused cluster label. */▼
label[x,y] = largest_label;▼
▲if occupied[x,y] then
label[x,y] = find(left);▼
else if (left == 0) and (above != 0) then /* One neighbor, above. */
▲left = occupied[x-1,y];
label[x,y] = find(above);▼
else /* Neighbors BOTH to the left and above. */▼
▲above = occupied[x,y-1];
union(left,above); /* Link the left and above clusters. */▼
}
▲largest_label = largest_label + 1; /* Make a new, as-yet-unused cluster label. */
▲label[x,y] = largest_label;
▲else if (left != 0) and (above == 0) then /* One neighbor, to the left. */
▲label[x,y] = find(left);
▲else if (left == 0) and (above != 0) then /* One neighbor, above. */
▲label[x,y] = find(above);
▲else /* Neighbors BOTH to the left and above. */
▲union(left,above); /* Link the left and above clusters. */
}
void union(int x, int y) {
labels[find(x)] = find(y);▼
}
int y = x;▼
while (labels[y] != y)▼
y = labels[y];▼
while (labels[
int z = labels[x];▼
x = z;▼
}
return y;▼
▲int y = x;
▲while (labels[y] != y)
▲y = labels[y];
▲while (labels[x] != x)
▲int z = labels[x];
▲labels[x] = y;
▲x = z;
▲return y;
}
</div>
|