The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+15 votes
1.4k 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
asked in Algorithms by Veteran (59.7k points)
edited by | 1.4k views

3 Answers

+14 votes
Best answer
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.
answered by Loyal (9.4k points)
edited by
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.
+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 (103k 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 Active (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

44,196 questions
49,669 answers
163,545 comments
65,817 users