# Islands in a graph using BFS

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

Example:

```Input : mat[][] = {{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}
Output : 5
```

What is an island?
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}
```

This is a variation of the standard problem: connected component. A connected component of an undirected graph is a subgraph in which every two vertices are connected to each other by a path(s), and which is connected to no other vertices outside the subgraph.
For example, the graph shown below has three connected components. ## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A graph where all vertices are connected with each other has exactly one connected component, consisting of the whole graph. Such graph with only one connected component is called as Strongly Connected Graph.

We have discussed a DFS solution for islands is already discussed. This problem can also solved by applying BFS() on each component. In each BFS() call, a component or a sub-graph is visited. We will call BFS on the next un-visited component. The number of calls to BFS() gives the number of connected components. BFS can also be used.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A cell in 2D matrix can be connected to 8 neighbours. So, unlike standard BFS(), where we process all adjacent vertices, we process 8 neighbours only. We keep track of the visited 1s so that they are not visited again.

## CPP

 `// A BFS based solution to count number of ` `// islands in a graph. ` `#include ` `using` `namespace` `std; ` ` `  `// R x C matrix ` `#define R 5 ` `#define C 5 ` ` `  `// A function to check if a given cell ` `// (u, v) can be included in DFS ` `bool` `isSafe(``int` `mat[R][C], ``int` `i, ``int` `j, ` `            ``bool` `vis[R][C]) ` `{ ` `    ``return` `(i >= 0) && (i < R) &&  ` `           ``(j >= 0) && (j < C) &&  ` `           ``(mat[i][j] && !vis[i][j]); ` `} ` ` `  `void` `BFS(``int` `mat[R][C], ``bool` `vis[R][C], ` `         ``int` `si, ``int` `sj) ` `{ ` ` `  `    ``// These arrays are used to get row and ` `    ``// column numbers of 8 neighbours of ` `    ``// a given cell ` `    ``int` `row[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; ` `    ``int` `col[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; ` ` `  `    ``// Simple BFS first step, we enqueue ` `    ``// source and mark it as visited ` `    ``queue > q; ` `    ``q.push(make_pair(si, sj)); ` `    ``vis[si][sj] = ``true``; ` ` `  `    ``// Next step of BFS. We take out ` `    ``// items one by one from queue and ` `    ``// enqueue their univisited adjacent ` `    ``while` `(!q.empty()) { ` ` `  `        ``int` `i = q.front().first; ` `        ``int` `j = q.front().second; ` `        ``q.pop(); ` ` `  `        ``// Go through all 8 adjacent ` `        ``for` `(``int` `k = 0; k < 8; k++) { ` `            ``if` `(isSafe(mat, i + row[k], ` `                       ``j + col[k], vis)) { ` `             ``vis[i + row[k]][j + col[k]] = ``true``; ` `             ``q.push(make_pair(i + row[k], j + col[k])); ` `            ``} ` `        ``} ` `    ``} ` `} ` ` `  `// This function returns number islands (connected ` `// components) in a graph. It simply works as  ` `// BFS for disconnected graph and returns count ` `// of BFS calls. ` `int` `countIslands(``int` `mat[R][C]) ` `{ ` `    ``// Mark all cells as not visited ` `    ``bool` `vis[R][C]; ` `    ``memset``(vis, 0, ``sizeof``(vis)); ` ` `  `    ``// Call BFS for every unvisited vertex ` `    ``// Whenever we see an univisted vertex, ` `    ``// we increment res (number of islands) ` `    ``// also. ` `    ``int` `res = 0; ` `    ``for` `(``int` `i = 0; i < R; i++) { ` `        ``for` `(``int` `j = 0; j < C; j++) { ` `            ``if` `(mat[i][j] && !vis[i][j]) { ` `                ``BFS(mat, vis, i, j); ` `                ``res++; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// main function ` `int` `main() ` `{ ` `    ``int` `mat[][C] = { { 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 } }; ` ` `  `    ``cout << countIslands(mat); ` ` `  `    ``return` `0; ` `} `

