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.

Build pipeline

The jobs of a CI pipeline for a web service, and how many minutes each one takes. Each edge points from a job to a job that waits for it.

A directed graph with 7 vertices and 9 edges.

Which waits between jobs can the pipeline drop, because a longer chain of jobs already forces the same order?

The reduction keeps 8 of the 9 edges. It drops Checkout to Docker image.

Step by step

  1. 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.
  2. Check the 2 edges out of Checkout, to Install and Docker image.
  3. Checkout to Docker image is implied by the longer path Checkout, Install, Compile, Docker image. Drop it.
  4. Keep the remaining edge out of Checkout, to Install.
  5. Check the 2 edges out of Install, to Lint and Compile. Neither target reaches the other, so keep both.
  6. Keep the only edge out of Lint, to Deploy. No other path leaves Lint.

3 more steps lead to the last one:

  1. Done. Deploy has no outgoing edges. The reduction keeps 8 of the 9 edges and drops 1 implied edge. 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

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.

All algorithms