Deviopath › JavaScript

Task Order (Topological Sort)

Hard · Graphs

Write a function topoSort that orders the names in tasks so that every dependency is respected. Each item of deps is a pair [before, after]: before must come earlier than after.

Whenever several tasks are ready (all their dependencies are done), pick the alphabetically smallest one first. If the dependencies contain a cycle, return [].

Next: Detect a Cycle