## Java

 `// A BFS based solution to count number of  ` `// islands in a graph.  ` `import` `java.util.*; ` ` `  `class` `GFG ` `{  ` ` `  `// R x C matrix  ` `static` `final` `int` `R = ``5``;  ` `static` `final` `int` `C = ``5` `; ` `static` `class` `pair ` `{  ` `    ``int` `first, second;  ` `    ``public` `pair(``int` `first, ``int` `second)  ` `    ``{  ` `        ``this``.first = first;  ` `        ``this``.second = second;  ` `    ``}  ` `} ` ` `  `// A function to check if a given cell  ` `// (u, v) can be included in DFS  ` `static` `boolean` `isSafe(``int` `mat[][], ``int` `i, ``int` `j,  ` `                       ``boolean` `vis[][])  ` `{  ` `    ``return` `(i >= ``0``) && (i < R) &&  ` `        ``(j >= ``0``) && (j < C) &&  ` `        ``(mat[i][j]==``1` `&& !vis[i][j]);  ` `}  ` ` `  `static` `void` `BFS(``int` `mat[][], ``boolean` `vis[][],  ` `                ``int` `si, ``int` `sj)  ` `{  ` ` `  `    ``// These arrays are used to get row and  ` `    ``// column numbers of 8 neighbours of  ` `    ``// a given cell  ` `    ``int` `row[] = { -``1``, -``1``, -``1``, ``0``, ``0``, ``1``, ``1``, ``1` `};  ` `    ``int` `col[] = { -``1``, ``0``, ``1``, -``1``, ``1``, -``1``, ``0``, ``1` `};  ` ` `  `    ``// Simple BFS first step, we enqueue  ` `    ``// source and mark it as visited  ` `    ``Queue q = ``new` `LinkedList();  ` `    ``q.add(``new` `pair(si, sj));  ` `    ``vis[si][sj] = ``true``;  ` ` `  `    ``// Next step of BFS. We take out  ` `    ``// items one by one from queue and  ` `    ``// enqueue their univisited adjacent  ` `    ``while` `(!q.isEmpty())  ` `    ``{  ` ` `  `        ``int` `i = q.peek().first;  ` `        ``int` `j = q.peek().second;  ` `        ``q.remove();  ` ` `  `        ``// Go through all 8 adjacent  ` `        ``for` `(``int` `k = ``0``; k < ``8``; k++)  ` `        ``{  ` `            ``if` `(isSafe(mat, i + row[k],  ` `                    ``j + col[k], vis))  ` `            ``{  ` `                ``vis[i + row[k]][j + col[k]] = ``true``;  ` `                ``q.add(``new` `pair(i + row[k], j + col[k]));  ` `            ``}  ` `        ``}  ` `    ``}  ` `}  ` ` `  `// This function returns number islands (connected  ` `// components) in a graph. It simply works as  ` `// BFS for disconnected graph and returns count  ` `// of BFS calls.  ` `static` `int` `countIslands(``int` `mat[][])  ` `{  ` `    ``// Mark all cells as not visited  ` `    ``boolean` `[][]vis = ``new` `boolean``[R][C];  ` ` `  `    ``// Call BFS for every unvisited vertex  ` `    ``// Whenever we see an univisted vertex,  ` `    ``// we increment res (number of islands)  ` `    ``// also.  ` `    ``int` `res = ``0``;  ` `    ``for` `(``int` `i = ``0``; i < R; i++)  ` `    ``{  ` `        ``for` `(``int` `j = ``0``; j < C; j++)  ` `        ``{  ` `            ``if` `(mat[i][j]==``1` `&& !vis[i][j]) ` `            ``{  ` `                ``BFS(mat, vis, i, j);  ` `                ``res++;  ` `            ``}  ` `        ``}  ` `    ``}  ` `    ``return` `res;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `mat[][] = { { ``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.print(countIslands(mat));  ` `} ` `}  ` ` `  `// This code is contributed by PrinciRaj1992 `

