Hoshen–Kopelman algorithm

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. 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".

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  and can be empty with the probability. 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.

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  returns whether items   and   are members of the same equivalence class. Because equivalence relations are transitive, all the items equivalent to  are equivalent to all the items equivalent to. Thus for any item, there is a set of items which are all equivalent to   (called the equivalence class). A second function  returns a representative member of the equivalence class to which   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  operation is performed, to specify that these neighboring cells are in fact members of the same equivalence class. Then the 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. 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 { for y in 0 to n_rows { if occupied[x, y] then left = label[x-1, y]; above = label[x, y-1]; if (left == 0) and (above == 0) then /* Neither a label above nor to the left. */                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. */                label[x, y] = find(left); } } Union void union(int x, int y) { labels[find(x)] = find(y); } Find int find(int x) { 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; }

Example
Consider the following example. The dark cells in the grid in  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  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.
 * Starting from cell  i.e. the first cell. The cell is occupied and it doesn't have cells to the left or above so we will label this cell with a new label say.
 * and  both are unoccupied so they are not labeled.
 * is occupied so check cell to the left which is unoccupied so we increment the current label value and assign the label to the cell as.
 * ,  and   are unoccupied so they are not labeled.
 * is occupied so check cell to the left and above, both the cells are unoccupied so assign a new label.
 * is occupied so check cell to the left and above, only the cell to the left is occupied so assign the label of a cell on the left to this cell.
 * is occupied so check cell to the left and above, both the cells are occupied, so merge the two clusters and assign the cluster label of the cell above to the cell on the left and to this cell i.e. . (Merging using union algorithm will  label all the cells with label   to  )
 * is occupied so check cell to the left and above, only the cell to the left is occupied so assign the label of a cell on the left to this cell.
 * ,  and   are unoccupied so they are not labeled.
 * is occupied so check cell to the left and above, only cell above is occupied so assign the label of the cell above to this cell.
 * ,  and   are unoccupied so they are not labeled.
 * is occupied so check cell above which is unoccupied so we increment the current label value and assign the label to the cell as.
 * is occupied so check cell to the left and above, only the cell to the left is occupied so assign the label of the cell on the left to this cell.
 * is unoccupied so it is not labeled.
 * is occupied so check cell to the left and above, both the cells are unoccupied so assign a new label.
 * is occupied so check cell to the left and above, only the cell to the left is occupied so assign the label of the cell on the left to this cell.
 * ,  and   are unoccupied so they are not labeled.
 * is occupied so check cell to the left and above, both the cells are unoccupied so assign a new label.
 * is occupied so check cell to the left and above, both, cell to the left and above are occupied so merge the two clusters and assign the cluster label of the cell above to the cell on the left and to this cell i.e. . (Merging using union algorithm will  label all the cells with label   to  ).
 * is unoccupied so it is not labeled.
 * is occupied so check cell to the left and above, both the cells are unoccupied so assign a new label.
 * , ,   and   are unoccupied so they are not labeled.
 * is occupied so check cell to the left and above, both the cells are unoccupied so assign a new label.
 * is occupied so check cell to the left and above, both, cell to the left and above are occupied so merge the two clusters and assign the cluster label of the cell above to the cell on the left and to this cell i.e. . (Merging using union algorithm will  label all the cells with label   to  ).

Applications

 * Determination of Nodal Domain Area and Nodal Line Lengths
 * Nodal Connectivity Information
 * Modeling of electrical conduction