Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A directed acyclic graph has the vertices D, E, L, M, N, X, Z and the edges L→X, L→N, L→M, D→L, M→N, M→E, Z→E. 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. Z D L X M E N
  2. B. D Z L M X E N
  3. C. D L M N X Z E
  4. D. Z D L X M N E

Correct answer: C. D L M N X Z E

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic