Topological Sort with Parallelized DFS
Problem Given a directed acyclic graph (DAG), produce a topological ordering of its nodes, then extend the discussion to how the traversal could be parallelised across multiple workers or threads.
Input / Output
- Input: a DAG as an adjacency list over
nnodes. - Output: a linear ordering of all nodes such that every edge
u -> vplacesubeforev. Any valid ordering is acceptable — the ordering is generally not unique.
Constraints
- Graph is guaranteed acyclic.
- Up to ~10^5 nodes and edges; the graph may be disconnected.
- Independent branches exist that could legitimately be processed concurrently.
Example
- A task dependency graph:
build -> test,build -> lint,test -> deploy,lint -> deploy. Valid orders includebuild, test, lint, deployandbuild, lint, test, deploy—testandlintare mutually independent and could run at the same time. - Tricky case: an isolated node with no edges must still appear in the output.
asked …