Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

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

Correct answer: A. Q H A E N F R

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic