I’m implementing Kahn’s algorithm, but some acyclic inputs return an empty result because indegree seems wrong after setup. I expect ["build","test","deploy"] for the example below. What am I counting incorrectly?
function topo(edges) {
const indegree = new Map(), graph = new Map();
for (const [a, b] of edges) {
graph.set(a, (graph.get(a) || []).concat(b));
indegree.set(a, (indegree.get(a) || 0) + 1);
if (!indegree.has(b)) indegree.set(b, 0);
}
const q = [...[...indegree].filter(([, d]) => d === 0).map(([n]) => n)];
return q;
}
Quelly