Data Structures & AlgorithmsVery hardProduct company OA

A course plan has prerequisites (a → b means a must come before b): 1→3, 2→3, 3→5, 4→5, 5→6, 2→4, 6→7. Any number of courses with all prerequisites done can be taken in one semester. What is the minimum number of semesters?

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

Correct answer: D. 5

The answer is the number of vertices on the longest path, found level by level with Kahn's algorithm: semester 1: {1, 2}; 2: {3, 4}; 3: {5}; 4: {6}; 5: {7}. Longest chain 1 → 3 → 5 → 6 → 7 (or 2 → 4 → 5 → 6 → 7) has 5 courses. '7' takes one course per semester.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic