in Algorithms edited by
427 views
2 votes
2 votes

Consider the following graph and sequences given below :

Given answer for no of DFS traversal was 2 : S1 and S2.

How S2 is a DFS traversal ?

in Algorithms edited by
427 views

2 Comments

In S2 : Stack will be like this

visit a : neighbor = b,c

visit c : neighbor = d,b

visit d : neighbor = e,f

visit e : neighbor = b,f

visit b : all neighbors are visited. pop

visit f : since it is e's neighbor

Hence sequence : acdebf
0
0
After visiting c stack still contains d,b  and after d stack contains e,f,b (not just e,f). e can't further put b in stack as already in there and so after e it go to f and then pop b. Please correct me where I went wrong.
0
0

1 Answer

0 votes
0 votes
Yes answer is right S2 is correct .

A ---> C ---> D --->E --->B now we will have no new node to visit again therefore backtrack from B and check unvisited node unvisited node from B is F therefore

A ---> C ---> D --->E --->B--->F is also one DFS path

1 comment

Can you provide the stack states for this.
0
0

Related questions