Data Structures & AlgorithmsHardProduct-company MCQ pattern

Kahn's algorithm is run on the DAG with edges 5→2, 5→0, 4→0, 4→1, 2→3, 3→1, always removing the SMALLEST-numbered vertex of in-degree 0 (a min-heap instead of a queue). What order is produced?

  1. A. 4 5 2 0 3 1
  2. B. 0 1 2 3 4 5
  3. C. 4 5 0 2 3 1
  4. D. 5 4 2 3 1 0

Correct answer: C. 4 5 0 2 3 1

Start: in-degree 0 are 4 and 5, take 4 (0 still needs 5, and 1 needs 3). Take 5, which frees 0 and 2; take 0, then 2, which frees 3; then 3, which frees 1. 5 4 2 3 1 0 is another valid topological order, but not the one this rule produces.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic