Course Schedule Problem
Description
LeetCode Problem 207.
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 b_i first if you want to take course a_i.
- For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.
Example 1:
1
2
3
4
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
1
2
3
4
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Constraints:
- 1 <= numCourses <= 10^5
- 0 <= prerequisites.length <= 5000
- prerequisites[i].length == 2
- 0 <= a_i, b_i < numCourses
- All the pairs prerequisites[i] are unique.
Sample C++ Code
Breadth-First Search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
graph g = buildGraph(numCourses, prerequisites);
vector<int> degrees = computeIndegrees(g);
for (int i = 0; i < numCourses; i++) {
int j = 0;
for (; j < numCourses; j++) {
if (!degrees[j]) {
break;
}
}
if (j == numCourses) {
return false;
}
degrees[j]--;
for (int v : g[j]) {
degrees[v]--;
}
}
return true;
}
private:
typedef vector<vector<int>> graph;
graph buildGraph(int numCourses, vector<pair<int, int>>& prerequisites) {
graph g(numCourses);
for (auto p : prerequisites) {
g[p.second].push_back(p.first);
}
return g;
}
vector<int> computeIndegrees(graph& g) {
vector<int> degrees(g.size(), 0);
for (auto adj : g) {
for (int v : adj) {
degrees[v]++;
}
}
return degrees;
}
};
Depth-First Search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
graph g = buildGraph(numCourses, prerequisites);
vector<bool> todo(numCourses, false), done(numCourses, false);
for (int i = 0; i < numCourses; i++) {
if (!done[i] && !acyclic(g, todo, done, i)) {
return false;
}
}
return true;
}
private:
typedef vector<vector<int>> graph;
graph buildGraph(int numCourses, vector<pair<int, int>>& prerequisites) {
graph g(numCourses);
for (auto p : prerequisites) {
g[p.second].push_back(p.first);
}
return g;
}
bool acyclic(graph& g, vector<bool>& todo, vector<bool>& done, int node) {
if (todo[node]) {
return false;
}
if (done[node]) {
return true;
}
todo[node] = done[node] = true;
for (int v : g[node]) {
if (!acyclic(g, todo, done, v)) {
return false;
}
}
todo[node] = false;
return true;
}
};
Topological Sort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool canFinish(int n, vector<pair<int, int>>& pre) {
vector<vector<int>> adj(n, vector<int>());
vector<int> degree(n, 0);
for (auto &p: pre) {
adj[p.second].push_back(p.first);
degree[p.first]++;
}
queue<int> q;
for (int i = 0; i < n; i++)
if (degree[i] == 0) q.push(i);
while (!q.empty()) {
int curr = q.front(); q.pop(); n--;
for (auto next: adj[curr])
if (--degree[next] == 0) q.push(next);
}
return n == 0;
}