C++ Program to Traverse a Graph using BFS

This C++ program displays the breadth first traversal of all nodes present in a graph. A graph is a set of nodes connected by edges.

Here is the source code of the C++ program to display the breadth first method of traversing nodes in a graph along with a display of the start and end time of a visited node. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C Program to Traverse a Graph using BFS
  3.  */
  4. #include <iostream>
  5. #include <conio.h>
  6. using namespace std;
  7. int c = 0, t = 0;
  8. struct node_info
  9. {
  10.     int no;
  11.     int st_time;
  12. }*q = NULL, *r = NULL, *x = NULL;
  13. struct node
  14. {
  15.     node_info *pt;
  16.     node *next;
  17. }*front = NULL, *rear = NULL, *p = NULL, *np = NULL;
  18. void push(node_info *ptr)
  19. {
  20.     np = new node;
  21.     np->pt = ptr;
  22.     np->next = NULL;
  23.     if (front == NULL)
  24.     {
  25.         front = rear = np;
  26.         rear->next = NULL;
  27.     }
  28.     else
  29.     {
  30.         rear->next = np;
  31.         rear = np;
  32.         rear->next = NULL;
  33.     }
  34. }
  35. node_info *remove()
  36. {
  37.     if (front == NULL)
  38.     {
  39.         cout<<"empty queue\n";
  40.     }
  41.     else
  42.     {
  43.         p = front;
  44.         x = p->pt;
  45.         front = front->next;
  46.         delete(p);
  47.         return(x);
  48.     }
  49. }
  50. void bfs(int *v,int am[][7],int i)
  51. { 
  52.     if (c == 0)
  53.     {
  54.         q = new node_info;
  55.         q->no = i;
  56.         q->st_time = t++;
  57.         cout<<"time of visitation for node "<<q->no<<":"<<q->st_time<<"\n\n";
  58.         v[i] = 1;
  59.         push(q);
  60.     }
  61.     c++;
  62.     for (int j = 0; j < 7; j++)
  63.     {
  64.         if (am[i][j] == 0 || (am[i][j] == 1 && v[j] == 1))
  65.             continue;
  66.         else if (am[i][j] == 1 && v[j] == 0)
  67.         {
  68.             r = new node_info;
  69.             r->no = j;
  70.             r->st_time = t++;
  71.             cout<<"time of visitation for node "<<r->no<<":"<<r->st_time<<"\n\n";
  72.             v[j] = 1;
  73.             push(r);
  74.         }
  75.     }
  76.     remove();
  77.     if (c <= 6 && front != NULL)
  78.         bfs(v, am, remove()->no);
  79. }  
  80. int main()
  81. {
  82.     int v[7], am[7][7];
  83.     for (int i = 0; i < 7; i++)
  84.         v[i] = 0;
  85.     for (int i = 0; i < 7; i++)
  86.     {
  87.         cout<<"enter the values for adjacency matrix row:"<<i+1<<endl;
  88.         for (int j = 0; j < 7; j++)
  89.         {
  90.             cin>>am[i][j];
  91.         }
  92.     }
  93.     bfs(v, am, 0);
  94.     getch();
  95. }

Output
enter the values for adjacency matrix row:1
0
1
1
0
0
1
1
enter the values for adjacency matrix row:2
1
0
0
0
0
0
0
enter the values for adjacency matrix row:3
1
0
0
0
0
0
1
enter the values for adjacency matrix row:4
0
0
0
0
1
1
0
enter the values for adjacency matrix row:5
0
0
0
1
0
1
1
enter the values for adjacency matrix row:6
1
0
0
1
1
0
0
enter the values for adjacency matrix row:7
1
0
1
0
1
0
0
time of visitation for node 0:0
 
time of visitation for node 1:1
 
time of visitation for node 2:2
 
time of visitation for node 5:3
 
time of visitation for node 6:4
 
time of visitation for node 3:5
 
time of visitation for node 4:6

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

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.