Transitive reduction
Drops every edge that a longer path already implies. Each vertex still reaches the same vertices.
Time complexity O(V(V+E)), gograph function path.TransitiveReduction.
How it works
The transitive reduction keeps the fewest edges that still let every vertex reach the same vertices. An edge from A to C goes when there is already a longer path from A to C, such as A to B to C, since the path says the same thing. In a graph without cycles, only one graph fits that description.
path.TransitiveReduction takes each vertex's direct neighbors and drops any neighbor that another of those neighbors can already reach. The edges to the neighbors that are left are the ones the graph needs. It caches the vertices each neighbor reaches, so the work is O(V(V+E)) at most.
It returns a new graph with the weights of the edges it keeps, and leaves the input as it was. It returns an error for an undirected graph or one with a cycle. Use it to draw a dependency graph without the clutter of implied edges, or to see which requirements are direct.
Spreadsheet formulas
A sheet that works out the profit of one quarter. Row 2 holds the units sold from January to March, and G2 to G5 hold the price, unit cost, monthly fixed costs and tax rate. Rows 3 to 7 work out revenue, cost, profit before tax, tax and net profit, and E3, E7 and E8 hold the quarter's revenue, net profit and margin. Each cell takes 1 to 4 milliseconds to work out. Each edge points from a cell to a formula that reads it.
A directed graph with 25 vertices and 41 edges.
Which references can a diagram of the sheet leave out, because a longer chain of formulas already connects the same cells?
The reduction keeps 38 of the 41 edges. It drops B5 to B7, C5 to C7 and D5 to D7.
Step by step
- Check the edges out of each vertex in turn. An edge is implied when a longer path already leads from its start to its end, so the reduction drops it.
- Check the 2 edges out of B2, to B3 and B4. Neither target reaches the other, so keep both.
- Check the 2 edges out of C2, to C3 and C4. Neither target reaches the other, so keep both.
- Check the 2 edges out of D2, to D3 and D4. Neither target reaches the other, so keep both.
- Check the 3 edges out of G2, to B3, C3 and D3. No target reaches another, so keep all 3.
- Check the 3 edges out of G3, to B4, C4 and D4. No target reaches another, so keep all 3.
25 more steps lead to the last one:
- Done. E8 has no outgoing edges. The reduction keeps 38 of the 41 edges and drops 3 implied edges. Every vertex still reaches the same vertices.
Use it in Go
reduced, err := path.TransitiveReduction(g)
if err != nil {
return err
}
for _, e := range reduced.AllEdges() {
fmt.Println(e.Source().Label(), "->", e.Destination().Label())
}
Example graphs
- Build pipeline, 7 vertices and 9 edges
- Course prerequisites, 14 vertices and 18 edges
- Spreadsheet formulas, 25 vertices and 41 edges
- Go module graph, 62 vertices and 116 edges
- Monorepo build, 100 vertices and 266 edges
More in Dependencies
- Descendants: Finds every vertex reachable from a vertex, such as all the jobs that wait for one step of a build.
- Ancestors: Finds every vertex that can reach a vertex, such as all the courses one course needs first.
- Affected: Finds the changed vertices and every vertex reachable from them, such as all the cells a spreadsheet edit recomputes.
- Critical path: Finds the longest chain of tasks that wait on each other, which sets how long the whole run takes however many tasks run in parallel.
- Diff: Compares two graphs and returns the vertices, edges, weights and edge labels that differ, matching vertices by label.