algorithm

Kahn Algorithm

The Kahn Algorithm is a topological sorting algorithm used to linearly order the vertices of a directed acyclic graph (DAG) such that for every directed edge from vertex u to vertex v, u comes before v in the ordering. It works by repeatedly removing vertices with no incoming edges (in-degree zero) and updating the graph, producing a valid topological order if one exists. This algorithm is efficient, typically running in O(V + E) time, where V is the number of vertices and E is the number of edges.

Also known as: Kahn's Algorithm, Topological Sort Algorithm, Kahn's Topological Sort, In-degree based topological sort, BFS-based topological sort
🧊Why learn Kahn Algorithm?

Developers should learn the Kahn Algorithm when working with dependency resolution problems, such as task scheduling, build systems (e.g., Make, npm), or course prerequisites, where tasks or nodes must be processed in a specific order without cycles. It is particularly useful in scenarios involving directed graphs where cycles must be detected, as the algorithm fails if a cycle is present, indicating an invalid ordering. This makes it essential for applications in compiler design, package management, and project management tools.

Compare Kahn Algorithm

Learning Resources

Related Tools

Alternatives to Kahn Algorithm