Topological Sort
Kahn's algorithm for ordering dependencies in a DAG
Turn a partial order into a valid sequence
In a directed dependency graph, edge u -> v means that u must appear before v. A topological order satisfies every such edge. It exists only when the graph is acyclic.
Remove vertices with no remaining prerequisite
Kahn's algorithm counts every vertex's indegree. Any vertex with indegree 0 is ready to output. Removing it deletes its outgoing edges, which may reduce another vertex to indegree 0. The player shows indegrees as node badges, the active ready vertex in amber, and completed vertices in green.
Count the incoming edges of every vertex.
Cycles and non-unique answers
Several vertices may be ready at once, so a DAG can have many valid orders. This demonstration chooses the lowest-index ready vertex for a deterministic trace. If vertices remain but none has indegree 0, those dependencies contain a directed cycle. Processing each vertex and edge once takes O(V + E) time.
Continue the graph path with Dijkstra's shortest-path invariant.