leetcode207 Course Schedule

 1 """
 2 There are a total of n courses you have to take, labeled from 0 to n-1.
 3 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]
 4 Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
 5 Example 1:
 6 Input: 2, [[1,0]]
 7 Output: true
 8 Explanation: There are a total of 2 courses to take.
 9              To take course 1 you should have finished course 0. So it is possible.
10 Example 2:
11 Input: 2, [[1,0],[0,1]]
12 Output: false
13 Explanation: There are a total of 2 courses to take.
14              To take course 1 you should have finished course 0, and to take course 0 you should
15              also have finished course 1. So it is impossible.
16 """
17 """
18 此题是判断有向无环图
19 即拓扑排序
20 提供两种思路
21 第一种思路是维护一个indegrees数组
22 每次删除度数为0的结点,并将其指向结点度数减1
23 N次把所有结点取完 即返回True
24 """
25 class Solution1(object):
26     def canFinish(self, numCourses, prerequisites):
27         from collections import defaultdict
28         graph = defaultdict(list)    #defaultdict(<class 'list'>)
29         indegrees = defaultdict(int) #defaultdict(<class 'int'>)
30         for u, v in prerequisites:   #构建图{初始结点1:[目的结点1, 目的结点2, ...], ...}
31             graph[v].append(u)       #结点度数{结点1:度数, 结点2:度数, ...}
32             indegrees[u] += 1
33         N = numCourses
34         for i in range(N):           #每次循环将度为0的结点削掉,共N次
35             zeroDegree = False
36             for j in range(N):
37                 if indegrees[j] == 0: #找到度为0的结点
38                     zeroDegree = True
39                     break
40             if not zeroDegree:
41                 return False
42             indegrees[j] -= 1         #将度为0的结点减为-1
43             for node in graph[j]:     #将其所连的结点各减1
44                 indegrees[node] -= 1
45         return True
46 
47 """
48 第二种思路用DFS,递归,没有掌握
49 传送门:https://blog.csdn.net/fuxuemingzhu/article/details/82951771
50 """
51 class Solution2(object):
52     def canFinish(self, numCourses, prerequisites):
53         from collections import defaultdict
54         graph = defaultdict(list)  # defaultdict(<class 'list'>)
55         for u, v in prerequisites: #构造图
56             graph[v].append(u)
57         N = numCourses
58         visited = [0] * N
59         # 0 = Unknown, 1 = visiting, 2 = visited
60         for i in range(N):         #分别从N个结点出发调用dfs判断是否有环
61             if not self.dfs(graph, visited, i):  #有环dfs返回True
62                 return False
63         return True
64     def dfs(self, graph, visited, i):   #这个函数云里雾里。。没懂
65         if visited[i] == 1:
66             return False
67         if visited[i] == 2:
68             return True
69         visited[i] = 1
70         for j in graph[i]:
71             if not self.dfs(graph, visited, j):
72                 return False
73         visited[i] = 2
74         return True
原文地址:https://www.cnblogs.com/yawenw/p/12260066.html