zoukankan      html  css  js  c++  java
  • [LintCode] 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

    Given n = 2, prerequisites = [[1,0]]
    Return [0,1]

    Given n = 4, prerequisites = [1,0],[2,0],[3,1],[3,2]]
    Return [0,1,2,3] or [0,2,1,3]

     
    This problem is essentially the same with Course Schedule. The only difference is that this problem asks for a concrete topological ordering.
    We can leverage the solutions from Topological Sorting and solve this problem using both BFS and DFS. The difference here would be cycle detection logic since it is not guranteed that we can find a topological ordering.
     
    Solution 1. BFS 
     1 public class Solution {
     2     /**
     3      * @param numCourses a total of n courses
     4      * @param prerequisites a list of prerequisite pairs
     5      * @return the course order
     6      */
     7     public int[] findOrder(int numCourses, int[][] prerequisites) {
     8         ArrayList<ArrayList<Integer>> edges = new ArrayList<ArrayList<Integer>>();
     9         int[] incomingEdgeNum = new int[numCourses];
    10         int[] result = new int[numCourses];
    11         
    12         for(int i = 0; i < numCourses; i++)
    13         {
    14             edges.add(new ArrayList<Integer>());
    15         }
    16         int courseNum;
    17         for(int i = 0; i < prerequisites.length; i++)
    18         {
    19             courseNum = prerequisites[i][0];
    20             incomingEdgeNum[courseNum]++;
    21             edges.get(prerequisites[i][1]).add(courseNum);
    22         }
    23         
    24         Queue<Integer> queue = new LinkedList<Integer>();
    25         int currIdx = 0;
    26         for(int i = 0; i < incomingEdgeNum.length; i++)
    27         {
    28             if(incomingEdgeNum[i] == 0)
    29             {
    30                 queue.add(i);
    31                 result[currIdx] = i;
    32                 currIdx++;
    33             }
    34         }
    35         
    36         int count = 0;
    37         while(!queue.isEmpty())
    38         {
    39             int course = queue.poll();
    40             count++;
    41             int n = edges.get(course).size();
    42             for(int i = 0; i < n; i++)
    43             {
    44                 int neighbor = edges.get(course).get(i);
    45                 incomingEdgeNum[neighbor]--;
    46                 if(incomingEdgeNum[neighbor] == 0)
    47                 {
    48                     queue.add(neighbor);
    49                     result[currIdx] = neighbor;
    50                     currIdx++;
    51                 }
    52             }
    53         }
    54         if(count < numCourses)
    55         {
    56             return new int[0];
    57         }
    58         return result;
    59     }
    60 }
    Solution 2.DFS 
    The depth of the recursive calls can get big if the input size gets big.
     
     1 public class Solution {
     2     public int[] findOrder(int numCourses, int[][] prerequisites) {
     3         if(numCourses <= 0) {
     4             return new int[0];
     5         }
     6         if(prerequisites == null) {
     7             int[] result = new int[numCourses];
     8             for(int i = 0; i < numCourses; i++) {
     9                 result[i] = i;
    10             }
    11             return result;
    12         }
    13         ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
    14         for(int i = 0; i < numCourses; i++) {
    15             graph.add(new ArrayList<Integer>());
    16         }
    17         for(int j = 0; j < prerequisites.length; j++) {
    18             graph.get(prerequisites[j][1]).add(prerequisites[j][0]);
    19         }
    20         boolean hasCycle = false;
    21         Set<Integer> exploring = new HashSet<Integer>();
    22         Set<Integer> explored = new HashSet<Integer>();
    23         Stack<Integer> stack = new Stack<Integer>();
    24         int[] result = new int[numCourses];
    25         for(int i = 0; i < numCourses; i++) {
    26             if(!explored.contains(i) && dfs(i, exploring, explored, graph, stack)) {
    27                 hasCycle = true;
    28                 break;
    29             }
    30         }
    31         if(hasCycle) {
    32             return new int[0];
    33         }
    34         int idx = 0;
    35         while(!stack.empty()) {
    36             result[idx] = stack.pop();
    37             idx++;
    38         }
    39         return result;
    40     }
    41     private boolean dfs(int current, Set<Integer> exploring, Set<Integer> explored,
    42                         ArrayList<ArrayList<Integer>> graph, Stack<Integer> stack) {
    43         exploring.add(current);
    44         for(Integer neighbor : graph.get(current)) {
    45             if(explored.contains(neighbor)) {
    46                 continue;
    47             }
    48             if(exploring.contains(neighbor)) {
    49                 return true;
    50             }
    51             if(dfs(neighbor, exploring, explored, graph, stack)) {
    52                 return true;
    53             }
    54         }
    55         exploring.remove(current);
    56         explored.add(current);
    57         stack.push(current);
    58         return false;
    59     } 
    60 }
     
    Related Problems
    Course Schedule
    Sequence Reconstruction
    Topological Sorting
     
  • 相关阅读:
    课程的添加与发布
    openlayers 框选得到在选框内的要素,并标注出这些要素的名称
    手写js前端分页功能实现
    eclipse安装html编辑器插件
    Redis持久化技术
    java获取指定时间
    java生成Cron表达式
    CentOS7 ifcfg-ens33(没有eth0网卡) 网卡配置 静态IP地址
    java代码关闭tomcat程序
    Tomcat控制台乱码问题
  • 原文地址:https://www.cnblogs.com/lz87/p/7493993.html
Copyright © 2011-2022 走看看