【图的遍历】返回上课的顺序 Course Schedule II

问题:express

There are a total of n courses you have to take, labeled from 0 to n - 1.ide

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]ui

Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.spa

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.指针

For example:code

2, [[1,0]]

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]递归

4, [[1,0],[2,0],[3,1],[3,2]]

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].ip

Note:ci

  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。

Hints:get

  1. This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
  2. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
  3. Topological sort could also be done via BFS.

解决:

① 与Course Schedule相同的思路,只是记录下通过的节点的值。BFS,首先查找入度为0 的端点,而后依次遍历。

class Solution {//38ms
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[] res = new int[numCourses];
        int p = 0;//指向结果的指针
        int len = prerequisites.length;//关系的长度
        if (numCourses == 0){
            return res;
        }
        if (len == 0){//没有关系,直接返回结果
            for (int i = 0;i < numCourses;i ++){
                res[i] = i;
            }
            return res;
        }
        int[] in = new int[numCourses];//记录节点的入度。
        for (int i = 0;i < len;i ++){//遍历有向边,初始化入度
            in[prerequisites[i][0]] ++;
        }
        Queue<Integer> queue = new LinkedList<>();//保存入度为0的点
        for (int i = 0;i < numCourses;i ++){
            if (in[i] == 0){
                queue.offer(i);
            }
        }
        int count = queue.size();//记录入度为0的节点数
        while(! queue.isEmpty()){
            int top = queue.poll();
            res[p ++] = top;
            for (int i = 0;i < len;i ++){//将当前节点所指向的节点的入度-1
                if (prerequisites[i][1] == top){
                    in[prerequisites[i][0]] --;
                    if (in[prerequisites[i][0]] == 0){
                        count ++;
                        queue.offer(prerequisites[i][0]);
                    }
                }
            }
        }
        if (count == numCourses){
            return res;
        }else{
            return new int[0];
        }
    }
}

② dfs的解法。

class Solution { //14ms
    public static int[] findOrder(int numCourses, int[][] prerequisites) {
        Map<Integer,List<Integer>> adjacent = new HashMap<>();//记录每个节点的相邻节点
        boolean[] isvisited = new boolean[numCourses];//标记是否遍历过
        boolean[] isonstack = new boolean[numCourses];//用来标记节点入栈
        List<Integer> list = new ArrayList<>();//用于存放结果
        for (int[] tmp : prerequisites){//添加相邻节点
            if (! adjacent.containsKey(tmp[1])){
                List<Integer> t = new ArrayList<>();
                t.add(tmp[0]);
                adjacent.put(tmp[1],t);
            }else{
                adjacent.get(tmp[1]).add(tmp[0]);
            }
        }
        for (int i = 0;i < numCourses;i ++){//开始遍历
            if (isvisited[i]) continue;//跳过已经遍历过的节点
            if (dfs(adjacent,i,isvisited,isonstack,list)){//存在环
                return new int[0];
            }
        }
        int[] res = new int[list.size()];
        for (int i = 0;i < list.size();i ++){
            res[i] = list.get(i);
        }
        return res;
    }
    public static boolean dfs(Map<Integer,List<Integer>> adjacent,int i,boolean[] isvisited,
                       boolean[] isonstack,List<Integer> list){
        isvisited[i] = true;//遍历当前节点
        isonstack[i] = true;//入栈
        if (adjacent.containsKey(i) && adjacent.get(i).size() > 0){//遍历当前节点的子节点
            for (int j : adjacent.get(i)){//递归遍历子节点
                if (! isvisited[j] && dfs(adjacent,j,isvisited,isonstack,list)) return true;//没有遍历过子节点而且子节点递归遍历中存在环
                if (isonstack[j]) return true;//子节点已经存在于栈中,存在环
            }
        }
        list.add(0,i);//倒序从最后一个节点开始加入结果链表
        isonstack[i] = false;//出栈
        return false;//没有环
    }
}

③ 在discuss中看到的效率最高的解法。。。

class Solution { //3ms     public int[] findOrder(int numCourses, int[][] prerequisites) {         int edge_len = prerequisites.length;         int[] edge = new int[prerequisites.length];         int[] edge_next = new int[prerequisites.length];         int[] last = new int [numCourses];         int[] indegree = new int[numCourses];         int mark = 0;         for (int i = 0;i < numCourses;i ++) last[i] = -1;         for (int i = 0;i < numCourses;i ++) indegree[i] = 0;         for (int i = 0;i < edge_len;i ++) {             edge[mark] = prerequisites[i][0];             indegree[prerequisites[i][0]] ++;             int x = prerequisites[i][1];             edge_next[mark] = last[x];             last[x] = mark;             mark ++;         }         int[] queue = new int [numCourses];         int l = -1, r= -1;         for (int i = 0;i < numCourses;i ++) {             if (indegree[i] == 0) queue[++ r] = i;         }         while (l < r) {             l ++;             int now = queue[l];             int edge_now = last[now];             while (edge_now != -1) {                 indegree[edge[edge_now]] --;                 if (indegree[edge[edge_now]] == 0)                     queue[++ r] = edge[edge_now];                 edge_now = edge_next[edge_now];             }         }         if (r + 1 != numCourses)             return new int[0];         else return queue;     } }  

相关文章
相关标签/搜索