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

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic