Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

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

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

Correct answer: C. 1 4 5 0 3 6 7 2

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