Home▸Articles▸Networks & Graph Theory

Topological Sort: Organizing Tasks with Kahn's Algorithm

A fundamental technique in graph theory that ensures tasks are completed in a valid order.

mysimulator teamUpdated June 2026≈ 3 min read▶ Open the simulation

What is Topological Sort?

Topological sort, or ordering, is a linear arrangement of vertices in a directed acyclic graph (DAG) such that for every directed edge uv from vertex u to v, u comes before v in the sequence. This technique is crucial in scheduling tasks with dependencies and resolving circular references.

Kahn's algorithm, named after Arthur B. Kahn Jr., provides an efficient method to perform topological sorting by repeatedly removing vertices with no incoming edges (in-degrees of zero) from the graph until all nodes are sorted or a cycle is detected.

How Does Kahn's Algorithm Work?

The algorithm begins by initializing an empty list for the topological sort and identifying all vertices with no incoming edges. These vertices are added to a queue. The process then iteratively removes nodes from the queue, adds them to the sorted list, and decreases the in-degrees of their neighbors. If any node’s in-degree becomes zero during this step, it is added to the queue. This continues until all nodes are processed or a cycle is detected.

A cycle detection mechanism ensures that no circular dependencies exist in the graph. If at any point there are still nodes with non-zero in-degrees and the queue is empty, a cycle exists, indicating an invalid DAG.

live demo · related simulation● LIVE

Why Does It Matter?

Topological sorting has numerous applications beyond task scheduling, including dependency resolution, course prerequisites, and even software package installation. By ensuring tasks are completed in the correct order, it prevents errors that could arise from incorrect dependencies.

In computer science, topological sorting is a key component of algorithms for data flow analysis, deadlock detection, and more complex graph-based problems.

Real-World Examples

Consider a project management scenario where tasks have dependencies. If Task B depends on Task A being completed first, the topological sort ensures that all such dependencies are respected, allowing for an efficient and error-free schedule.

In computer networks, routers use topological sorting to determine the order in which packets should be processed based on their routing tables.

Frequently asked questions

What is a directed acyclic graph (DAG)?

A DAG is a directed graph with no cycles, meaning there are no paths that start and end at the same vertex. This property makes topological sorting applicable to such graphs.

Can Kahn's algorithm handle cycles in a graph?

No, Kahn's algorithm cannot handle cycles as it relies on the absence of cycles for its operation. The presence of a cycle would indicate an invalid DAG and prevent correct topological sorting.

What happens if there are multiple valid topological sorts?

If there are no cycles, Kahn's algorithm will produce one of many possible valid topological sorts. However, the specific order may vary depending on the initial selection of nodes with zero in-degrees.

How can I detect a cycle in a graph using Kahn's algorithm?

If at any point there are still nodes with non-zero in-degrees and the queue is empty, it indicates that a cycle exists. This condition triggers an error or warning during the topological sort process.

Try it live

Everything above runs in your browser — open Topological Sort (Kahn): 2D Graph and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open Topological Sort (Kahn): 2D Graph simulation

What did you find?

Add reproduction steps (optional)