Data Structures & AlgorithmsModerateGATE CS · ICPC regional

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

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

Correct answer: B. 0 2 3 1 4 5

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