1.2k views

Consider the following graph:

Among the following sequences:

1. abeghf
2. abfehg
3. abfhge
4. afghbe

Which are the depth-first traversals of the above graph?

1. I, II and IV only
2. I and IV only
3. II, III and IV only
4. I, III and IV only
edited | 1.2k views

For GATE purpose, without actually applying DFS, we can answer by just seeing options.
In DFS, we go in depth first i.e., one node to another in depth first order.

Here, $abfehg$  is not possible as we can not go from $f$ to $e$ directly.
Thus, option $(D)$ is correct.

In all the other options we can reach directly from the node to the next node.

So, just visualize and do.
edited
0
I am nt getting wat u said ... u hav solved it without using stack ???
+2
keep moving from the root vertex like- moving from root to any of the child node, then from the child node to any of its child node and backtrack only when u have exhausted all the child nodes of the current node, in this manner if u can visit all the nodes its a successful DFS. If in the option like option b here, you happen to backtrack before exhausting all the child nodes of the current node, it is not a successful DFS.
0
Technically u r imagining a stack ... right ??
+1
yes but this approach will give u answer in 1 minute
+1
Yes. Instead of applying DFS, the best approach for solving questions like this is - trace the traversals given in the options and see whether it can be a depth-first traversal sequence or not.

DFS goes upto how much depth possible and then backtrack and go to the next link.

Here only 'abfehg' not possible because e and h consecutively is not possible by any backtracking of DFS traversal

In dfs think of a stack as if every adjacent node is being put on top of it lifo and chosen randomly while in bfs think of a queue i.e.fifo here option d.