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

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

  1. A. ABCGEFD
  2. B. ABCDEGF
  3. C. ABCDEFG
  4. D. FGEDCBA

Correct answer: B. ABCDEGF

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic