-
Notifications
You must be signed in to change notification settings - Fork 312
/
topologicalSort.ts
50 lines (41 loc) · 1.33 KB
/
topologicalSort.ts
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
import Queue from '../data-structures/Queue';
import nullthrows from '../utils/nullthrows';
interface Graph<T> {
[key: string]: Array<T>;
}
/**
* Performs a topological sort on a directed graph.
* @param {Object} graph Node to array of traversable neighboring nodes.
* @return {Array<T>} A topological traversal of nodes.
*/
function topologicalSort(graph: Graph<string>): Array<string> {
const nodes = new Map<string, { in: number; out: Set<string> }>();
const order: Array<string> = [];
const queue = new Queue<string>();
Object.keys(graph).forEach((node) => {
nodes.set(node, { in: 0, out: new Set(graph[node]) });
});
Object.keys(graph).forEach((node) => {
graph[node].forEach((neighbor) => {
nullthrows(nodes.get(String(neighbor))).in += 1;
});
});
nodes.forEach((value, node) => {
if (value.in === 0) {
queue.enqueue(node);
}
});
while (queue.length) {
const node = queue.dequeue();
nullthrows(nodes.get(nullthrows(node))).out.forEach((neighbor) => {
const neighborNode = nullthrows(nodes.get(String(neighbor)));
neighborNode.in -= 1;
if (neighborNode.in === 0) {
queue.enqueue(String(neighbor));
}
});
order.push(nullthrows(node));
}
return order.length === Object.keys(graph).length ? order : [];
}
export default topologicalSort;