Skip to content
DSA Visualizerpreview

Topological sort (Kahn)

Order tasks so every arrow points forward; if that is impossible, there is a cycle.

O(V + E) time · O(V) space

Directed edges101-201, 101-202, 150-202, 201-301, 202-301, 202-310, 301-401, 310-401

Count incoming edges for each node (shown after the dot). Ready now: 101, 150.

Graph with nodes 101, 150, 201, 202, 301, 310, 401 and edges 101→201, 101→202, 150→202, 201→301, 202→301, 202→310, 301→401, 310→401.
  • compare
  • swap or write
  • settled

1 / 17

Speed

Space play · ← → step · Home End jump · drag the strip to scrub