forked from Adam-Jimenez/binarysearch-editorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourse Scheduling.py
More file actions
29 lines (25 loc) · 1.01 KB
/
Course Scheduling.py
File metadata and controls
29 lines (25 loc) · 1.01 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
"""
Course Scheduling
This problem is about checking if topological sorting is possible, which equates to checking if the graph is a DAG (Directed Acyclic Graph).
To check whether our graph has cycles, we can simply do a depth-first search on every node and check if we ever return on a visited value.
When we completed the depth-first search on a node, we never need to check it again, hence the checked array.
Relevant reading: https://en.wikipedia.org/wiki/Topological_sorting
"""
class Solution:
def solve(self, matrix):
visited=[False for _ in matrix]
checked=[False for _ in matrix]
def dfs(i):
if visited[i]: return False
if checked[i]: return True
visited[i]=True
for j in matrix[i]:
if not dfs(j):
return False
visited[i]=False
checked[i]=True
return True
for i in range(len(matrix)):
if not dfs(i):
return False
return True