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.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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:
- Start node
- Method with its new node ID
- From a method to a method it calls
Solution
Section titled “Solution”// 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// 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// 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// 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, writeThe 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.
Good to know
Section titled “Good to know”- Three ways to select. A sorted list that Graphity returned, such as the result of a search,
goes to
NodeMapping.forSubgraph(IntList)NodeMapping::for_subgraph_sortedNodeMapping.forSubgraph(selected: IntList)NodeMapping.ForSubgraph(SortedIntView)as it is. Node IDs written out or in an array can come in any order. They go toNodeMapping.forSubgraph(int...)NodeMapping::for_subgraphNodeMapping.forSubgraph(...selected: number[])NodeMapping.ForSubgraph(params ReadOnlySpan<int>), which sorts them in a copy. Nodes that a filter marked in aBitSet[bool]BitSetBitArraygo toNodeMapping.forSubgraph(BitSet)NodeMapping::for_subgraph_maskNodeMapping.forSubgraph(selected: BitSet)NodeMapping.ForSubgraph(BitArray). A list in any order that Graphity returned, such as a cycle that it found, is sorted in a copy as well:NodeMapping.forSubgraph(cycle)NodeMapping::for_subgraph(&cycle)?NodeMapping.forSubgraph(cycle)NodeMapping.ForSubgraph(cycle.AsSpan()). - Only the edges between selected nodes. An edge from or to a node that is left out is left out as well.
- The data comes along through the mapping. See Carry node data over to a transformed graph and Carry edge data over to a transformed graph.
- Condense instead of leave out. You can also replace groups of nodes by one node each, rather than drop nodes. See Condense groups of your own choice.
API used
Section titled “API used”NodeMapping.forSubgraph(IntList)NodeMapping::for_subgraph_sortedNodeMapping.forSubgraph(selected: IntList)NodeMapping.ForSubgraph(SortedIntView)NodeMapping.forSubgraph(int...)NodeMapping::for_subgraphNodeMapping.forSubgraph(...selected: number[])NodeMapping.ForSubgraph(params ReadOnlySpan<int>)NodeMapping.forSubgraph(BitSet)NodeMapping::for_subgraph_maskNodeMapping.forSubgraph(selected: BitSet)NodeMapping.ForSubgraph(BitArray)Graphs.remapped(Graph, NodeMapping)graphs::remappedGraphs.remappedGraphs.RemappedNodeMapping.gather(T[], T[])NodeMapping::gather_intoNodeMapping.gather<T>(values: ArrayLike<T>, newValues: T[])NodeMapping.Gather<T>(ReadOnlySpan<T>, Span<T>)Graphs.reachableFrom(Graph, int)graphs::reachable_fromGraphs.reachableFromGraphs.ReachableFrom(Graph, int)