-
Notifications
You must be signed in to change notification settings - Fork 0
/
topological-sort.js
34 lines (30 loc) · 967 Bytes
/
topological-sort.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
// Topological Sort a.k.a. Kahn's Algorithm, helps you walk through a graph in an ideal way
// Topological sorting only works for graphs that do not contain a cycle (acyclical)
// And we also need a starting node with zero dependencies
let graph = {
'a': ['d'],
'b': ['d'],
'c': ['b'],
'd': [],
}
let countIn = {};
for (let key in graph) {
if (!countIn[key]) countIn[key] = 0;
for (let val of graph[key]) {
if (!countIn[val]) countIn[val] = 0;
countIn[val]++;
}
}
let queue = [];
for (let key in countIn) {
if (countIn[key] === 0) queue.push(key); // push keys that have nothing incoming
}
while (queue.length > 0) {
let val = queue.shift();
console.log(val);
for (let value of graph[val]) {
countIn[value]--; // remove the val from the remaining dependencies
if (countIn[value] === 0) queue.push(value); // continue to push keys that have nothing incoming
}
}
// logs a, c, b, d