Data Structures & AlgorithmsHardGATE CS · ICPC regional

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

Correct answer: C. A B Y S K X

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic