Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B

A directed acyclic graph has the vertices D, L, P, Q, R, U, V and the edges V→R, Q→R, U→R, U→Q, U→D, U→P, L→Q. 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. U L Q V D R P
  2. B. L U D P Q V R
  3. C. L U V D Q R P
  4. D. V U P L Q R D

Correct answer: B. L U D P Q V R

Kahn's algorithm that always takes the smallest available vertex (a min-heap) gives L U D P Q V R. A FIFO queue gives L U V D P Q R, which can differ, and a reverse-postorder DFS gives V U P L Q R D.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic