[LeetCode] 207 Course Schedule_Medium tag: BFS, DFS

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, is it possible for you to finish all courses?

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:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

我的理解是这个题就是问, 如果有两门课互为prerequisite课, 那么就False, 否则True, 注意的是这里的互为有可能是中间隔了几门课, 而不是直接的prerequisite, 例如: [(0,1),(2,0),(1,2)], 这里 1 是0 的前置课, 0 是2 的前置课, 所以1是2 的间接前置课, 但是最后一个input说2 是1 的前置课, 所以就矛盾, 不可能完成, return False. 所以思路为, 建一个dictionary, 分别将input 的每个pair(c1, c2)放入dictionary里面, 前置课(c2)为key, 后置课(c1)为value, 不过放入之前要用bfs 判断c1 是否为c2 的前置课, 如果是, 那么矛盾, return False. 否则一直判断到最后的pair, 返回Ture.

1. Constraints:

1) 实际这里的n对我这个做法没有什么用处, 因为课程id 是unique的.

2. Ideas

      BFS:     T: O(n)   number of nodes,     S: O(n)

1) init dictionary, d

2) for pair(c1,c2) in prerequisites, use bfs to see if c1 is a prerequisity of c2, if so , return False, else, d[c2].add(c1), and until all pairs been checked. return True

3) bfs: use queue and visited to check whether there is a path from source to target.

3. Code

 1 class Solution:
 2     def courseSchedule(self, numCourse, prerequisites):
 3         def bfs(d, source, target):
 4             if source not in d: return False
 5             queue, visited = collections.deque([source]), set([source])
 6             while queue:
 7                 node = queue.popleft()
 8                 if node == target: return True
 9                 for each in d[node]:
10                     if each not in visited:
11                         queue.append(each)
12                         visited.add(each)
13             return False
14         
15         d = collections.defaultdict(set)
16         for c1, c2 in prerequisites:
17             if bfs(d,c1, c2): return False
18             d[c2].add(c1)
19         return True

4. Test cases:

1) [(0,1),(2,0),(1,2)],  =>   False

猜你喜欢

转载自www.cnblogs.com/Johnsonxiong/p/9266306.html