207. Course Schedule I & 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, is it possible for you to finish all courses?
For example:
2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
2, [[1,0],[0,1]]
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.
From: http://www.programcreek.com/2014/05/leetcode-course-schedule-java/
分析:
建立有向图,利用Toplogical sort逐一去掉没有father的节点。如果最后总的节点大于没有父节点的个数,表面里面有环。
1 public boolean canFinish(int numCourses, int[][] prerequisites) { 2 if (prerequisites == null) return true; 3 Map<Integer, Node> map = new HashMap<Integer, Node>(); 4 5 for (int[] row : prerequisites) { 6 if (!map.containsKey(row[0])) { 7 map.put(row[0], new Node(row[0], 0)); 8 } 9 10 if (!map.containsKey(row[1])) { 11 map.put(row[1], new Node(row[1], 0)); 12 } 13 14 map.get(row[0]).inDegree++; 15 map.get(row[1]).list.add(map.get(row[0])); 16 } 17 18 Queue<Node> queue = new LinkedList<Node>(); 19 for (Node node : map.values()) { 20 if (node.inDegree == 0) { 21 queue.offer(node); 22 } 23 } 24 25 while (!queue.isEmpty()) { 26 Node node = queue.poll(); 27 for (Node child : node.list) { 28 child.inDegree--; 29 if (child.inDegree == 0) { 30 queue.offer(child); 31 } 32 } 33 } 34 for (Node node : map.values()) { 35 if (node.inDegree != 0) { 36 return false; 37 } 38 } 39 return true; 40 41 } 42 } 43 44 class Node { 45 int value; 46 int inDegree; 47 List<Node> list; 48 49 public Node(int value, int inDegree) { 50 this.value = value; 51 this.inDegree = inDegree; 52 list = new ArrayList<Node>(); 53 } 54 }
还有一种方法就是用dfs来看是否有back edge.
class Solution { public static boolean hasCycle(List<List<Integer>> graph) { // null input checks, etc int numNodes = graph.size(); boolean[] visited = new boolean[numNodes]; boolean[] current = new boolean[numNodes]; for (int i = 0; i < numNodes; ++i) { if (!visited[i]) { boolean foundCycle = dfs(graph, visited, current, i); if (foundCycle) { return true; } } } return false; } private static boolean dfs(List<List<Integer>> graph, boolean[] visited, boolean[] current, int pos) { if (current[pos]) { return true; } if (visited[pos]) { return false; } visited[pos] = true; current[pos] = true; List<Integer> adj = graph.get(pos); for (int dest : adj) { boolean res = dfs(graph, visited, current, dest); if (res) { return true; } } current[pos] = false; return false; } }
Course Schedule II
找出选课的顺序。
public class Solution { public int[] findOrder(int n, int[][] prerequisites) { if (prerequisites == null) return new int[0]; Map<Integer, Node> map = new HashMap<Integer, Node>(); // very important for (int i = 0; i < n; i++){ map.put(i, new Node(i, 0)); } for (int[] row : prerequisites) { map.get(row[0]).inDegree++; map.get(row[1]).list.add(map.get(row[0])); } List<Integer> list = new ArrayList<Integer>(); Queue<Node> queue = new LinkedList<Node>(); for (Node node : map.values()) { if (node.inDegree == 0) { queue.offer(node); } } while (!queue.isEmpty()) { Node node = queue.poll(); list.add(node.value); for (Node child : node.list) { child.inDegree--; if (child.inDegree == 0) { queue.offer(child); } } } if (list.size() < n) return new int[0]; int[] result = new int[list.size()]; for (int i = 0; i < list.size(); i++) { result[i] = list.get(i); } return result; } } class Node { int value; int inDegree; List<Node> list; public Node(int value, int inDegree) { this.value = value; this.inDegree = inDegree; list = new ArrayList<Node>(); } }