Codacoda
Back to Academy

graphs

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 comes before v. It uses either DFS or Kahn's algorithm (BFS with in-degree tracking). It is essential for scheduling tasks with dependencies.

Use Cases

  • Task scheduling with dependency resolution
  • Build systems and compilation order
  • Course prerequisite planning

Complexity Analysis

MetricBestAverageWorst
TimeO(V + E)O(V + E)O(V + E)
SpaceO(V)

Visualization

4251810263ABCDEF
Speed:1x
Graph readyStep 1 / 8

Implementation

Output

Click "Run Code" to see output...