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

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic