Skip to content

4.6 Remove edges that other paths make redundant

You get the transitive reduction of a DAG (directed acyclic graph), in which every node reaches the same nodes as before, with as few edges as possible.

You want to remove the edges of a graph that add nothing. An edge adds nothing if a path through other nodes already leads from its source to its target. Such an edge is called redundant. Removing redundant edges makes a graph easier to read and to draw, and shows the edges that matter. Redundant edges are found in:

  • dependencies, where a package uses another that one of its other dependencies brings anyway;
  • plans, where the order of two steps already follows from the steps between them;
  • drawings of hierarchies, where a longer path also leads from the source of an edge to its target.

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. package waits for compile - but it also waits for test, which waits for compileTests, which waits for compile. Which edges are redundant? The picture after shows the reduction in green. The dashed edge compile -> package is left out.

The steps of the build
The transitive reduction and the redundant edge
0: generate1: compile2: lint3: compileTests4: docs5: test6: package7: sign8: publish
0: generate1: compile2: lint3: compileTests4: docs5: test6: package7: sign8: publish
Output
  • Step of the reduction
  • Edge of the reduction
  • Redundant edge, left out
// steps has no cycles.
// Leave out every edge that a longer path makes redundant.
Graph reduction = Graphs.transitiveReduction(steps);
int edgeCount = reduction.edgeCount(); // 9 - one fewer than the 10 of steps
// Which edges are redundant? Those the reduction leaves out.
Edges leftOut = Graphs.diff(steps, reduction).removedEdges();
// leftOut: compile -> package, edge 3 of steps -
// compile -> compileTests -> test -> package
Full example
// steps has no cycles.
// Leave out every edge that a longer path makes redundant.
let reduction = graphs::transitive_reduction(&steps)?;
let edge_count = reduction.edge_count(); // 9 - one fewer than the 10 of steps
// Which edges are redundant? Those the reduction leaves out.
let left_out = graphs::diff(&steps, &reduction).removed_edges().clone();
// left_out: compile -> package, edge 3 of steps -
// compile -> compileTests -> test -> package
Full example
// steps has no cycles.
// Leave out every edge that a longer path makes redundant.
const reduction = Graphs.transitiveReduction(steps);
const edgeCount = reduction.edgeCount; // 9 - one fewer than the 10 of steps
// Which edges are redundant? Those the reduction leaves out.
const leftOut = Graphs.diff(steps, reduction).removedEdges;
// leftOut: compile -> package, edge 3 of steps -
// compile -> compileTests -> test -> package
Full example
// steps has no cycles.
// Leave out every edge that a longer path makes redundant.
Graph reduction = Graphs.TransitiveReduction(steps);
int edgeCount = reduction.EdgeCount; // 9 - one fewer than the 10 of steps
// Which edges are redundant? Those the reduction leaves out.
Edges leftOut = Graphs.Diff(steps, reduction).RemovedEdges;
// leftOut: compile -> package, edge 3 of steps -
// compile -> compileTests -> test -> package
Full example

The reduction has 9 edges instead of 10. It leaves out compile -> package, which is the only edge that another path replaces. Each step reaches the same steps as before.