Class Csr
- Namespace
- Graphity
- Assembly
- Graphity.Core.dll
Access to the arrays in which a graph keeps its edges, for algorithm code that loops over all edges. These arrays are called the CSR (compressed sparse row) layout of the graph.
public static class Csr
- Inheritance
-
Csr
- Inherited Members
Examples
Here, calls holds the calls between the methods of a program, and the loop counts the
callers of each method:
ReadOnlySpan<int> start = Csr.OutAdjacencyStart(calls);
ReadOnlySpan<int> callees = Csr.OutAdjacency(calls);
int[] callerCount = new int[calls.NodeCount];
for (int method = 0; method < calls.NodeCount; method++)
{
for (int edge = start[method], end = start[method + 1]; edge < end; edge++)
{
callerCount[callees[edge]]++;
}
}
// callerCount: 3 for format, 2 for parse, 0 for main, 1 for every other method
Remarks
Each method returns a span over the graph's own array, not a copy. The out-nodes of node v
are OutAdjacency[OutAdjacencyStart[v]..OutAdjacencyStart[v + 1] - 1], in ascending order,
and the position of an out-node in OutAdjacency is the edge index of its edge.
OutAdjacencyStart has NodeCount + 1 values, from 0 to EdgeCount. A
loop loads the two spans into local variables once and reads them without a method call or an
argument check per edge.
A BiAdjacentGraph also holds the in-nodes of every node, called its in-adjacency.
The in-adjacency has the same layout, in InAdjacencyStart(BiAdjacentGraph) and
InAdjacency(BiAdjacentGraph). A position in InAdjacency is not an edge index of the graph,
though, but of its transposed graph; TransposedEdgeMapping(Graph) translates all of
them in one pass.
The spans cannot modify the arrays. That keeps what every algorithm relies on: that the graph is
immutable and safe to share between threads, and that the out-nodes of each node are sorted. A
span lives on the stack only; to keep an array beyond a method, copy it with ToArray() or
keep the graph and ask again. The algorithms of this library read the arrays the same way. Code
that reads the out-nodes of a few nodes is better served by OutNodes(int) and the
edge index methods of Graph, which check their arguments.
Methods
InAdjacency(BiAdjacentGraph)
Returns the in-nodes of all nodes, node after node.
public static ReadOnlySpan<int> InAdjacency(BiAdjacentGraph graph)
Parameters
graphBiAdjacentGraphThe graph.
Returns
- ReadOnlySpan<int>
A read-only span over the graph's own array, of length EdgeCount.
Remarks
A position in this span is the edge index of the edge in the transposed graph, not in
graph.
Time: O(1).
InAdjacencyStart(BiAdjacentGraph)
Returns where the in-nodes of each node start in InAdjacency(BiAdjacentGraph), followed by EdgeCount.
public static ReadOnlySpan<int> InAdjacencyStart(BiAdjacentGraph graph)
Parameters
graphBiAdjacentGraphThe graph.
Returns
- ReadOnlySpan<int>
A read-only span over the graph's own array, of length NodeCount + 1.
Remarks
Time: O(1).
OutAdjacency(Graph)
Returns the out-nodes of all nodes, node after node, at the positions of their edge indices.
public static ReadOnlySpan<int> OutAdjacency(Graph graph)
Parameters
graphGraphThe graph.
Returns
- ReadOnlySpan<int>
A read-only span over the graph's own array, of length EdgeCount.
Remarks
Time: O(1).
OutAdjacencyStart(Graph)
Returns where the out-nodes of each node start in OutAdjacency(Graph), followed by EdgeCount.
public static ReadOnlySpan<int> OutAdjacencyStart(Graph graph)
Parameters
graphGraphThe graph.
Returns
- ReadOnlySpan<int>
A read-only span over the graph's own array, of length NodeCount + 1.
Remarks
Time: O(1).