Skip to content

3.3 Put the nodes in dependency order although the graph has cycles

You get an order of the strongly connected components in which each comes after the ones it depends on.

You need an order in which every node comes after the nodes it depends on. But the graph has cycles, so there is no topological order. For a graph without cycles, see Put the nodes in dependency order. The nodes on a cycle cannot be ordered among themselves, so they have to be handled together. The other nodes can still be ordered before and after such a group. You need this for:

  • a build, in which packages that use each other are built together;
  • the initialization of modules or services that refer to each other;
  • the evaluation of formulas that refer to each other: a group of formulas that does so in a cycle is computed together, again and again until its values no longer change.

The example graph dependencies models the dependencies between the packages of a small program. It has an edge from each package to each package it uses. The array names holds the names of the packages. model and storage use each other, so no order puts each package after all packages it uses. In which order can the packages be built? The picture after shows the SCCs in green frames, with their IDs. util comes first and app comes last. model and storage come together: the two orange edges of their cycle lie inside one frame.

The packages and their dependencies
The SCCs, numbered in dependency order
7: util1: cli6: json4: model5: storage3: service2: ui0: app
7: util1: cli6: json4: model5: storage3: service2: ui0: appSCC 6SCC 5SCC 4SCC 3SCC 2SCC 1SCC 0
Input
  • Package asked about
Context
  • Edge on a cycle
Output
  • SCC

The SCC IDs are already the order:

// model and storage use each other.
Sccs sccs = Components.sccsOf(dependencies);
// The SCCs in the order of their IDs: each after the SCCs it uses.
List<List<String>> buildOrder = new ArrayList<>();
for (int scc = 0; scc < sccs.sccCount(); scc++) {
buildOrder.add(sccs.nodesOf(scc).intStream().mapToObj(p -> names[p]).toList());
}
// buildOrder: [[util], [cli], [json], [model, storage], [service], [ui], [app]]
// Which of two packages comes first? Compare their SCC IDs.
boolean jsonBeforeUi = sccs.sccOf(json) < sccs.sccOf(ui); // true
Full example
// model and storage use each other.
let sccs = components::sccs_of(&dependencies);
// The SCCs in the order of their IDs: each after the SCCs it uses.
let build_order: Vec<Vec<&str>> = (0..sccs.scc_count())
.map(|scc| {
sccs.nodes_of(scc)
.iter()
.map(|&p| names[p as usize])
.collect()
})
.collect();
// build_order: [["util"], ["cli"], ["json"], ["model", "storage"], ["service"],
// ["ui"], ["app"]]
// Which of two packages comes first? Compare their SCC IDs.
let json_before_ui = sccs.scc_of(json) < sccs.scc_of(ui); // true
Full example
// model and storage use each other.
const sccs = Components.sccsOf(dependencies);
// The SCCs in the order of their IDs: each after the SCCs it uses.
const buildOrder: string[][] = [];
for (let scc = 0; scc < sccs.sccCount; scc++) {
buildOrder.push(Array.from(sccs.nodesOf(scc), (p) => names[p]));
}
// buildOrder: [[util], [cli], [json], [model, storage], [service], [ui], [app]]
// Which of two packages comes first? Compare their SCC IDs.
const jsonBeforeUi = sccs.sccOf(json) < sccs.sccOf(ui); // true
Full example
// model and storage use each other.
Sccs sccs = Components.SccsOf(dependencies);
// The SCCs in the order of their IDs: each after the SCCs it uses.
List<List<string>> buildOrder = [];
for (int scc = 0; scc < sccs.SccCount; scc++)
{
buildOrder.Add([.. sccs.NodesOf(scc).Select(p => names[p])]);
}
// buildOrder: [[util], [cli], [json], [model, storage], [service], [ui], [app]]
// Which of two packages comes first? Compare their SCC IDs.
bool jsonBeforeUi = sccs.SccOf(json) < sccs.SccOf(ui); // true
Full example

The result lists the packages SCC by SCC, in the order of the SCC IDs of the picture after. Each package comes after the packages it uses. The exception is the one SCC of two packages, which must be built together. To compare the positions of two packages, compare their SCC IDs: json comes before ui.

  • Why the IDs are an order. Components.sccsOfcomponents::sccs_ofComponents.sccsOfComponents.SccsOf numbers the SCCs so that every edge between two SCCs goes from the higher SCC ID to the lower one: SCC 0 has no edge to another SCC. On edges that mean “uses” or “depends on”, the IDs from 0 upward are therefore an order in which every SCC comes after the SCCs it uses. That order is the reverse of a topological order of the SCCs.
  • Without cycles. In a DAG (directed acyclic graph), every SCC is a single node, and the SCC IDs order the nodes themselves. That order is the reverse of a topological order.
  • The condensed graph. See Condense each group into one node to get a DAG for when you need its edges and not just the order.