-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ207CourseSchedule.java
More file actions
67 lines (63 loc) · 1.91 KB
/
Q207CourseSchedule.java
File metadata and controls
67 lines (63 loc) · 1.91 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.LinkedList;
import java.util.Queue;
/**
* @author ahscuml
* @date 2019/1/3
* @time 20:01
*/
// TODO DFS的方法
public class Q207CourseSchedule {
/**
* 测试函数
*/
public static void main(String[] args) {
int numCourses = 2;
int[][] preprequisites = {{1, 0}};
System.out.println(canFinishBFS(numCourses, preprequisites));
}
/**
* 利用BFS方法
*/
public static boolean canFinishBFS(int numCourses, int[][] prerequisites) {
// BFS
// 总共有多少个pairs
int length = prerequisites.length;
// 相当于建立一个图,课程关联的图 提前学的 > 学的
int[][] matrix = new int[numCourses][numCourses];
// 每个节点入度
int[] indegree = new int[numCourses];
for (int i = 0; i < length; i++) {
// 要提前学的
int pre = prerequisites[i][1];
// 不用提前学的
int ready = prerequisites[i][0];
// 入队加1
indegree[ready]++;
matrix[pre][ready] = 1;
}
int count = 0;
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < numCourses; i++) {
// 入队为0的节点,就是不需要提前学习的
if (indegree[i] == 0) {
queue.offer(i);
}
}
while (!queue.isEmpty()) {
// 出队
int temp = queue.poll();
count++;
for (int i = 0; i < numCourses; i++) {
if (matrix[temp][i] == 1) {
// 入度减少
indegree[i]--;
// 如果没有入度(先修课程全部学完)可以入队
if (indegree[i] == 0) {
queue.offer(i);
}
}
}
}
return count == numCourses;
}
}