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

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic