Data Structures & AlgorithmsModerateCodeforces Div 2 D · ICPC regional

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

Correct answer: B. X A G V Z E

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic