9.4 Carry edge data over to a transformed graph
You get the edge mapping between a transformed graph and the original, and gather edge data through it.
Problem
Section titled “Problem”You derived a new graph from an old one and want your data on the edges, such as weights, counts or labels, to come along. This data is kept in arrays by edge index. The new graph numbers its edges anew, just like its nodes. A mapping of the node IDs tells which new node is which old one, but not which new edge is which old one. You need a mapping of the edges. This comes up:
- after you take a subgraph, when an analysis of it needs the weights or labels of its edges;
- after a renumbering, which changes the edge indices along with the node IDs;
- after edges were added or left out by building the graph again, which shifts the edge indices;
- when you translate results on the new graph, such as the edges a search followed, back to the original edges.
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. A place in the code where one method calls another is
called a call site. The array
siteCountsite_countsiteCountsiteCount holds the number of
call sites of each call: run calls show from two places, and parse calls itself from two. The
graph loading is the subgraph of the methods that load reaches. It
numbers nodes and edges 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 call sites does
each call of the subgraph have? The picture after shows the subgraph with the call sites on its
edges:
- Start node
- Call with its call sites
Solution
Section titled “Solution”EdgeMapping edges = Graphs.edgeMapping(loading, calls, newToOld);int[] loadingSiteCount = edges.gather(siteCount);// loadingSiteCount: [1, 1, 2] - load -> parse, load -> read, parse -> parselet edges = graphs::edge_mapping(&loading, &calls, &new_to_old);let loading_site_count = edges.gather(&site_count);// loading_site_count: [1, 1, 2] - load -> parse, load -> read, parse -> parseconst edges = Graphs.edgeMapping(loading, calls, newToOld);const loadingSiteCount = edges.gather(siteCount);// loadingSiteCount: [1, 1, 2] - load -> parse, load -> read, parse -> parseEdgeMapping edges = Graphs.EdgeMapping(loading, calls, newToOld);int[] loadingSiteCount = edges.Gather(siteCount);// loadingSiteCount: [1, 1, 2] - load -> parse, load -> read, parse -> parseThe edge mapping leads from each edge of loading to the edge of calls it comes from.
gathergathergatherGather carries the call sites over through it. The
result holds 1, 1 and 2 for load -> parse, load -> read and parse -> parse, in the order of
their new edge indices, as in the picture after.
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 labels,EdgeMapping.gather(T[], T[])EdgeMapping::gather_intoEdgeMapping.gather<T>(values: ArrayLike<T>, newValues: T[])EdgeMapping.Gather<T>(ReadOnlySpan<T>, Span<T>)fills an array you pass. - A graph built again. Its node IDs stay the same, so
edgeMappingedge_mappingedgeMappingEdgeMappingneeds only a mapping of every node to itself:NodeMapping.of(newGraph.nodes().toArray())NodeMapping::of(&new_graph.nodes().collect::<Vec<_>>())?NodeMapping.of(Array.from(newGraph.nodes()))NodeMapping.Of(newGraph.Nodes().ToArray()).gathergathergatherGatherthen carries the edge data over, with 0 for a new edge. Where the new graph has nodes that the old one lacks, they map to-1NONE-1-1instead. - Composing and inverting work as for nodes.
EdgeMapping.composeEdgeMapping::composeEdgeMapping.composeEdgeMapping.Composechains two edge mappings.EdgeMapping.invertedEdgeMapping::invertedEdgeMapping.invertedEdgeMapping.Invertedturns one around, from the old edges to the new ones. See Carry node data over to a transformed graph. - The transposed graph needs
Graphs.transposedEdgeMappinggraphs::transposed_edge_mappingGraphs.transposedEdgeMappingGraphs.TransposedEdgeMappinginstead, as its edges run the other way. See Turn every edge around. - Merged edges. A condensed graph merges several edges into one, so a mapping of single edges cannot describe it. The data has to be reduced instead. See Reduce the data of the nodes or edges of a group.
API used
Section titled “API used”Graphs.edgeMapping(Graph, Graph, NodeMapping)graphs::edge_mappingGraphs.edgeMappingGraphs.EdgeMappingEdgeMapping.gather(int[])EdgeMapping::gatherEdgeMapping.gather<A>(values: A)EdgeMapping.Gather<T>(ReadOnlySpan<T>)EdgeMapping.gather(long[])EdgeMapping::gatherEdgeMapping.gather<A>(values: A)EdgeMapping.Gather<T>(ReadOnlySpan<T>)EdgeMapping.gather(double[])EdgeMapping::gatherEdgeMapping.gather<A>(values: A)EdgeMapping.Gather<T>(ReadOnlySpan<T>)EdgeMapping.gather(T[], T[])EdgeMapping::gather_intoEdgeMapping.gather<T>(values: ArrayLike<T>, newValues: T[])EdgeMapping.Gather<T>(ReadOnlySpan<T>, Span<T>)EdgeMapping.compose(EdgeMapping)EdgeMapping::composeEdgeMapping.composeEdgeMapping.ComposeEdgeMapping.inverted(int)EdgeMapping::invertedEdgeMapping.invertedEdgeMapping.InvertedGraphs.transposedEdgeMapping(Graph)graphs::transposed_edge_mappingGraphs.transposedEdgeMappingGraphs.TransposedEdgeMappingNodeMapping.of(int[])NodeMapping::ofNodeMapping.ofNodeMapping.OfGraph.nodes()Graph::nodesGraph.nodesGraph.Nodes