2.2 Put the nodes in dependency order
You get a topological order, in which every node comes before its out-nodes.
Problem
Section titled “Problem”You have tasks that run one after the other, and some tasks need others to be finished first. You
want an order that respects all of these needs. As a graph, each task is a node, and an edge
a -> b says that a must come before b. The order you want lists every node before its
out-nodes. Such an order is called a topological order. You
need this for:
- a build, in which every step runs after the steps it needs;
- an installation, in which every package is installed after the packages it requires;
- a spreadsheet, in which every cell is computed after the cells it refers to.
Example
Section titled “Example”The example graph steps models the steps that build and publish a small program. It has an edge
a -> b if step b may start only after step a. The array names holds the names of the steps.
In which order can the steps run? The picture after shows the steps in the order found, one below
the other: every edge points downward.
- Step in topological order
Solution
Section titled “Solution”IntList order = TopOrder.of(steps); // [2, 0, 1, 4, 3, 5, 6, 7, 8]List<String> runOrder = order.intStream().mapToObj(step -> names[step]).toList();// runOrder: [lint, generate, compile, docs, compileTests, test, package, sign,// publish]let order = top_order::of(&steps)?; // [2, 0, 1, 4, 3, 5, 6, 7, 8]let run_order: Vec<&str> = order.iter().map(|&step| names[step as usize]).collect();// run_order: ["lint", "generate", "compile", "docs", "compileTests", "test",// "package", "sign", "publish"]const order = TopOrder.of(steps); // [2, 0, 1, 4, 3, 5, 6, 7, 8]const runOrder = Array.from(order, (step) => names[step]);// runOrder: [lint, generate, compile, docs, compileTests, test, package, sign,// publish]ImmutableArray<int> order = TopOrder.Of(steps); // [2, 0, 1, 4, 3, 5, 6, 7, 8]List<string> runOrder = [.. order.Select(step => names[step])];// runOrder: [lint, generate, compile, docs, compileTests, test, package, sign,// publish]TopOrder.oftop_order::ofTopOrder.ofTopOrder.Of returns the node IDs in topological
order, and the code turns them into names. Every step comes after the steps it needs: compile
comes after generate, and package comes after compile, lint and test. publish comes
last.
Good to know
Section titled “Good to know”- One of several orders. A graph has many topological orders when some nodes have no
path between them, like
lintandgenerate.TopOrder.oftop_order::ofTopOrder.ofTopOrder.Ofalways returns the same one for the same graph.TopOrderAndLevels.ofTopOrderAndLevels::ofTopOrderAndLevels.ofTopOrderAndLevels.Ofreturns another, which lists the nodes by level. See Find which nodes can be processed in parallel. - Edges that mean “depends on” point the other way than here: from a node to the nodes it needs. The order then lists every node before the nodes it depends on, so read it from the back to the front.
- Node IDs in this order. You can renumber a graph in a topological order, so that every edge leads to a higher node ID. See Number the nodes in a different order.
- Cycles. A graph with a cycle has no topological order, and
TopOrder.oftop_order::ofTopOrder.ofTopOrder.Ofrejects it. See Find out whether a graph has a cycle, and show one for how to check first. See Put the nodes in dependency order although the graph has cycles for an order despite cycles. Where edges mean “depends on”, you read that order from the front: it lists every node after the nodes it depends on. The exception is the nodes on a common cycle, which come together.