-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCourse Schedule Using matrix.py
55 lines (48 loc) · 1.84 KB
/
Course Schedule Using matrix.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
# Course Schedule
# https://leetcode.com/problems/course-schedule/description/
class Solution(object):
def __init__(self):
self.graph = []
def createGraph(self, numCourses, preReq):
self.graph = [[0 for col in range(numCourses)] for row in range(numCourses)]
for c in preReq:
# there is an edge from c[1] to c[0]
self.graph[c[1]][c[0]] = 1
def display(self):
for row in range(len(self.graph)):
for col in range(len(self.graph[0])):
print self.graph[row][col],
print ""
print ""
def topologicalSort(self, node, visited, stack):
if (node in visited):
return False
# mark the node visited
visited.append(node)
# get all children of node
for child in range(len(self.graph)):
# we wanna make sure child is not in stack, it means it's already been explored
# but if the child is not in stack but in visited that means there's a cycle
if (self.graph[node][child] == 1 and (child not in stack)):
if not(self.topologicalSort(child, visited, stack)):
return False
# when all children of the current node are explored
stack.append(node)
return True
def canFinish(self, numCourses, prerequisites):
# create graph
self.createGraph(numCourses, prerequisites)
#self.display()
visited = []
outputStack = []
for course in range(numCourses):
if (course in outputStack):
continue
if not (self.topologicalSort(course, visited, outputStack)):
return False
return True
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""