9.5 Turn every edge around
You turn every edge around without copying the graph, and get the edge indices and edge data of the result.
Problem
Section titled “Problem”You want to follow the edges of a graph the other way round. The graph with every edge turned around is called the transposed graph. Every algorithm that follows edges works on it as well, so you need no new algorithm. The node IDs stay the same. The edge indices of the transposed graph differ, so edge data needs translating. You need this for:
- the callers of a method instead of the methods it calls;
- the packages that use a package instead of those it uses;
- the paths to an exit of a function instead of the paths from its entry.
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. 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. The variable
loadToReadload_to_readloadToReadloadToRead holds the edge
index of the call load -> read. Which methods call format, and from how many places each? The
transposed graph has an edge from each method to each method that calls it, so the answer is the
out-nodes of format there. The picture after shows the transposed graph, with the callers of
format and the edges to them:
- Called method
- Caller
- Edge to a caller
Solution
Section titled “Solution”BiAdjacentGraph both = BiAdjacentGraph.of(calls);BiAdjacentGraph calledBy = both.transposed(); // without copyingint transposedIndex = both.transposedEdgeIndex(loadToRead); // 10 - read -> load
// The edge data, by edge index of the transposed graph:int[] calledBySiteCount = Graphs.transposedEdgeMapping(calls).gather(siteCount);List<String> callersOfFormat = new ArrayList<>();for (int edge = calledBy.outEdgeStart(format); edge < calledBy.outEdgeEnd(format); edge++) { callersOfFormat.add( names[calledBy.edgeTarget(edge)] + ": " + calledBySiteCount[edge]);}// callersOfFormat: [save: 1, show: 1, format: 1]let both = BiAdjacentGraph::of(&calls);let called_by = both.transposed(); // without copyinglet transposed_index = both.transposed_edge_index(load_to_read); // 10 - read -> load
// The edge data, by edge index of the transposed graph:let called_by_site_count = graphs::transposed_edge_mapping(&calls).gather(&site_count);let mut callers_of_format = Vec::new();for edge in called_by.out_edge_start(format)..called_by.out_edge_end(format) { let caller = names[called_by.edge_target(edge) as usize]; callers_of_format .push(format!("{caller}: {}", called_by_site_count[edge as usize]));}// callers_of_format: ["save: 1", "show: 1", "format: 1"]const both = BiAdjacentGraph.of(calls);const calledBy = both.transposed(); // without copyingconst transposedIndex = both.transposedEdgeIndex(loadToRead); // 10 - read -> load
// The edge data, by edge index of the transposed graph:const calledBySiteCount = Graphs.transposedEdgeMapping(calls).gather(siteCount);const callersOfFormat: string[] = [];for ( let edge = calledBy.outEdgeStart(format); edge < calledBy.outEdgeEnd(format); edge++) { callersOfFormat.push( `${names[calledBy.edgeTarget(edge)]}: ${calledBySiteCount[edge]}`, );}// callersOfFormat: [save: 1, show: 1, format: 1]BiAdjacentGraph both = BiAdjacentGraph.Of(calls);BiAdjacentGraph calledBy = both.Transposed(); // without copyingint transposedIndex = both.TransposedEdgeIndex(loadToRead); // 10 - read -> load
// The edge data, by edge index of the transposed graph:int[] calledBySiteCount = Graphs.TransposedEdgeMapping(calls).Gather(siteCount);List<string> callersOfFormat = [];for (int edge = calledBy.OutEdgeStart(format); edge < calledBy.OutEdgeEnd(format); edge++){ callersOfFormat.Add( $"{names[calledBy.EdgeTarget(edge)]}: {calledBySiteCount[edge]}");}// callersOfFormat: [save: 1, show: 1, format: 1]In the transposed graph calledBycalled_bycalledBycalledBy, the out-nodes of format are
its callers, as in the picture after: save, show and format itself, each with one call site.
The number of call sites comes from calledBySiteCountcalled_by_site_countcalledBySiteCountcalledBySiteCount,
which holds it by edge index of the transposed graph. The edge load -> read is edge index 6 of the
calls. Turned around to read -> load, it is edge index 10 of the transposed graph.
Good to know
Section titled “Good to know”- No copy.
BiAdjacentGraph.transposedBiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.Transposedswaps the in-nodes and out-nodes of aBiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraph: the result is a view that costs no time and no memory. A plainGraphGraphGraphGraphneedsBiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Ofonce, which builds the in-nodes. - The two directions.
BiAdjacentGraph.transposedEdgeIndexBiAdjacentGraph::transposed_edge_indexBiAdjacentGraph.transposedEdgeIndexBiAdjacentGraph.TransposedEdgeIndextranslates an edge index of the graph into the edge index of the turned-around edge in the transposed graph.Graphs.transposedEdgeMappinggraphs::transposed_edge_mappingGraphs.transposedEdgeMappingGraphs.TransposedEdgeMappingmaps the other way: from each edge of the transposed graph to the edge of the graph it turns around.gathergathergatherGatherneeds this direction to carry data over to the transposed graph. Called on the transposed graph,transposedEdgeIndextransposed_edge_indextransposedEdgeIndexTransposedEdgeIndextranslates back. - Every search works backwards this way. See Find everything that reaches a node.
API used
Section titled “API used”BiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfBiAdjacentGraph.transposed()BiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.TransposedBiAdjacentGraph.transposedEdgeIndex(int)BiAdjacentGraph::transposed_edge_indexBiAdjacentGraph.transposedEdgeIndexBiAdjacentGraph.TransposedEdgeIndexGraphs.transposedEdgeMapping(Graph)graphs::transposed_edge_mappingGraphs.transposedEdgeMappingGraphs.TransposedEdgeMappingEdgeMapping.gather(int[])EdgeMapping::gatherEdgeMapping.gather<A>(values: A)EdgeMapping.Gather<T>(ReadOnlySpan<T>)Graph.outEdgeStart(int)Graph::out_edge_startGraph.outEdgeStartGraph.OutEdgeStartGraph.outEdgeEnd(int)Graph::out_edge_endGraph.outEdgeEndGraph.OutEdgeEndGraph.edgeTarget(int)Graph::edge_targetGraph.edgeTargetGraph.EdgeTarget