Used to deduct if there is any circle in the graph
`Example: Course Schedule
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 course0you have to first take course1.
Return true if you can finish all courses. Otherwise, return false.
Solution Step:
- Create in degree array from given 2D array
- Use queue to traverse BFS
- Create graph of adj node from given 2D array relation array
- insert all indegree in queue
- loop till queue is not empty
- take from queue and reduce its indegree, if remaining indegree is 0 insert in queue.
- all picked item from queue will be sequence of topological sort.
static int[] topoSort(int V, ArrayList<ArrayList<Integer>> adj) {
int indegree[] = new int[V];
for (int i = 0; i < V; i++) {
for (int it : adj.get(i)) {
indegree[it]++;
}
}
Queue<Integer> q = new LinkedList<Integer>();
;
for (int i = 0; i < V; i++) {
if (indegree[i] == 0) {
q.add(i);
}
}
int topo[] = new int[V];
int i = 0;
while (!q.isEmpty()) {
int node = q.peek();
q.remove();
topo[i++] = node;
// node is in your topo sort
// so please remove it from the indegree
for (int it : adj.get(node)) {
indegree[it]--;
if (indegree[it] == 0) {
q.add(it);
}
}
}
return topo;
}