Data Structures & AlgorithmsHardGATE CS · ICPC regional

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

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

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

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