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

Correct answer: D. B E F D A C

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic