C Program to Check if a Graph is Strongly Connected or Not

This is a C Program to find the connected components of the undirected graph. This can be done using depth first search. If the number of connected components is greater than one graph is weakly connected else its strongly connected.

Here is source code of the C Program to Check Whether a Graph is Strongly Connected or Not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4.  
  5. #define ROW 5
  6. #define COL 5
  7. int i, j, k;
  8. // A function to check if a given cell (row, col) can be included in DFS
  9. int isSafe(int M[][COL], int row, int col, bool visited[][COL]) {
  10.     return (row >= 0) && (row < ROW) && // row number is in range
  11.             (col >= 0) && (col < COL) && // column number is in range
  12.             (M[row][col] && !visited[row][col]); // value is 1 and not yet visited
  13. }
  14.  
  15. // A utility function to do DFS for a 2D boolean matrix. It only considers
  16. // the 8 neighbors as adjacent vertices
  17. void DFS(int M[][COL], int row, int col, bool visited[][COL]) {
  18.     // These arrays are used to get row and column numbers of 8 neighbors
  19.     // of a given cell
  20.     static int rowNbr[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
  21.     static int colNbr[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
  22.  
  23.     // Mark this cell as visited
  24.     visited[row][col] = true;
  25.  
  26.     // Recur for all connected neighbours
  27.     for (k = 0; k < 8; ++k)
  28.         if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited))
  29.             DFS(M, row + rowNbr[k], col + colNbr[k], visited);
  30. }
  31.  
  32. // The main function that returns count of islands in a given boolean
  33. // 2D matrix
  34. int countIslands(int M[][COL]) {
  35.     // Make a bool array to mark visited cells.
  36.     // Initially all cells are unvisited
  37.     bool visited[ROW][COL];
  38.     memset(visited, 0, sizeof(visited));
  39.  
  40.     // Initialize count as 0 and travese through the all cells of
  41.     // given matrix
  42.     int count = 0;
  43.     for (i = 0; i < ROW; ++i)
  44.         for (j = 0; j < COL; ++j)
  45.             if (M[i][j] && !visited[i][j]) // If a cell with value 1 is not
  46.             { // visited yet, then new island found
  47.                 DFS(M, i, j, visited); // Visit all cells in this island.
  48.                 ++count; // and increment island count
  49.             }
  50.  
  51.     return count;
  52. }
  53.  
  54. // Driver program to test above function
  55. int main() {
  56.     int M[][COL] = { { 1, 1, 0, 0, 0 },
  57.                      { 0, 1, 0, 0, 1 },
  58.                      { 1, 0, 0, 1, 1 },
  59.                      { 0, 0, 0, 0, 0 },
  60.                      { 1, 0, 1, 0, 1 }
  61.                    };
  62.  
  63.     if(countIslands(M)>1)
  64.     {
  65.         printf("Graph is weakly connected.");
  66.     }
  67.     else
  68.     {
  69.         printf("Graph is strongly connected.");
  70.     }
  71.  
  72.     return 0;
  73. }

Output:

$ gcc CheckStronglyConnectedGraph.c
$ ./a.out
 
Graph is weakly connected.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.