All mock tests are FREE till 31 December — register to save your progress
Unit 7 · Data Structures and Algorithms Paper 2 CS · September 2022

For the graph shown, the following depth-first search sequences are given: (A) abcghf (B) abfchg (C) abfhgc (D) afghbc. Which of the following is correct?

Figure

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: D. (A), (C) and (D) only

Explanation

A sequence is a valid DFS if each new vertex is adjacent to the most recent vertex that still has unvisited neighbours. Checking each sequence against the figure's edges, the official key accepts (A), (C) and (D); in (B), the step after f to c is not possible by DFS.

More Data Structures and Algorithms questions