Number of connected components in a 2-D matrix of strings - GeeksforGeeks

02 Aug, 2021
Given a 2-D matrix mat[][] the task is to count the number of connected components in the matrix. A connected component is formed by all equal elements that share some common side with at least one other element of the same component.Examples:  
Input: mat[][] = {"bbba", 
                                   "baaa"}
Output: 2
The two connected components are:
bbb       
b

AND

  a
aaa

Input: mat[][] = {"aabba", 
                                   "aabba", 
                                   "aaaca"}
Output: 4
 
Approach: For every cell that hasn’t been visited before performing DFS. DFS will cover all the connected cells (up, left, right, and down) with same value. So the answer would be the total times DFS is run.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;#define maxRow 500#define maxCol 500 bool visited[maxRow][maxCol] = { 0 }; // Function that return true if mat[row][col]// is valid and hasn't been visitedbool isSafe(string M[], int row, int col, char c,                                    int n, int l){    // If row and column are valid and element    // is matched and hasn't been visited then    // the cell is safe    return (row >= 0 && row < n)           && (col >= 0 && col < l)           && (M[row][col] == c && !visited[row][col]);} // Function for depth first searchvoid DFS(string M[], int row, int col, char c,                                 int n, int l){    // These arrays are used to get row and column    // numbers of 4 neighbours of a given cell    int rowNbr[] = { -1, 1, 0, 0 };    int colNbr[] = { 0, 0, 1, -1 };     // Mark this cell as visited    visited[row][col] = true;     // Recur for all connected neighbours    for (int k = 0; k < 4; ++k)        if (isSafe(M, row + rowNbr[k],                  col + colNbr[k], c, n, l))             DFS(M, row + rowNbr[k],                col + colNbr[k], c, n, l);} // Function to return the number of// connectewd components in the matrixint connectedComponents(string M[], int n){    int connectedComp = 0;    int l = M[0].length();     for (int i = 0; i < n; i++) {        for (int j = 0; j < l; j++) {            if (!visited[i][j]) {                char c = M[i][j];                DFS(M, i, j, c, n, l);                connectedComp++;            }        }    }     return connectedComp;} // Driver codeint main(){    string M[] = {"aabba", "aabba", "aaaca"};    int n = sizeof(M)/sizeof(M[0]);     cout << connectedComponents(M, n);     return 0;}
// Java implementation of the approachclass GFG{static final int maxRow = 500;static final int maxCol = 500; static boolean visited[][] = new boolean[maxRow][maxCol]; // Function that return true if mat[row][col]// is valid and hasn't been visitedstatic boolean isSafe(String M[], int row, int col,                                  char c, int n, int l){    // If row and column are valid and element    // is matched and hasn't been visited then    // the cell is safe    return (row >= 0 && row < n) &&           (col >= 0 && col < l) &&           (M[row].charAt(col) == c &&           !visited[row][col]);} // Function for depth first searchstatic void DFS(String M[], int row, int col,                        char c, int n, int l){    // These arrays are used to get row and column    // numbers of 4 neighbours of a given cell    int rowNbr[] = {-1, 1, 0, 0};    int colNbr[] = {0, 0, 1, -1};     // Mark this cell as visited    visited[row][col] = true;     // Recur for all connected neighbours    for (int k = 0; k < 4; ++k)    {        if (isSafe(M, row + rowNbr[k],                      col + colNbr[k], c, n, l))        {            DFS(M, row + rowNbr[k],                   col + colNbr[k], c, n, l);        }    }} // Function to return the number of// connectewd components in the matrixstatic int connectedComponents(String M[], int n){    int connectedComp = 0;    int l = M[0].length();     for (int i = 0; i < n; i++)    {        for (int j = 0; j < l; j++)        {            if (!visited[i][j])            {                char c = M[i].charAt(j);                DFS(M, i, j, c, n, l);                connectedComp++;            }        }    }     return connectedComp;} // Driver codepublic static void main(String[] args){    String M[] = {"aabba", "aabba", "aaaca"};    int n = M.length;    System.out.println(connectedComponents(M, n));}} // This code contributed by PrinciRaj1992
# Python3 implementation of the approachimport numpy as np maxRow = 500maxCol = 500 visited = np.zeros((maxCol, maxRow)) # Function that return true if mat[row][col]# is valid and hasn't been visiteddef isSafe(M, row, col, c, n, l) :                                             # If row and column are valid and element    # is matched and hasn't been visited then    # the cell is safe    return ((row >= 0 and row < n) and            (col >= 0 and col < l) and            (M[row][col] == c and not             visited[row][col])); # Function for depth first searchdef DFS(M, row, col, c, n, l) :     # These arrays are used to get row    # and column numbers of 4 neighbours    # of a given cell    rowNbr = [ -1, 1, 0, 0 ];    colNbr = [ 0, 0, 1, -1 ];     # Mark this cell as visited    visited[row][col] = True;     # Recur for all connected neighbours    for k in range(4) :        if (isSafe(M, row + rowNbr[k],                   col + colNbr[k], c, n, l)) :             DFS(M, row + rowNbr[k],                col + colNbr[k], c, n, l); # Function to return the number of# connectewd components in the matrixdef connectedComponents(M, n) :     connectedComp = 0;    l = len(M[0]);     for i in range(n) :        for j in range(l) :            if (not visited[i][j]) :                c = M[i][j];                DFS(M, i, j, c, n, l);                connectedComp += 1;             return connectedComp; # Driver codeif __name__ == "__main__" :     M = ["aabba", "aabba", "aaaca"];    n = len(M)     print(connectedComponents(M, n)); # This code is contributed by Ryuga
// C# implementation of the approachusing System; class GFG{     static readonly int maxRow = 500;static readonly int maxCol = 500; static bool [,]visited = new bool[maxRow,maxCol]; // Function that return true if mat[row,col]// is valid and hasn't been visitedstatic bool isSafe(String []M, int row, int col,                                char c, int n, int l){    // If row and column are valid and element    // is matched and hasn't been visited then    // the cell is safe    return (row >= 0 && row < n) &&        (col >= 0 && col < l) &&        (M[row][col] == c &&        !visited[row,col]);} // Function for depth first searchstatic void DFS(String []M, int row, int col,                        char c, int n, int l){    // These arrays are used to get row and column    // numbers of 4 neighbours of a given cell    int []rowNbr = {-1, 1, 0, 0};    int []colNbr = {0, 0, 1, -1};     // Mark this cell as visited    visited[row,col] = true;     // Recur for all connected neighbours    for (int k = 0; k < 4; ++k)    {        if (isSafe(M, row + rowNbr[k],                    col + colNbr[k], c, n, l))        {            DFS(M, row + rowNbr[k],                col + colNbr[k], c, n, l);        }    }} // Function to return the number of// connectewd components in the matrixstatic int connectedComponents(String []M, int n){    int connectedComp = 0;    int l = M[0].Length;     for (int i = 0; i < n; i++)    {        for (int j = 0; j < l; j++)        {            if (!visited[i,j])            {                char c = M[i][j];                DFS(M, i, j, c, n, l);                connectedComp++;            }        }    }     return connectedComp;} // Driver codepublic static void Main(String[] args){    String []M = {"aabba", "aabba", "aaaca"};    int n = M.Length;    Console.WriteLine(connectedComponents(M, n));}} // This code contributed by Rajput-Ji
<script> // JavaScript implementation of the approach       var maxRow = 500;      var maxCol = 500;     var visited =    Array(maxRow).fill().map(()=>Array(maxCol).fill(false));     // Function that return true if mat[row][col]    // is valid and hasn't been visited    function isSafe( M , row , col,  c , n , l) {        // If row and column are valid and element        // is matched and hasn't been visited then        // the cell is safe        return (row >= 0 && row < n) &&        (col >= 0 && col < l) && (M[row].charAt(col) == c        && !visited[row][col]);    }     // Function for depth first search    function DFS( M , row , col,  c , n , l) {        // These arrays are used to get row and column        // numbers of 4 neighbours of a given cell        var rowNbr = [ -1, 1, 0, 0 ];        var colNbr = [ 0, 0, 1, -1 ];         // Mark this cell as visited        visited[row][col] = true;         // Recur for all connected neighbours        for (var k = 0; k < 4; ++k) {            if (isSafe(M, row + rowNbr[k], col +            colNbr[k], c, n, l))            {                DFS(M, row + rowNbr[k], col +                colNbr[k], c, n, l);            }        }    }     // Function to return the number of    // connectewd components in the matrix    function connectedComponents( M , n) {        var connectedComp = 0;        var l = M[0].length;         for (var i = 0; i < n; i++) {            for (j = 0; j < l; j++) {                if (!visited[i][j]) {                    var c = M[i].charAt(j);                    DFS(M, i, j, c, n, l);                    connectedComp++;                }            }        }         return connectedComp;    }     // Driver code             var M = [ "aabba", "aabba", "aaaca" ];        var n = M.length;        document.write(connectedComponents(M, n)); // This code contributed by gauravrajput1 </script>
4
 
Time Complexity: O(row * cols)Auxiliary Space: O(row * cols)
ankthon
princiraj1992
Rajput-Ji
GauravRajput1
pankajsharmagfg
DFS
Algorithms
Competitive Programming
Matrix
DFS
Matrix
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
DSA Sheet by Love Babbar
Quadratic Probing in Hashing
SCAN (Elevator) Disk Scheduling Algorithms
K means Clustering - Introduction
Difference between Informed and Uninformed Search in AI
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Competitive Programming - A Complete Guide
Modulo 10^9+7 (1000000007)
Top 10 Algorithms and Data Structures for Competitive Programming