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

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic