Topological Sort with Parallelized DFS

viaGlassdoor

Problem: Given a directed acyclic graph (DAG), produce a topological ordering of its nodes, and additionally discuss/implement how the traversal (DFS) could be parallelized across multiple workers/threads.

Constraints: Graph is a DAG (no cycles); nodes may have multiple independent branches that could be processed concurrently.

Example: Task dependency graph where some tasks have no interdependency and can run in parallel, while others must respect ordering.

Approach: Standard topological sort via DFS - visit each unvisited node, recursively visit all its neighbors, then push the node onto a stack after all its neighbors are processed; reverse the stack for the final order (or use Kahn's algorithm with in-degree counting and a queue, which parallelizes more naturally). For parallelization: process all nodes with in-degree 0 concurrently (they have no unresolved dependencies), then atomically decrement in-degrees of their neighbors as each finishes, adding newly-zero-in-degree nodes to the next parallel batch - this is essentially a level-by-level (BFS/Kahn's-style) parallel execution model, since pure DFS parallelizes less naturally than Kahn's algorithm.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account