leetcode刷题笔记-topological sort拓扑排序(DFS)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sengo_GWU/article/details/81966972

207. Course Schedule 找环

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.

思路:找是否存在环,已经访问完的标记为1,正在访问的标记为2,如果遇到正在访问的就是遇到环

class Solution(object):
    def canFinish(self, numCourses, prerequisites):
        """
        :type numCourses: int
        :type prerequisites: List[List[int]]
        :rtype: bool
        """
        mark = collections.defaultdict(int)
        prere = collections.defaultdict(list)
        for c, p in prerequisites:
            prere[c].append(p)

        def dfs(course):
            if mark[course] == 1:  # visited
                return True

            if mark[course] == 2:  # visiting
                return False

            # unvisited
            mark[course] = 2  # mark as visiting
            preCourses = prere[course]
            if preCourses == []:
                mark[course] = 1
                return True
            else:
                for preCourse in preCourses:
                    re = dfs(preCourse)
                    if not re:
                        return False
                mark[course] = 1  # mark as visited
                return True

        for course, pre in prerequisites:
            re = dfs(course)
            if not re:
                return False
        return True

210. Course Schedule II 拓扑排序

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, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

Example 1:

Input: 2, [[1,0]] 
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished   
             course 0. So the correct course order is [0,1] .

Example 2:

Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both     
             courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. 
             So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3] .

class Solution(object):
    def findOrder(self, numCourses, prerequisites):
        """
        :type numCourses: int
        :type prerequisites: List[List[int]]
        :rtype: List[int]
        """
        self.graph = collections.defaultdict(list)
        self.res = []
        for c, p in prerequisites:
            self.graph[c].append(p)
        
        self.visited = [0 for x in xrange(numCourses)]
        for node in xrange(numCourses):
            if not self.DFS(node):
                return []
        
        return self.res
        
    
    def DFS(self, node):
        if self.visited[node] == -1:  # visiting
            return False
        if self.visited[node] == 1:  # visited and add to res
            return True 
        self.visited[node] = -1  # mark as visiting
        
        for x in self.graph[node]:
            if not self.DFS(x):
                return False
            
        self.visited[node] = 1
        self.res.append(node)
        return True
        

猜你喜欢

转载自blog.csdn.net/Sengo_GWU/article/details/81966972