- #1
Saitama
- 4,243
- 93
I am trying to implement a graph and perform BFS on it in C++. Following is the code I have written so far:
The code works fine till the point when "cout<<BFS(0)" is called. I get a runtime error when the function BFS is executed. I do not have much experience with using iterators in C++ so I believe the error comes from the iterator statements in BFS function.
Any help is appreciated. Thanks!
Code:
#include <iostream>
#include <vector>
#include <list>
#include <queue>
using namespace std;
const int V=5;
vector<list<int> > a(V);
int BFS(int s)
{
int visited[V]={0};
queue<int> Q;
visited[s]=1;
Q.push(s);
while(!Q.empty())
{
int x=Q.front();
vector<list<int> >::iterator it1=a.begin()+x;
list<int> it2=*it1;
list<int>::iterator iter=it2.begin();
while(iter!=it2.end())
{
if(visited[*iter]==0)
{
visited[*iter]=1;
Q.push(*iter);
}
visited[x]=2;
Q.pop();
iter++;
}
}
return 0;
}
void addEdge(int i, int j)
{
a[i].push_back(j);
a[j].push_back(i);
}
int main() {
vector<list<int> >::iterator it1=a.begin();
addEdge(0,1);
addEdge(0,2);
addEdge(2,1);
while(it1!=a.end())
{
list<int> it2=*it1;
list<int>::iterator iter=it2.begin();
while(iter!=it2.end())
{
cout<<*iter<<" ";
iter++;
}
cout<<endl;
it1++;
}
cout<<BFS(0);
return 0;
}
The code works fine till the point when "cout<<BFS(0)" is called. I get a runtime error when the function BFS is executed. I do not have much experience with using iterators in C++ so I believe the error comes from the iterator statements in BFS function.
Any help is appreciated. Thanks!