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

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic