Problem In DFS

Hi @all
I studied DFS and tried to implement it, but I think I am doing some mistake and thus unable to find all vertices, please check my code

With Regars
Thank you

this is ur corrected code…LINK…hope u understand the mistake u were making…:slight_smile:

P.S.: have made this mistake many times…:stuck_out_tongue:

1 Like

ohh… thank you kunal and I was unable to figure out that… :slight_smile: really thanks :slight_smile:

When you’re trying to debug your code, you should realized that you “Find vertex 0”, but there is no such vertex.

So when you look to your code, where you starting new dfs you find:

for(u=0; u != adj[v].size(); u++)
 if(!visited[u])
    dfs(u);

But, u is just index to adjeceny list, not actuall vertex. So you should write:

for(u=0; u != adj[v].size(); u++)
 if(!visited[adj[v][u]])
    dfs(adj[v][u]);

Because adj[v][u] store real vertex.

glad could help…:slight_smile: