JavaScript Snippet

Topological Sort (Kahn's Algorithm)

Difficulty: Hard

Topological sort orders the nodes of a DAG so that every edge points from earlier to later. It is the algorithm behind build-system dependency resolution, course-prerequisite scheduling, and pipelined computation. This snippet covers Kahn's BFS-based algorithm with indegree tracking, a DFS-based variant that produces the same order via post-order, and a cycle-detection variant that returns null when the graph is not acyclic.

Code Snippets
/

Topological Sort (Kahn's Algorithm)

Topological Sort (Kahn's Algorithm)

Topological sort orders the nodes of a DAG so that every edge points from earlier to later. It is the algorithm behind build-system dependency resolution, course-prerequisite scheduling, and pipelined computation. This snippet covers Kahn's BFS-based algorithm with indegree tracking, a DFS-based variant that produces the same order via post-order, and a cycle-detection variant that returns null when the graph is not acyclic.

JavaScript
Hard
algorithms
topological-sort
code-template
bfs

681 views

7

This code snippet is available for premium members only.

Upgrade to Premium