C Program to Check the Connectivity of Graph Using DFS

This is a C Program to check the connectivity of directed graph using DFS. Depth-first search is a systematic way to find all the vertices reachable from a source vertex, s. Historically, depth-first was first stated formally hundreds of years ago as a method for traversing mazes. Like breadth-first search, DFS traverse a connected component of a given graph and defines a spanning tree. The basic idea of depth-first search is this: It methodically explore every edge. We start over from different vertices as necessary. As soon as we discover a vertex, DFS starts exploring from it

Here is source code of the C Program to Check the Connectivity of Graph Using DFS. 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<conio.h>
  3. int a[20][20], reach[20], n;
  4. void dfs(int v) {
  5.     int i;
  6.     reach[v] = 1;
  7.     for (i = 1; i <= n; i++)
  8.         if (a[v][i] && !reach[i]) {
  9.             printf("\n %d->%d", v, i);
  10.             dfs(i);
  11.         }
  12. }
  13. int main(int argc, char **argv) {
  14.     int i, j, count = 0;
  15.     printf("\n Enter number of vertices:");
  16.     scanf("%d", &n);
  17.     for (i = 1; i <= n; i++) {
  18.         reach[i] = 0;
  19.         for (j = 1; j <= n; j++)
  20.             a[i][j] = 0;
  21.     }
  22.     printf("\n Enter the adjacency matrix:\n");
  23.     for (i = 1; i <= n; i++)
  24.         for (j = 1; j <= n; j++)
  25.             scanf("%d", &a[i][j]);
  26.     dfs(1);
  27.     printf("\n");
  28.     for (i = 1; i <= n; i++) {
  29.         if (reach[i])
  30.             count++;
  31.     }
  32.     if (count == n)
  33.         printf("\n Graph is connected");
  34.     else
  35.         printf("\n Graph is not connected");
  36.     return 0;
  37. }

Output:

$ gcc ConnectivityUsingDFS.c
$ ./a.out
 
Enter number of vertices: 8
Enter the adjacency matrix:
0 1 0 0 0 0 0 1 0 
1 0 1 0 0 0 0 1 0 
0 1 0 1 0 1 0 0 1 
0 0 0 1 0 1 0 0 0  
0 0 1 0 1 0 1 0 0
0 0 0 1 0 1 0 1 1  
1 1 0 0 0 0 1 0 1  
0 0 1 0 0 0 1 1 0  
 
1->2
2->4
4->3
3->6
3->8
8->5
5->7
 
Graph is 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.