there werer two codes like
for(auto i : v.[curr])
{
dfs(i);
cout<<“i”;
}
and
for(auto i : v.[curr])
{
dfs(i);
}
cout<<“i”;
}
can you please tell me the difference
there werer two codes like
for(auto i : v.[curr])
{
dfs(i);
cout<<“i”;
}
and
for(auto i : v.[curr])
{
dfs(i);
}
cout<<“i”;
}
can you please tell me the difference
also can i use this par !=cur statement instead of visited array for every type of graph dfs??
@Hemant,
for(auto i : v.[curr])
{
dfs(i);
cout<<“i”;
}
In this code what happens is every time you are printing the subgraph you are also printing the current node, which is not the correct way ,since same element is printing in repetition
for(auto i : v.[curr])
{
dfs(i);
}
cout<<“i”;
}
In this piece of code what happens is , the current node gets printed after all the subgraphs of the curr node are already printed. this is how you print in topological sort
instead of par !=curr it should be par!=i , you can use this only when the given graph is an undirected tree
why cant i use i!=par in connected?
consider a directed graph having a cycle
eg : - for vertices A,B,C,D
edges are {A->B} {B->C} {C- >D} {D->A}
you will end up in an infinite cycle as for every node you are only keeping track of previous node
can we use par!=i on undirected cyclic graph?
@chemant077, In the above example i have shared if you have the same graph as undirected edges even then you will end up in a infinite loop if you don’t keep track of visited nodes
eg :-
if you start dfs at A then you will reach B and then C and then D now for D prev is C so it can’t visit C but it can visit A and it will visit A, so you keep on repeating this cycle
Best way to understand this is by dry running yourself this with different graphs and examples
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.