-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path210. Course Schedule II.java
97 lines (80 loc) · 3.15 KB
/
210. Course Schedule II.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Solution {
private enum NodeStatus {UNTOUCHED, IN_PROGRESS, COMPLETED};
public int[] findOrder(int numCourses, int[][] prerequisites) {
List[] graph = getGraph(numCourses, prerequisites);
// System.out.println(Arrays.toString(graph));
/**
* if cycle in DAG return empty[],
* because pre-req cannot be completed
*
* it is impossible to finish all courses, return an empty array.
*/
NodeStatus[] tracking = new NodeStatus[numCourses];
Arrays.fill(tracking, NodeStatus.UNTOUCHED);
for (int node = 0; node < graph.length; node++) {
if (isCompletlyDiscovered(tracking[node], NodeStatus.UNTOUCHED)) {
if (isCyclic(graph, node, tracking)){
return new int[]{};
}
}
}
/**
* find the dependency array
* by finding the topological ordering of nodes
*/
Stack<Integer> topologicalOrdering = new Stack<>();
boolean[] visited = new boolean[numCourses];
for (int node = 0; node < graph.length; node++) {
if (visited[node]==false) {
findTopologicalOrder(graph, node, visited, topologicalOrdering);
}
}
int[] answer = new int[topologicalOrdering.size()];
int index = 0;
while (topologicalOrdering.isEmpty()==false) {
int v = topologicalOrdering.pop();
answer[index] = v;
index++;
}
return answer;
}
private void findTopologicalOrder(List<Integer>[] graph, int node, boolean[] visited, Stack<Integer> topologicalOrdering) {
visited[node] = true;
for (int v : graph[node]) {
if (!visited[v]) {
findTopologicalOrder(graph, v, visited, topologicalOrdering);
}
}
topologicalOrdering.add(node);
}
private boolean isCyclic(List<Integer>[] graph, int node, NodeStatus[] tracking) {
if (isCompletlyDiscovered(tracking[node], NodeStatus.COMPLETED)) return false;
if(isInProgress(tracking[node], NodeStatus.IN_PROGRESS)) return true;
tracking[node] = NodeStatus.IN_PROGRESS;
for (int adjNode : graph[node]) {
boolean rres = isCyclic(graph, adjNode, tracking);
if (rres) return true;
}
tracking[node] = NodeStatus.COMPLETED;
return false;
}
private boolean isInProgress(NodeStatus nodeStatus, NodeStatus inProgress) {
return nodeStatus == inProgress;
}
private boolean isCompletlyDiscovered(NodeStatus nodeStatus, NodeStatus completed) {
return nodeStatus == completed;
}
private List[] getGraph(int numCourses, int[][] prerequisites) {
List[] graph = new List[numCourses];
for (int i = 0; i < graph.length; i++) {
graph[i] = new ArrayList<Integer>();
}
for (int[] nodes : prerequisites) {
int firstNode = nodes[0];
int secondNode = nodes[1];
// second --> first
graph[secondNode].add(firstNode);
}
return graph;
}
}