Skip to content

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.

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.

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:

The calls and the method format
The transposed graph and the callers of format
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
Input
  • Called method
Output
  • Caller
  • Edge to a caller
BiAdjacentGraph both = BiAdjacentGraph.of(calls);
BiAdjacentGraph calledBy = both.transposed(); // without copying
int 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]
Full example
let both = BiAdjacentGraph::of(&calls);
let called_by = both.transposed(); // without copying
let 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"]
Full example
const both = BiAdjacentGraph.of(calls);
const calledBy = both.transposed(); // without copying
const 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]
Full example
BiAdjacentGraph both = BiAdjacentGraph.Of(calls);
BiAdjacentGraph calledBy = both.Transposed(); // without copying
int 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]
Full example

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.