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?
Example 1:
Input: 2, [[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:
Input: 2, [[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.
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.
Hints:
- 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.
- 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个课程,标记从0-n-1, 课程之间有依赖关系,例如希望完成A课程,可能需要先完成B课程。已知n个课程的依赖关系,求是否可以将n个课程全部完成。
思路:
核心是求该有向图中是否存在环。用DFS与BFS均可实现。
DFS:深度优先搜索,如果在搜索这一顶点时,又回到了该顶点,那么证明图中有环。借助boolean[] visited数组标明各顶点是否在之前已经被标记。
BFS:广度优先搜索(拓扑排序)。只将入度为0的点添加至队列。当完成一个顶点的搜索后(从队列中取出),它指向的所有顶点入度都减一,若此时某顶点入度为0则添加至队列。若完成广度搜索后,所有的入度均为0,则图无环,否则有环。
// DFS
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
ArrayList[] graph = new ArrayList[numCourses];
for(int i=0;i<numCourses;i++)
graph[i] = new ArrayList();
boolean[] visited = new boolean[numCourses];
for(int i=0; i<prerequisites.length;i++){
graph[prerequisites[i][1]].add(prerequisites[i][0]);
}
for(int i=0; i<numCourses; i++){
if(!dfs(graph,visited,i))
return false;
}
return true;
}
private boolean dfs(ArrayList[] graph, boolean[] visited, int course){
if(visited[course])
return false;
else
visited[course] = true;;
for(int i=0; i<graph[course].size();i++){
if(!dfs(graph,visited,(int)graph[course].get(i)))
return false;
}
visited[course] = false;
return true;
}
}
//BFS 比较快
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
ArrayList[] graph = new ArrayList[numCourses];
int[] degree = new int[numCourses];
Queue queue = new LinkedList();
// int count=0;
for(int i=0;i<numCourses;i++)
graph[i] = new ArrayList();
for(int i=0; i<prerequisites.length;i++){
degree[prerequisites[i][1]]++;
graph[prerequisites[i][0]].add(prerequisites[i][1]);
}
for(int i=0; i<degree.length;i++){
if(degree[i] == 0){
queue.add(i);
// count++;
}
}
while(queue.size() != 0){
int course = (int)queue.poll();
for(int i=0; i<graph[course].size();i++){
int pointer = (int)graph[course].get(i);
degree[pointer]--;
if(degree[pointer] == 0){
queue.add(pointer);
// count++;
}
}
}
// if(count == numCourses)
// return true;
// else
// return false;
for(int i=0; i<degree.length;i++)
{
// system.out.print(degree[i]);
if(degree[i]!= 0)
{
return false;
}
}
return true;
}
}
博主学习笔记,转载请注明出处,谢谢~