Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 C

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

Correct answer: A. C F A D E B

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic