Skip to content

9.1 Take the subgraph of some nodes

You take a graph of selected nodes and the edges between them, with a mapping back to the original nodes.

You want to work on some of the nodes of a graph and the edges between them, and leave out the rest. Running each analysis on the whole graph and filtering its results is wasteful and easy to get wrong. A graph of only those nodes and edges is called a subgraph. On a subgraph, the analyses see only the nodes that matter. The nodes you keep can be:

  • the code that a method reaches, for an analysis of what it may run;
  • the modules of one team, or of one layer of an architecture;
  • the nodes that pass a filter, such as the classes that a commit changed.

The example graph calls models the calls between the ten methods of a small program. It has an edge from each method to each method it calls. The array names holds the names of the methods. parse and format call themselves. Which methods does load reach, and how do they call each other? They should form a graph of their own, for the analyses that follow. The picture after shows the subgraph that the code returns, with its new node IDs:

The calls and the method load
The subgraph of the methods load reaches
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: load1: parse2: read
Input
  • Start node
Output
  • Method with its new node ID
  • From a method to a method it calls
// The subgraph of the methods that load reaches:
NodeMapping newToOld = NodeMapping.forSubgraph(Graphs.reachableFrom(calls, load));
Graph loading = Graphs.remapped(calls, newToOld);
// loading: 0 -> [1, 2], 1 -> [1], 2 -> [] - load -> [parse, read], parse -> [parse]
// The names come along through the mapping, from new to old node IDs.
String[] loadingNames = new String[loading.nodeCount()];
newToOld.gather(names, loadingNames); // [load, parse, read]
// Or a subgraph of nodes given by their node IDs: save, show, format and write.
Graph savingAndShowing = Graphs.remapped(calls, NodeMapping.forSubgraph(4, 5, 7, 9));
// savingAndShowing: 0 -> [2, 3], 1 -> [2], 2 -> [2], 3 -> [] - save, show, format,
// write
Full example
// The subgraph of the methods that load reaches, from the sorted list that
// reachable_from returns:
let new_to_old =
NodeMapping::for_subgraph_sorted(&graphs::reachable_from(&calls, load))?;
let loading = graphs::remapped(&calls, &new_to_old);
// loading: 0 -> [1, 2], 1 -> [1], 2 -> [] - load -> [parse, read], parse -> [parse]
// The names come along through the mapping, from new to old node IDs.
let mut loading_names = vec![""; loading.node_count() as usize];
new_to_old.gather_into(names, &mut loading_names); // ["load", "parse", "read"]
// Or a subgraph of nodes given by their node IDs: save, show, format and write.
let saving_and_showing =
graphs::remapped(&calls, &NodeMapping::for_subgraph(&[4, 5, 7, 9])?);
// saving_and_showing: 0 -> [2, 3], 1 -> [2], 2 -> [2], 3 -> [] - save, show, format,
// write
Full example
// The subgraph of the methods that load reaches:
const newToOld = NodeMapping.forSubgraph(Graphs.reachableFrom(calls, load));
const loading = Graphs.remapped(calls, newToOld);
// loading: 0 -> [1, 2], 1 -> [1], 2 -> [] - load -> [parse, read], parse -> [parse]
// The names come along through the mapping, from new to old node IDs.
const loadingNames = new Array<string>(loading.nodeCount);
newToOld.gather(names, loadingNames); // [load, parse, read]
// Or a subgraph of nodes given by their node IDs: save, show, format and write.
const savingAndShowing = Graphs.remapped(calls, NodeMapping.forSubgraph(4, 5, 7, 9));
// savingAndShowing: 0 -> [2, 3], 1 -> [2], 2 -> [2], 3 -> [] - save, show, format,
// write
Full example
// The subgraph of the methods that load reaches:
NodeMapping newToOld = NodeMapping.ForSubgraph(Graphs.ReachableFrom(calls, load));
Graph loading = Graphs.Remapped(calls, newToOld);
// loading: 0 -> [1, 2], 1 -> [1], 2 -> [] - load -> [parse, read],
// parse -> [parse]
// The names come along through the mapping, from new to old node IDs.
string[] loadingNames = new string[loading.NodeCount];
newToOld.Gather(names, loadingNames); // [load, parse, read]
// Or a subgraph of nodes given by their node IDs: save, show, format and write.
Graph savingAndShowing =
Graphs.Remapped(calls, NodeMapping.ForSubgraph(4, 5, 7, 9));
// savingAndShowing: 0 -> [2, 3], 1 -> [2], 2 -> [2], 3 -> [] - save, show,
// format, write
Full example

The subgraph loading has three nodes, numbered 0 to 2 in the order of their old node IDs: load, parse and read. It keeps the calls between them. The mapping newToOldnew_to_oldnewToOldnewToOld gives the old node ID of each new node, and NodeMapping.gatherNodeMapping::gather_intoNodeMapping.gatherNodeMapping.Gather carries the names over with it. The second subgraph has the nodes save, show, format and write, in this order.