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.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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:
- Added graph
- End of the edge between the graphs
- 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.
Solution
Section titled “Solution”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 -> writerGraph 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 10int libraryOffset = builder.addGraph(library);builder.addEdge(programOffset + 6, libraryOffset); // json -> apiGraph combined = builder.build();// combined: 11 nodes, 17 edges// 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 -> writerlet 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 7let library_offset = builder.add_graph(&library); // 8 - the library becomes 8 to 10builder.add_edge(program_offset + 6, library_offset); // json -> apilet combined = builder.build();// combined: 11 nodes, 17 edges// 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 -> writerlibraryBuilder.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 7const programOffset = builder.addGraph(dependencies);// 8 - the library becomes 8 to 10const libraryOffset = builder.addGraph(library);builder.addEdge(programOffset + 6, libraryOffset); // json -> apiconst combined = builder.build();// combined: 11 nodes, 17 edges// 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 -> writerlibraryBuilder.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 7int programOffset = builder.AddGraph(dependencies);// 8 - the library becomes 8 to 10int libraryOffset = builder.AddGraph(library);builder.AddEdge(programOffset + 6, libraryOffset); // json -> apiGraph combined = builder.Build();// combined: 11 nodes, 17 edgesThe 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 samebuilder.addEdge(ui, storage);Graph nextVersion = builder.build();// nextVersion: 8 nodes, 15 edges// 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 samebuilder.add_edge(ui, storage);let next_version = builder.build();// next_version: 8 nodes, 15 edges// 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 samebuilder.addEdge(ui, storage);const nextVersion = builder.build();// nextVersion: 8 nodes, 15 edges// 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 samebuilder.AddGraph(dependencies);builder.AddEdge(ui, storage);Graph nextVersion = builder.Build();// nextVersion: 8 nodes, 15 edgesThe new graph has the 8 nodes and 14 edges of the old one, and the edge ui -> storage.
Good to know
Section titled “Good to know”- Data comes along by offset. The arrays with the data of the nodes of each graph go into one array at the same offsets. The names of the library, for example, go to the positions 8 to 10.
- Edge data needs a lookup.
GraphBuilder.buildGraphBuilder::buildGraphBuilder.buildGraphBuilder.Buildnumbers the edges anew by source and target, so an added edge moves the edge indices of the edges after it. In the new graph,ui -> storagegets the edge index 6, andui -> utiland every edge after it move up by one.Graph.edgeIndexOfGraph::edge_index_ofGraph.edgeIndexOfGraph.EdgeIndexOffinds the new edge index of each edge. See Keep data on nodes and edges. - Edges of a result.
GraphBuilder.addEdgesGraphBuilder::add_edgesGraphBuilder.addEdgesGraphBuilder.AddEdgesadds the edges that an algorithm returned, such as the bridges of a graph, to a graph under construction.GraphBuilder.addEdgesAndNodesGraphBuilder::add_edges_and_nodesGraphBuilder.addEdgesAndNodesGraphBuilder.AddEdgesAndNodesadds the missing nodes with them. - Removing nodes or edges is not possible in a builder. Take a subgraph instead, or build the graph again without them. See Take the subgraph of some nodes for the first way. See Remove edges to break every cycle for the second.
API used
Section titled “API used”GraphBuilder()GraphBuilder::newnew GraphBuildernew GraphBuilderGraphBuilder.addNodes(int)GraphBuilder::add_nodesGraphBuilder.addNodesGraphBuilder.AddNodesGraphBuilder.addOutEdges(int, int...)GraphBuilder::add_out_edgesGraphBuilder.addOutEdges(source: number, ...targets: number[])GraphBuilder.AddOutEdgesGraphBuilder.addGraph(Graph)GraphBuilder::add_graphGraphBuilder.addGraphGraphBuilder.AddGraphGraphBuilder.addEdge(int, int)GraphBuilder::add_edgeGraphBuilder.addEdgeGraphBuilder.AddEdgeGraphBuilder.build()GraphBuilder::buildGraphBuilder.buildGraphBuilder.BuildGraphBuilder.addEdges(Edges)GraphBuilder::add_edgesGraphBuilder.addEdgesGraphBuilder.AddEdgesGraphBuilder.addEdgesAndNodes(Edges)GraphBuilder::add_edges_and_nodesGraphBuilder.addEdgesAndNodesGraphBuilder.AddEdgesAndNodesGraph.edgeIndexOf(int, int)Graph::edge_index_ofGraph.edgeIndexOfGraph.EdgeIndexOf