Skip to content

12.4 Loop over the arrays of a graph directly

You read the out-nodes and in-nodes of a graph from the arrays of its CSR layout (compressed sparse row), for the fastest loops over all edges.

You want a loop over all edges to run as fast as it can. A graph keeps its edges in two arrays, so your loop can read these arrays directly, with no method call and no argument check per edge. On graphs with millions of edges, this saves noticeable time. You need this for:

  • a sum or a count over the edges of each node;
  • an iterative algorithm, such as a ranking of the nodes, that visits every edge again and again;
  • an export of a graph into the format of another program.

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. How many call sites does the code of each method have? How many methods call each method? From how many places in other methods is each method called? The picture before shows the number of call sites on each call. The picture after shows three numbers in brackets for each method: the call sites in its code, its callers, and the call sites in other methods that call it:

The calls with their call sites
The call sites in the code of each method, its callers, and the call sites in other methods
1111211111210: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
1111211111210: main(2, 0, 0)1: parseArgs(0, 1, 1)2: run(4, 1, 1)3: load(2, 1, 1)4: save(2, 1, 1)5: show(1, 1, 2)6: parse(2, 2, 1)7: format(1, 3, 2)8: read(0, 1, 1)9: write(0, 1, 1)
Output
  • Method (sites in it, callers, sites in others)

The two arrays are the graph’s CSR layout (compressed sparse row). See Use arrays you already have as a graph. The out-nodes of node v stand from position start[v] up to just before position start[v + 1]. The array of starts has one entry more than the graph has nodes. This last entry is the edge count, so the rule holds for the last node too. The position of an out-node in its array is the edge index of its edge, so the edge data is at the same position:

