-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathcourse_schedule.py
More file actions
53 lines (43 loc) · 1.42 KB
/
course_schedule.py
File metadata and controls
53 lines (43 loc) · 1.42 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
# coding: utf-8
"""
https://leetcode.com/problems/course-schedule/
"""
from collections import defaultdict
from typing import List
class Graph:
def __init__(self):
self.vertices = set()
self.outgoing_edges = defaultdict(dict)
def add_edge(self, u, v):
self.outgoing_edges[u][v] = 1
self.vertices.add(u)
self.vertices.add(v)
def has_cycles(self):
global_visited = set()
def has_cycles_dfs(v, visited):
if v in global_visited:
return False
global_visited.add(v)
visited.add(v)
for des in self.outgoing_edges[v].keys():
# If there is any visited vertex in the call stack, the graph has cycles.
if des in visited:
return True
else:
if has_cycles_dfs(des, visited):
return True
visited.remove(v)
return False
for v in self.vertices:
visited = {v, }
if has_cycles_dfs(v, visited):
return True
return False
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
# If there is any cycle in the graph,
# it's impossible to finish all courses.
graph = Graph()
for u, v in prerequisites:
graph.add_edge(v, u)
return not graph.has_cycles()