There are a total of _n _courses you have to take, labeled from0ton-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 1:
Input:
2, [[1,0]]
Output:
[0,1]
Explanation:
There are a total of 2 courses to take. To take course 1 you should have finished
course 0. So the correct course order is
[0,1] .
Example 2:
Input:
4, [[1,0],[2,0],[3,1],[3,2]]
Output:
[0,1,2,3] or [0,2,1,3]
Explanation:
There are a total of 4 courses to take. To take course 3 you should have finished both
courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is
[0,1,2,3]
. Another correct ordering is
[0,2,1,3] .
Note:
The input prerequisites is a graph represented by a list of edges , not adjacency matrices. Read more about how a graph is represented.
You may assume that there are no duplicate edges in the input prerequisites.
Analysis
Similar to Course Schedule, this problem just needs to return the topologically sorted results.
Hints:
This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
int[] incLinkCounts = new int[numCourses];
List < List < Integer >> adjs = new ArrayList < > (numCourses);
initialiseGraph(incLinkCounts, adjs, prerequisites);
return solveByBFS(incLinkCounts, adjs);
// return solveByDFS(adjs);
}
private void initialiseGraph(int[] incLinkCounts, List < List < Integer >> adjs, int[][] prerequisites) {
int n = incLinkCounts.length;
while (n-- > 0) adjs.add(new ArrayList < > ());
for (int[] edge: prerequisites) {
incLinkCounts[edge[0]]++;
adjs.get(edge[1]).add(edge[0]);
}
}
private int[] solveByBFS(int[] incLinkCounts, List < List < Integer >> adjs) {
int[] order = new int[incLinkCounts.length];
Queue < Integer > toVisit = new ArrayDeque < > ();
for (int i = 0; i < incLinkCounts.length; i++) {
if (incLinkCounts[i] == 0) toVisit.offer(i);
}
int visited = 0;
while (!toVisit.isEmpty()) {
int from = toVisit.poll();
order[visited++] = from;
for (int to: adjs.get(from)) {
incLinkCounts[to]--;
if (incLinkCounts[to] == 0) toVisit.offer(to);
}
}
return visited == incLinkCounts.length ? order : new int[0];
}
}
BFS - Jiuzhang
public int[] findOrder(int numCourses, int[][] prerequisites) {
// Write your code here
List[] edges = new ArrayList[numCourses];
int[] degree = new int[numCourses];
for (int i = 0;i < numCourses; i++)
edges[i] = new ArrayList<Integer>();
for (int i = 0; i < prerequisites.length; i++) {
degree[prerequisites[i][0]] ++ ;
edges[prerequisites[i][1]].add(prerequisites[i][0]);
}
Queue queue = new LinkedList();
for(int i = 0; i < degree.length; i++){
if (degree[i] == 0) {
queue.add(i);
}
}
int count = 0;
int[] order = new int[numCourses];
while(!queue.isEmpty()){
int course = (int)queue.poll();
order[count] = course;
count ++;
int n = edges[course].size();
for(int i = n - 1; i >= 0 ; i--){
int pointer = (int)edges[course].get(i);
degree[pointer]--;
if (degree[pointer] == 0) {
queue.add(pointer);
}
}
}
if (count == numCourses)
return order;
return new int[0];
}
BFS - LeetCode Official - Using In-degree
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
boolean isPossible = true;
Map<Integer, List<Integer>> adjList = new HashMap<Integer, List<Integer>>();
int[] indegree = new int[numCourses];
int[] topologicalOrder = new int[numCourses];
// Create the adjacency list representation of the graph
for (int i = 0; i < prerequisites.length; i++) {
int dest = prerequisites[i][0];
int src = prerequisites[i][1];
List<Integer> lst = adjList.getOrDefault(src, new ArrayList<Integer>());
lst.add(dest);
adjList.put(src, lst);
// Record in-degree of each vertex
indegree[dest] += 1;
}
// Add all vertices with 0 in-degree to the queue
Queue<Integer> q = new LinkedList<Integer>();
for (int i = 0; i < numCourses; i++) {
if (indegree[i] == 0) {
q.add(i);
}
}
int i = 0;
// Process until the Q becomes empty
while (!q.isEmpty()) {
int node = q.remove();
topologicalOrder[i++] = node;
// Reduce the in-degree of each neighbor by 1
if (adjList.containsKey(node)) {
for (Integer neighbor : adjList.get(node)) {
indegree[neighbor]--;
// If in-degree of a neighbor becomes 0, add it to the Q
if (indegree[neighbor] == 0) {
q.add(neighbor);
}
}
}
}
// Check to see if topological sort is possible or not.
if (i == numCourses) {
return topologicalOrder;
}
return new int[0];
}
}
DFS - LeetCode Official
class Solution {
static int WHITE = 1;
static int GRAY = 2;
static int BLACK = 3;
boolean isPossible;
Map<Integer, Integer> color;
Map<Integer, List<Integer>> adjList;
List<Integer> topologicalOrder;
private void init(int numCourses) {
this.isPossible = true;
this.color = new HashMap<Integer, Integer>();
this.adjList = new HashMap<Integer, List<Integer>>();
this.topologicalOrder = new ArrayList<Integer>();
// By default all vertces are WHITE
for (int i = 0; i < numCourses; i++) {
this.color.put(i, WHITE);
}
}
private void dfs(int node) {
// Don't recurse further if we found a cycle already
if (!this.isPossible) {
return;
}
// Start the recursion
this.color.put(node, GRAY);
// Traverse on neighboring vertices
for (Integer neighbor : this.adjList.getOrDefault(node, new ArrayList<Integer>())) {
if (this.color.get(neighbor) == WHITE) {
this.dfs(neighbor);
} else if (this.color.get(neighbor) == GRAY) {
// An edge to a GRAY vertex represents a cycle
this.isPossible = false;
}
}
// Recursion ends. We mark it as black
this.color.put(node, BLACK);
this.topologicalOrder.add(node);
}
public int[] findOrder(int numCourses, int[][] prerequisites) {
this.init(numCourses);
// Create the adjacency list representation of the graph
for (int i = 0; i < prerequisites.length; i++) {
int dest = prerequisites[i][0];
int src = prerequisites[i][1];
List<Integer> lst = adjList.getOrDefault(src, new ArrayList<Integer>());
lst.add(dest);
adjList.put(src, lst);
}
// If the node is unprocessed, then call dfs on it.
for (int i = 0; i < numCourses; i++) {
if (this.color.get(i) == WHITE) {
this.dfs(i);
}
}
int[] order;
if (this.isPossible) {
order = new int[numCourses];
for (int i = 0; i < numCourses; i++) {
order[i] = this.topologicalOrder.get(numCourses - i - 1);
}
} else {
order = new int[0];
}
return order;
}
}