10.2 Find out cheaply whether a graph has changed
You get a 64-bit hash of the nodes and edges of a graph, and every run gives the same hash.
Problem
Section titled “Problem”Has the graph changed since last time? Comparing it with an old copy means keeping the copy. A hash of 64 bits is enough to tell, and the chance of an error is very small. You need this for:
- a build tool that skips an analysis when the dependency graph is the same as in the last run;
- a cache that needs a key for the results computed on a graph;
- an iteration that stops when a transformation no longer changes the graph.
Example
Section titled “Example”The example graph dependencies models the dependencies between the eight packages of a small
program. It has an edge from each package to each package it uses. The code builds the same graph a
second time from a list of pairs of node IDs. The pairs come in another
order, and one of them is given twice. See Build a graph from a list of
edges for this way of building a graph. The picture after shows a
later version of the program, which changes two dependencies. storage no longer uses model, so
the edge storage -> model is gone. The edge ui -> storage is new. The graph
nextVersionnext_versionnextVersionnextVersion holds this
later version. Do the hashes show which two graphs are equal and which one has changed?
Solution
Section titled “Solution”// The same graph built a second time, from its edges as pairs in another order, with// the pair {2, 4} given twice:int[][] uses = { {5, 7}, {2, 4}, {0, 3}, {6, 7}, {4, 5}, {2, 7}, {3, 5}, {1, 7}, {5, 4}, {0, 1}, {2, 3}, {3, 4}, {5, 6}, {0, 2}, {2, 4}};var builder = new GraphBuilder();builder.addNodes(8);for (int[] pair : uses) { builder.addEdge(pair[0], pair[1]);}Graph rebuilt = builder.build();
// The hash of dependencies, compared with those of the other two graphs:long hash = Graphs.topologyHash(dependencies);boolean sameForEqualGraphs = hash == Graphs.topologyHash(rebuilt); // trueboolean differentForNextVersion = hash != Graphs.topologyHash(nextVersion); // true// The same graph built a second time, from its edges as pairs in another order, with// the pair (2, 4) given twice:#[rustfmt::skip]let uses = [ (5, 7), (2, 4), (0, 3), (6, 7), (4, 5), (2, 7), (3, 5), (1, 7), (5, 4), (0, 1), (2, 3), (3, 4), (5, 6), (0, 2), (2, 4),];let mut builder = GraphBuilder::new();builder.add_nodes(8);for (source, target) in uses { builder.add_edge(source, target);}let rebuilt = builder.build();
// The hash of dependencies, compared with those of the other two graphs:let hash = graphs::topology_hash(&dependencies);let same_for_equal_graphs = hash == graphs::topology_hash(&rebuilt); // truelet different_for_next_version = hash != graphs::topology_hash(&next_version); // true// The same graph built a second time, from its edges as pairs in another order, with// the pair [2, 4] given twice:const uses = [ [5, 7], [2, 4], [0, 3], [6, 7], [4, 5], [2, 7], [3, 5], [1, 7], [5, 4], [0, 1], [2, 3], [3, 4], [5, 6], [0, 2], [2, 4]];const builder = new GraphBuilder();builder.addNodes(8);for (const [p, q] of uses) { builder.addEdge(p, q);}const rebuilt = builder.build();
// The hash of dependencies, a 64-bit bigint, compared with those of the other two// graphs:const hash = Graphs.topologyHash(dependencies);const sameForEqualGraphs = hash === Graphs.topologyHash(rebuilt); // trueconst differentForNextVersion = hash !== Graphs.topologyHash(nextVersion); // true// The same graph built a second time, from its edges as pairs in another order,// with the pair [2, 4] given twice:int[][] uses =[ [5, 7], [2, 4], [0, 3], [6, 7], [4, 5], [2, 7], [3, 5], [1, 7], [5, 4], [0, 1], [2, 3], [3, 4], [5, 6], [0, 2], [2, 4],];var builder = new GraphBuilder();builder.AddNodes(8);foreach (int[] pair in uses){ builder.AddEdge(pair[0], pair[1]);}Graph rebuilt = builder.Build();
// The hash of dependencies, compared with those of the other two graphs:long hash = Graphs.TopologyHash(dependencies);// truebool sameForEqualGraphs = hash == Graphs.TopologyHash(rebuilt);// truebool differentForNextVersion = hash != Graphs.TopologyHash(nextVersion);The graph built a second time (rebuilt) has the same hash as dependencies. It has the same nodes
and edges, although its pairs came in another order and one of them came twice. The later version
(nextVersionnext_versionnextVersionnextVersion) has
another hash.
Good to know
Section titled “Good to know”- The same in every run. The hash depends on nothing but the nodes and edges, so it can be
stored and compared across runs and machines. A release of Graphity that changes it says so in
its changelog.
Graph.hashCode<Graph as Hash>::hashGraph.hashCodeGraph.GetHashCodemakes no such promise. - The same numbering. The hash covers the node IDs and edges, not your names for the nodes. The same packages numbered in another order give another hash. Other packages numbered alike can give the same hash. Number the nodes the same way in every run, for example in the order of their sorted names. Where the names can change, keep them in the key next to the hash.
- Computed on every call. Compute the hash once and keep it, for example as the key of a cache.
- Not a proof. Equal hashes make equal graphs very likely, but do not prove them. Different
graphs can also be made to have equal hashes on purpose. Where it matters, compare with
Graph.equals<Graph as PartialEq>::eqGraph.equalsGraph.Equals. See Find the edges added and removed between two versions for how to find the differences.
API used
Section titled “API used”Graphs.topologyHash(Graph)graphs::topology_hashGraphs.topologyHashGraphs.TopologyHashGraph.hashCode()<Graph as Hash>::hashGraph.hashCodeGraph.GetHashCodeGraph.equals(Object)<Graph as PartialEq>::eqGraph.equalsGraph.EqualsGraphBuilder()GraphBuilder::newnew GraphBuildernew GraphBuilderGraphBuilder.addNodes(int)GraphBuilder::add_nodesGraphBuilder.addNodesGraphBuilder.AddNodesGraphBuilder.addEdge(int, int)GraphBuilder::add_edgeGraphBuilder.addEdgeGraphBuilder.AddEdgeGraphBuilder.build()GraphBuilder::buildGraphBuilder.buildGraphBuilder.Build