-
Notifications
You must be signed in to change notification settings - Fork 2
/
CourseSchedule.java
46 lines (39 loc) · 1.35 KB
/
CourseSchedule.java
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
package leetcode.graph;
import java.util.LinkedList;
import java.util.Queue;
public class CourseSchedule {
// This problem is equivalent to finding if a cycle exists in a directed graph.
public boolean canFinish(int numCourses, int[][] prerequisites) {
// Makes use of Kahn algorithm to do BFS topological sorting.
int[] inDegree = new int[numCourses];
// Creates an adjacency matrix to represent the graph.
int[][] matrix = new int[numCourses][numCourses];
for (int[] edge: prerequisites) {
if (matrix[edge[0]][edge[1]] == 0) {
// To avoid duplicate edges.
inDegree[edge[1]]++;
}
matrix[edge[0]][edge[1]] = 1;
}
int count = 0;
Queue<Integer> toVisit = new LinkedList<>();
for (int i = 0; i < numCourses; i++) {
if (inDegree[i] == 0) {
toVisit.add(i);
}
}
while(!toVisit.isEmpty()) {
int current = toVisit.poll();
count++;
for (int i = 0; i < numCourses; i++) {
if (matrix[current][i] != 0) {
inDegree[i]--;
if (inDegree[i] == 0) {
toVisit.add(i);
}
}
}
}
return count == numCourses;
}
}