Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions Java/graphs/TopologicalSort.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> 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<Integer> q = new LinkedList<>();
List<Integer> 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;
}
}