Problem
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] = [ai, bi]
indicates that you must take course bi
first if you want to take course ai
.
- For example, the pair
[0, 1]
, indicates that to take course0
you have to first take course1
.
Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
This is extension of Course Schedule 1 - Is it Possible.
Similar Problem
We’re given a hashmap associating each courseId
key with a list of courseIds
values, which represents that the prerequisites of courseId
are courseIds
. Return a sorted ordering of courses such that we can finish all courses.
Return null if there is no such ordering.
For example, given {'CSC300': ['CSC100', 'CSC200'], 'CSC200': ['CSC100'], 'CSC100': []}, should return ['CSC100', 'CSC200', 'CSCS300']
.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Solution
Method 1 - BFS on Graph
This is similar to Topological Sorting. We need to figure out if there is no cycle, then we have the course list in topological order.
Here we are using array pCounter
as map to track the dependency between courses, which are like nodes in graph.
Code
|
|
Complexity
- ⏰ Time complexity:
O(E + V)
- We iterate through the
prerequisites
array once to count the number of prerequisites for each course, which takes ( O(E) ) time, where ( E ) is the total number of prerequisites. - We iterate through all courses to add those with zero prerequisites to the queue, which takes ( O(V) ) time, where ( V ) is the number of courses.
- The while loop runs until the queue is empty, processing each course once. Inside the loop, for each course removed, we again iterate through all prerequisites. In the worst case, this nested iteration checks each prerequisite once for each course, which takes ( O(V \cdot E) ) time. However, since each prerequisite involves a constant amount of work, this will actually be ( O(V + E) ) in amortized time.
- We iterate through the
- 🧺 Space complexity:
O(V)
- The
pCounter
array takes ( O(V) ) space to store the prerequisite counts for each course. - In the worst case, the queue can contain all ( V ) courses, thus taking ( O(V) ) space.
- The
result
array stores the topological order of the courses and takes ( O(V) ) space. - The stack space used by the LinkedList operations in the worst scenario might be considered, but it is minimal compared to the main data structures.
- The
Method 2 - Using DFS
We will use hashset to go through graph, track path; and when we encounter the same node again, that means we have a cycle. So, we return 0.
Now, each node has 3 states:
- Visited - Course has been added to output
- Visiting - Course has not yet added to output, but added to check cycle
- Unvisited - Course is yet not processed
Code
|
|
Complexity
- ⏰ Time complexity:
O(V+E)
- Graph creation takes O(E) time
- DFS traversal takes O(E+V) time
- 🧺 Space complexity:
O(V + E)
- Adjacency list takes O(E+V) space
- Visiting and visited set takes O(V) space
- Recursion stack takes O(V)