Search for a command to run...
Order a directed acyclic graph by prerequisites
Graph playground
Edit directed edges and replay the ordering algorithm.
Calculate incoming edge counts
Every node with indegree zero is ready to appear first in the topological order.
Kahn's Algorithm
1function topologicalSort(graph) {2 const indegree = countIncomingEdges(graph);3 const queue = nodesWithZeroIndegree(indegree);//queue: [0]order: []4 const order = [];5 6 while (queue.length > 0) {7 const current = queue.shift();8 order.push(current);9 for (const neighbor of graph[current]) {10 if (--indegree[neighbor] === 0) queue.push(neighbor);11 }12 }13 return order;14}