10.3 Find the edges added and removed between two versions
You list the edges added and removed between two graphs over the same nodes.
Problem
Section titled “Problem”What changed between two versions of a graph? You want the edges that were added and the edges that were removed. You need this for:
- a review of the dependencies that a change adds or removes;
- a monitor that reports the network links between servers that appeared or disappeared since yesterday;
- an incremental analysis that updates its results instead of starting over.
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. In version 1, model and
storage use each other. The edges between them lie on a cycle, which the
picture before shows in orange. In version 2, storage no longer uses model, which breaks the
cycle. But ui now uses storage. The graph
nextVersionnext_versionnextVersionnextVersion holds
version 2. Which edges were added, and which were removed? The picture after shows version 2. The
added edge ui -> storage is in solid green. The removed edge storage -> model is in dashed
green, because version 2 no longer has it.
- Edge on a cycle
- Added edge
- Removed edge
Solution
Section titled “Solution”GraphDiff diff = Graphs.diff(dependencies, nextVersion);Edges added = diff.addedEdges(); // ui -> storage, edge 6 of nextVersionEdges removed = diff.removedEdges(); // storage -> model, edge 10 of dependenciesboolean unchanged = diff.isEmpty(); // falselet diff = graphs::diff(&dependencies, &next_version);let added = diff.added_edges(); // ui -> storage, edge 6 of next_versionlet removed = diff.removed_edges(); // storage -> model, edge 10 of dependencieslet unchanged = diff.is_empty(); // falseconst diff = Graphs.diff(dependencies, nextVersion);const added = diff.addedEdges; // ui -> storage, edge 6 of nextVersionconst removed = diff.removedEdges; // storage -> model, edge 10 of dependenciesconst unchanged = diff.isEmpty; // falseGraphDiff diff = Graphs.Diff(dependencies, nextVersion);Edges added = diff.AddedEdges; // ui -> storage, edge 6 of nextVersionEdges removed = diff.RemovedEdges; // storage -> model, edge 10 of dependenciesbool unchanged = diff.IsEmpty; // falseThe diff has one added edge and one removed edge. The added edge is ui -> storage, with the edge
index 6 in version 2
(nextVersionnext_versionnextVersionnextVersion). The
removed edge is storage -> model, with the edge index 10 in version 1 (dependencies). With these
edge indices, you can look up your data on the edges of either version.
An EdgesEdgesEdgesEdges lists its edges position by position: the
source in Edges.sourceNodesEdges::source_nodesEdges.sourceNodesEdges.SourceNodes, the target in
Edges.targetNodesEdges::target_nodesEdges.targetNodesEdges.TargetNodes and the edge index in Edges.edgeIndicesEdges::edge_indicesEdges.edgeIndicesEdges.EdgeIndices.
See Work with the lists Graphity returns.
Good to know
Section titled “Good to know”- The same nodes. Both graphs must have the same nodes: the same node count, and the same
node ID for the same package in both.
Graphs.diffgraphs::diffGraphs.diffGraphs.Diffrejects two graphs with different node counts (IllegalArgumentExceptionpanicErrorArgumentException). - Versions with different nodes. Number the nodes over both versions together, so that a node keeps its node ID. For example, give one node ID to each package name that occurs in either version. A map from names to node IDs does that. See Build a graph from objects of your own for such a map. A node that one version lacks has no edges there. The diff then also lists the edges of the nodes that were added or removed. A node without edges does not appear in the diff, so compare the lists of names as well.
- Equal or not.
GraphDiff.isEmptyGraphDiff::is_emptyGraphDiff.isEmptyGraphDiff.IsEmptyanswers this: an empty diff means equal graphs. - In edge index order. Both lists are in edge index order, that is, by source and then by target. So the changed outgoing edges of one package stand together.
- Only whether it changed? A hash answers that without the old graph at hand. See Find out cheaply whether a graph has changed.
API used
Section titled “API used”Graphs.diff(Graph, Graph)graphs::diffGraphs.diffGraphs.DiffGraphDiff.addedEdges()GraphDiff::added_edgesGraphDiff.addedEdgesGraphDiff.AddedEdgesGraphDiff.removedEdges()GraphDiff::removed_edgesGraphDiff.removedEdgesGraphDiff.RemovedEdgesGraphDiff.isEmpty()GraphDiff::is_emptyGraphDiff.isEmptyGraphDiff.IsEmptyEdges.sourceNodes()Edges::source_nodesEdges.sourceNodesEdges.SourceNodesEdges.targetNodes()Edges::target_nodesEdges.targetNodesEdges.TargetNodesEdges.edgeIndices()Edges::edge_indicesEdges.edgeIndicesEdges.EdgeIndices