Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 C

Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): B→A, C→A, C→G, D→C, E→A, E→B, E→C. Kahn's algorithm starts a FIFO queue with all in-degree-0 vertices in alphabetical order; it repeatedly dequeues a vertex, outputs it, and enqueues each out-neighbour (scanned alphabetically) whose remaining in-degree becomes 0. What is the output sequence?

  1. A. F E B D C G A
  2. B. D E B C A F G
  3. C. F E D C G B A
  4. D. D E F B C A G

Correct answer: D. D E F B C A G

The FIFO queue outputs D E F B C A G. Replacing the queue with a stack gives F E B D C G A, and always taking the alphabetically smallest available vertex gives D E B C A F G; only the queue discipline is asked.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic