Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 C

Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): A→E, B→E, C→E, C→F, D→A, D→C, G→E, G→F. 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. B D G A C E F
  2. B. G D C F A B E
  3. C. B D A C G E F
  4. D. G D C F B A E

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic