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

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic