GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Kahn's topological sort uses a FIFO queue seeded with all vertices of in-degree 0 in increasing order; vertices are dequeued, and out-neighbours (in increasing order) are enqueued when their in-degree reaches 0. The DAG has edges 1->4, 2->4, 2->6, 3->1, 3->5, 4->7, 5->7, 5->8, 6->8, 7->9, 8->9. What is the output order and the maximum queue length?

  1. A. 1 2 3 4 5 6 7 8 9; max 3
  2. B. 2 3 6 1 5 4 8 7 9; max 2
  3. C. 9 7 8 4 5 1 6 3 2; max 4
  4. D. 2 3 6 1 5 4 8 7 9; max 3

Correct answer: D. 2 3 6 1 5 4 8 7 9; max 3

Initially the queue is 2 3 (in-degree 0); vertices become free as their predecessors are processed. FIFO processing gives the order shown, and the queue peaks at 3.

More Programming and Data Structures (GATE) questions

Exams that ask this topic