fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void bfstraversal(int graph[5][5])
  5. {
  6. bool visited[5];
  7. queue<int>q;
  8. int source=4;
  9. for(int i=0;i<5;i++)
  10. {
  11. visited[i]=false;
  12. }
  13. q.push(source);
  14. visited[source]=true;
  15. while(!q.empty())
  16. {
  17. int v= q.front();
  18. q.pop();
  19. printf("%d ",v);
  20. for(int i=0;i<5;i++)
  21. {
  22. if(!visited[i] && graph[v][i])
  23. {
  24. q.push(i);
  25. visited[i]=true;
  26. }
  27. }
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. int graph[5][5]={{0,1,0,0,1},
  34. {0,0,1,0,0},
  35. {0,1,0,1,1},
  36. {0,0,1,0,1},
  37. {1,0,0,1,0}};
  38. bfstraversal(graph);
  39. return 0;
  40. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
4 0 3 1 2