int[] start = Csr.outAdjacencyStart(calls);
int[] callees = Csr.outAdjacency(calls);
int[] sitesIn = new int[calls.nodeCount()];
int[] callerCount = new int[calls.nodeCount()];
for (int method = 0; method < calls.nodeCount(); method++) {
// positions = edges
for (int edge = start[method]; edge < start[method + 1]; edge++) {
sitesIn[method] += siteCount[edge];
callerCount[callees[edge]]++;
}
}
// sitesIn: [2, 0, 4, 2, 2, 1, 2, 1, 0, 0] - run calls from 4 places in its code
// callerCount: [0, 1, 1, 1, 1, 1, 2, 3, 1, 1] - format has 3 callers, itself included
// The in-nodes of a BiAdjacentGraph have the same layout, but a position there is an
// edge index of the transposed graph: the edge data has to follow first.
BiAdjacentGraph both = BiAdjacentGraph.of(calls);
int[] inStart = Csr.inAdjacencyStart(both);
int[] callers = Csr.inAdjacency(both);
int[] calledBySiteCount = Graphs.transposedEdgeMapping(calls).gather(siteCount);
int[] sitesFromOthers = new int[calls.nodeCount()];
for (int method = 0; method < calls.nodeCount(); method++) {
for (int edge = inStart[method]; edge < inStart[method + 1]; edge++) {
if (callers[edge] != method) {
sitesFromOthers[method] += calledBySiteCount[edge];
}
}
}
// sitesFromOthers: [0, 1, 1, 1, 1, 2, 1, 2, 1, 1] - show is called from 2 places
Full example
let start = csr::out_adjacency_start(&calls);
let callees = csr::out_adjacency(&calls);
let mut sites_in = vec![0; calls.node_count() as usize];
let mut caller_count = vec![0; calls.node_count() as usize];
for method in 0..calls.node_count() as usize {
for edge in start[method] as usize..start[method + 1] as usize {
// positions = edges
sites_in[method] += site_count[edge];
caller_count[callees[edge] as usize] += 1;
}
}
// sites_in: [2, 0, 4, 2, 2, 1, 2, 1, 0, 0] - run calls from 4 places in its code
// caller_count: [0, 1, 1, 1, 1, 1, 2, 3, 1, 1] - format has 3 callers, itself
// included
// The in-nodes of a BiAdjacentGraph have the same layout, but a position there is an
// edge index of the transposed graph: the edge data has to follow first.
let both = BiAdjacentGraph::of(&calls);
let in_start = csr::in_adjacency_start(&both);
let callers = csr::in_adjacency(&both);
let called_by_site_count =
graphs::transposed_edge_mapping(&calls).gather(&site_count);
let mut sites_from_others = vec![0; calls.node_count() as usize];
for method in 0..calls.node_count() as usize {
for edge in in_start[method] as usize..in_start[method + 1] as usize {
if callers[edge] as usize != method {
sites_from_others[method] += called_by_site_count[edge];
}
}
}
// sites_from_others: [0, 1, 1, 1, 1, 2, 1, 2, 1, 1] - show is called from 2 places
Full example
const start = Csr.outAdjacencyStart(calls);
const callees = Csr.outAdjacency(calls);
const sitesIn = new Int32Array(calls.nodeCount);
const callerCount = new Int32Array(calls.nodeCount);
for (let method = 0; method < calls.nodeCount; method++) {
// positions = edges
for (let edge = start[method]; edge < start[method + 1]; edge++) {
sitesIn[method] += siteCount[edge];
callerCount[callees[edge]]++;
}
}
// sitesIn: [2, 0, 4, 2, 2, 1, 2, 1, 0, 0] - run calls from 4 places in its code
// callerCount: [0, 1, 1, 1, 1, 1, 2, 3, 1, 1] - format has 3 callers, itself included
// The in-nodes of a BiAdjacentGraph have the same layout, but a position there is an
// edge index of the transposed graph: the edge data has to follow first.
const both = BiAdjacentGraph.of(calls);
const inStart = Csr.inAdjacencyStart(both);
const callers = Csr.inAdjacency(both);
const calledBySiteCount = Graphs.transposedEdgeMapping(calls).gather(siteCount);
const sitesFromOthers = new Int32Array(calls.nodeCount);
for (let method = 0; method < calls.nodeCount; method++) {
for (let edge = inStart[method]; edge < inStart[method + 1]; edge++) {
if (callers[edge] !== method) {
sitesFromOthers[method] += calledBySiteCount[edge];
}
}
}
// sitesFromOthers: [0, 1, 1, 1, 1, 2, 1, 2, 1, 1] - show is called from 2 places
Full example
ReadOnlySpan<int> start = Csr.OutAdjacencyStart(calls);
ReadOnlySpan<int> callees = Csr.OutAdjacency(calls);
int[] sitesIn = new int[calls.NodeCount];
int[] callerCount = new int[calls.NodeCount];
for (int method = 0; method < calls.NodeCount; method++)
{
// positions = edges
for (int edge = start[method]; edge < start[method + 1]; edge++)
{
sitesIn[method] += siteCount[edge];
callerCount[callees[edge]]++;
}
}
// sitesIn: [2, 0, 4, 2, 2, 1, 2, 1, 0, 0] - run calls from 4 places in its code
// callerCount: [0, 1, 1, 1, 1, 1, 2, 3, 1, 1] - format has 3 callers, itself
// included
// The in-nodes of a BiAdjacentGraph have the same layout, but a position there is
// an edge index of the transposed graph: the edge data has to follow first.
BiAdjacentGraph both = BiAdjacentGraph.Of(calls);
ReadOnlySpan<int> inStart = Csr.InAdjacencyStart(both);
ReadOnlySpan<int> callers = Csr.InAdjacency(both);
int[] calledBySiteCount = Graphs.TransposedEdgeMapping(calls).Gather(siteCount);
int[] sitesFromOthers = new int[calls.NodeCount];
for (int method = 0; method < calls.NodeCount; method++)
{
for (int edge = inStart[method]; edge < inStart[method + 1]; edge++)
{
if (callers[edge] != method)
{
sitesFromOthers[method] += calledBySiteCount[edge];
}
}
}
// sitesFromOthers: [0, 1, 1, 1, 1, 2, 1, 2, 1, 1] - show is called from 2 places
Full example

sitesInsites_insitesInsitesIn sums the call sites of the calls each method makes. callerCountcaller_countcallerCountcallerCount counts how often each method is the target of an edge. The in-nodes come from a BiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraph, which BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of builds from the graph. Their positions are edge indices of the transposed graph. So gathergathergatherGather first carries the call sites over to them, with the mapping from Graphs.transposedEdgeMappinggraphs::transposed_edge_mappingGraphs.transposedEdgeMappingGraphs.TransposedEdgeMapping. sitesFromOtherssites_from_otherssitesFromOtherssitesFromOthers then sums for each method the call sites in other methods: show is called from two places in run.