Parallel Courses II Problem
Description
LeetCode Problem 1494.
You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given an array relations where relations[i] = [prevCourse_i, nextCourse_i], representing a prerequisite relationship between course prevCourse_i and course nextCourse_i: course prevCourse_i has to be taken before course nextCourse_i. Also, you are given the integer k.
In one semester, you can take at most k courses as long as you have taken all the prerequisites in the previous semesters for the courses you are taking.
Return the minimum number of semesters needed to take all courses. The testcases will be generated such that it is possible to take every course.
Example 1:

Input: n = 4, relations = [[2,1],[3,1],[1,4]], k = 2
Output: 3
Explanation: The figure above represents the given graph.
In the first semester, you can take courses 2 and 3.
In the second semester, you can take course 1.
In the third semester, you can take course 4.
Example 2:

Input: n = 5, relations = [[2,1],[3,1],[4,1],[1,5]], k = 2
Output: 4
Explanation: The figure above represents the given graph.
In the first semester, you can only take courses 2 and 3 since you cannot take more than two per semester.
In the second semester, you can take course 4.
In the third semester, you can take course 1.
In the fourth semester, you can take course 5.
Constraints:
1 <= n <= 151 <= k <= n0 <= relations.length <= n * (n-1) / 2relations[i].length == 21 <= prevCourse_i, nextCourse_i <= nprevCourse_i != nextCourse_i- All the pairs
[prevCourse_i, nextCourse_i]are unique. - The given graph is a directed acyclic graph.
Solution
Approach: Breadth-First Search Over Sets of Taken Courses
With n <= 15 we can describe "which courses are done" as a bit mask, with bit i set when course i + 1 has been taken. Each semester moves us from one mask to a bigger one, so the answer is the length of the shortest path from the empty mask to the full mask: a breadth-first search, one level per semester.
For every course, precompute pre[i], the mask of its prerequisites. From a mask done, a course is ready if it is not in done and pre[i] & done == pre[i]. Then:
- If at most
kcourses are ready, take all of them. Taking a ready course now is never worse than waiting, so there is only one next state. - Otherwise, we can take only
k, and whichkmatters: try every subset of exactlykready courses (taking fewer thanknever helps).
A seen set makes sure each mask is expanded once, and the first level that contains the full mask is the answer.
The tricky case is the second bullet. A greedy rule such as "take the courses with the most dependents first" fails on some graphs, so we really have to try every choice of k; the bit masks keep that affordable.
Complexity: at most 2n masks are visited, and expanding a mask enumerates subsets of its ready courses, so the time is O(3n) in the worst case (much less in practice) and the space is O(2n).
from itertools import combinations
class Solution:
def minNumberOfSemesters(self, n: int, relations: List[List[int]], k: int) -> int:
pre = [0] * n # pre[i]: bit mask of the prerequisites of course i (0-indexed)
for a, b in relations:
pre[b - 1] |= 1 << (a - 1)
full = (1 << n) - 1
seen = {0}
level = [0] # bit masks of the courses taken after `semesters` semesters
semesters = 0
while True:
nxt_level = []
for done in level:
if done == full:
return semesters
# courses not taken yet whose prerequisites are all taken
ready = [1 << i for i in range(n) if not done >> i & 1 and pre[i] & done == pre[i]]
if len(ready) <= k:
choices = [sum(ready)] # take all of them
else:
choices = [sum(c) for c in combinations(ready, k)] # exactly k of them, every way
for sub in choices:
if done | sub not in seen:
seen.add(done | sub)
nxt_level.append(done | sub)
level = nxt_level
semesters += 1