Skip to content

1.8 Use arrays you already have as a graph

You make a graph from the two arrays of a compressed sparse row (CSR) layout, and get the arrays of a graph for code of your own.

You already have a graph as arrays and want to use it without adding its edges one by one. Or you have a graph and want its arrays, for code of your own that reads every edge. The arrays have the layout that most graph libraries use inside. One array holds the out-nodes of node 0, then those of node 1, and so on. A second array holds, for each node, the position in the first array where its out-nodes start. The arrays come from:

  • another graph library or a native library that keeps its graphs this way;
  • a file that stores a graph or a sparse matrix in this layout;
  • an earlier stage of your own program that already keeps its edges this way.

The method loadloadloadLoad of a small program reads the lines of a file and parses each line that is not blank into a note:

List<Note> load(Path file) {
List<String> lines = read(file); // entry
List<Note> notes = new ArrayList<>();
for (int i = 0; i < lines.size(); i++) { // header: i < lines.size()
String line = lines.get(i); // body
if (!line.isBlank()) {
notes.add(parse(line)); // then
}
} // next: i++
return notes; // exit
}
fn load(file: &Path) -> Vec<Note> {
let lines = read(file); // entry
let mut notes = Vec::new();
let mut i = 0;
while i < lines.len() { // header: i < lines.len()
let line = &lines[i]; // body
if !line.trim().is_empty() {
notes.push(parse(line)); // then
}
i += 1; // next
}
notes // exit
}
function load(file: string): Note[] {
const lines = read(file); // entry
const notes: Note[] = [];
for (let i = 0; i < lines.length; i++) { // header: i < lines.length
const line = lines[i]; // body
if (line.trim() !== "") {
notes.push(parse(line)); // then
}
} // next: i++
return notes; // exit
}
List<Note> Load(string file)
{
string[] lines = Read(file); // entry
var notes = new List<Note>();
for (int i = 0; i < lines.Length; i++) // header: i < lines.Length
{
string line = lines[i]; // body
if (!string.IsNullOrWhiteSpace(line))
{
notes.Add(Parse(line)); // then
}
} // next: i++
return notes; // exit
}

The example graph models the control flow of this method and is called a control-flow graph. It has a node for each basic block. Another tool hands it over as two arrays. One holds the out-nodes of all blocks: 1, 2, 5, 3, 4, 4, 1. The other holds the position where the out-nodes of each block start: 0, 1, 3, 5, 6, 7, 7.

The second array has one entry more than there are blocks. Its last entry is the number of edges, and it marks where the out-nodes of the last block end. exit has no out-nodes, so its start and the last entry are both 7. How do the two arrays become a graph, without adding the edges one by one? In the picture, each edge is labeled with its edge index. That is the position of its target in the array of out-nodes:

edge 0edge 1edge 2edge 3edge 4edge 5edge 60: entry1: header2: body3: then4: next5: exit

Graph.ofGraph::ofGraph.ofGraph.Of takes the two arrays, checks them and keeps copies:

// The control flow of the method load, as another tool might hand it over: the
// out-nodes of all its blocks, block after block, and where the out-nodes of each
// block start. The blocks entry, header, body, then, next and exit are the nodes
// 0 to 5.
int[] start = {0, 1, 3, 5, 6, 7, 7};
int[] adjacency = {1, 2, 5, 3, 4, 4, 1};
Graph cfg = Graph.of(start, adjacency); // copies and checks both arrays
// cfg: 6 nodes, 7 edges
// The other way round: the arrays of a graph, for code of your own that reads every
// edge. They are the graph's own arrays, not copies: read them, never change them.
int[] outStart = Csr.outAdjacencyStart(cfg); // [0, 1, 3, 5, 6, 7, 7]
int[] outNodes = Csr.outAdjacency(cfg); // [1, 2, 5, 3, 4, 4, 1]
Full example
// The control flow of the method load, as another tool might hand it over: the
// out-nodes of all its blocks, block after block, and where the out-nodes of each
// block start. The blocks entry, header, body, then, next and exit are the nodes
// 0 to 5.
let start = [0, 1, 3, 5, 6, 7, 7];
let adjacency = [1, 2, 5, 3, 4, 4, 1];
let cfg = Graph::of(&start, &adjacency)?; // copies and checks both arrays
// cfg: 6 nodes, 7 edges
// The other way round: the arrays of a graph, for code of your own that reads every
// edge. They are the graph's own arrays, not copies, borrowed as slices that cannot
// be written to.
let out_start = csr::out_adjacency_start(&cfg); // [0, 1, 3, 5, 6, 7, 7]
let out_nodes = csr::out_adjacency(&cfg); // [1, 2, 5, 3, 4, 4, 1]
Full example
// The control flow of the method load, as another tool might hand it over: the
// out-nodes of all its blocks, block after block, and where the out-nodes of each
// block start. The blocks entry, header, body, then, next and exit are the nodes
// 0 to 5.
const start = [0, 1, 3, 5, 6, 7, 7];
const adjacency = [1, 2, 5, 3, 4, 4, 1];
const cfg = Graph.of(start, adjacency); // copies and checks both arrays
// cfg: 6 nodes, 7 edges
// The other way round: the arrays of a graph, for code of your own that reads every
// edge. They are the graph's own arrays, not copies: read them, never change them.
const outStart = Csr.outAdjacencyStart(cfg); // [0, 1, 3, 5, 6, 7, 7]
const outNodes = Csr.outAdjacency(cfg); // [1, 2, 5, 3, 4, 4, 1]
Full example
// The control flow of the method Load, as another tool might hand it over: the
// out-nodes of all its blocks, block after block, and where the out-nodes of each
// block start. The blocks entry, header, body, then, next and exit are the nodes
// 0 to 5.
int[] start = [0, 1, 3, 5, 6, 7, 7];
int[] adjacency = [1, 2, 5, 3, 4, 4, 1];
Graph cfg = Graph.Of(start, adjacency); // copies and checks both arrays
// cfg: 6 nodes, 7 edges
// The other way round: the arrays of a graph, for code of your own that reads
// every edge. They are the graph's own arrays, not copies, handed out as
// read-only spans.
// [0, 1, 3, 5, 6, 7, 7]
ReadOnlySpan<int> outStart = Csr.OutAdjacencyStart(cfg);
// [1, 2, 5, 3, 4, 4, 1]
ReadOnlySpan<int> outNodes = Csr.OutAdjacency(cfg);
Full example

The result is the control-flow graph of the picture, with 6 nodes and 7 edges. The out-nodes of block v stand in adjacency from position start[v] up to just before position start[v + 1]. The position of an out-node is the edge index of its edge. CsrcsrCsrCsr gives access to the copies that the graph keeps. They hold the same values as the arrays the graph was made from.

  • Checked, so sorted. The out-nodes of each node must be strictly ascending, and the starts must begin at 0, never decrease and end at the length of the out-nodes. Otherwise Graph.ofGraph::ofGraph.ofGraph.Of rejects the arrays. For edges in any order or with duplicates, use a builder. See Build a graph from a list of edges.
  • The arrays of a graph are its own. CsrcsrCsrCsr hands them out without copying, for code that visits every edge as fast as possible. Never write to them, even where their type allows it: every user of the graph shares them, and a write would change the graph for all of them. See Loop over the arrays of a graph directly for such code.