Diff

Compares two graphs and returns the vertices, edges, weights and edge labels that differ, matching vertices by label.

Time complexity O(V+E), gograph function gograph.Diff.

How it works

A dependency upgrade changes more of the module graph than the one line in go.mod: modules come and go, and versions move under the ones that stay. gograph.Diff compares two graphs and returns a GraphDiff with the vertices and edges added, removed and changed, and whether the graph became weighted or acyclic.

It matches vertices by label and edges by the labels of their two ends, so the two graphs can be built in any order. A vertex changes when its weight does, and an edge when its weight or its edge label does. A removed vertex takes its edges with it, and RemovedEdges lists those edges too, so the diff is complete on its own. In an undirected graph, each edge is listed once. Two graphs where one is directed and the other isn't return ErrIncompatibleGraphs.

GraphDiff.String prints one change per line, which is what a command line tool or a CI job would show, and the GraphDiff encodes as JSON. Apply makes the changes to a copy of the first graph, so Diff of that copy and the second graph is empty. WithWeightTolerance treats weights that differ by a little as equal. Diff takes O(V+E) for the vertices and edges of both graphs.

Office network

Switches and rooms in a four-team office, wired through a core switch.

An undirected graph with 13 vertices and 16 edges.

Core and Support go down for maintenance. Which links go down with them?

The diff has 2 vertices removed and 7 edges removed. Apply on the old graph gives a graph whose Diff with the new one is empty.

Step by step

  1. Diff matches the vertices of the two graphs by label and their edges by the labels of their ends, and finds 9 differences. The graph shows the old one. What only the new graph has is faint until a step adds it. The steps make the changes in the order Apply does: removals, then additions, then changes.
  2. Remove the edge between Core and Sales, since Core goes too.
  3. Remove the edge between Core and Support, since Core and Support go too.
  4. Remove the edge between Core and Ops, since Core goes too.
  5. Remove the edge between Core and Dev, since Core goes too.
  6. Remove the edge between Support and Uplink, since Support goes too.

4 more steps lead to the last one:

  1. Done. The old graph with these 9 changes is the new one: Apply makes the same changes to a copy of the old graph, and Diff of that copy and the new graph is empty.

Use it in Go

d, err := gograph.Diff(before, after)
if err != nil {
	return err // gograph.ErrIncompatibleGraphs
}
fmt.Println(d) // + vertex, - edge, ~ edge ... weight 5 -> 7

upgraded, err := d.Apply(before)
if err != nil {
	return err
}
again, err := gograph.Diff(upgraded, after)
if err != nil {
	return err
}
fmt.Println(again.IsEmpty()) // true

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.
  • Transitive reduction: Drops every edge that a longer path already implies. Each vertex still reaches the same vertices.
  • 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.

All algorithms