# Find the number of Islands | Set 2 (Using Disjoint Set)

Given a boolean 2D matrix, find the number of islands.

A group of connected 1s forms an island. For example, the below matrix contains 5 islands

```{1, 1, 0, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1} ```

A cell in 2D matrix can be connected to 8 neighbors.

This is an variation of the standard problem: “Counting number of connected components in a undirected graph”. We have discussed a DFS based solution in below set 1.

Find the number of islands

We can also solve the question using disjoint set data structure explained here. The idea is to consider all 1 values as individual sets. Traverse the matrix and do union of all adjacent 1 vertices. Below are detailed steps.

Approach:

1) Initialize result (count of islands) as 0
2) Traverse each index of the 2D matrix.
3) If value at that index is 1, check all its 8 neighbours. If a neighbour is also equal to 1, take union of index and its neighbour.
4) Now define an array of size row*column to store frequencies of all sets.
5) Now traverse the matrix again.
6) If value at index is 1, find its set.
7) If frequency of the set in the above array is 0, increment the result be 1.

Following is Java implementation of above steps.

 `// Java program to fnd number of islands using Disjoint ` `// Set data structure. ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `public` `class` `Main ` `{ ` `    ``public` `static` `void` `main(String[] args)``throws` `IOException ` `    ``{ ` `        ``int``[][] a = ``new` `int``[][] {{``1``, ``1``, ``0``, ``0``, ``0``}, ` `                                 ``{``0``, ``1``, ``0``, ``0``, ``1``}, ` `                                 ``{``1``, ``0``, ``0``, ``1``, ``1``}, ` `                                 ``{``0``, ``0``, ``0``, ``0``, ``0``}, ` `                                 ``{``1``, ``0``, ``1``, ``0``, ``1``} ` `                                ``}; ` `        ``System.out.println(``"Number of Islands is: "` `+ ` `                           ``countIslands(a)); ` `     ``} ` ` `  `     ``// Returns number of islands in a[][] ` `     ``static` `int` `countIslands(``int` `a[][]) ` `     ``{ ` `        ``int` `n = a.length; ` `        ``int` `m = a[``0``].length; ` ` `  `        ``DisjointUnionSets dus = ``new` `DisjointUnionSets(n*m); ` ` `  `        ``/* The following loop checks for its neighbours ` `           ``and unites the indexes  if both are 1. */` `        ``for` `(``int` `j=``0``; j= ``0` `&& a[j-``1``][k]==``1``) ` `                    ``dus.union(j*(m)+k, (j-``1``)*(m)+k); ` `                ``if` `(k+``1` `< m && a[j][k+``1``]==``1``) ` `                    ``dus.union(j*(m)+k, (j)*(m)+k+``1``); ` `                ``if` `(k-``1` `>= ``0` `&& a[j][k-``1``]==``1``) ` `                    ``dus.union(j*(m)+k, (j)*(m)+k-``1``); ` `                ``if` `(j+``1``=``0` `&& a[j+``1``][k-``1``]==``1``) ` `                    ``dus.union(j*m+k, (j+``1``)*(m)+k-``1``); ` `                ``if` `(j-``1``>=``0` `&& k+``1``=``0` `&& k-``1``>=``0` `&& a[j-``1``][k-``1``]==``1``) ` `                    ``dus.union(j*m+k, (j-``1``)*m+k-``1``); ` `            ``} ` `        ``} ` ` `  `        ``// Array to note down frequency of each set ` `        ``int``[] c = ``new` `int``[n*m]; ` `        ``int` `numberOfIslands = ``0``; ` `        ``for` `(``int` `j=``0``; j

Output:

```Number of Islands is: 5
```

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

code

load comments