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.
Campus network
A hundred switches, servers and computers in ten buildings of a university campus, A to J. Each id is a building letter and a number, so D7 is number 7 in building D. A is the data center, where the core switches A1 and A2 link to the main switch of every other building, always number 1. The larger buildings have more switches between the main switch and the computers, and two pairs of neighboring buildings also have a direct link.
An undirected graph with 100 vertices and 114 edges.
The core switches A1 and A2 go down. Which links does the campus network lose?
The diff has 2 vertices removed and 23 edges removed. Apply on the old graph gives a graph whose Diff with the new one is empty.
Step by step
- Diff matches the vertices of the two graphs by label and their edges by the labels of their ends, and finds 25 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.
- Remove the edge between A1 and A2, since A1 and A2 go too.
- Remove the edge between A1 and A3, since A1 goes too.
- Remove the edge between A1 and A4, since A1 goes too.
- Remove the edge between A1 and B1, since A1 goes too.
- Remove the edge between A1 and C1, since A1 goes too.
20 more steps lead to the last one:
- Done. The old graph with these 25 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
- Delivery area, 8 vertices and 12 edges
- Office network, 13 vertices and 16 edges
- Karate club, 34 vertices and 78 edges
- Go module upgrade, 67 vertices and 131 edges
- Campus network, 100 vertices and 114 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.
- 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.