|
| 1 | +## Course Schedule |
| 2 | + |
| 3 | +There are a total of n courses you have to take, labeled from 0 to n - 1. |
| 4 | + |
| 5 | +Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] |
| 6 | + |
| 7 | +Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses? |
| 8 | + |
| 9 | +For example: |
| 10 | + |
| 11 | +``` |
| 12 | +2, [[1,0]] |
| 13 | +``` |
| 14 | + |
| 15 | +There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible. |
| 16 | + |
| 17 | +``` |
| 18 | +2, [[1,0],[0,1]] |
| 19 | +``` |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +Note: |
| 24 | +The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented. |
| 25 | + |
| 26 | +Hints: |
| 27 | + |
| 28 | +* This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. |
| 29 | +* Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. |
| 30 | +* Topological sort could also be done via BFS. |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +显然这是一道拓扑排序题。求拓扑排序的算法过程: |
| 35 | + |
| 36 | +* 求出所有节点的入度`in-degree` |
| 37 | +* 搜索`in-degree`,找出入度为0的节点,若没有找到入读为0的节点,则一定存在环,返回false,否则,从节点中删除该节点,并与之相关联的所有有向边。 |
| 38 | +* 重复以上,直到所有的节点访问完毕,返回true |
| 39 | + |
| 40 | +首先求出所有的入度(注意题目的图是后面指向前面): |
| 41 | + |
| 42 | +```cpp |
| 43 | +vector<int> degree(n, 0); |
| 44 | +for (auto p : request) { |
| 45 | + //g[p.second][p.first] = 1; |
| 46 | + degree[p.first]++; |
| 47 | +} |
| 48 | +``` |
| 49 | +
|
| 50 | +寻找入度等于0的节点, 返回-1表示没有找到: |
| 51 | +
|
| 52 | +```cpp |
| 53 | +int findZero(const vector<int> &v) { |
| 54 | + int n = v.size(); |
| 55 | + for (int i = 0; i < n; ++i) { |
| 56 | + if (v[i] == 0) |
| 57 | + return i; |
| 58 | + } |
| 59 | + return -1; |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +实现拓扑排序判断: |
| 64 | + |
| 65 | +```cpp |
| 66 | +bool canTopsort(const vector<pair<int, int>> &request, vector<int> °ree) { |
| 67 | + int n = degree.size(); |
| 68 | + vector<bool> visited(n, false); |
| 69 | + int sum = 0; |
| 70 | + while (sum < n) { // 还没有访问完 |
| 71 | + int cur = findZero(degree); |
| 72 | + if (cur >= 0) { // 访问节点cur |
| 73 | + sum++; |
| 74 | + degree[cur] = -1; // 标记当前节点为已经访问状态 |
| 75 | + for (auto p : request) { |
| 76 | + if (p.second == cur) |
| 77 | + degree[p.first]--; // 去掉已访问节点 |
| 78 | + } |
| 79 | + } else |
| 80 | + return false; |
| 81 | + } |
| 82 | + return true; |
| 83 | +} |
| 84 | +``` |
| 85 | +
|
| 86 | +以上方法实质就是BFS,也可以使用DFS,从一个节点出发,顺着边往下走,若回到已经访问的节点,说明存在环。 |
| 87 | +
|
| 88 | +为了标示正在访问的节点、未访问节点和已经访问节点,我们分别使用-1, 0, 1表示。 |
| 89 | +
|
| 90 | +```cpp |
| 91 | +bool dfs(const vector<pair<int, int>> &request, vector<int> &visited, int i) { |
| 92 | + if (visited[i] == -1) // 回到了出发点,说明存在环 |
| 93 | + return false; |
| 94 | + if (visited[i] == 1) // 已经访问过该节点了 |
| 95 | + return true; |
| 96 | + visited[i] = -1; // -1 表示正在访问 |
| 97 | + for (auto p : request) { |
| 98 | + if (p.second == i) { |
| 99 | + if (!dfs(request, visited, p.first)) // 访问下一个节点 |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + visited[i] = 1; // 标识为已经访问过 |
| 104 | + return true; |
| 105 | +} |
| 106 | +
|
| 107 | +``` |
| 108 | +## 扩展 |
| 109 | + |
| 110 | +[Course Schedule II](../CourseScheduleII): 输出拓扑排序结果 |
0 commit comments