Advertisement
yimengael

Course Schedule 2

Mar 9th, 2022
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1.     static Map<Integer, ArrayList<Integer>> adj_list;
  2.     static boolean[] visited;
  3.     static boolean[] explored;
  4.     static Stack<Integer> stack;
  5.    
  6.     static ArrayList<Integer> course_schedule(Integer n, ArrayList<ArrayList<Integer>> prerequisites) {
  7.         // Build the graph
  8.         adj_list = new HashMap<>();
  9.         for (int course = 0; course < n; course++) {
  10.             adj_list.put(course, new ArrayList<Integer>());
  11.         }
  12.         for (ArrayList<Integer> prerequisite : prerequisites) {
  13.             adj_list.get(prerequisite.get(1)).add(prerequisite.get(0));
  14.         }
  15.        
  16.         // Check if there is cycle
  17.         visited = new boolean[n];
  18.         explored = new boolean[n];
  19.         for (int course = 0; course < n; course++) {
  20.             if (!visited[course]) {
  21.                 if (isCyclic(course)) {
  22.                     return new ArrayList<Integer>(Arrays.asList(-1));
  23.                 }
  24.             }
  25.         }
  26.        
  27.         //Apply topological sort
  28.         stack = new Stack<Integer>();
  29.         visited = new boolean[n];
  30.         for (int course = 0; course < n; course++) {
  31.             if (!visited[course]) {
  32.                 topologicalSort(course);
  33.             }
  34.         }
  35.        
  36.         // Retrieve the result
  37.         ArrayList<Integer> result = new ArrayList<Integer>();
  38.         while (!stack.isEmpty()) {
  39.             result.add(stack.pop());
  40.         }
  41.        
  42.         return result;
  43.     }
  44.    
  45.     /**
  46.      * This function is used to check the cycles in a DAG.
  47.      * This si simply a DFS traversal of the graph.
  48.      **/
  49.     static boolean isCyclic(int course) {
  50.         visited[course] = true;
  51.         for (int preCourse : adj_list.get(course)) {
  52.             if (!visited[preCourse]) {
  53.                 if (isCyclic(preCourse)) {
  54.                     return true;
  55.                 }
  56.             } else if (!explored[preCourse]) {
  57.                 return true;
  58.             }
  59.         }
  60.         explored[course] = true;
  61.         return false;
  62.     }
  63.    
  64.     /**
  65.      * This function is used to perform the topological sort in a DAG.
  66.      * It is using a stack.
  67.      **/
  68.     static void topologicalSort(int course) {
  69.         visited[course] = true;
  70.         for (int preCourse : adj_list.get(course)) {
  71.             if (!visited[preCourse]) {
  72.                 topologicalSort(preCourse);
  73.             }
  74.         }
  75.         stack.push(course);
  76.     }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement