207. Course Schedule
2018-03-07 08:36
281 查看
There are a total of n courses you have to take, labeled from
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
You may assume that there are no duplicate edges in the input prerequisites.
As suggested by the hints, this problem is equivalent to detecting a cycle in the graph represented by
4000
FS can be used to solve it using the idea of topological sort. If you find yourself unfamiliar with these concepts, you may refer to their wikipedia pages. Specifically, you may only need to refer to the link in the third hint to solve this problem.Since
BFSBFS uses the indegrees of each node. We will first try to find a node with
0to
n - 1.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]Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?For example:2, [[1,0]]There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.2, [[1,0],[0,1]]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.Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
You may assume that there are no duplicate edges in the input prerequisites.
As suggested by the hints, this problem is equivalent to detecting a cycle in the graph represented by
prerequisites. Both BFS and D
4000
FS can be used to solve it using the idea of topological sort. If you find yourself unfamiliar with these concepts, you may refer to their wikipedia pages. Specifically, you may only need to refer to the link in the third hint to solve this problem.Since
pair<int, int>is inconvenient for the implementation of graph algorithms, we first transform it to a graph. If course
uis a prerequisite of course
v, we will add a directed edge from node
uto node
v.
BFSBFS uses the indegrees of each node. We will first try to find a node with
0indegree. If we fail to do so, there must be a cycle in the graph and we return
false. Otherwise we have found one. We set its indegree to be
-1to prevent from visiting it again and reduce the indegrees of all its neighbors by
1. This process will be repeated for
n(number of nodes) times. If we have not returned
false, we will return
true.
class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { vector<unordered_set<int>> graph = make_graph(numCourses, prerequisites); vector<int> degrees = compute_indegree(graph); 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] = -1; for (int neigh : graph[j]) degrees[neigh]--; } return true; } private: vector<unordered_set<int>> make_graph(int numCourses, vector<pair<int, int>>& prerequisites) { vector<unordered_set<int>> graph(numCourses); for (auto pre : prerequisites) graph[pre.second].insert(pre.first); return graph; } vector<int> compute_indegree(vector<unordered_set<int>>& graph) { vector<int> degrees(graph.size(), 0); for (auto neighbors : graph) for (int neigh : neighbors) degrees[neigh]++; return degrees; } };
相关文章推荐
- 207. Course Schedule
- 207. Course Schedule
- 207. Course Schedule
- DFS解207. Course Schedule(判断有向图是否存在环)
- [LeetCode] 207. Course Schedule 课程安排
- 207. Course Schedule
- LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)
- leetcode 207. Course Schedule
- 207. Course Schedule
- 207. Course Schedule
- Leetcode 207. Course Schedule
- leetcode 207. Course Schedule
- 207. Course Schedule
- 207. Course Schedule
- [LeetCode] 207. Course Schedule
- 207. Course Schedule
- 算法课第3周第2题——207. Course Schedule
- 拓扑排序-207. Course Schedule[medium]
- [Leetcode] 207. Course Schedule 解题报告
- 207. Course Schedule