diff --git a/Java/graphs/TopologicalSort.java b/Java/graphs/TopologicalSort.java new file mode 100644 index 00000000..5b421b46 --- /dev/null +++ b/Java/graphs/TopologicalSort.java @@ -0,0 +1,74 @@ +/*There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. +You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. + +For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. +Return true if you can finish all courses. Otherwise, return false. + +Example 1: +Input: numCourses = 2, prerequisites = [[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: numCourses = 2, prerequisites = [[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. + +Constraints: +1 <= numCourses <= 2000 +0 <= prerequisites.length <= 5000 +prerequisites[i].length == 2 +0 <= ai, bi < numCourses +All the pairs prerequisites[i] are unique. */ + +import java.util.*; + +public class TopologicalSort { + public static void main(String[] args) { + int numCourses = 2; + int[][] prerequisites = {{1, 0}}; + + boolean canFinish = canFinish(numCourses, prerequisites); + System.out.println("Can finish all courses: " + canFinish); + } + + + public static boolean canFinish(int numCourses, int[][] prerequisites) { + int[] indegree = new int[numCourses]; + List> adjList = new ArrayList<>(); + for(int i = 0; i < numCourses; i++){ + adjList.add(new ArrayList<>()); + } + for(int[] pre : prerequisites){ + adjList.get(pre[1]).add(pre[0]); + } + for(int i = 0; i < numCourses; i++){ + for(int neighbor : adjList.get(i)){ + indegree[neighbor]++; + } + } + Queue q = new LinkedList<>(); + List ans = new ArrayList<>(); + + for(int i = 0; i < indegree.length; i++){ + if(indegree[i] == 0){ + q.add(i); + } + } + + while(!q.isEmpty()){ + int curr = q.poll(); + ans.add(curr); + + for(int neighbor : adjList.get(curr)){ + indegree[neighbor]--; + if(indegree[neighbor] == 0){ + q.add(neighbor); + } + } + } + + return ans.size() == numCourses; + } +} \ No newline at end of file