Facebook
From Fahmid Al Rifat, 3 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 314
  1. // Online C++ compiler to run C++ program online
  2. #include <iostream>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. int graph[4][4] = {0,1,1,0,
  7.                    1,0,0,0,
  8.                    1,0,0,1,
  9.                    0,0,1,0};
  10. int color[4];
  11. int dis[4];
  12. int parent[4];
  13.  
  14. void bfs(int s)
  15. {
  16.     for(int i=0;i<4;i++)
  17.     {
  18.         color[i] = 0;
  19.         dis[4] = -1;
  20.         parent[4] = -1;    
  21.     }
  22.    
  23.     color[s] = 1;
  24.     dis[s] = 0;
  25.     parent[4] = -1;
  26.     queue<int> Q;
  27.     Q.push(s);
  28.    
  29.     while(!Q.empty())
  30.     {
  31.         int u = Q.front();
  32.         Q.pop();
  33.         cout<<"Node = "<<u<<endl;
  34.         cout<<"Distance = "<<dis[u]<<endl;
  35.         cout<<"Parent = "<<parent[u]<<endl;
  36.         cout<<endl;
  37.        
  38.         for(int i=0;i<< " BFS"<<endl;
  39.     bfs(0);
  40.     return 0;
  41. }