## C#

 `// A BFS based solution to count number of  ` `// islands in a graph.  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{  ` ` `  `// R x C matrix  ` `static` `readonly` `int` `R = 5;  ` `static` `readonly` `int` `C = 5 ; ` `class` `pair ` `{  ` `    ``public` `int` `first, second;  ` `    ``public` `pair(``int` `first, ``int` `second)  ` `    ``{  ` `        ``this``.first = first;  ` `        ``this``.second = second;  ` `    ``}  ` `} ` ` `  `// A function to check if a given cell  ` `// (u, v) can be included in DFS  ` `static` `bool` `isSafe(``int` `[,]mat, ``int` `i, ``int` `j,  ` `                    ``bool` `[,]vis)  ` `{  ` `    ``return` `(i >= 0) && (i < R) &&  ` `        ``(j >= 0) && (j < C) &&  ` `        ``(mat[i, j]==1 && !vis[i, j]);  ` `}  ` ` `  `static` `void` `BFS(``int` `[,]mat, ``bool` `[,]vis,  ` `                ``int` `si, ``int` `sj)  ` `{  ` ` `  `    ``// These arrays are used to get row and  ` `    ``// column numbers of 8 neighbours of  ` `    ``// a given cell  ` `    ``int` `[]row = { -1, -1, -1, 0, 0, 1, 1, 1 };  ` `    ``int` `[]col = { -1, 0, 1, -1, 1, -1, 0, 1 };  ` ` `  `    ``// Simple BFS first step, we enqueue  ` `    ``// source and mark it as visited  ` `    ``List q = ``new` `List();  ` `    ``q.Add(``new` `pair(si, sj));  ` `    ``vis[si, sj] = ``true``;  ` ` `  `    ``// Next step of BFS. We take out  ` `    ``// items one by one from queue and  ` `    ``// enqueue their univisited adjacent  ` `    ``while` `(q.Count != 0)  ` `    ``{  ` `        ``int` `i = q.first;  ` `        ``int` `j = q.second;  ` `        ``q.RemoveAt(0);  ` ` `  `        ``// Go through all 8 adjacent  ` `        ``for` `(``int` `k = 0; k < 8; k++)  ` `        ``{  ` `            ``if` `(isSafe(mat, i + row[k],  ` `                    ``j + col[k], vis))  ` `            ``{  ` `                ``vis[i + row[k], j + col[k]] = ``true``;  ` `                ``q.Add(``new` `pair(i + row[k], j + col[k]));  ` `            ``}  ` `        ``}  ` `    ``}  ` `}  ` ` `  `// This function returns number islands (connected  ` `// components) in a graph. It simply works as  ` `// BFS for disconnected graph and returns count  ` `// of BFS calls.  ` `static` `int` `countIslands(``int` `[,]mat)  ` `{  ` `    ``// Mark all cells as not visited  ` `    ``bool` `[,]vis = ``new` `bool``[R, C];  ` ` `  `    ``// Call BFS for every unvisited vertex  ` `    ``// Whenever we see an univisted vertex,  ` `    ``// we increment res (number of islands)  ` `    ``// also.  ` `    ``int` `res = 0;  ` `    ``for` `(``int` `i = 0; i < R; i++)  ` `    ``{  ` `        ``for` `(``int` `j = 0; j < C; j++)  ` `        ``{  ` `            ``if` `(mat[i, j]==1 && !vis[i, j]) ` `            ``{  ` `                ``BFS(mat, vis, i, j);  ` `                ``res++;  ` `            ``}  ` `        ``}  ` `    ``}  ` `    ``return` `res;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``int` `[,]mat = { { 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 } };  ` ` `  `    ``Console.Write(countIslands(mat));  ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```5
```

Time Complexity : O(V + E) where V is number of vertices and E is number of edges. Note that the given solution is simply works as BFS for disconnected graph.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : princiraj1992, 29AjayKumar

Article Tags :
Practice Tags :

6

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.