Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Topological order generator
Linear ordering of vertices in directed acyclic graph
Select an algorithm and generate steps to begin visualization
Topological sort produces a linear ordering of the vertices of a directed acyclic graph (DAG) such that every edge points from an earlier vertex to a later one. It answers the question: in what order can tasks be performed when some tasks depend on others?
Two standard approaches exist. Kahn's algorithm repeatedly removes a vertex with no incoming edges, appending it to the order and decrementing the in-degree of its neighbors; a queue holds the current zero in-degree vertices. The DFS approach performs a depth-first search and outputs vertices in reverse order of their finish times. Both run in O(V + E). If vertices remain unprocessed (Kahn) or a back edge appears (DFS), the graph has a cycle and no valid order exists.
Topological ordering schedules build systems such as Make and Gradle, resolves package installation order, sequences university courses with prerequisites, orders spreadsheet cell evaluation, and schedules instruction execution in compilers. It is among the most common medium-difficulty interview questions on directed graphs.
Two standard formulations, both linear. Kahn works forward from nodes with no prerequisites; the DFS version works backward from finish times.
// Kahn: repeatedly remove a node with no incoming edges
compute indegree[v] for every vertex
queue = all vertices with indegree 0
order = []
while queue is not empty:
u = queue.pop()
order.append(u)
for each edge (u, v):
indegree[v] -= 1
if indegree[v] == 0: queue.push(v)
if order.length < V: the graph has a cycle
// DFS variant: reverse of finish order
run DFS; push each vertex onto a stack when it finishes
the stack, popped, is a valid topological orderKahn has one practical advantage worth knowing: because it detects a cycle by counting how many nodes it managed to emit, the vertices left over are exactly those involved in or downstream of a cycle. That makes it far more useful than a boolean when you need to report which dependencies are circular.
Run Kahn on a small build-dependency graph, where an edge X to Y means X must be built before Y.
Example graph: Directed edges A to C, B to C, C to D, B to D.
A valid order is A, B, C, D. Note that B, A, C, D is equally valid: A and B are both prerequisite-free and their relative order is unconstrained. Topological order is not unique unless the graph is a single chain, which is why tests should verify that every edge points forward rather than compare against one expected sequence.
Time: O(V + E) · Space: O(V)
Computing all indegrees requires one scan of every edge, O(E). Each vertex is enqueued and dequeued exactly once, O(V). Each edge is examined exactly once, when its source is emitted and the target indegree is decremented, another O(E). Space holds the indegree array, the queue and the output list, all O(V). The DFS variant has the same bounds, with the recursion stack replacing the queue. Neither can be improved, since any correct algorithm must read every edge to know the constraints.
Kahn and DFS produce equally valid orders. Choose on what you need alongside the ordering.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Kahn (BFS style) | You want cycle diagnostics, or a lexicographically smallest order via a priority queue, or you need to avoid deep recursion. | O(V + E) |
| DFS finish order | You are already running a DFS for other reasons, or you want the shortest possible implementation. | O(V + E) |
| Tarjan SCC | The graph has cycles and you want to condense them into a DAG rather than reject the input. | O(V + E) |
| Longest path / CPM | Nodes carry durations and you want the critical path. That is a topological order followed by a DP pass. | O(V + E) |
Read the full article: Topological Sort Explained Step by Step
Read the full article: Graph Algorithms in Coding Interviews
Related algorithms: Depth-First Search, Cycle Detection, Critical Path Method (CPM)