Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

An undirected graph has adjacency lists A:B B:ACGH C:BDFG D:CE E:D F:C G:BC H:B. Neighbours are always explored in alphabetical order. Which is the DFS (recursive, pre-order) visiting order starting at A?

  1. A. ABCDEFHG
  2. B. ABCDEFGH
  3. C. HGFEDCBA
  4. D. ABCGHDFE

Correct answer: B. ABCDEFGH

DFS goes as deep as possible before backtracking, taking neighbours in alphabetical order, giving ABCDEFGH.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic