Course Schedule IV Problem
Description
LeetCode Problem 1462.
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [a_i, b_i] indicates that you must take course a_i first if you want to take course b_i.
- For example, the pair
[0, 1]indicates that you have to take course0before you can take course1.
Prerequisites can also be indirect. If course a is a prerequisite of course b, and course b is a prerequisite of course c, then course a is a prerequisite of course c.
You are also given an array queries where queries[j] = [u_j, v_j]. For the j^th query, you should answer whether course u_j is a prerequisite of course v_j or not.
Return a boolean array answer, where answer[j] is the answer to the j^th query.
Example 1:

Input: numCourses = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]]
Output: [false,true]
Explanation: The pair [1, 0] indicates that you have to take course 1 before you can take course 0.
Course 0 is not a prerequisite of course 1, but the opposite is true.
Example 2:
Input: numCourses = 2, prerequisites = [], queries = [[1,0],[0,1]]
Output: [false,false]
Explanation: There are no prerequisites, and each course is independent.
Example 3:

Input: numCourses = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]]
Output: [true,true]
Constraints:
2 <= numCourses <= 1000 <= prerequisites.length <= (numCourses * (numCourses - 1) / 2)prerequisites[i].length == 20 <= a_i, b_i <= numCourses - 1a_i != b_i- All the pairs
[a_i, b_i]are unique. - The prerequisites graph has no cycles.
1 <= queries.length <= 10^40 <= u_i, v_i <= numCourses - 1u_i != v_i
Solution
Approach: Transitive Closure with Floyd-Warshall
A query [u, v] asks whether there is a directed path from u to v in the prerequisite graph. With at most 100 courses but up to 104 queries, it pays to work out reachability for every pair once and then answer each query by looking it up.
Keep a boolean table reach[i][j] that starts with the direct prerequisites. Then, for each course k in turn, allow paths that pass through k: if i reaches k and k reaches j, then i reaches j. This is Floyd-Warshall with "or" and "and" in place of "min" and "+". The order of the loops matters: k must be the outermost loop, so that by the time we use k as a stop, every path whose stops are all among the earlier courses is already in the table.
When reach[i][k] is false, the inner loop can be skipped, which saves a lot of time on sparse graphs.
Complexity: O(n3 + q) time for n courses and q queries, and O(n2) extra space. Running a DFS or BFS from every course gives O(n · (n + e)) for e prerequisites, which is no worse here.
class Solution:
def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]:
n = numCourses
reach = [[False] * n for _ in range(n)] # reach[i][j]: i is a prerequisite of j
for a, b in prerequisites:
reach[a][b] = True
for k in range(n): # allow k as an intermediate course
for i in range(n):
if reach[i][k]:
row_i, row_k = reach[i], reach[k]
for j in range(n):
if row_k[j]:
row_i[j] = True
return [reach[u][v] for u, v in queries]