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): C→A, D→E, E→C, F→C, G→A, G→B, G→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. D F G E B C A
  2. B. G B F D E C A
  3. C. D E F G B C A
  4. D. G F D E C B A

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic