top of page
Clusters

 

Consider a matrix in which each element is filled (1) or empty (0). Consider two sites touching if they are immediately adjacent, i.e. you can reach one from the other by incrementing or decrementing your position by 1 in x or y. In this way each site has 4 neighbors. Also consider that the boundary on the left is joined to the right and vice versa, and the same is true for the top and bottom. In this way an element in the top row is adjacent to the element on the bottom row at the same horizontal location.
 
Using the definitions above, for any given matrix one could define a cluster as all those elements that are connected to each other by adjacencies.
 
Write a function that accepts as input a matrix of 1 and 0 values and returns the number of clusters present in the matrix.

 

bottom of page