-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseSchedule.java
More file actions
654 lines (492 loc) · 21.4 KB
/
CourseSchedule.java
File metadata and controls
654 lines (492 loc) · 21.4 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package Algorithms.Graphs;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 14 Feb 2025
* @link 207. Course Schedule <a href="https://leetcode.com/problems/course-schedule/">LeetCode Link</a>
* @topics Graph, Topological Sort, DFS, BFS
* @companies Meta(12), Google(8), Amazon(6), Roblox(5), Microsoft(3), TikTok(3), Uber(3), LinkedIn(2), Apple(2), Oracle(2), Bloomberg(5), ByteDance(2), Tesla(2), Nvidia(2), Anduril(2), Coupang(8), Snowflake(5), Snap(5), Adobe(4), Nordstrom(4), Visa(4), Flipkart(3), Swiggy(3), eBay(3), DoorDash(3)
<pre>
DESCRIPTION:
Given an integer numCourses and a list of prerequisite pairs, where prerequisites[i] = [ai, bi] indicates that you must take course ai first if you want to take course "bi".
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
This problem statement resembles more of the
- DAG (Directed Acyclic Graph) and
- Khan's Algorithm using Topological sort problem ✅ and
- it's not related to Disjoint Sets Union Find (DSU)❌
- cause in DSU
* a node can have many children (but it only has one parent)
* DAGs are not possible - multiple nodes can have same children
PRE-REQUISITE KNOWLEDGE:
1. Directed Acyclic Graphs (DAGs) 🔥
2. Topological sort 🔥
1. Directed Acyclic Graph (DAG)
-------------------------------
To complete all courses, the graph has to be non-cyclic -> check the Directed Acyclic Graph DAG
Example 1:
[[1,2],[2,3],[3,4],[4,1]] -> canFinish = false ---> Directed Cyclic Graph
1 --> 2
↑ ↓
4 <-- 3
Example 2:
[[1,2],[2,3],[3,4],[1,4]] -> canFinish = true ---> Directed Acyclic Graph (DAG)
1 --> 2
↓ ↓
4 <-- 3
Example 3:
[[0,1],[0,2],[1,2]] -> canFinish = true ---> Directed Acyclic Graph (DAG)
0 --> 1
| |
-> 2 <-
Example 4:
[[1,0],[2,6],[1,7],[6,4],[7,0],[0,5]] -> canFinish = true ---> Directed Acyclic Graph (DAG)
1 --> 0 --> 5
↓ ↑
7 -----
2 --> 6 --> 4
here in Example 4 DAG, we are traversing 1-0-5 and 1-7-0-5 ---> i.e we are traversing 0-5 path multiple times
2. Topological sort
-------------------
Topological sort is only possible if the graph has no cycles --- only one direction
It means one or more things should be done before the current thing / or one or more things are dependent on the current thing
Examples:
1) Dressing up for party
2) Course Schedule with prerequisites
3) Even scheduling
4) Assembly instructions
5) Program build dependencies
6) Spring beans initialization -> @DependsOn
7) Detect circular dependencies
SHIRT 👕 ------------------------> HOODIE 🦸 -------
|
|
INNER 🩳 --------> PANTS 👖---- --------> DRIVE 🚗 -----> PARTY 🎊
| |
↓ |
SHOES 🥾-----------------
↑
|
SOCKS 🧦 ----------------------
or
SOCKS 🧦 ---> SHIRT 👕 ---> HOODIE 🦸 ---> INNER 🩳 ---> PANTS 👖 ---> SHOES 🥾 ---> DRIVE 🚗 ---> PARTY 🎊
or
INNER 🩳 ---> PANTS 👖 ---> SHIRT 👕 ---> HOODIE 🦸 ---> SOCKS 🧦 ---> SHOES 🥾 ---> DRIVE 🚗 ---> PARTY 🎊
NOTE:
1) Topological Ordering ---> A valid linear order (straight line) of nodes in a Directed Acyclic Graphs (DAGs) where all dependencies are respected
2) Topological Sort ---> The process or algorithm used to compute that ordering
3) Pre-requisites ---> Parent nodes ---> dependencies
3) InDegree (Number of incoming edges) ---> how dependencies/prerequisites/parents does the current node have, Ex: inDegree[to]++ --→ "to" has one more prerequisite InDegree
4) OutDegree (Number of outgoing edges) ---> for how nodes does the current node is dependency/prerequisite/parent, outDegree[from]++ --→ "from" is a prerequisite for one more nodeOutDegree
3) We need Directed Acyclic Graphs (DAGs) ---> is a finite graph that contains directed edges and no cycles
4) Topological ordering can be done in BFS or DFS
5) Topological ordering is not unique
6) Any straight line order (Topological ordering) works as long as it doesn't disturb the dependencies
7) Just traverse from a node to it's all adjacent nodes and the other left out nodes till all nodes are traversed or until there is a cycle
8) Degree of a node is the number of incoming edges
9) So, it's better to start traversal from these inDegree of 0 nodes first (maintain a inDegree array int[])
So, a topological sort is an ordering of the nodes in a directed graph where for each directed edge from nodeU to nodeV u → v, u comes before v in the ordering.
🔥 Khan's algorithm 🔥 is a simple topological sort algorithm that can find a topological ordering in O(V + E) time.
</pre>
*/
public class CourseSchedule {
public static void main(String[] args) {
int numCourses;
int[][] prerequisites;
numCourses = 5;
prerequisites = new int[][]{{1,3},{0,1},{2,0},{1,2}};
// numCourses = 20;
// prerequisites = new int[][]{{0,10},{3,18},{5,5},{6,11},{11,14},{13,1},{15,1},{17,4}};
System.out.println("canFinish using BFS - Using Kahn's Algorithm Topological Sort => " + canFinishUsingBfsKahnsAlgorithmTopologicalSort(numCourses, prerequisites));
System.out.println("canFinish using DFS => " + canFinishUsingDfs1(numCourses, prerequisites));
System.out.println("canFinish using Graph Class => " + canFinishUsingDfsWithGraphClass(numCourses, prerequisites) );
}
/**
* @TimeComplexity O(u + v), where u = numCourses / nodes and v = children list / vertices
* @SpaceComplexity O(u + v)
Here
preReqs = [[1,3], [0,1], [2,0], [1,2]]
numCourses = 5; ---> preReqs didn't mention any pre-reqs of 4, but we still have this node in the graph - separate graph
[1,3] means - to complete course 1, we need to complete course 3 first
so, 3 is parent of 1
=============================== DECISION TREE ===============================
INITIALLY
3 4 (no pre-requisites)
↓
1
/ ↖
↓ |
0 -→ 2
inDegree = incoming arrows = how dependencies/prerequisites/parents does the current node have
0 1 2 3 4
inDegree = [1, 2, 1, 0, 0] ---> i.e., 3 and 4 have inDegree=0
queue = [3, 4]; 0 1 2 3 4
int currNode = 3; then queue=[4,1]; inDegree = [1, 1, 1, 0, 0]
3 4
1
/ ↖
↓ |
0 -→ 2
queue = [4, 1]; 0 1 2 3 4
int currNode = 4; then queue=[]; inDegree = [1, 1, 1, 0, 0]
3 4
1
/ ↖
↓ |
0 -→ 2
queue = []; ❌ ---> stop iteration
nodesVisited ≠ numCourses --> 2 ≠ 5
in-degree means = number of incoming arrows
[[0,1],[2,1],[1,3]]
3
↓
0 --> 1
↓
2
This topological representation looks like the opposite of normal graph DFS
Cause in topological representation, first complete the preReq[1] and move to preReq[0]
*/
public static boolean canFinishUsingBfsKahnsAlgorithmTopologicalSort(int numCourses, int[][] prerequisites) {
int[] inDegree = new int[numCourses]; // by default all nodes have inDegree=0
List<List<Integer>> adj = new ArrayList<>(numCourses);
// create the adjacency list for all nodes -- no children yet
for (int i = 0; i < numCourses; i++) {
adj.add(new ArrayList<>());
}
// prepare the adjacency list with children
for (int[] prereq : prerequisites) {
adj.get(prereq[1]).add(prereq[0]);
inDegree[prereq[0]]++;
// prereq[0] has one more dependency/prereq/parent --->
// [0,1] means to complete course 0, we need to complete course 1 first. So, 1 is parent of 0;
// i.e., first complete 1 and then move to 0 ---> multiple courses are dependent on 1
// inDegree = incoming arrows = how dependencies/prerequisites/parents do the current node have
/*
// or
adj.get(prereq[0]).add(prereq[1]);
inDegree[prereq[1]]++;
*/
}
Queue<Integer> queue = new LinkedList<>(); // for BFS
// Push all the nodes with indegree zero in the queue.
for (int i = 0; i < numCourses; i++) {
if (inDegree[i] == 0) {
queue.offer(i); // i.e start traversal from inDegree=0 nodes i.e., no pre-requisites
}
}
int nodesVisited = 0;
while (!queue.isEmpty()) {
int node = queue.poll();
nodesVisited++;
for (int neighbor : adj.get(node)) {
inDegree[neighbor]--; // currNode's preReq dependency is satisfied in currNeighbor; So, Delete the edge "curr node -> curr neighbor" ---> decrease preReqs of neighbour
if (inDegree[neighbor] == 0) { // is curr neighbor has no preReqs anymore?
queue.offer(neighbor);
}
}
}
return nodesVisited == numCourses; // is no cycle?
}
/**
* @TimeComplexity O(u + v), where u = numCourses / nodes and v = children list / vertices
* @SpaceComplexity O(u + v)
* Solve by focusing on Directed Acyclic Graphs (DAGs)
*/
static boolean[] seenPath;
static List<Integer>[] al; // adjacencyListGraph
static Boolean[] completed;
public static boolean canFinishUsingDfs1(int numCourses, int[][] prerequisites) {
al = new List[numCourses];
for (int i=0; i<numCourses; i++) al[i] = new ArrayList<>();
seenPath = new boolean[numCourses];
completed = new Boolean[numCourses];
// 1. PREPARE GRAPH
for(int[] pair: prerequisites) {
int u = pair[0];
int v = pair[1];
al[u].add(v);
}
// 2. DFS
for (int i=0; i<numCourses; i++) {
if (isCyclic(i)) return false;
}
return true;
}
private static boolean isCyclic(int course) {
if (completed[course] != null) return completed[course];
for (int nei: al[course]) {
if (seenPath[nei]) return completed[course] = true;
seenPath[nei]=true;
if (isCyclic(nei)) return completed[course] = true;
seenPath[nei]=false;
}
return completed[course] = false;
}
private static boolean isCyclic2(int course) { // here instead of marking seenPath[nei], we mark seenPath[course]
if (completed[course] != null) return completed[course];
seenPath[course] = true;
for (int nei : al[course]) {
if (seenPath[nei]) return completed[course] = true;
if (isCyclic2(nei)) return completed[course] = true;
}
seenPath[course] = false;
return completed[course] = false;
}
static int[] state;
/**
state
0 = unvisited
1 = visiting (in current DFS path)
2 = visited (fully processed, no cycle
same as {@link #canFinishUsingDfs1} with {@link #isCyclic2}
*/
public static boolean canFinishUsingDfs2(int numCourses, int[][] prerequisites) {
// 1. Build graph
al = new List[numCourses];
for (int i = 0; i < numCourses; i++) {
al[i] = new ArrayList<>();
}
for (int[] p : prerequisites) {
int course = p[0];
int prereq = p[1];
al[course].add(prereq);
}
// 2. Initialize state array
state = new int[numCourses];
// 3. Run DFS from every node
for (int i = 0; i < numCourses; i++) {
if (state[i] == 0) {
if (dfs(i)) return false; // cycle detected
}
}
return true;
}
/**
* same as {@link #isCyclic2}
*/
private static boolean dfs(int node) { // isCycle
if (state[node] == 1) return true; // If node is already in current DFS path → cycle
if (state[node] == 2) return false; // If node is already fully processed → no cycle from here
state[node] = 1; // Mark as visiting
for (int nei : al[node]) { // Explore neighbors
if (dfs(nei)) return true;
}
state[node] = 2; // Mark as fully processed
return false;
}
/**
* @TimeComplexity O(u + v), where u = numCourses / nodes and v = children list / vertices
* @SpaceComplexity O(u + v)
*/
public static boolean canFinishUsingDfs3(int numCourses, int[][] prerequisites) {
List<List<Integer>> adjList = new ArrayList<>(numCourses);
for (int i = 0; i < numCourses; i++) { // all nodes from 0 to numCourses i.e inDegree=0 nodes don't have any prereqs
adjList.add(new ArrayList<>());
}
for (int[] prerequisite : prerequisites) {
adjList.get(prerequisite[1]).add(prerequisite[0]);
}
boolean[] visit = new boolean[numCourses];
boolean[] inStack = new boolean[numCourses];
for (int i = 0; i < numCourses; i++) {
if (dfs(i, adjList, visit, inStack)) { // isCyclic?
return false;
}
}
return true;
}
public static boolean dfs(int node, List<List<Integer>> adj, boolean[] visit, boolean[] inStack) {
// If the node is already in the stack, we have a cycle.
if (inStack[node]) {
return true;
}
if (visit[node]) {
return false;
}
// Mark the current node as visited and part of current recursion stack.
visit[node] = true;
inStack[node] = true;
for (int neighbor : adj.get(node)) {
if (dfs(neighbor, adj, visit, inStack)) {
return true;
}
}
// Remove the node from the stack.
inStack[node] = false;
return false;
}
public static boolean canFinishUsingDfs4(int numCourses, int[][] prerequisites) {
Map<Integer, List<Integer>> graph = new HashMap<>(); // prepare graph using adjacencyList List[] or Map<node, List<childNode>>
for(int[] pre: prerequisites) {
graph.computeIfAbsent(pre[0], k-> new ArrayList<>()).add(pre[1]);
}
Set<Integer> detectCycle = new HashSet<>(); // no need to pass brand new "new HashSet<>()" visited in each for loop iteration because, we are resetting the seen nodes every time i.e., mark before the dfs() and unmark after the dfs()
for (Integer c: graph.keySet()) {
if (!dfs(c, detectCycle, graph)) return false;
}
return true;
}
private static boolean dfs(int c, Set<Integer> visited, Map<Integer, List<Integer>> graph) {
if (visited.contains(c)) return false;
else if (!graph.containsKey(c) || graph.get(c) == null) return true; // noChildren || completed
visited.add(c);
for (int preReq: graph.get(c)) {
if (!dfs(preReq, visited, graph)) return false;
}
visited.remove(c);
graph.put(c, null); // mark the node as "completed" ---- NODE: if you do graph.remove(c), you'll get ConcurrentModificationException. So, remove the children nodes List instead
return true;
}
public boolean canFinishUsingDfs5(int numCourses, int[][] prerequisites) {
Map<Integer, List<Integer>> graph = new HashMap<>(); // prepare graph using adjacency list int[] or hashMap
for(int[] pre: prerequisites) {
graph.computeIfAbsent(pre[0], k-> new ArrayList<>()).add(pre[1]);
}
Boolean[] finished = new Boolean[numCourses];
for(int course=0; course<numCourses; course++) {
if(!dfs(course, graph, new HashSet<>(), finished)) {
return false;
}
}
return true;
}
private boolean dfs(int course, Map<Integer, List<Integer>> graph, Set<Integer> seen, Boolean[] finished) {
if (finished[course] != null) {
return finished[course];
} else if(!graph.containsKey(course)) { // we can complete it without any issue
return true;
} else if(!seen.add(course)) {
return false;
}
boolean isFinished = true;
for (int child: graph.get(course)) {
if(!dfs(child, graph, seen, finished)) {
isFinished = false;
break;
}
}
return finished[course] = isFinished;
}
static class Node {
int vertice;
Node next;
Node(int vertice, Node next) {
this.vertice = vertice;
this.next = next;
}
}
static class Graph {
Node[] nodes;
Graph(int n, int[][] edges) {
nodes = new Node[n];
for (int[] edge : edges) {
int fromVertice = edge[0];
int toVertice = edge[1];
nodes[fromVertice] = new Node(toVertice, nodes[fromVertice]);
}
}
}
private static Graph graph;
private static boolean[] checkedCourses;
private static boolean[] reachedCourses;
public static boolean canFinishUsingDfsWithGraphClass(int numCourses, int[][] prerequisites) {
graph = new Graph(numCourses, prerequisites);
checkedCourses = new boolean[numCourses];
reachedCourses = new boolean[numCourses];
for (int i = 0; i < numCourses; i++) {
if (hasCycle(i)) {
return false;
}
}
return true;
}
private static boolean hasCycle(int course) {
if (reachedCourses[course]) {
return true;
} else if (checkedCourses[course]) {
return false;
}
reachedCourses[course] = true;
checkedCourses[course] = true;
Node node = graph.nodes[course];
while (node != null) {
if (hasCycle(node.vertice)) {
return true;
}
node = node.next;
}
reachedCourses[course] = false;
return false;
}
/**
* Working but TLE
*/
public static boolean canFinishUsingBruteForce(int numCourses, int[][] prerequisites) {
Map<Integer, Set<Integer>> map = new HashMap<>(); // [c, pre-reqs]
for (int[] cpr: prerequisites) { // course pre-req
int c = cpr[0]; // course
int pr = cpr[1]; // current pre-req
if (c == pr || isRelatedUsingCsParents(c, pr, map)) return false;
if (map.containsKey(c)) {
map.get(c).add(pr); // add pre-reqs
} else {
map.put(c, new HashSet<>(Arrays.asList(pr) ) );
}
}
// System.out.println(map);
return true;
}
// {0=[2], 1=[0], 2=[1]} --> c=2, pr=1 | DFS the related courses
private static boolean isRelatedUsingCsParents(int c, int pr, Map<Integer, Set<Integer>> map) {
boolean res = false;
for (Map.Entry<Integer, Set<Integer>> e: map.entrySet()) {
if (e.getValue().contains(c)) { // is this c is pre-req of another c?
if (e.getKey()==pr) res = true; // if c-parent == pr?
else res = isRelatedUsingPrPrs(e.getKey(), pr, map); // check c-parent is pre-req of any other c?
}
if(res) break;
}
return res;
}
private static boolean isRelatedUsingPrPrs(int c, int pr, Map<Integer, Set<Integer>> map) {
// System.out.printf("c:%s, pr:%s\n", c, pr);
boolean res = false;
// does pr have any pre-reqs?
if (map.containsKey(pr)) {
res = map.get(pr).contains(c);
if (res) return true;
else {
for (int i: map.get(pr)) {
res = isRelatedUsingPrPrs(c, i, map);
if (res) break;
}
}
}
return res;
}
/**
It'll fail for Directed Acyclic Graphs DAGs
*/
public boolean canFinishUsingUfNotWorking(int numCourses, int[][] prerequisites) {
int[] parent = new int[numCourses];
for (int i=0; i<numCourses; i++) parent[i]=i;
for (int[] pair: prerequisites) {
int u = pair[0];
int v = pair[1];
if (!canUnion(u,v,parent)) return false;
}
return true;
}
private boolean canUnion(int u, int v, int[] parent) {
int pu = find(u, parent);
int pv = find(v, parent);
if (pu == pv) return false;
parent[pu] = pv;
return true;
}
private int find(int x, int[] parent) {
while (parent[x] != x) {
x = find(parent[x], parent);
}
return x;
}
}