C Program to Check the Connectivity of Graph Using BFS

This is a C Program to check the connectivity of directed graph using BFS. Breadth-first search is a way to find all the vertices reachable from the a given source vertex, s. Like depth first search, BFS traverse a connected component of a given graph and defines a spanning tree. Intuitively, the basic idea of the breath-first search is this: send a wave out from source s. The wave hits all vertices 1 edge from s. From there, the wave hits all vertices 2 edges from s. Etc. We use FIFO queue Q to maintain the wavefront: v is in Q if and only if wave has hit v but has not come out of v yet.

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

Output:

$ gcc ConnectivityUsingBFS.c
$ ./a.out
 
Enter the number of vertices: 8
Enter graph data in matrix form:
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 
 
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.