Data Structures & AlgorithmsModerateCodeforces Div 2 C · GATE CS

A directed acyclic graph has the vertices A, H, N, Q, X, Z and the edges N→Z, H→Z, A→H, Q→H, Q→Z, Q→X. A topological order lists every vertex once so that each edge goes from an earlier to a later vertex. Which topological order is lexicographically smallest when read as a sequence of letters?

  1. A. N Q A H Z X
  2. B. A N Q H X Z
  3. C. N A Q H Z X
  4. D. Q X N A H Z

Correct answer: B. A N Q H X Z

Kahn's algorithm that always takes the smallest available vertex (a min-heap) gives A N Q H X Z. A FIFO queue gives A N Q H X Z, which can differ, and a reverse-postorder DFS gives Q X N A H Z.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic