schemedoc / cookbook

New Scheme Cookbook
https://cookbook.scheme.org
29 stars 3 forks source link

[Recipe] Topological sort #44

Closed lassik closed 2 years ago

lassik commented 2 years ago

Problem

Given a directed acyclic graph (DAG) describing how nodes depend on other nodes, visit the nodes in an order where all dependencies of each node are visited before that node. This algorithm is known as topological sort.

For example, the Unix make program does a topological sort to figure out the build order for the files.

Solution

Public domain, from Shiro Kawai: https://github.com/shirok/Gauche/blob/master/lib/util/toposort.scm

lassik commented 2 years ago

https://en.wikipedia.org/wiki/Topological_sorting

lassik commented 2 years ago

Now at https://cookbook.scheme.org/topological-sort/