Post-Order
We perform DFS and wait for a node to "finish" all its neighbors before adding it to our topological sort.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
A linear ordering of vertices in a Directed Acyclic Graph (DAG). The DFS approach works by visiting nodes and adding them to the result list in reverse order of their completion.
We perform DFS and wait for a node to "finish" all its neighbors before adding it to our topological sort.
A recursion stack tracks the currently active nodes. This helps detect if the graph is truly acyclic.
If we revisit a node that is already on the recursion stack, we've found a cycle! Topological sort is impossible.
Because we add nodes as they finish, the result is in reverse topological order. We prepend them to get the final list.