AlgoScope

DFS Topological Sort

AlgorithmintermediateTopological Sort

Reverse postorder of a DFS is a topological order, and a back edge means there is none.

Decision · step 2 of 14Topological Sort: DFS finish order
012345u

Enter 0. Its edges go to 1, 2. Unvisited: 1, 2. Go into 1 first.

Open in the player →or start at step 2

What you will see

Vertices are pushed on a stack when they finish; popping the stack gives the order.

How topological sort works →

Cost

BestO(V + E)
AverageO(V + E)
WorstO(V + E)
SpaceO(V)

How you work with it here

play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.

Screen readers: Vertices announce name, discovery numbers where relevant and component; each step announces the structural conclusion (bridge found, component closed).

Reduced motion: Component colouring and ordering changes crossfade in place.

Taught by the same lesson

Topological Sort covers these too, in the same run.