Data Structures & AlgorithmsHardTCS Digital · Infosys pattern

DFS-based topological sort on the DAG with adjacency lists a: [b, c], b: [d], c: [d, e], d: [f], e: [f], f: [] starts at a, visits neighbours in list order, and outputs vertices in REVERSE finishing order. What order is produced?

  1. A. a, b, c, d, e, f
  2. B. a, b, d, f, c, e
  3. C. f, d, b, e, c, a
  4. D. a, c, e, b, d, f

Correct answer: D. a, c, e, b, d, f

Finishing order: f (via a→b→d→f), d, b, then from c: d is done, e → f done, so e, then c, then a: f, d, b, e, c, a. Reversed: a, c, e, b, d, f. 'a, b, d, f, c, e' is the DFS DISCOVERY order, which is not a valid topological order here (c → d is violated); f, d, b, e, c, a is the finishing order itself, not reversed.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic