DrawCode Algorithm Visualizer • Graph • Medium

Topological Sort

Tags: Graph, DAG, Ordering, DFS, Kahn

1. One-Liner

A topological order of a DAG lists vertices so every edge u→v appears with u before v.


2. The Problem It Solves

Prerequisites: course schedules, build systems (Make), task scheduling with dependencies — any partial order you must linearize.


3. The Core Idea

DFS postorder (reverse finish times) or Kahn: repeatedly remove vertices with in-degree 0 (sources).


4. How It Works (Step-by-Step)

MethodSteps
DFSRun DFS; push vertex to order after exploring all outgoing edges; reverse
KahnQueue all in-degree 0; pop u, append, decrement neighbors' in-degree; repeat

5. Dry Run Example

Edges: 0→1, 0→2, 1→3. Valid topsorts include 0,1,2,3 or 0,2,1,3. If you add 3→0, graph has a cycle — no topological order.


6. Key Properties

PropertyValue
Exists iffGraph is a DAG
Not uniqueOften many valid orders
TimeO(V + E)

7. Where It Is Used

SystemUse
npm / GradleTask dependency order
CompilerSymbol dependency resolution

8. Interview Tips

Detect cycle during DFS (back edge) or when Kahn processes < V vertices. Follow-ups: longest path in DAG, critical path.


9. Comparison with Other Algorithms

DFS topoKahn
StyleRecursive stackBFS-like queue
Cycle detectBack edgeCount < V

10. Complexity

----
TimeO(V + E)
SpaceO(V)

Implementation Example (PYTHON)

def topological_sort_dfs(n, adj):
    vis = [0] * n
    order = []
    def dfs(u):
        vis[u] = 1
        for v in adj[u]:
            if vis[v] == 1: raise ValueError('cycle')
            if vis[v] == 0: dfs(v)
        vis[u] = 2
        order.append(u)
    for i in range(n):
        if vis[i] == 0: dfs(i)
    return order[::-1]

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer