Hey everyone, I’m wiring up a small build-order helper for plugin loading, and I’m trying to keep it simple with Kahn’s algorithm instead of a DFS version because I want clearer failure output when there really is a cycle.
function order(nodes, edges) {
const indegree = new Map();
const graph = new Map();
for (const n of nodes) {
indegree.set(n, 0);
graph.set(n, []);
}
for (const [a, b] of edges) {
graph.get(a).push(b);
indegree.set(a, indegree.get(a) + 1);
}
const queue = [];
for (const [n, d] of indegree) {
if (d === 0) queue.push(n);
}
const out = [];
while (queue.length) {
const cur = queue.shift();
out.push(cur);
for (const next of graph.get(cur)) {
indegree.set(next, indegree.get(next) - 1);
if (indegree.get(next) === 0) queue.push(next);
}
}
return out.length === nodes.length ? out : null;
}
Why does this return null on a valid dependency list, and what am I counting in the wrong direction here?
Sora ![]()