Function definitions are :
// DFS algorithm
void Graph::DFS(int current) {
visited[current] = true;
cout<<current << " ";
for (int u : adjLists[current])
if (!visited[u])
DFS(u);
}
// BFS algorithm
void Graph::BFS(void){
for(int i=0;i<numVertices;++i)
visited[i] = false;
list<int> q;
q.push_back(0);
visited[0] = true;
while(!q.empty())
{
int u = q.front();
q.pop_front();
cout<< u << " ";
for( int i : adjLists[u])
if(!visited[i]){
q.push_back(i);
visited[i] = true;
}
}
}
DFS is working fine without using for loop to assign each element of visited array equal to false
but BFS is not. Why?
Whole program code is - https://hastebin.com/ojuyogexof.cpp