Info & Theory
A topological sort arranges the vertices of a
directed acyclic graph (DAG) into a line so that every
edge u → v points forward — u always
appears before v. It answers the question
"in what order can I do these tasks given their dependencies?".
Kahn's algorithm
Compute the indegree (number of incoming edges) of each vertex, then:
- Put every
indegree = 0vertex in a queue. - Dequeue a vertex and append it to the output order.
- For each successor, decrement its indegree; if it hits 0, enqueue it.
- Repeat until the queue empties.
Why acyclic matters
If the graph has a cycle, the vertices on it keep a positive indegree forever and never enter the queue. Kahn's algorithm therefore ends having placed fewer vertices than exist — and that shortfall is exactly how it detects a cycle. Try the Add edge button: an edge that points backward turns the DAG into a cyclic graph and the simulation reports that no order exists.
Many valid orders
Whenever two vertices have indegree 0 at the same moment, either may go next, so most DAGs admit many topological orders. A fully chained graph has exactly one; a graph with no edges accepts every permutation.
Complexity
Each vertex is enqueued and dequeued once and each edge is
inspected once, so Kahn's algorithm runs in
O(V + E) time — linear in the size of the graph.
Where it is used
Build systems (make, package managers) compile in
dependency order, course planners respect prerequisites,
spreadsheets recalculate cells, and compilers schedule
instructions — all by topologically sorting a dependency DAG.
Frequently asked questions
What is a topological sort?
A topological sort is a linear ordering of the vertices of a directed acyclic graph such that for every directed edge u to v, u comes before v in the ordering. It is the order in which you can carry out tasks when some tasks must finish before others can start. A graph can have many valid topological orders or, if it contains a cycle, none at all.
How does Kahn's algorithm work?
Kahn's algorithm first computes the indegree of every vertex, then puts all zero-indegree vertices in a queue. It repeatedly removes a vertex from the queue, appends it to the output order, and decrements the indegree of each of its successors. Any successor whose indegree drops to zero is added to the queue, and the process repeats until the queue is empty.
What is the indegree of a node?
The indegree of a node is the number of edges pointing into it, that is the count of its direct predecessors. A node with indegree zero has no remaining dependencies and is therefore ready to be placed next in the order. Kahn's algorithm is driven entirely by tracking how indegrees fall to zero as nodes are removed.
Why must the graph be acyclic?
A cycle means a group of vertices each depend, directly or indirectly, on one another, so no member of the cycle can ever be first. With a cycle present, every vertex in it keeps a positive indegree forever and never enters the queue. That is why a topological order only exists for a directed acyclic graph (DAG).
How does the simulation detect a cycle?
It uses a property of Kahn's algorithm: if the algorithm finishes and the number of vertices placed in the order is less than the total number of vertices, the remaining vertices must lie on a cycle. The simulation lets you add a back edge that creates a cycle, then reports that no topological order exists because the count came up short.
Is a topological order unique?
No. Whenever two or more vertices have indegree zero at the same time, you may choose either one next, and each choice can lead to a different valid order. The number of distinct topological orders depends on the structure of the graph, and a fully chained graph has exactly one while a graph with no edges has every permutation.
What is the time complexity of Kahn's algorithm?
Kahn's algorithm runs in O(V + E) time, where V is the number of vertices and E the number of edges. Each vertex is enqueued and dequeued once, and each edge is examined once when its source is removed. This linear performance is why topological sort scales well to very large dependency graphs.
How is topological sort used in build systems?
Build tools like Make, Bazel, and package managers model files or packages as nodes and their dependencies as edges, then topologically sort that graph to decide a safe compile or install order. Building a target only after all of its dependencies are built is exactly the guarantee a topological order provides. If the dependency graph contains a cycle, the build tool reports it as an error rather than looping forever.
What other problems use topological sorting?
Course scheduling with prerequisites, spreadsheet cell recalculation, task and project scheduling, instruction scheduling in compilers, and resolving symbol dependencies in linkers all rely on topological order. Any setting where some items must precede others is a candidate. It is also a first step in algorithms that find the longest or shortest path in a DAG.
What is the difference between Kahn's algorithm and a DFS-based topological sort?
Kahn's algorithm is iterative and works forward by repeatedly removing zero-indegree vertices using a queue. The depth-first search approach instead recurses to the deepest descendants first and prepends each finished vertex to the order, producing the reverse of the finish-time order. Both run in O(V + E) time and both can detect cycles, but Kahn's makes the notion of dependencies ready to process more explicit.