Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A directed acyclic graph has the vertices D, E, I, Q, R, U, Z and the edges D→R, D→E, Z→D, Z→R, Z→Q, Z→E, R→U, Q→D, I→D. 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. I Z Q D R E U
  2. B. Z I Q D R E U
  3. C. I Z Q D E R U
  4. D. I Z Q D R U E

Correct answer: C. I Z Q D E R U

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic