Finding islands in a 2D matrix

173 Views Asked by At

Forgive me if this has been asked before, but what equation or mathematical notation would be appropriate to find the number of islands in a 2D matrix?

Let islands be defined as connected groups of orthogonally adjacent like elements.

Let finite matrix $A$ be comprised of zeroes and ones where $|A|$ is square. $A$ contains two islands, the sum of mutually exclusive unconnected subsets of $A$.

$A = \begin{bmatrix}0&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0\\ 0&1&1&1&0&0&0&0\\ 0&0&1&0&0&0&0&0\\ 0&0&1&0&1&1&1&0\\ 0&0&0&0&1&0&1&0\\ 0&0&0&0&1&1&1&0\\ 0&0&0&0&0&0&0&0\end{bmatrix}$

Programmatically this could be solved with an algorithm by iterating through an array until you find a 1, incrementing the number of islands, recursively searching adjacent elements while marking each element as visited until the entire island is found, and continuing to the end.

Thank you,