-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkCycle.py
52 lines (37 loc) · 1.37 KB
/
checkCycle.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
'''
There are a total of numCourses courses you have to take, labeled from 0 to numCourses-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?
'''
from collections import defaultdict
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
graph = defaultdict(list)
indegree = defaultdict()
for i in range(numCourses):
indegree[i]=0
graph[i]=[]
for edge in prerequisites:
graph[edge[1]].append(edge[0])
indegree[edge[0]]+=1
visited = [False]*numCourses
queue = []
print(graph)
for i in range(numCourses):
if indegree[i] == 0:
queue.append(i)
cnt = 0
while queue:
u = queue.pop(0)
print(u)
for i in graph[u]:
indegree[i] -= 1
# If in-degree becomes zero, add it to queue
if indegree[i] == 0:
queue.append(i)
cnt += 1
print(cnt)
if cnt != numCourses:
return False
else:
return True