#include <iostream>
#include <cstdlib>
#include <queue>
using namespace std;
bool visited[5];
int graph[5][5] = {{0,1,0,0,1},
{1,0,1,1,0},
{0,1,0,0,1},
{0,1,1,0,1},
{1,0,1,1,1}};
queue<int> q;
void BFS(int start_v, int n);
int main()
{
visited[0] = true;
BFS(0,5);
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void BFS(int start_v, int n)
{
cout << start_v << " ";
for(int i = 0; i < n; i++)
{
if(graph[start_v][i] == 1 && visited[i] == false)
{
q.push(i);
visited[i] = true;
}
}
while(!q.empty())
{
int tmp = q.front();
q.pop();
BFS(tmp,n);
}
}
Filed under: Data Structure | Tagged: BFS, graph, queue
A good tutorial, thanks for sharing. I really like to learn more about BFS.
Keep posting.