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.

Course prerequisites

Fourteen courses of a computer science degree, each one semester long. Each edge points from a course to a course that requires it.

A directed graph with 14 vertices and 18 edges.

Which prerequisites can the course list leave out, because they already come through another course?

The reduction keeps 17 of the 18 edges. It drops Discrete math to Algorithms.

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. Keep the only edge out of Programming 1, to Programming 2. No other path leaves Programming 1.
  3. Check the 3 edges out of Discrete math, to Data structures, Probability and Algorithms.
  4. Discrete math to Algorithms is implied by the longer path Discrete math, Data structures, Algorithms. Drop it.
  5. Keep the 2 remaining edges out of Discrete math, to Data structures and Probability.
  6. Check the 2 edges out of Calculus 1, to Calculus 2 and Linear algebra. Neither target reaches the other, so keep both.

7 more steps lead to the last one:

  1. Done. The 4 vertices with no outgoing edges have nothing to check. The reduction keeps 17 of the 18 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