Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

A directed acyclic graph has the vertices A, F, J, K, P, R, S and the edges P→A, P→F, A→F, J→F, J→S, J→R, S→F. 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. K J P S R A F
  2. B. J K P A R S F
  3. C. J K P R S A F
  4. D. J S P K R A F

Correct answer: B. J K P A R S F

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic