Hoshen–Kopelman algorithm explained

The Hoshen–Kopelman algorithm is a simple and efficient algorithm for labeling clusters on a grid, where the grid is a regular network of cells, with the cells being either occupied or unoccupied. This algorithm is based on a well-known union-finding algorithm.[1] The algorithm was originally described by Joseph Hoshen and Raoul Kopelman in their 1976 paper "Percolation and Cluster Distribution. I. Cluster Multiple Labeling Technique and Critical Concentration Algorithm".[2]

Percolation theory

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 can be empty with the 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 the 4-connected neighborhood that is top, bottom, left and right. Each occupied cell is independent of the status of its neighborhood. The number of clusters, the size of each cluster and their distribution are important topics in percolation theory.

Consider 5x5 grids in figure (a) and (b).
In figure (a), the probability of occupancy is p = 6/25 = 0.24.
In figure (b), the probability of occupancy is p = 16/25 = 0.64.

Hoshen–Kopelman algorithm for cluster finding

In this algorithm, we scan through a grid looking for occupied cells and labeling them with cluster labels. The scanning process is called a raster scan. The algorithm begins with scanning the grid cell by cell and checking whether the cell is occupied or not. If the cell is occupied, then it must be labeled with a cluster label. This cluster label is assigned based on the neighbors of that cell. (For this we are going to use Union-Find Algorithm which is explained in the next section.) If the cell doesn’t have any occupied neighbors, then a new label is assigned to the cell.

Union-find algorithm

This algorithm is a simple method for computing equivalence classes. Calling the function union(x,y) returns whether items x and y are members of the same equivalence class. Because equivalence relations are transitive, all the items equivalent to x are equivalent to all the items equivalent to y. Thus for any item x, there is a set of items which are all equivalent to x (called the equivalence class). A second function find(x) returns a representative member of the equivalence class to which x belongs.

Pseudocode

During the raster scan of the grid, whenever an occupied cell is encountered, neighboring cells are scanned to check whether any of them have already been scanned. If we find already scanned neighbors, the union operation is performed, to specify that these neighboring cells are in fact members of the same equivalence class. Then thefind operation is performed to find a representative member of that equivalence class with which the current cell will be labeled.

On the other hand, if the current cell has no neighbors, it is assigned a new, previously unused, label. The entire grid is processed in this way.

Following pseudocode is referred from Tobin Fricke's implementation of the same algorithm.[3] Raster Scan and Labeling on the Grid largest_label = 0; label = zeros[n_columns, n_rows] labels = [0:n_columns*n_rows] /* Array containing integers from 0 to the size of the image. */ for x in 0 to n_columns Union void union(int x, int y) Find int find(int x)

Example

Consider the following example. The dark cells in the grid in figure (a) represent that they are occupied and the white ones are empty. So by running H–K algorithm on this input we would get the output as shown in figure (b) with all the clusters labeled.

The algorithm processes the input grid, cell by cell, as follows: Let's say that grid is a two-dimensional array.

Consider 6x6 grids in figure (a) and (b).
Figure (a), This is the input to the Hoshen–Kopelman algorithm.
Figure (b), This is the output of the algorithm with all the clusters labeled.

Applications

See also

Notes and References

  1. https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf
  2. Percolation and cluster distribution. I. Cluster multiple labeling technique and critical concentration algorithm. J.. Hoshen. R.. Kopelman. 15 October 1976. Phys. Rev. B. 14. 8. 3438–3445. APS. 10.1103/PhysRevB.14.3438. 1976PhRvB..14.3438H.
  3. Web site: The Hoshen-Kopelman Algorithm for cluster identification. Tobin . Fricke . ocf.berkeley.edu . 2004-04-21 . 2016-09-17.
  4. Web site: Introduction to the Hoshen-Kopelman algorithm and its application to nodal domain statistics . Christian Joas . Webhome.weizmann.ac.il . 2016-09-17.
  5. Web site: Clustering.
  6. Web site: Fuzzy c-means clustering.