forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
207_course.py
86 lines (74 loc) · 2.69 KB
/
207_course.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
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 15:31:00 2020
@author: leiya
"""
'''
Caution:用bfs判断有向图是否有环--拓扑排序
0709
1.邻接表存的是每个node的出node
2.入度表存的是每个node的母node的个数
3.每个node都会从queue中弹出一次
4.无向图的邻接表和有向图的不一样,无向图必须找到所有与他相连的
'''
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
indegree = [0 for _ in range(numCourses)]
adajancy = [[] for _ in range(numCourses)]
for node1,node2 in prerequisites:
adajancy[node2].append(node1)
indegree[node1] += 1
queue = []
for node in range(len(indegree)):
if indegree[node] == 0:
queue.append(node)
while queue:
pop_node = queue.pop(0)
numCourses -= 1
for next_node in adajancy[pop_node]:
indegree[next_node] -= 1
if indegree[next_node] == 0:
queue.append(next_node)
return numCourses == 0
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
indegrees = [0 for _ in range(numCourses)]
adjacency = [[] for _ in range(numCourses)]
for node,node1 in prerequisites:
indegrees[node] += 1
#有向图邻接表,每个node的出边
adjacency[node1].append(node)
queue = []
for i in range(len(indegrees)):
if indegrees[i] == 0:
queue.append(i)
while queue:
pop_node = queue.pop(0)
numCourses -= 1
for next_node in adjacency[pop_node]:
indegrees[next_node] -= 1
if indegrees[next_node] == 0:
queue.append(next_node)
if numCourses == 0:
return True
else:
return False
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
indegrees = [0 for _ in range(numCourses)]
adjacency = [[] for _ in range(numCourses)]
for cur, pre in prerequisites:
indegrees[cur] += 1
adjacency[pre].append(cur)
queue = []
for i in range(len(indegrees)):
if not indegrees[i]:
queue.append(i)
while queue:
pop_node = queue.pop(0)
numCourses -= 1
for node in adjacency[pop_node]:
indegrees[node] -= 1
if indegrees[node] == 0:
queue.append(node)
return not numCourses