Course Schedule II
1. Clarify the problem: Before diving into solving the problem, let's clarify the requirements:
- We are given the total number of courses, represented by an integer
numCourses
, and a list of prerequisite pairs, where each pair is represented by a list. - Our task is to determine the order in which the courses should be taken such that we can complete all the courses.
- If it is impossible to finish all the courses, we need to return an empty array.
2. Analyze the problem: To solve this problem, we can use a graph-based approach using topological sorting.
- We can represent the courses and their prerequisites as a directed graph.
- The prerequisites define the edges of the graph.
- The problem reduces to finding a topological ordering of the graph, i.e., an ordering of the nodes such that for every directed edge
u -> v
, nodeu
comes before nodev
in the ordering. - If the graph contains a cycle, it is not possible to complete all the courses.
3. Design an algorithm: Here is the algorithm to solve the problem:
- Create an adjacency list representation of the graph using a dictionary.
- Initialize an array
indegree
of sizenumCourses
to store the indegree (number of incoming edges) of each course. - Iterate through the prerequisite pairs and populate the adjacency list and indegree array.
- Create a queue and enqueue all the courses with an indegree of 0.
- Initialize a variable
count
to keep track of the number of courses visited. - While the queue is not empty:
- Dequeue a course from the queue.
- Increment
count
. - Iterate through the neighbors of the dequeued course in the adjacency list:
- Decrement the indegree of the neighbor by 1.
- If the indegree of the neighbor becomes 0, enqueue the neighbor.
- If
count
is equal tonumCourses
, return the result as a valid order of courses. - Otherwise, return an empty array, as it is not possible to complete all the courses.
4. Explain your approach:
The approach involves creating an adjacency list representation of the graph and calculating the indegree of each course. We use a queue to perform a breadth-first search (BFS) traversal of the graph, starting with courses that have an indegree of 0. We visit each course and decrement the indegree of its neighbors. If a neighbor's indegree becomes 0, we enqueue it. We continue this process until all courses have been visited or there are no more courses with an indegree of 0. If we have visited all the courses (count
is equal to numCourses
), we return the result as a valid order of courses. Otherwise, we return an empty array.
5. Write clean and readable code:
python
def findOrder(numCourses, prerequisites):
# Step 1: Create adjacency list and indegree array
adjacency = {i: [] for i in range(numCourses)}
indegree = [0] * numCourses
# Step 2: Populate adjacency list and indegree array
for pair in prerequisites:
course, prerequisite = pair
adjacency[prerequisite].append(course)
indegree[course] += 1
# Step 3: Perform topological sorting using BFS
queue = []
for i in range(numCourses):
if indegree[i] == 0:
queue.append(i)
order = []
count = 0
while queue:
course = queue.pop(0)
order.append(course)
count += 1
for neighbor in adjacency[course]:
indegree[neighbor] -= 1
if indegree[neighbor] == 0:
queue.append(neighbor)
# Step 4: Check if all courses have been visited
if count == numCourses:
return order
else:
return []
6. Test your code: Let's test the code with different test cases:
numCourses = 4
,prerequisites = [[1,0],[2,0],[3,1],[3,2]]
- Expected output:
[0, 1, 2, 3]
- Explanation: The valid order of courses is 0 -> 1 -> 2 -> 3. All the courses can be completed.
- Expected output:
numCourses = 2
,prerequisites = [[1,0],[0,1]]
- Expected output:
[]
- Explanation: There is a cycle in the graph, so it is not possible to complete all the courses.
- Expected output:
7. Optimize if necessary: The initial solution already follows an optimal approach using topological sorting, so there is no further optimization required.
8. Handle error cases: The code handles the case where the input prerequisites list is empty. It returns an order that includes all the courses in that case.
9. Discuss complexity analysis:
- The time complexity of the solution is O(V + E), where V is the number of courses (vertices) and E is the number of prerequisite pairs (edges). We visit each course and its prerequisite once, which takes O(V + E) time.
- The space complexity is O(V + E), as we use the adjacency list and indegree array, both of which require O(V + E) space.
- The solution has an optimal time and space complexity.