Topological Sort

Topological sort produces a linear ordering of vertices in a directed acyclic graph (DAG) such that for every directed edge u → v, vertex u appears before vertex v in the output. Put simply, if task B depends on task A, then A must come first. Topological sort is the standard algorithm for turning a web of dependency relationships into a safe, executable sequence.

What Is Topological Sort?#

Course Prerequisites (DAG)
Rendering diagram...
Valid Topological Orders
Rendering diagram...

Key Properties#

  • Only works on DAGs: A cycle creates a logical contradiction — if A depends on B and B depends on A, neither can go first — so topological sort is undefined for cyclic graphs
  • Not unique: Multiple valid orderings typically exist; any ordering that respects all dependency edges is acceptable
  • Guaranteed to exist: Every DAG has at least one valid topological ordering, and often many

Real-World Applications#

ApplicationVerticesEdges
Build SystemsSource files, targetsDependencies between files
Package ManagersPackages (npm, pip)Package dependencies
Course SchedulingCoursesPrerequisites
Task SchedulingTasks/jobsTask dependencies
Spreadsheet FormulasCellsFormula references
Compilation OrderModulesImport/include dependencies

Two Approaches#

AlgorithmApproachKey Idea
Kahn's AlgorithmBFS-basedProcess vertices with in-degree 0, reduce neighbors' in-degrees
DFS-basedDFS post-orderAdd vertex to result after all descendants are processed

Kahn's Algorithm (BFS-based)#

The in-degree of a vertex is the number of edges pointing into it. A vertex with in-degree 0 has no prerequisites — it is safe to schedule immediately.

Kahn's algorithm operates iteratively: at each step it selects a vertex with in-degree 0, appends it to the output, and decrements the in-degree of each of its neighbors (since those dependencies are now satisfied). Any neighbor whose in-degree drops to zero is added to the queue and processed next. If the algorithm processes all vertices, the graph is acyclic and the output is a valid topological order; if some vertices remain unprocessed, they are part of a cycle.

Rendering diagram...

Kahn's Algorithm (BFS Topological Sort)

Topological sort using in-degree counting and BFS

Time:O(V + E)Space:O(V)Best:O(V + E)Worst:O(V + E)

DFS-based Topological Sort#

The DFS approach builds the ordering through reverse post-order. The key insight is this: a vertex is added to the result only after every vertex it points to (its descendants) has already been added. This guarantees that in the final output, every dependency appears before the vertex that depends on it.

Concretely, the implementation appends each vertex to a list after finishing its DFS subtree, then reverses the list at the end. An equivalent formulation is to prepend each vertex instead — the pseudocode below uses this approach to avoid an explicit reversal step.

DFS Post-Order
Rendering diagram...
Example: A → B → D, A → C → D
Rendering diagram...

The implementations below use a three-color marking scheme to track each vertex's state during DFS:

  • WHITE — unvisited; the vertex has not been reached yet
  • GRAY — in progress; the vertex is on the current recursion stack (its DFS call is still running)
  • BLACK — finished; the vertex and all of its descendants have been fully processed

A back edge occurs when DFS reaches a GRAY vertex — a vertex that is already on the current call stack. This means the graph contains a cycle, because we have found a path from a vertex back to one of its own ancestors.

DFS Topological Sort

Topological sort using DFS and reverse post-order

Time:O(V + E)Space:O(V)Best:O(V + E)Worst:O(V + E)

Comparison: Kahn's vs DFS#

AspectKahn's AlgorithmDFS-based
ApproachBFS with in-degree countingDFS with reverse post-order
Data StructureQueue + in-degree mapRecursion stack + visited set
Cycle DetectionImplicit: not all vertices processedExplicit: back edge detection
Level InformationCan track processing levelsNo natural level tracking
ParallelizationEasier to parallelizeHarder to parallelize
ImplementationMore complex setupSimpler if you know DFS

Practical Example: Build System#

Build System Task Ordering

Use topological sort to determine compilation order

Given tasks with dependencies, find a valid execution order respecting all dependencies

Cycle Detection via Topological Sort#

Cycle detection comes for free with both algorithms — you do not need a separate pass. The reasoning differs slightly between them:

  • Kahn's: Every vertex in a cycle has at least one unresolved incoming edge from another cycle vertex, so its in-degree never reaches zero. If BFS terminates before all vertices are processed, the unprocessed ones must form one or more cycles.
  • DFS: A back edge (reaching a GRAY vertex) means there is a path from a vertex back to one of its own ancestors, which is exactly a cycle.

In both cases, the cycle check is simply a consequence of the algorithm's normal bookkeeping:

AlgorithmCycle Detection
Kahn'sIf result has fewer vertices than graph, there's a cycle
DFSBack edge (visiting a GRAY vertex) indicates a cycle
Kahn's Cycle Detection
Rendering diagram...
DFS Cycle Detection
Rendering diagram...

Reviewing AI-Generated Code#

AI tools can generate topological sort code quickly, but they often get the edge cases wrong — particularly cycle detection and the reversal step in DFS. Knowing both algorithms well lets you spot these mistakes before they cause silent, hard-to-debug failures in production.

What to Verify#

IssueWhat to CheckCommon AI Mistake
DAG RequirementAlgorithm must detect/handle cyclesSilently returning invalid order for cyclic graph
Result OrderingDFS needs reversal, Kahn's is directForgetting to reverse DFS post-order result
In-degree InitInclude all vertices, even with in-degree 0Missing vertices not in any edge's destination
Neighbor UpdatesDecrease in-degree correctly in Kahn'sDecrementing wrong vertex or double-counting
All VerticesResult must include all V verticesStopping early or missing disconnected parts

Common AI Pitfalls#

Topological Sort Bugs

Common mistakes AI makes in topological sort

Correct Kahn's algorithm with cycle detection

Questions to Ask AI#

When reviewing AI-generated topological sort code, these targeted questions expose the most common omissions:

  • How does this detect a cycle in the input graph? What does it return if one exists?
  • Is the result guaranteed to be in valid topological order — does u appear before v for every directed edge u → v?
  • What does the algorithm return when multiple valid orderings exist?
  • Does this handle disconnected graphs where some vertices have no edges at all?

Summary#

ConceptKey Takeaway
Topological SortLinear ordering where u→v means u comes before v
Only for DAGsImpossible if graph has cycles
Kahn's AlgorithmBFS: process in-degree 0 vertices, update neighbors
DFS ApproachAdd vertex after all descendants processed (reverse post-order)
Cycle DetectionBoth algorithms naturally detect cycles
Not UniqueMultiple valid orderings may exist
ApplicationsBuild systems, package managers, task scheduling
Time ComplexityO(V + E) for both algorithms

Topological sort is indispensable wherever a system must respect ordering constraints. Build pipelines, package managers, task schedulers, spreadsheet engines, and compiler dependency analysis all rely on it. Understanding both algorithms gives you flexibility: Kahn's algorithm is more natural when you need level-based parallel scheduling or want cycle detection to identify the problematic vertices explicitly. DFS-based sort integrates cleanly into systems that already traverse the graph for other purposes, such as cycle detection or connectivity analysis. Both run in O(V + E) time, so the choice usually comes down to the surrounding context and which approach is clearer to reason about.