-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopologicalSortUsingBFS.js
70 lines (64 loc) · 1.94 KB
/
TopologicalSortUsingBFS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const buildGraph = (N, relations) => {
const graph = new Array(N).fill(0).map(a => []);
for (const [u, v] of relations) {
graph[u-1].push(v-1);
}
return graph;
}
/**
* @param {number} N
* @param {number[][]} relations
* @return {number}
*/
var minimumSemesters = function(N, relations) {
/**
- Approach1:
- Apply topological sort
- Also find out the depth of each component while running toplogical sort
- get the max value of depth as an answer
- Which topological sort algorithm can be applied?
- DFS approach?
- DFS doesn't gives shortest depth therefore can't be used
- BFS approach?
- BFS gives shortest depth therefore we should use BFS for this problem
*/
const graph = buildGraph(N, relations);
const inDegree = new Array(N).fill(0);
for (const [u, v] of relations) {
inDegree[v-1]++;
}
// Initialize queue by nodes with in-degree 0
const queue = [];
for (let i=0; i < N; i++) {
if(inDegree[i] === 0) {
queue.push(i);
}
}
// Edge case: no nodes with indegree 0 or all nodes has indegree 0
if(queue.length === 0 || queue.length === N) {
return -1;
}
const capture = [];
let semesters = 0;
while(queue.length > 0) {
const size = queue.length;
semesters++;
for (let i=0; i < size; i++) {
const node = queue.shift();
capture.push(node);
// process neighbors
for (const neighbor of graph[node]) {
// use indegree to track visited nodes
inDegree[neighbor]--;
if(inDegree[neighbor] === 0) {
queue.push(neighbor);
}
}
}
}
// if processed nodes is not same as total then cycle exists
if(capture.length !== N) {
return -1;
}
return semesters;
};