Open In App

Algorithms | Graph Traversals | Question 12

Like Article
Like
Save
Share
Report

Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the resulting depth-first search tree. Let u be a vertex in G and let v be the first new (unvisited) vertex visited after visiting u in the traversal. Which of the following statements is always true? (GATE CS 2000)

(A)

{u,v} must be an edge in G, and u is a descendant of v in T

(B)

{u,v} must be an edge in G, and v is a descendant of u in T

(C)

If {u,v} is not an edge in G then u is a leaf in T

(D)

If {u,v} is not an edge in G then u and v must have the same parent in T



Answer: (C)

Explanation:

In DFS, if \'v\' is visited
after \'u\', then one of the following is true.
1) (u, v) is an edge.
     u
   /   \\
  v     w
 /     / \\
x     y   z

2) \'u\' is a leaf node.
     w
   /   \\
  x     v
 /     / \\
u     y   z 

In DFS, after visiting a node, we first recur for all unvisited children. If there are no unvisited children (u is leaf), then control goes back to parent and parent then visits next unvisited children.

Hence (C) is the correct answer.


Quiz of this Question
Please comment below if you find anything wrong in the above post


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads