Consider the following graph. Among the following sequences I. a b e g h f II.…

2021

Consider the following graph.

Among the following sequences

I. a b e g h f

II. a b f e h g

III. a b f h g e

IV. a f g h b e

Which are depth first traversals of the above graph?

  1. A.

    I, II, and IV only

  2. B.

    I and IV only

  3. C.

    II, III, and IV only

  4. D.

    I, III, and IV only

Attempted by 376 students.

Show answer & explanation

Correct answer: D

Answer: I, III, and IV (the sequences a b e g h f; a b f h g e; and a f g h b e) are valid depth-first traversals.

Key idea: A sequence is a valid DFS order if it can be produced by starting at the first node and always visiting an unvisited neighbor (recursively) before backtracking. Whether a sequence is possible depends on the graph's adjacency (which edges exist) and on the chosen neighbor exploration order.

  • Sequence "a b e g h f" — Valid. One possible DFS: start at a, go to b; from b go to e; from e go to g; from g go to h; from h go to f. Each consecutive pair is connected by an edge, and this path can be produced by choosing neighbors in that order.

  • Sequence "a b f h g e" — Valid. One possible DFS: a → b → f → h → g → e. Each step follows an existing edge and the traversal can be obtained by exploring f from b and then continuing deeper along h and g before backtracking to reach e.

  • Sequence "a f g h b e" — Valid. Start at a, visit f first; from f go to g; from g go to h; from h go to b; from b go to e. This is a valid DFS order given the graph's connections.

  • Sequence "a b f e h g" — Not valid. This sequence would require a direct edge between f and e so that f → e is possible in one step, but the graph does not have an edge connecting f and e directly. Therefore this order cannot be produced by a DFS on the given graph.

Conclusion: The correct set of depth-first traversals among the choices is the one listing the three valid sequences above.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Coding For Placement