Data Structures & AlgorithmsHardGATE CS · ICPC regional

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

Correct answer: D. Q A R F H I K

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic