Skip to content

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.

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.

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:

The calls, the method load and the methods to look up
The subgraph of the methods load reaches, with lines of code
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: load (25)1: parse (60)2: read (10)
Input
  • Method to look up
Context
  • Start node
Output
  • Method with its lines of code
// 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 loading
boolean mainKept = oldToNew.isMapped(main); // false - main is not in loading
// true - 7 of 10 old nodes are not kept
boolean partial = oldToNew.isPartial();
int mappedCount = oldToNew.mappedCount(); // 3 - load, parse, read
int 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
Full example
// 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 loading
let main_kept = old_to_new.is_mapped(main); // false - main is not in loading
let partial = old_to_new.is_partial(); // true - 7 of 10 old nodes are not kept
let mapped_count = old_to_new.mapped_count(); // 3 - load, parse, read
let 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
Full example
// 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 loading
const mainKept = oldToNew.isMapped(main); // false - main is not in loading
// true - 7 of 10 old nodes are not kept
const partial = oldToNew.isPartial;
const mappedCount = oldToNew.mappedCount; // 3 - load, parse, read
const 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
Full example
// The lines of code of each method:
int[] linesOf = [12, 30, 20, 25, 18, 15, 60, 40, 10, 10];
// [25, 60, 10] - load, parse, read
int[] 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 loading
bool mainKept = oldToNew.IsMapped(main); // false - main is not in loading
// true - 7 of 10 old nodes are not kept
bool partial = oldToNew.IsPartial;
int mappedCount = oldToNew.MappedCount; // 3 - load, parse, read
int 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
Full example

gathergathergatherGather 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.