Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 C

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

Correct answer: A. B E D C F A

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic