> [[Graph traversal]] in which each [[Node|node]] `v` is visited only after all its dependencies are visited. ## Script ```js const topological = graph => { let order = null if (!cycles(graph).hasCycle) { order = depthFirstOrder(graph).reversePost } return { isDag: order !== null, order } } ``` Depends on: - [[Cycles]] to determine before whether dealing with [[Directed acyclic graph|DAG]] - [[Depth First Order#Reverse post]] ## Example [[tinyGraph]] topologically sorted: `2 3 8 7 6 0 1 5 4 9 10 11 12` ![[tinyGraph#^mermaid]] [^1]: https://en.wikipedia.org/wiki/Topological_sorting