Graph Theory37 sections · 1633 units
Open in Course

Topological Order Refresher

Linear ordering of nodes

Topological sort gives you a valid processing order for a DAG. Two common algorithms: Kahn's algorithm (BFS-based) and DFS-based. Kahn's algorithm: Start with nodes that have no incoming edges. Remove them one by one, update incoming edge counts for neighbors, repeat.

DFS-based: Run DFS, add nodes to a stack on backtrack, then reverse the stack. Both run in O(V+E)O(V + E). Pick whichever you find clearer. I'll use DFS-based topological sort in the examples that follow.

Space complexity is O(V)O(V) for the data structures used.