The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+14 votes
897 views

Consider the following graph: 

Among the following sequences:

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

which are 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
asked in Algorithms by Veteran (69k points)
edited by | 897 views

3 Answers

+11 votes
Best answer

For GATE purpose, without actually Applying DFS, you could 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 could reach directly from the node to the next node.

So, just visualize and do.

answered by Boss (9.3k points)
edited by
I am nt getting wat u said ... u hav solved it without using stack ???
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.
Technically u r imagining a stack ... right ??
yes but this approach will give u answer in 1 minute
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.
+7 votes

Answer will be (D)

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

answered by Veteran (81.5k points)
+5 votes
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.
answered by Loyal (3.3k points)
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

33,705 questions
40,252 answers
114,340 comments
38,861 users