1462. Course Schedule IV - Detailed Explanation
Problem Statement
Description:
You are given an integer n representing the total number of courses labeled from 0 to n - 1. You are also given a list of prerequisites where each element is a pair [u, v] indicating that course u is a prerequisite of course v. Additionally, you are provided with a list of queries where each query is a pair [u, v]. For each query, you need to determine whether course u is a prerequisite of course v (i.e. whether there exists a direct or indirect path from u to v in the directed graph).
Examples:
Example 1:
- Input:
 n = 2
 prerequisites = [[1, 0]]
 queries = [[0, 1], [1, 0]]
- Output: [false, true]
- Explanation:
 Course1is a prerequisite for course0. Hence, in the query[1, 0]the answer istrue, while in the query[0, 1]the answer isfalse.
Example 2:
- Input:
 n = 2
 prerequisites = []
 queries = [[1, 0], [0, 1]]
- Output: [false, false]
- Explanation:
 There are no prerequisites. Every query returnsfalse.
Example 3:
- Input:
 n = 3
 prerequisites = [[1, 2], [1, 0], [2, 0]]
 queries = [[1, 0], [1, 2], [2, 1]]
- Output: [true, true, false]
- Explanation:
- From course 1, you can directly go to0and2(and even indirectly from1to0via2is possible, though here a direct edge exists).
- There is no path from course 2to course1.
 
- From course 
Constraints:
- 2 ≤ n ≤ 100
- The number of prerequisites can be up to several thousand.
- prerequisitesand- queriesconsist of pairs of distinct integers.
Hints
- 
Graph Reachability: 
 Think of the courses as nodes in a directed graph. A prerequisite relationship means there is a directed edge from one course to another. The problem then becomes checking if there is a path from one node to another.
- 
Transitive Closure: 
 Consider using algorithms that compute the transitive closure of the graph. With the constraints provided (n ≤ 100), methods like the Floyd–Warshall algorithm or DFS from each node are both viable.
Approach Overview
There are several ways to solve this problem. Let’s discuss two common approaches:
1. DFS (Depth-First Search) Approach
- 
Idea: 
 For each course, perform a DFS (or BFS) to mark all courses that can be reached from it. Store this reachability information in a data structure (e.g., a 2D boolean array) so that each query can be answered in O(1) time.
- 
Steps: - Build an adjacency list from the prerequisite pairs.
- For each course i(from0ton - 1), run a DFS/BFS to find all courses reachable fromiand mark them.
- For each query [u, v], simply return the stored reachability result for whethervis reachable fromu.
 
- 
Time Complexity: 
 O(n * (n + e)), wherenis the number of courses andeis the number of edges (prerequisites).
- 
Space Complexity: 
 O(n²) for storing the reachability matrix.
2. Floyd–Warshall Algorithm Approach
- 
Idea: 
 Use the Floyd–Warshall algorithm to compute the transitive closure of the graph. Create a 2D boolean matrixreachablewherereachable[i][j]istrueif there is a path from courseito coursej.
- 
Steps: - Initialize a matrix reachableof sizen x nwithfalse.
- For each direct prerequisite [u, v], setreachable[u][v] = true.
- For every intermediate course kfrom0ton - 1, update the matrix so that ifreachable[i][k]andreachable[k][j]are bothtrue, thenreachable[i][j]becomestrue.
- Answer each query by returning the value in the reachablematrix.
 
- Initialize a matrix 
- 
Time Complexity: 
 O(n³), which is acceptable for n up to 100.
- 
Space Complexity: 
 O(n²).
Code Implementation
Python Code
Java Code
Complexity Analysis
- Time Complexity:
- 
Floyd–Warshall Approach: O(n³) due to the triple nested loops over courses. 
- 
DFS Approach (alternative): O(n * (n + e)), where eis the number of prerequisite pairs.
 
- 
- Space Complexity:
- O(n²) for storing the reachability matrix.
 
Step-by-Step Walkthrough and Visual Example
Consider Example 3 with:
- n = 3
- prerequisites = [[1, 2], [1, 0], [2, 0]]
- queries = [[1, 0], [1, 2], [2, 1]]
- 
Graph Representation: - Course 1→ Course2
- Course 1→ Course0
- Course 2→ Course0
 
- Course 
- 
Initialization: - Build a 3×3 matrix reachable, and mark direct prerequisites astrue.
 
- Build a 3×3 matrix 
- 
Transitive Closure (Floyd–Warshall): - Using each course as an intermediate:
- For k = 1:- Since reachable[1][2]is true andreachable[2][0]is true, markreachable[1][0]as true (if not already).
 
- Since 
- For other values of k, update as needed.
 
- For 
 
- Using each course as an intermediate:
- 
Answering Queries: - Query [1, 0]: Check if course0is reachable from course1→true.
- Query [1, 2]: Check if course2is reachable from course1→true.
- Query [2, 1]: Check if course1is reachable from course2→false.
 
- Query 
Common Mistakes & Edge Cases
- 
Common Mistakes: - Not considering indirect prerequisite relationships (only checking direct prerequisites).
- Forgetting to initialize the reachability matrix properly.
- Overcomplicating the solution when a well-known algorithm (Floyd–Warshall or DFS) suffices.
 
- 
Edge Cases: - 
When there are no prerequisites, every query should return false.
- 
When the prerequisites form a chain, ensure that indirect connections are correctly computed. 
- 
When multiple queries ask about the same pair, the answer should remain consistent. 
 
- 
Alternative Variations & Related Problems
- 
Alternative Variations: - 
Course Schedule I & II: Determine if it’s possible to finish all courses given prerequisites, and produce a valid course order. 
- 
Dynamic Queries: Modify the graph (add or remove prerequisites) and answer reachability queries in real time. 
 
- 
- 
Related Problems for Further Practice: 
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78