- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathCourseSchedule207.java
More file actions
Latest commit
190 lines (166 loc) · 6 KB
/
Copy pathCourseSchedule207.java
File metadata and controls
190 lines (166 loc) · 6 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* 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.
*
* 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.
*
*/
publicclassCourseSchedule207 {
publicbooleancanFinish(intnumCourses, int[][] prerequisites) {
Map<Integer, Set<Integer>> graph = initGraph(prerequisites);
Set<Integer> visited = newHashSet<>();
for (inti=0; i<numCourses; i++) {
if (graph.containsKey(i) && !visited.contains(i) && !dfs(graph, visited, newHashSet<>(), i)) returnfalse;
}
returntrue;
}
privatebooleandfs(Map<Integer, Set<Integer>> graph, Set<Integer> visited, Set<Integer> onStack, Integerpre) {
if (onStack.contains(pre)) returnfalse;
visited.add(pre);
onStack.add(pre);
if (!graph.containsKey(pre)) returntrue;
Set<Integer> adj = graph.get(pre);
for (Integercurr: adj) {
if (graph.containsKey(curr) && !dfs(graph, visited, onStack, curr)) returnfalse;
}
onStack.remove(pre);
returntrue;
}
privateMap<Integer, Set<Integer>> initGraph(int[][] prerequisites) {
Map<Integer, Set<Integer>> graph = newHashMap<>();
for (int[] p: prerequisites) {
addEdge(graph, p);
}
returngraph;
}
privatevoidaddEdge(Map<Integer, Set<Integer>> graph, int[] p) {
Set<Integer> adj = graph.getOrDefault(p[1], newHashSet<>());
adj.add(p[0]);
graph.put(p[1], adj);
}
/**
* https://discuss.leetcode.com/topic/15762/java-dfs-and-bfs-solution
*/
publicbooleancanFinish2s(intnumCourses, int[][] prerequisites) {
ArrayList[] graph = newArrayList[numCourses];
int[] degree = newint[numCourses];
Queuequeue = newLinkedList();
intcount=0;
for(inti=0;i<numCourses;i++)
graph[i] = newArrayList();
for(inti=0; i<prerequisites.length;i++){
degree[prerequisites[i][1]]++;
graph[prerequisites[i][0]].add(prerequisites[i][1]);
}
for(inti=0; i<degree.length;i++){
if(degree[i] == 0){
queue.add(i);
count++;
}
}
while(queue.size() != 0){
intcourse = (int)queue.poll();
for(inti=0; i<graph[course].size();i++){
intpointer = (int)graph[course].get(i);
degree[pointer]--;
if(degree[pointer] == 0){
queue.add(pointer);
count++;
}
}
}
if(count == numCourses)
returntrue;
else
returnfalse;
}
/**
* Kahn’s algorithm for Topological Sorting
* https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/
*/
publicbooleancanFinish3(intnumCourses, int[][] prerequisites) {
int[] indegree = newint[numCourses];
Set<Integer>[] graph = newSet[numCourses];
for (int[] link: prerequisites) {
if (graph[link[1]] == null) graph[link[1]] = newHashSet<Integer>();
graph[link[1]].add(link[0]);
indegree[link[0]]++;
}
Queue<Integer> q = newLinkedList<>();
for (inti=0; i<numCourses; i++) {
if (indegree[i] == 0) q.add(i);
}
intcount = 0;
while (!q.isEmpty()) {
intcurr = q.poll();
if (graph[curr] == null) {
count++;
continue;
}
for (intnext: graph[curr]) {
indegree[next]--;
if (indegree[next] == 0) q.add(next);
}
count++;
}
returncount == numCourses;
}
publicbooleancanFinish4(intnumCourses, int[][] prerequisites) {
Set<Integer>[] graph = newSet[numCourses];
for (int[] link: prerequisites) {
if (graph[link[1]] == null) graph[link[1]] = newHashSet<Integer>();
graph[link[1]].add(link[0]);
}
return !isCyclic(graph, numCourses);
}
privatebooleanisCyclic(Set<Integer>[] graph, intnumCourses) {
boolean[] visited = newboolean[numCourses];
boolean[] trace = newboolean[numCourses];
for (inti=0; i<numCourses; i++) {
if (isCyclic(graph, i, visited, trace)) returntrue;
}
returnfalse;
}
privatebooleanisCyclic(Set<Integer>[] graph, intstart, boolean[] visited, boolean[] trace) {
if (trace[start]) returntrue;
if (visited[start]) returnfalse;
visited[start] = true;
if (graph[start] == null) returnfalse;
trace[start] = true;
for (intnext: graph[start]) {
if (isCyclic(graph, next, visited, trace)) returntrue;
}
trace[start] = false;
returnfalse;
}
}