Skip to content

1.4 Combine several graphs into one

You put graphs side by side in one graph, with edges between them, and extend a graph by new edges.

You have several graphs and want to analyze them as one. The node IDs of each graph start at 0, so their edges cannot simply be copied into one list. Or you have a graph and want a version of it with a few more edges. You need this for:

  • the dependencies of a program together with those of the libraries it uses;
  • the call graphs of several modules, analyzed as one program;
  • a graph extended by edges that an analysis found or a user added.

The first example graph dependencies models the dependencies between the eight packages of a small program. The second example graph library models the dependencies between the three packages of a library for JSON (JavaScript Object Notation): api, parser and writer. Both have an edge from each package to each package it uses. The package json of the program uses the library. Together they make one graph of eleven packages. In it, the node IDs of the library come after those of the program, and json uses api. The frames show which graph each node comes from:

The program, the library and the two ends of the edge between them
The combined graph, with new node IDs and added edges
0: app1: cli2: ui3: service4: model5: storage6: json7: util0: api1: parser2: writerlibraryprogram
0: app1: cli2: ui3: service4: model5: storage6: json7: util8: api9: parser10: writerlibraryprogram
Input
  • Added graph
  • End of the edge between the graphs
Output
  • Node with its new node ID
  • Added edge

In the picture after, the nodes of the library are green: the combined graph gives them the new node IDs 8 to 10. The green edges are the ones it adds to the program: api -> parser and api -> writer of the library, and json -> api between the two graphs.

GraphBuilder.addGraphGraphBuilder::add_graphGraphBuilder.addGraphGraphBuilder.AddGraph adds each graph after the nodes the builder already has. It returns the offset: node v of the added graph becomes node offset + v.

// library is a JSON library of three packages - api uses parser and writer - that the
// program's package json (6) uses.
var libraryBuilder = new GraphBuilder();
libraryBuilder.addNodes(3);
libraryBuilder.addOutEdges(0, 1, 2); // api -> parser, api -> writer
Graph library = libraryBuilder.build();
// Put both into one graph; the nodes of each graph keep their order, after an offset.
var builder = new GraphBuilder();
int programOffset = builder.addGraph(dependencies); // 0 - the program keeps 0 to 7
// 8 - the library becomes 8 to 10
int libraryOffset = builder.addGraph(library);
builder.addEdge(programOffset + 6, libraryOffset); // json -> api
Graph combined = builder.build();
// combined: 11 nodes, 17 edges
Full example
// library is a JSON library of three packages - api uses parser and writer - that the
// program's package json (6) uses.
let mut library_builder = GraphBuilder::new();
library_builder.add_nodes(3);
library_builder.add_out_edges(0, &[1, 2]); // api -> parser, api -> writer
let library = library_builder.build();
// Put both into one graph; the nodes of each graph keep their order, after an offset.
let mut builder = GraphBuilder::new();
let program_offset = builder.add_graph(&dependencies); // 0 - the program keeps 0 to 7
let library_offset = builder.add_graph(&library); // 8 - the library becomes 8 to 10
builder.add_edge(program_offset + 6, library_offset); // json -> api
let combined = builder.build();
// combined: 11 nodes, 17 edges
Full example
// library is a JSON library of three packages - api uses parser and writer - that the
// program's package json (6) uses.
const libraryBuilder = new GraphBuilder();
libraryBuilder.addNodes(3);
// api -> parser, api -> writer
libraryBuilder.addOutEdges(0, 1, 2);
const library = libraryBuilder.build();
// Put both into one graph; the nodes of each graph keep their order, after an offset.
const builder = new GraphBuilder();
// 0 - the program keeps 0 to 7
const programOffset = builder.addGraph(dependencies);
// 8 - the library becomes 8 to 10
const libraryOffset = builder.addGraph(library);
builder.addEdge(programOffset + 6, libraryOffset); // json -> api
const combined = builder.build();
// combined: 11 nodes, 17 edges
Full example
// library is a JSON library of three packages - api uses parser and writer - that
// the program's package json (6) uses.
var libraryBuilder = new GraphBuilder();
libraryBuilder.AddNodes(3);
// api -> parser, api -> writer
libraryBuilder.AddOutEdges(0, 1, 2);
Graph library = libraryBuilder.Build();
// Put both into one graph; the nodes of each graph keep their order, after an
// offset.
var builder = new GraphBuilder();
// 0 - the program keeps 0 to 7
int programOffset = builder.AddGraph(dependencies);
// 8 - the library becomes 8 to 10
int libraryOffset = builder.AddGraph(library);
builder.AddEdge(programOffset + 6, libraryOffset); // json -> api
Graph combined = builder.Build();
// combined: 11 nodes, 17 edges
Full example

The program comes first into the empty builder, so its node IDs stay as they are. The library gets the node IDs 8 to 10. The edge between the two graphs uses the offsets. The result is the graph of the picture after, with 11 nodes and 17 edges.

A graph never changes. To add an edge to it, put it into an empty builder, add the edge and build a new graph. In the empty builder, its node IDs stay the same:

// A graph never changes: to add an edge, start a builder with it and build a new
// graph.
var builder = new GraphBuilder();
builder.addGraph(dependencies); // into an empty builder: the node IDs stay the same
builder.addEdge(ui, storage);
Graph nextVersion = builder.build();
// nextVersion: 8 nodes, 15 edges
Full example
// A graph never changes: to add an edge, start a builder with it and build a new
// graph.
let mut builder = GraphBuilder::new();
builder.add_graph(&dependencies); // into an empty builder: the node IDs stay the same
builder.add_edge(ui, storage);
let next_version = builder.build();
// next_version: 8 nodes, 15 edges
Full example
// A graph never changes: to add an edge, start a builder with it and build a new
// graph.
const builder = new GraphBuilder();
builder.addGraph(dependencies); // into an empty builder: the node IDs stay the same
builder.addEdge(ui, storage);
const nextVersion = builder.build();
// nextVersion: 8 nodes, 15 edges
Full example
// A graph never changes: to add an edge, start a builder with it and build a new
// graph.
var builder = new GraphBuilder();
// into an empty builder: the node IDs stay the same
builder.AddGraph(dependencies);
builder.AddEdge(ui, storage);
Graph nextVersion = builder.Build();
// nextVersion: 8 nodes, 15 edges
Full example

The new graph has the 8 nodes and 14 edges of the old one, and the edge ui -> storage.