LeetCode207:Course Schedule
阿新 • • 發佈:2019-01-12
There are a total of n courses you have to take, labeled from 0
to n-1
.
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]
Given the total number of courses and a list of prerequisite pairs
Example 1:
Input: 2, [[1,0]] Output: true Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: 2, [[1,0],[0,1]] Output: false Explanation:There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about
- You may assume that there are no duplicate edges in the input prerequisites.
LeetCode:連結
對於每一對課程的順序關係,把它看做是一個有向邊,邊是由兩個端點組成的,用兩個點來表示邊,所有的課程關係即構成一個有向圖,問題相當於判斷有向圖中是否有環。判斷有向圖是否有環的方法是拓撲排序。
拓撲排序:維護一張表記錄所有點的入度,移出入度為0的點並更新其他點的入度,重複此過程直到沒有點的入度為0。如果原有向圖有環的話,此時會有剩餘的點且其入度不為0;否則沒有剩餘的點。
圖的拓撲排序可以DFS或者BFS。遍歷所有邊,計算點的入度;將入度為0的點移出點集,並更新剩餘點的入度;重複步驟2,直至沒有剩餘點或剩餘點的入度均大於0。
import collections
class Solution(object):
def canFinish(self, N, prerequisites):
"""
:type N,: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
# 記錄圖的位置關係
graph = collections.defaultdict(list)
# 記錄圖上節點的度
indegrees = collections.defaultdict(int)
for u, v in prerequisites:
graph[v].append(u)
indegrees[u] += 1
# 正常取點N次
for i in range(N):
zeroDegree = False
for j in range(N):
# 每次都找一個入度為0的點
if indegrees[j] == 0:
zeroDegree = True
break
if not zeroDegree:
return False
# 把它的入度變為-1
indegrees[j] = -1
# 把從這個點指向的點的入度都-1
for node in graph[j]:
indegrees[node] -= 1
return True