9.3 Carry node data over to a transformed graph
You gather data on nodes into the numbering of a transformed graph, translate node IDs both ways, and compose mappings.
Problem
Section titled “Problem”You derived a new graph from an old one and want your data on the nodes to come along. Your arrays of node data are indexed by the old node IDs, but an analysis of the new graph needs them in the new numbering. You also want to translate single node IDs in both directions: the results on the new graph back to the old nodes, and the old nodes you care about to their new node IDs. This comes up:
- after you take a subgraph or renumber the nodes, when an analysis of the new graph needs the names, weights or sizes of its nodes;
- when the results of an analysis of the new graph go back into a report on the original nodes;
- after several transformations in a row, each with a mapping of its own.
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. An array holds the lines of code of each method. The
graph loading is the subgraph of the methods that load reaches. It
numbers them anew. The mapping
newToOldnew_to_oldnewToOldnewToOld gives the old node ID
of each of its nodes.
See Take the subgraph of some nodes for how to build both. How many lines of code
does each node of the subgraph have? Which node of the subgraph is parse, and is main in it at
all? The picture after shows the subgraph with the lines of code of each method in brackets:
- Method to look up
- Start node
- Method with its lines of code
Solution
Section titled “Solution”// The lines of code of each method:int[] linesOf = {12, 30, 20, 25, 18, 15, 60, 40, 10, 10};int[] loadingLines = newToOld.gather(linesOf); // [25, 60, 10] - load, parse, read
// From old to new node IDs: the inverse mapping. Not every old node has a new one.NodeMapping oldToNew = newToOld.inverted(calls.nodeCount());int newIdOfParse = oldToNew.get(parse); // 1 - parse is node 1 of loadingboolean mainKept = oldToNew.isMapped(main); // false - main is not in loading// true - 7 of 10 old nodes are not keptboolean partial = oldToNew.isPartial();int mappedCount = oldToNew.mappedCount(); // 3 - load, parse, readint indexCount = oldToNew.indexCount(); // 10 - the methods of calls
// Transformed twice - loading, then its subgraph of the out-nodes of its node 0,// load: compose the two mappings, and gather from the data of calls in one step.NodeMapping secondToFirst = NodeMapping.forSubgraph(loading.outNodes(0));NodeMapping secondToOriginal = newToOld.compose(secondToFirst);int[] calleeLines = secondToOriginal.gather(linesOf); // [60, 10] - parse, read// The lines of code of each method:let lines_of = [12, 30, 20, 25, 18, 15, 60, 40, 10, 10];let loading_lines = new_to_old.gather(&lines_of); // [25, 60, 10] - load, parse, read
// From old to new node IDs: the inverse mapping. Not every old node has a new one.let old_to_new = new_to_old.inverted(calls.node_count());let new_id_of_parse = old_to_new.get(parse); // Some(1) - parse is node 1 of loadinglet main_kept = old_to_new.is_mapped(main); // false - main is not in loadinglet partial = old_to_new.is_partial(); // true - 7 of 10 old nodes are not keptlet mapped_count = old_to_new.mapped_count(); // 3 - load, parse, readlet index_count = old_to_new.index_count(); // 10 - the methods of calls
// Transformed twice - loading, then its subgraph of the out-nodes of its node 0,// load: compose the two mappings, and gather from the data of calls in one step.let second_to_first = NodeMapping::for_subgraph_sorted(&loading.out_nodes(0))?;let second_to_original = new_to_old.compose(&second_to_first);let callee_lines = second_to_original.gather(&lines_of); // [60, 10] - parse, read// The lines of code of each method:const linesOf = Int32Array.of(12, 30, 20, 25, 18, 15, 60, 40, 10, 10);const loadingLines = newToOld.gather(linesOf); // [25, 60, 10] - load, parse, read
// From old to new node IDs: the inverse mapping. Not every old node has a new one.const oldToNew = newToOld.inverted(calls.nodeCount);const newIdOfParse = oldToNew.get(parse); // 1 - parse is node 1 of loadingconst mainKept = oldToNew.isMapped(main); // false - main is not in loading// true - 7 of 10 old nodes are not keptconst partial = oldToNew.isPartial;const mappedCount = oldToNew.mappedCount; // 3 - load, parse, readconst indexCount = oldToNew.indexCount; // 10 - the methods of calls
// Transformed twice - loading, then its subgraph of the out-nodes of its node 0,// load: compose the two mappings, and gather from the data of calls in one step.const secondToFirst = NodeMapping.forSubgraph(loading.outNodes(0));const secondToOriginal = newToOld.compose(secondToFirst);const calleeLines = secondToOriginal.gather(linesOf); // [60, 10] - parse, read// The lines of code of each method:int[] linesOf = [12, 30, 20, 25, 18, 15, 60, 40, 10, 10];// [25, 60, 10] - load, parse, readint[] loadingLines = newToOld.Gather(linesOf);
// From old to new node IDs: the inverse mapping. Not every old node has a new// one.NodeMapping oldToNew = newToOld.Inverted(calls.NodeCount);int newIdOfParse = oldToNew.Get(parse); // 1 - parse is node 1 of loadingbool mainKept = oldToNew.IsMapped(main); // false - main is not in loading// true - 7 of 10 old nodes are not keptbool partial = oldToNew.IsPartial;int mappedCount = oldToNew.MappedCount; // 3 - load, parse, readint indexCount = oldToNew.IndexCount; // 10 - the methods of calls
// Transformed twice - loading, then its subgraph of the out-nodes of its node 0,// load: compose the two mappings, and gather from the data of calls in one step.NodeMapping secondToFirst = NodeMapping.ForSubgraph(loading.OutNodes(0));NodeMapping secondToOriginal = newToOld.Compose(secondToFirst);int[] calleeLines = secondToOriginal.Gather(linesOf); // [60, 10] - parse, readgathergathergatherGather builds a new array in the new numbering:
load, parse and read with 25, 60 and 10 lines, as in the picture after. The inverse mapping
finds parse as node 1 of the subgraph and has no node for main. It is partial, with 3 of its 10
old nodes mapped. The second subgraph has the nodes parse and read. The composed mapping leads
from them straight to their node IDs in calls, which are 6 and 8, so that their lines come from
the original array in one step.
Good to know
Section titled “Good to know”- Any kind of data.
long[]Vec<i64>BigInt64Arraylong[]anddouble[]Vec<f64>Float64Arraydouble[]carry over the same way; for objects, such as names,NodeMapping.gather(T[], T[])NodeMapping::gather_intoNodeMapping.gather<T>(values: ArrayLike<T>, newValues: T[])NodeMapping.Gather<T>(ReadOnlySpan<T>, Span<T>)fills an array you pass. - Transformed several times? Compose the mappings, as at the end of the example, and gather once
from the original data, instead of gathering after every step. The mapping of the later step is
the argument and is applied first:
newToOld.compose(secondToFirst)new_to_old.compose(&second_to_first)newToOld.compose(secondToFirst)newToOld.Compose(secondToFirst)leads from the second subgraph to the first and then tocalls. The other way round, the call rejects the mappings. Where their sizes fit, it gives a wrong mapping instead. - Unmapped nodes. In a partial mapping,
IndexMapping.getIndexMapping::getIndexMapping.getIndexMapping.Getreturns-1None-1-1for a node without a counterpart;IndexMapping.isMappedIndexMapping::is_mappedIndexMapping.isMappedIndexMapping.IsMappedchecks a node. - Results back to the original nodes. The inverse mapping gathers the other way:
oldToNew.gather(values)old_to_new.gather(&values)oldToNew.gather(values)oldToNew.Gather(values)turns an array by node ID of the subgraph into one by node ID ofcalls, with 0 for the nodes that are not in the subgraph.gathergather_intogatherGatherwith objects leaves their entries as they are. Where 0 is a real value,isMappedis_mappedisMappedIsMappedtells whether a node is in the subgraph. - Edge data follows in the same way, through a mapping of the edges. See Carry edge data over to a transformed graph.
API used
Section titled “API used”NodeMapping.gather(int[])NodeMapping::gatherNodeMapping.gather<A>(values: A)NodeMapping.Gather<T>(ReadOnlySpan<T>)NodeMapping.gather(long[])NodeMapping::gatherNodeMapping.gather<A>(values: A)NodeMapping.Gather<T>(ReadOnlySpan<T>)NodeMapping.gather(double[])NodeMapping::gatherNodeMapping.gather<A>(values: A)NodeMapping.Gather<T>(ReadOnlySpan<T>)NodeMapping.gather(T[], T[])NodeMapping::gather_intoNodeMapping.gather<T>(values: ArrayLike<T>, newValues: T[])NodeMapping.Gather<T>(ReadOnlySpan<T>, Span<T>)NodeMapping.inverted(int)NodeMapping::invertedNodeMapping.invertedNodeMapping.InvertedIndexMapping.get(int)IndexMapping::getIndexMapping.getIndexMapping.GetIndexMapping.isMapped(int)IndexMapping::is_mappedIndexMapping.isMappedIndexMapping.IsMappedIndexMapping.isPartial()IndexMapping::is_partialIndexMapping.isPartialIndexMapping.IsPartialIndexMapping.mappedCount()IndexMapping::mapped_countIndexMapping.mappedCountIndexMapping.MappedCountIndexMapping.indexCount()IndexMapping::index_countIndexMapping.indexCountIndexMapping.IndexCountNodeMapping.compose(NodeMapping)NodeMapping::composeNodeMapping.composeNodeMapping.ComposeNodeMapping.forSubgraph(IntList)NodeMapping::for_subgraph_sortedNodeMapping.forSubgraph(selected: IntList)NodeMapping.ForSubgraph(SortedIntView)Graph.outNodes(int)Graph::out_nodesGraph.outNodesGraph.OutNodes