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.
Go module graph
The module graph of github.com/gin-gonic/gin v1.9.1, as go mod graph prints it. Each edge points from a module to a module version it requires.
A directed graph with 62 vertices and 116 edges.
Which requirements can a picture of the module graph leave out, because a path through other modules already leads to the same version?
The reduction keeps 76 of the 116 edges. It drops github.com/bytedance/sonic@v1.5.0 to github.com/klauspost/cpuid/v2@v2.0.9, github.com/bytedance/sonic@v1.9.1 to github.com/davecgh/go-spew@v1.1.1, github.com/bytedance/sonic@v1.9.1 to github.com/klauspost/cpuid/v2@v2.0.9 and 37 more.
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 27 edges out of github.com/gin-gonic/gin.
- github.com/gin-gonic/gin to github.com/chenzhuoyu/base64x@v0.0.0-20221115062448-fe3a3abad311 is implied by the longer path github.com/gin-gonic/gin, github.com/bytedance/sonic@v1.9.1, github.com/chenzhuoyu/base64x@v0.0.0-20221115062448-fe3a3abad311. Drop it.
- github.com/gin-gonic/gin to github.com/davecgh/go-spew@v1.1.1 is implied by a longer path of 7 edges through github.com/bytedance/sonic@v1.9.1. Drop it.
- github.com/gin-gonic/gin to github.com/pmezard/go-difflib@v1.0.0 is implied by a longer path of 8 edges through github.com/bytedance/sonic@v1.9.1. Drop it.
- github.com/gin-gonic/gin to github.com/twitchyliquid64/golang-asm@v0.15.1 is implied by the longer path github.com/gin-gonic/gin, github.com/bytedance/sonic@v1.9.1, github.com/chenzhuoyu/base64x@v0.0.0-20221115062448-fe3a3abad311, github.com/bytedance/sonic@v1.5.0, github.com/twitchyliquid64/golang-asm@v0.15.1. Drop it.
82 more steps lead to the last one:
- Done. The 27 vertices with no outgoing edges have nothing to check. The reduction keeps 76 of the 116 edges and drops 40 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.