Skip to content

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.

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.

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.

The packages, version 1
Version 2, with ui -> storage added and storage -> model removed
0: app1: cli2: ui3: service4: model5: storage6: json7: util
0: app1: cli2: ui3: service4: model5: storage6: json7: util
Context
  • Edge on a cycle
Output
  • Added edge
  • Removed edge
GraphDiff diff = Graphs.diff(dependencies, nextVersion);
Edges added = diff.addedEdges(); // ui -> storage, edge 6 of nextVersion
Edges removed = diff.removedEdges(); // storage -> model, edge 10 of dependencies
boolean unchanged = diff.isEmpty(); // false
Full example
let diff = graphs::diff(&dependencies, &next_version);
let added = diff.added_edges(); // ui -> storage, edge 6 of next_version
let removed = diff.removed_edges(); // storage -> model, edge 10 of dependencies
let unchanged = diff.is_empty(); // false
Full example
const diff = Graphs.diff(dependencies, nextVersion);
const added = diff.addedEdges; // ui -> storage, edge 6 of nextVersion
const removed = diff.removedEdges; // storage -> model, edge 10 of dependencies
const unchanged = diff.isEmpty; // false
Full example
GraphDiff diff = Graphs.Diff(dependencies, nextVersion);
Edges added = diff.AddedEdges; // ui -> storage, edge 6 of nextVersion
Edges removed = diff.RemovedEdges; // storage -> model, edge 10 of dependencies
bool unchanged = diff.IsEmpty; // false
Full example

The 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.

  • 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.Diff rejects 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.IsEmpty answers 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.