[LeetCode] 207. Course Schedule 课程安排
Posted 轻风舞动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 207. Course Schedule 课程安排相关的知识,希望对你有一定的参考价值。
There are a total of n courses you have to take, labeled from 0
to 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.
- 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.
- There are several ways to represent a graph. For example, the input prerequisites is a graph represented by a list of edges. Is this graph representation appropriate?
- Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
- Topological sort could also be done via BFS.
给定n个课程,上课的顺序有先后要求,用pair表示,判断是否能完成所有课程。
对于每一对课程的顺序关系,把它看做是一个有向边,边是由两个端点组成的,用两个点来表示边,所有的课程关系即构成一个有向图,问题相当于判断有向图中是否有环。判断有向图是否有环的方法是拓扑排序。
拓扑排序:维护一张表记录所有点的入度,移出入度为0的点并更新其他点的入度,重复此过程直到没有点的入度为0。如果原有向图有环的话,此时会有剩余的点且其入度不为0;否则没有剩余的点。
图的拓扑排序可以DFS或者BFS。遍历所有边,计算点的入度;将入度为0的点移出点集,并更新剩余点的入度;重复步骤2,直至没有剩余点或剩余点的入度均大于0。
这里不能使用邻接矩阵,应该使用邻接表来存储有向图的信息。邻接表可以使用结构体来实现,每个结构体存储一个值以及一个指向下一个节点的指针,同时维护一个存储多个头结点的数组即可。除此之外,在数据结构简单的情况下,还可以使用数组来模拟简单的邻接表。
Java:BFS
public class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { int[] pre = new int[numCourses]; List<Integer>[] satisfies = new List[numCourses]; for(int i=0; i<numCourses; i++) satisfies[i] = new ArrayList<>(); for(int i=0; i<prerequisites.length; i++) { satisfies[prerequisites[i][1]].add(prerequisites[i][0]); pre[prerequisites[i][0]] ++; } int finish = 0; LinkedList<Integer> queue = new LinkedList<>(); for(int i=0; i<numCourses; i++) { if (pre[i] == 0) queue.add(i); } while (!queue.isEmpty()) { int course = queue.remove(); finish ++; if (satisfies[course] == null) continue; for(int c: satisfies[course]) { pre[c] --; if (pre[c] == 0) queue.add(c); } } return finish == numCourses; } }
Java:DFS
public class Solution { private boolean[] canFinish; private boolean[] visited; private List<Integer>[] depends; private boolean canFinish(int course) { if (visited[course]) return canFinish[course]; visited[course] = true; for(int c: depends[course]) { if (!canFinish(c)) return false; } canFinish[course] = true; return canFinish[course]; } public boolean canFinish(int numCourses, int[][] prerequisites) { canFinish = new boolean[numCourses]; visited = new boolean[numCourses]; depends = new List[numCourses]; for(int i=0; i<numCourses; i++) depends[i] = new ArrayList<Integer>(); for(int i=0; i<prerequisites.length; i++) { depends[prerequisites[i][0]].add(prerequisites[i][1]); } for(int i=0; i<numCourses; i++) { if (!canFinish(i)) return false; } return true; } }
Python:
import collections class Solution(object): def canFinish(self, numCourses, prerequisites): """ :type numCourses: int :type prerequisites: List[List[int]] :rtype: bool """ zero_in_degree_queue, in_degree, out_degree = collections.deque(), {}, {} for i, j in prerequisites: if i not in in_degree: in_degree[i] = set() if j not in out_degree: out_degree[j] = set() in_degree[i].add(j) out_degree[j].add(i) for i in xrange(numCourses): if i not in in_degree: zero_in_degree_queue.append(i) while zero_in_degree_queue: prerequisite = zero_in_degree_queue.popleft() if prerequisite in out_degree: for course in out_degree[prerequisite]: in_degree[course].discard(prerequisite) if not in_degree[course]: zero_in_degree_queue.append(course) del out_degree[prerequisite] if out_degree: return False return True
C++: BFS
class Solution { public: bool canFinish(int numCourses, vector<vector<int>>& prerequisites) { vector<vector<int> > graph(numCourses, vector<int>(0)); vector<int> in(numCourses, 0); for (auto a : prerequisites) { graph[a[1]].push_back(a[0]); ++in[a[0]]; } queue<int> q; for (int i = 0; i < numCourses; ++i) { if (in[i] == 0) q.push(i); } while (!q.empty()) { int t = q.front(); q.pop(); for (auto a : graph[t]) { --in[a]; if (in[a] == 0) q.push(a); } } for (int i = 0; i < numCourses; ++i) { if (in[i] != 0) return false; } return true; } };
C++: DFS
class Solution { public: bool canFinish(int numCourses, vector<vector<int> >& prerequisites) { vector<vector<int> > graph(numCourses, vector<int>(0)); vector<int> visit(numCourses, 0); for (auto a : prerequisites) { graph[a[1]].push_back(a[0]); } for (int i = 0; i < numCourses; ++i) { if (!canFinishDFS(graph, visit, i)) return false; } return true; } bool canFinishDFS(vector<vector<int> > &graph, vector<int> &visit, int i) { if (visit[i] == -1) return false; if (visit[i] == 1) return true; visit[i] = -1; for (auto a : graph[i]) { if (!canFinishDFS(graph, visit, a)) return false; } visit[i] = 1; return true; } };
类似题目:
[LeetCode] 210. Course Schedule II 课程安排II
All LeetCode Questions List 题目汇总
以上是关于[LeetCode] 207. Course Schedule 课程安排的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 207. Course Schedule(拓扑排序)