How can I replace for loop of DFS() with for_each loop.Please let me know if there's any other efficient way to create graph or any sites teaching Graph through STL that I can basically use for competitive programming?
void DFS(int s)
{
visited[s] = true;
cout<<s<<" \n";
for(vector<pair<int,int> >::iterator it=AdjList[s].begin();it!=AdjList[s].end();it++)
{
if(!visited[it->first])
{
//cout<<it->first<<endl;
DFS(it->first);
edgeTo[it->first]=s;
}
}
}
That loop in DFS is very simple, so I doubt you need to use for_each for that. And for your information,
vector<pair<int,int> >::iterator it=AdjList[s].begin();
can just be
auto it=AdjList[s].begin();
Using auto keyword will reduce much volume from your code.
Well, personally I have implemented all major graph algorithms with STL. Seeing your code, I say you are on the right path to competitive programming already. While boost offers all kinds of readymade libraries, STL is sufficient enough for one to implement graph algorithms with.