

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the number of Islands Using Disjoint Set in C++
In this problem, we are given a 2D binary matrix. Our task is to find the number of islands Using DFS.
Island is a ground of 1 or more connected 1’s in the matrix.
Let’s take an example to understand the problem,
Input
bin[][] = {{ 1 0 0 0} {0 1 0 1} {0 0 0 0} {0 0 1 0}}
Output
3
Explanation
Islands are : bin00 - bin11 bin13 bin32
Solution Approach
To find the island from a binary matrix using a disjoint set data structure. To find island count, we will traverse the matrix and do union of all adjacent vertices by checking all 8 neighbours, if they are 1 take union of current index with its neighbour. Then do the second traversal of the matrix, and if at any index the value is 1, find its sent. If frequency is 0, increase to 1.
Example
Program to illustrate the working of our solution,
#include <bits/stdc++.h> using namespace std; class DisjointUnionSets{ vector<int> rank, parent; int n; public: DisjointUnionSets(int n){ rank.resize(n); parent.resize(n); this->n = n; makeSet(); } void makeSet(){ for (int i = 0; i < n; i++) parent[i] = i; } int find(int x){ if (parent[x] != x){ return find(parent[x]); } return x; } void Union(int x, int y){ int xRoot = find(x); int yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } }; int findIslandCount(vector<vector<int>> mat){ int n = mat.size(); int m = mat[0].size(); DisjointUnionSets *dus = new DisjointUnionSets(n * m); for (int j = 0; j < n; j++){ for (int k = 0; k < m; k++){ if (mat[j][k] == 0) continue; if (j + 1 < n && mat[j + 1][k] == 1) dus->Union(j * (m) + k, (j + 1) * (m) + k); if (j - 1 >= 0 && mat[j - 1][k] == 1) dus->Union(j * (m) + k, (j - 1) * (m) + k); if (k + 1 < m && mat[j][k + 1] == 1) dus->Union(j * (m) + k, (j) * (m) + k + 1); if (k - 1 >= 0 && mat[j][k - 1] == 1) dus->Union(j * (m) + k, (j) * (m) + k - 1); if (j + 1 < n && k + 1 < m && mat[j + 1][k + 1] == 1) dus->Union(j * (m) + k, (j + 1) * (m) + k + 1); if (j + 1 < n && k - 1 >= 0 && mat[j + 1][k - 1] == 1) dus->Union(j * m + k, (j + 1) * (m) + k - 1); if (j - 1 >= 0 && k + 1 < m && mat[j - 1][k + 1] == 1) dus->Union(j * m + k, (j - 1) * m + k + 1); if (j - 1 >= 0 && k - 1 >= 0 && mat[j - 1][k - 1] == 1) dus->Union(j * m + k, (j - 1) * m + k - 1); } } int *c = new int[n * m]; int islands = 0; for (int j = 0; j < n; j++){ for (int k = 0; k < m; k++){ if (mat[j][k] == 1){ int x = dus->find(j * m + k); if (c[x] == 0){ islands++; c[x]++; } else c[x]++; } } } return islands; } int main(void){ vector<vector<int>> mat = { {1, 1, 0, 1, 0}, {0, 1, 0, 1, 1}, {1, 0, 0, 1, 1}, {0, 0, 0, 0, 0}, {1, 1, 1, 0, 1} }; cout<<"The number of islands in binary matrix is : "<<findIslandCount(mat); }
Output
The number of islands in binary matrix is : 4
- Related Questions & Answers
- Find the number of islands Using DFS in C++
- Number of Islands in Python
- Find the number of distinct islands in a 2D matrix in Python
- Number of Closed Islands in C++
- Number of Distinct Islands in C++
- C++ Program to Find Maximum Number of Edge Disjoint Paths
- Number of Distinct Islands II in C++
- How to print number of islands in a given matrix using C#?
- Program to find number of islands, from where we cannot leave in Python
- Find the Number of Reflexive Relations on a Set using C++
- Program to count number of surrounded islands in the matrix in python
- C++ Program to Implement Disjoint Set Data Structure
- Find the number of zeroes using C++
- Program to find distance of shortest bridge between islands in Python
- Program to count number of islands in a given matrix in Python
Advertisements