Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 C

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

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic