-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcourse-schedule.py
828 lines (723 loc) · 26.8 KB
/
course-schedule.py
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
"""
207. Course Schedule
Medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.
Example 1:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: 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.
Constraints:
1 <= numCourses <= 105
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
All the pairs prerequisites[i] are unique.
"""
# V0
# IDEA : DFS, LC Course Schedule II
from collections import defaultdict
class Solution(object):
def canFinish(self, numCourses, prerequisites):
def dfs(res, graph, visited, x):
# 0 : not visited
# 1 : visiting
# 2 : visited
if visited[x] == 1:
return False
# NOTE this !!!!
if visited[x] == 2:
return True
visited[x] = 1
for k in graph[x]:
# NOTE THIS !!!! -> use dfs, instead of "not visited[k]"
#if not visited[k]:
if not dfs(res, graph, visited, k):
#dfs(graph, visited, k)
return False
# NOTE this !!!!
visited[x] = 2
res.insert(0, x)
return True
# edge case
if not prerequisites:
return True
# build graph
graph = defaultdict(list)
for p in prerequisites:
graph[p[0]].append(p[1])
# dfs
visited = [0] * numCourses
res = []
for c in range(numCourses-1):
# NOTE this !!!!
if not dfs(res, graph, visited, c):
return False
return len(res) > 0
# V0'
# IDEA : DFS
# https://github.com/neetcode-gh/leetcode/blob/main/python/0207-course-schedule.py
# https://www.youtube.com/watch?v=EgI5nU9etnU
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
# dfs
preMap = {i: [] for i in range(numCourses)}
# map each course to : prereq list
for crs, pre in prerequisites:
preMap[crs].append(pre)
visiting = set()
def dfs(crs):
if crs in visiting:
return False
if preMap[crs] == []:
return True
visiting.add(crs)
for pre in preMap[crs]:
if not dfs(pre):
return False
visiting.remove(crs)
preMap[crs] = []
return True
for c in range(numCourses):
if not dfs(c):
return False
return True
# V0
# IDEA : DFS, LC Course Schedule II
from collections import defaultdict
class Solution(object):
def canFinish(self, numCourses, prerequisites):
# edge case
if not prerequisites:
return [x for x in range(numCourses)]
# help func : dfs
# 3 cases : 0 : unknown, 1 :visiting, 2 : visited
def dfs(idx, visited, g, res):
if visited[idx] == 1:
return False
# NOTE !!! if visited[idx] == 2, means already visited, return True directly (and check next idx in range(numCourses))
if visited[idx] == 2:
return True
visited[idx] = 1
"""
NOTE this !!!
1) for j in g[idx] (but not for i in range(numCourses))
2) go through idx in g[idx]
"""
for j in g[idx]:
if not dfs(j, visited, g, res):
return False
"""
don't forget to make idx as visited (visited[idx] = 2)
"""
visited[idx] = 2
"""
NOTE : the main difference between LC 207, 210
-> we append idx to res (our ans)
"""
res.append(idx)
return True
# init
visited = [0] * numCourses
# build grath
g = defaultdict(list)
for p in prerequisites:
g[p[0]].append(p[1])
res = []
"""
NOTE : go through idx in numCourses (for idx in range(numCourses))
"""
for idx in range(numCourses):
if not dfs(idx, visited, g, res):
return False #[]
return len(res) > 0
# V0'
# IDEA : DFS
from collections import defaultdict
class Solution(object):
def canFinish(self, numCourses, prerequisites):
def check(course):
# 0 : not visited yet
# 1 : visiting
# 2 : visited
"""
NOTE : we DON'T need below :
#if visited[course] == 2:
"""
if visited[course] == 1:
return False
if visited[course] == 2:
return True
visited[course] = 1
for c in g[course]:
"""
NOTE !!! :
we DON'T need below :
# if visited[c] != 2:
# visited[c] = 2
# check(c)
"""
if not check(c):
return False
visited[course] = 2
return True
# edge case
if not prerequisites or len(prerequisites) == 1:
return True
# build graph
g = defaultdict(list)
for a, b in prerequisites:
g[a].append(b)
"""
NOTE !!! we DON'T need below :
# g[b].append(a)
"""
"""
NOTE we init visited, courses as below
"""
visited = [0 for _ in range(numCourses)]
courses = [_ for _ in range(numCourses)]
for course in courses:
if not check(course):
return False
return True
# V0'
# IDEA : DFS
from collections import defaultdict
class Solution(object):
def canFinish(self, numCourses, prerequisites):
# edge case
if not prerequisites:
return True
# help func : dfs
# 3 cases : 0 : unknown, 1 :visiting, 2 : visited
def dfs(idx, visited, g):
if visited[idx] == 1:
return False
# NOTE !!! if visited[idx] == 2, means already visited, return True directly (and check next idx in range(numCourses))
if visited[idx] == 2:
return True
visited[idx] = 1
"""
NOTE this !!!
1) for j in g[idx] (but not for i in range(numCourses))
2) go through idx in g[idx]
"""
for j in g[idx]:
if not dfs(j, visited, g):
return False
"""
don't forget to make idx as visited (visited[idx] = 2)
"""
visited[idx] = 2
return True
# init
visited = [0] * numCourses
# build grath
g = defaultdict(list)
for p in prerequisites:
g[p[0]].append(p[1])
#print ("g = " + str(p))
# dfs
"""
NOTE : go through idx in numCourses (for idx in range(numCourses))
"""
for idx in range(numCourses):
if not dfs(idx, visited, g):
return False
return True
# V0''
# IDEA : Backtracking
# https://leetcode.com/problems/course-schedule/solution/
# IDEA : -> check : if the corresponding graph is a DAG (Directed Acyclic Graph), i.e. there is no cycle existed in the graph.
class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
from collections import defaultdict
courseDict = defaultdict(list)
for relation in prerequisites:
nextCourse, prevCourse = relation[0], relation[1]
courseDict[prevCourse].append(nextCourse)
path = [False] * numCourses
for currCourse in range(numCourses):
if self.isCyclic(currCourse, courseDict, path):
return False
return True
def isCyclic(self, currCourse, courseDict, path):
"""
backtracking method to check that no cycle would be formed starting from currCourse
"""
if path[currCourse]:
# come across a previously visited node, i.e. detect the cycle
return True
# before backtracking, mark the node in the path
path[currCourse] = True
# backtracking
ret = False
for child in courseDict[currCourse]:
ret = self.isCyclic(child, courseDict, path)
if ret: break
# after backtracking, remove the node from the path
path[currCourse] = False
return ret
# V0''''
# IDEA : DFS + topological sort
import collections
class Solution:
def canFinish(self, numCourses, prerequisites):
_graph = collections.defaultdict(list)
for i in range(len(prerequisites)):
_graph[prerequisites[i][0]].append(prerequisites[i][1])
visited = [0] * numCourses
for i in range(numCourses):
if not self.dfs(_graph, visited, i):
return False
return True
# 0 : unknown, 1 :visiting, 2 : visited
def dfs(self, _graph, visited, i):
if visited[i] == 1:
return False
if visited[i] == 2:
return True
visited[i] = 1
for item in _graph[i]:
if not self.dfs(_graph, visited, item):
return False
visited[i] = 2
return True
# V0''''''
# IDEA : BFS + topological sort
from collections import defaultdict, deque
class Solution:
def canFinish(self, numCourses, prerequisites):
degree = defaultdict(int)
graph = defaultdict(set)
q = deque()
# init the courses with 0 deg
for i in range(numCourses):
degree[i] = 0
# add 1 to degree of course that needs prereq
# build edge from prerequisite to child course (directed graph)
for pair in prerequisites:
degree[pair[0]] += 1
graph[pair[1]].add(pair[0])
# start bfs queue with all classes that dont have a prerequisite
for key, val in degree.items():
if val == 0:
q.append(key)
stack = []
while q:
curr = q.popleft()
stack.append(curr)
for child in graph[curr]:
degree[child] -= 1
if degree[child] == 0:
q.append(child)
return len(stack) == numCourses
# V0''''''''
# IDEA : DFS + topological sort
import collections
class Solution(object):
def canFinish(self, N, prerequisites):
graph = collections.defaultdict(list)
for u, v in prerequisites:
graph[u].append(v)
# 0 = Unknown, 1 = visiting, 2 = visited
visited = [0] * N
for i in range(N):
if not self.dfs(graph, visited, i):
return False
return True
# Can we add node i to visited successfully?
def dfs(self, graph, visited, i):
# 0 = Unknown, 1 = visiting, 2 = visited
if visited[i] == 1: return False
if visited[i] == 2: return True
visited[i] = 1
for j in graph[i]:
if not self.dfs(graph, visited, j):
return False
visited[i] = 2
return True
# V0''''''' (AGAIN!)
# IDEA : BFS + topological sort
class Solution(object):
def canFinish(self, N, prerequisites):
graph = collections.defaultdict(list)
indegrees = collections.defaultdict(int)
for u, v in prerequisites:
graph[v].append(u)
indegrees[u] += 1
for i in range(N):
zeroDegree = False
for j in range(N):
if indegrees[j] == 0:
zeroDegree = True
break
if not zeroDegree: return False
indegrees[j] = -1
for node in graph[j]:
indegrees[node] -= 1
return True
# V1
# IDEA : BFS + topological sort
# https://leetcode.com/problems/course-schedule/discuss/811500/Python-Intuitive-Solution
from collections import defaultdict, deque
class Solution:
def canFinish(self, numCourses, prerequisites):
degree = defaultdict(int)
graph = defaultdict(set)
q = deque()
# init the courses with 0 deg
for i in range(numCourses):
degree[i] = 0
# add 1 to degree of course that needs prereq
# build edge from prerequisite to child course (directed graph)
for pair in prerequisites:
degree[pair[0]] += 1
graph[pair[1]].add(pair[0])
# start bfs queue with all classes that dont have a prerequisite
for key, val in degree.items():
if val == 0:
q.append(key)
stack = []
while q:
curr = q.popleft()
stack.append(curr)
for child in graph[curr]:
degree[child] -= 1
if degree[child] == 0:
q.append(child)
return len(stack) == numCourses
# V1
# IDEA : Backtracking
# https://leetcode.com/problems/course-schedule/solution/
# IDEA : -> check : if the corresponding graph is a DAG (Directed Acyclic Graph), i.e. there is no cycle existed in the graph.
class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
from collections import defaultdict
courseDict = defaultdict(list)
for relation in prerequisites:
nextCourse, prevCourse = relation[0], relation[1]
courseDict[prevCourse].append(nextCourse)
path = [False] * numCourses
for currCourse in range(numCourses):
if self.isCyclic(currCourse, courseDict, path):
return False
return True
def isCyclic(self, currCourse, courseDict, path):
"""
backtracking method to check that no cycle would be formed starting from currCourse
"""
if path[currCourse]:
# come across a previously visited node, i.e. detect the cycle
return True
# before backtracking, mark the node in the path
path[currCourse] = True
# backtracking
ret = False
for child in courseDict[currCourse]:
ret = self.isCyclic(child, courseDict, path)
if ret: break
# after backtracking, remove the node from the path
path[currCourse] = False
return ret
# V1
# IDEA : DFS
# https://leetcode.com/problems/course-schedule/solution/
class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
from collections import defaultdict
courseDict = defaultdict(list)
for relation in prerequisites:
nextCourse, prevCourse = relation[0], relation[1]
courseDict[prevCourse].append(nextCourse)
checked = [False] * numCourses
path = [False] * numCourses
for currCourse in range(numCourses):
if self.isCyclic(currCourse, courseDict, checked, path):
return False
return True
def isCyclic(self, currCourse, courseDict, checked, path):
""" """
# 1). bottom-cases
if checked[currCourse]:
# this node has been checked, no cycle would be formed with this node.
return False
if path[currCourse]:
# came across a marked node in the path, cyclic !
return True
# 2). postorder DFS on the children nodes
# mark the node in the path
path[currCourse] = True
ret = False
# postorder DFS, to visit all its children first.
for child in courseDict[currCourse]:
ret = self.isCyclic(child, courseDict, checked, path)
if ret: break
# 3). after the visits of children, we come back to process the node itself
# remove the node from the path
path[currCourse] = False
# Now that we've visited the nodes in the downstream,
# we complete the check of this node.
checked[currCourse] = True
return ret
# V1
# IDEA : Topological Sort
# https://leetcode.com/problems/course-schedule/solution/
class GNode(object):
""" data structure represent a vertex in the graph."""
def __init__(self):
self.inDegrees = 0
self.outNodes = []
class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
from collections import defaultdict, deque
# key: index of node; value: GNode
graph = defaultdict(GNode)
totalDeps = 0
for relation in prerequisites:
nextCourse, prevCourse = relation[0], relation[1]
graph[prevCourse].outNodes.append(nextCourse)
graph[nextCourse].inDegrees += 1
totalDeps += 1
# we start from courses that have no prerequisites.
# we could use either set, stack or queue to keep track of courses with no dependence.
nodepCourses = deque()
for index, node in graph.items():
if node.inDegrees == 0:
nodepCourses.append(index)
removedEdges = 0
while nodepCourses:
# pop out course without dependency
course = nodepCourses.pop()
# remove its outgoing edges one by one
for nextCourse in graph[course].outNodes:
graph[nextCourse].inDegrees -= 1
removedEdges += 1
# while removing edges, we might discover new courses with prerequisites removed, i.e. new courses without prerequisites.
if graph[nextCourse].inDegrees == 0:
nodepCourses.append(nextCourse)
if removedEdges == totalDeps:
return True
else:
# if there are still some edges left, then there exist some cycles
# Due to the dead-lock (dependencies), we cannot remove the cyclic edges
return False
# V1''
# IDEA : BFS + topological sort
# https://leetcode.com/problems/course-schedule/discuss/1656939/python
from collections import defaultdict, deque
class Solution:
def canFinish(self, n, prereq):
G = [[] for _ in range(n)]
indeg = [0] * n
for v, u in prereq:
G[u].append(v)
indeg[v] += 1
q = [u for u, d in enumerate(indeg) if not d]
# since we only track sink nodes. we don't need track which nodes are visited or not because sink nodes are guaranteed not to be visited again
while q:
u = q.pop()
n -= 1
for v in G[u]:
indeg[v] -= 1
if not indeg[v]:
q.append(v)
return n == 0
# V1'''
# IDEA : dfs + topological sort
# https://leetcode.com/problems/course-schedule/discuss/1041737/Python-DFS
class Solution:
def canFinish(self, numCourses, prerequisites):
g = [[] for _ in range(numCourses)]
visit = [0]*numCourses
for post, pre in prerequisites:
g[post].append(pre)
def dfs(node):
if visit[node] == -1:
return False
if visit[node] == 1:
return True
visit[node] = -1
for neighbor in g[node]:
if not dfs(neighbor):
return False
visit[node] = 1
return True
for i in range(numCourses):
if not dfs(i):
return False
return True
# V1''''
# https://blog.csdn.net/fuxuemingzhu/article/details/82951771
# diagram explaination:
# https://leetcode.com/problems/course-schedule/discuss/658379/Python-by-DFS-and-cycle-detection-w-Graph
# IDEA : DFS + topological sort
class Solution(object):
def canFinish(self, N, prerequisites):
"""
:type N,: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
graph = collections.defaultdict(list)
for u, v in prerequisites:
graph[u].append(v)
# 0 = Unknown, 1 = visiting, 2 = visited
visited = [0] * N
for i in range(N):
if not self.dfs(graph, visited, i):
return False
return True
# Can we add node i to visited successfully?
def dfs(self, graph, visited, i):
# 0 = Unknown, 1 = visiting, 2 = visited
if visited[i] == 1: return False
if visited[i] == 2: return True
visited[i] = 1
for j in graph[i]:
if not self.dfs(graph, visited, j):
return False
visited[i] = 2
return True
# V1'''''
# IDEA : DFS + topological sort
# https://leetcode.com/problems/course-schedule/discuss/203028/Python-solution
# IDEA :
# We first build a directed graph from prerequisites. The nodes are 0 to n-1, and there is an edge from i to j if i is the prerequisite of j. Then the courses can be finished if and only if the directed graph can be topologically sorted (equivalently, if and only if the directed graph is acyclic).
# We start by labelling each node 0, meaning that they have not been dfs visited. Then we iterate i in range(numCourses), and if i has not been dfs visited, we dfs visit i. If any of such dfs visits return False, we return False; Else we return True. For the dfs visit procedure, we first label i to be 1, meaning that we are currently dfs visiting the descendants of i in the dfs tree. Then for each neighbor j of i, If j has label 1, then j is a predecessor of i in the dfs visit, and i -> j is a back edge, so the graph contains a cycle, we return False; Else if j has label 0, it has not been visited, and we need to do dfs(j). If dfs(j) returns False, it means that the dfs subgraph starting with j contains a cycle, and we need to return False. Finally, if no dfs(j) returns False, it means that the dfs subgraph starting with i is acyclic, we label i to be 2, meaning that we finished dfs searches all the descendants of i, and we return True.
# The time complexity is O(n+m), and the space complexity is O(n+m), where n = numCourses, and m = len(prerequisites).
class Solution(object):
def canFinish(self, numCourses, prerequisites):
def dfs(i):
color[i] = 1
if i in graph:
for j in graph[i]:
if color[j] == 0:
if not dfs(j):
return False
elif color[j] == 1:
return False
color[i] = 2
return True
graph = {}
for pair in prerequisites:
if pair[1] in graph:
graph[pair[1]].add(pair[0])
else:
graph[pair[1]] = set([pair[0]])
color = [0]*numCourses
for i in range(numCourses):
if color[i] == 0:
if not dfs(i):
return False
return True
# V1''''''
# https://www.jiuzhang.com/solution/course-schedule/#tag-highlight-lang-python
from collections import deque
class Solution:
# @param {int} numCourses a total of n courses
# @param {int[][]} prerequisites a list of prerequisite pairs
# @return {boolean} true if can finish all courses or false
def canFinish(self, numCourses, prerequisites):
# Write your code here
edges = {i: [] for i in range(numCourses)}
degrees = [0 for i in range(numCourses)]
for i, j in prerequisites:
edges[j].append(i)
degrees[i] += 1
queue, count = deque([]), 0
for i in range(numCourses):
if degrees[i] == 0:
queue.append(i)
while queue:
node = queue.popleft()
count += 1
for x in edges[node]:
degrees[x] -= 1
if degrees[x] == 0:
queue.append(x)
return count == numCourses
# V1''''''''
# https://blog.csdn.net/fuxuemingzhu/article/details/82951771
# IDEA : BFS + topological sort
class Solution(object):
def canFinish(self, N, prerequisites):
"""
:type N,: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
graph = collections.defaultdict(list)
indegrees = collections.defaultdict(int)
for u, v in prerequisites:
graph[v].append(u)
indegrees[u] += 1
for i in range(N):
zeroDegree = False
for j in range(N):
if indegrees[j] == 0:
zeroDegree = True
break
if not zeroDegree: return False
indegrees[j] = -1
for node in graph[j]:
indegrees[node] -= 1
return True
# V2
# Time: O(|V| + |E|)
# Space: O(|E|)
from collections import defaultdict, deque
class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
zero_in_degree_queue = deque()
in_degree, out_degree = defaultdict(set), defaultdict(set)
for i, j in prerequisites:
in_degree[i].add(j)
out_degree[j].add(i)
for i in xrange(numCourses):
if i not in in_degree:
zero_in_degree_queue.append(i)
while zero_in_degree_queue:
prerequisite = zero_in_degree_queue.popleft()
if prerequisite in out_degree:
for course in out_degree[prerequisite]:
in_degree[course].discard(prerequisite)
if not in_degree[course]:
zero_in_degree_queue.append(course)
del out_degree[prerequisite]
if out_degree:
return False
return True