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 . Pick whichever you find clearer. I'll use DFS-based topological sort in the examples that follow.
Space complexity is for the data structures used.