retagged by
12,614 views
32 32 votes

Consider the following sequence of nodes for the undirected graph given below:

  1. $a$ $b$ $e$ $f$ $d$ $g$ $c$
  2. $a$ $b$ $e$ $f$ $c$ $g$ $d$
  3. $a$ $d$ $g$ $e$ $b$ $c$ $f$
  4. $a$ $d$ $b$ $c$ $g$ $e$ $f$

A Depth First Search (DFS) is started at node $a$. The nodes are listed in the order they are first visited. Which of the above is/are possible output(s)?

  1. $1$ and $3$ only
  2. $2$ and $3$ only
  3. $2, 3$ and $4$ only
  4. $1, 2$ and $3$ only

4 Answers

Best answer
36 36 votes

Answer: B
1. After $f$ is visited, $c$ or $g$ should be visited next. So, the traversal is incorrect.
4. After $c$ is visited, $e$ or $f$ should be visited next. So, the traversal is incorrect.
$2$ and $3$ are correct.

edited by
2 2 votes

Answer is B.

1. Visit a then b then e then f then we can't go back to d because we have a chance to go c or g which are still not visited so so abefdgc is wrong.      

2. Visit a then b then e then f then c now all the path adjacent to c is already covered so we have to backtrack to f then go to g then d

3. Similarly a then d then g then e then b then c then f can be visited.

4. But a,d then b is not possible because we can't go back to a then b because there are nodes adjacent to explore so this is wrong      

 

 

0 0 votes

I) After visiting 'f', 'c' or 'g' should be visited next. So, the traversal is incorrect.
IV) After visiting 'c', 'e' or 'f' should be visited next. So, the traversal is incorrect.

0 0 votes

Golden Rule

DFS me agar tum kisi vertex par ho, to jab tak us vertex ke subtree me unvisited vertex available hain, DFS backtrack nahi karega.

 

Option 1: a b e f d g c

Track karo:

 
a → b → e → f

Ab f ke unvisited neighbours = {c, g}

Lekin sequence bol raha hai next = d.

DFS directly d par jump nahi kar sakta.

Hence, incorrect.

 

Option 2: a b e f c g d

 
a → b → e → f → c

c ke saare neighbours already visited hain (b,e,f).

Backtrack to f.

Then g.

Then d.

Sab legal hai. Possible

 

Option 3: a d g e b c f

 
a → d → g → e → b → c

At c, neighbour f unvisited hai.

So next f aa sakta hai.

Possible.

 

Option 4: a d b c g e f

 
a → d → b → c

At c, unvisited neighbours = {e,f}.

DFS must go to one of them.

But sequence says next = g.

Hence, incorrect. 

Answer:
Position:
Show:

Related questions

43 43 votes
9 answers 9 answers
20.3k
20.3k views
Ishrat Jahan asked Oct 31, 2014
20,333 views
Consider the depth-first-search of an undirected graph with $3$ vertices $P$, $Q$, and $R$. Let discovery time $d(u)$ represent the time instant when the vertex $u$ is fi...
81 81 votes
11 answers 11 answers
23.6k
23.6k views
Ishrat Jahan asked Oct 29, 2014
23,642 views
A depth-first search is performed on a directed acyclic graph. Let $d[u]$ denote the time at which vertex $u$ is visited for the first time and $f[u]$ the time at which t...
20 20 votes
4 answers 4 answers
8.4k
8.4k views
Misbah Ghaya asked Nov 30, 2016
8,448 views
In the graph shown above, the depth-first spanning tree edges are marked with a $’ T’$. Identify the forward, backward, and cross edges.
1 1 vote
4 4 answers
5.0k
5.0k views
eyeamgj asked May 10, 2018
4,997 views
In the following graph, discovery time stamps and finishing time stamps of Depth First Search (DFS) are shown as x/yx/y, where x is discovery time stamp and y is finishin...