Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 C

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

Correct answer: C. B C F E D A

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic