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.
Problem
Section titled “Problem”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.
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. 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.
- Step of the reduction
- Edge of the reduction
- Redundant edge, left out
Solution
Section titled “Solution”// 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// 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// 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// 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 -> packageThe 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.
Good to know
Section titled “Good to know”- Only for DAGs. On a DAG, the reduction is unique.
Graphs.transitiveReductiongraphs::transitive_reductionGraphs.transitiveReductionGraphs.TransitiveReductionrejects a graph with a cycle (IllegalArgumentExceptionErrorKind::InvalidArgumentErrorArgumentException). A self-loop counts as a cycle. For a graph with cycles, take the transitive reduction of the condensed graph of its SCCs (strongly connected components). See Condense each group into one node to get a DAG. - Which edges go.
Graphs.diffgraphs::diffGraphs.diffGraphs.Difflists them with their edge indices. See Find the edges added and removed between two versions. - The other way.
Graphs.transitiveClosuregraphs::transitive_closureGraphs.transitiveClosureGraphs.TransitiveClosurecomputes the transitive closure, which adds an edge wherever a path leads. See Answer many reachability questions at once.