Course Schedule II
The problem of finding the order of courses with given prerequisites can be solved using topological sorting. We can model the courses and prerequisites as a directed graph where nodes represent courses, and a directed edge from course A to course B means that A is a prerequisite for B.
Here’s a step-by-step explanation to find the correct order of courses:
Create a Graph: Represent the courses and prerequisites as a directed graph. Use an adjacency list to store the edges.
Calculate In-Degrees: In-degree of a node is the number of prerequisites for a course. It represents how many courses need to be completed before taking that course.
Find Starting Points: Courses with an in-degree of 0 can be taken immediately, as they have no prerequisites.
Topological Sort: a. Add all the nodes with in-degree 0 to a queue. b. Remove a node from the queue, add it to the result, and reduce the in-degree of its neighbors by 1. c. If the in-degree of a neighbor becomes 0, add it to the queue. d. Repeat until the queue is empty.
Check for Cycles: If there’s a cycle in the graph, some courses can’t be taken, and the result will be an empty array.
Here’s the code to implement the above logic:
|
|
Explanation of the code:
- The graph is represented using an adjacency list, and the in-degree of each node is stored in an array.
- A queue is used to process the nodes in topological order.
- If the result does not contain all the courses, it means there’s a cycle, and an empty array is returned.
This code has a time complexity of O(N + E), where N is the number of courses, and E is the number of prerequisites, and a space complexity of O(N).