Data Structures & AlgorithmsHardGATE CS · ICPC regional

A directed acyclic graph on vertices 0 to 6 has these directed edges (u → v): 0→3, 0→4, 0→5, 1→3, 1→5, 3→6, 4→2, 5→6. Among all topological orders, which one is the lexicographically smallest (compare the sequences of vertex numbers from the left)?

  1. A. 0 1 4 3 5 2 6
  2. B. 1 0 5 4 3 6 2
  3. C. 1 0 5 4 2 3 6
  4. D. 0 1 3 4 2 5 6

Correct answer: D. 0 1 3 4 2 5 6

Always taking the smallest currently available vertex (Kahn's algorithm with a min-heap) gives the lexicographically smallest topological order. A FIFO queue or a reversed DFS finish order gives valid orders that are not the smallest.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic