The number of edges.
Time: O(1).
Whether this graph has no nodes. Equivalent to nodeCount === 0.
Time: O(1).
The number of nodes.
Time: O(1).
Returns the edge index of the edge source -> target, or -1 if there is none.
GraphBuilder sorts the edges and merges duplicates, so an edge does not keep the
position at which it was added; this method finds where it went. Here, calls was built with
one edge per call site, and the call sites of each call are counted:
const siteCount = new Int32Array(calls.edgeCount);
for (const site of callSites) {
siteCount[calls.edgeIndexOf(site.caller, site.callee)]++;
}
// 2 for run -> show and for parse -> parse, 1 for every other edge
Time: O(log outDegree(source)).
node ID in 0..nodeCount-1
node ID; a value out of range yields -1
edge index in 0..edgeCount-1, or -1
Returns the source of edge edgeIndex.
Unlike the target, the source is searched for; Graph.forEachEdge gives every edge with its source without a search.
Time: O(log nodeCount).
edge index in 0..edgeCount-1
source node ID
Returns whether other is a graph with the same nodes and edges (Java: equals(Object)).
The in-adjacency does not count: a BiAdjacentGraph equals a Graph with the same
nodes and edges.
Time: O(nodeCount + edgeCount).
Calls action for every edge with its source, target and edge index, in edge index order.
The source comes with each edge at no cost, whereas Graph.edgeSource searches for it.
Time: O(nodeCount + edgeCount).
called with (source, target, edgeIndex) for each edge
Returns a hash code over the nodes and edges, consistent with Graph.equals. It has the same value as in Java.
It is computed on every call, so each lookup of a graph by this hash walks the whole graph. Graphs.topologyHash gives a 64-bit hash to compute once and keep.
Time: O(nodeCount + edgeCount).
Returns the node IDs 0..nodeCount-1 as a generator, in ascending order (Java: an
IntStream).
Time: O(1) to create the generator.
node IDs
Returns the end, exclusive, of the edge index range of the out-edges of node; see
Graph.outEdgeStart.
Time: O(1).
node ID in 0..nodeCount-1
end of the range, exclusive
Returns the start of the edge index range of the out-edges of node. The out-edges are the
edges with node as source.
Their edge indices are outEdgeStart(node)..outEdgeEnd(node)-1, in the order of
Graph.outNodes; the range is empty if node has no out-edges. A loop over the range
reads the data of each edge from arrays indexed by edge index. Here, it lists the methods that
run calls, with the number of call sites of each call in siteCount:
for (let edge = calls.outEdgeStart(run); edge < calls.outEdgeEnd(run); edge++) {
console.log(`${names[calls.edgeTarget(edge)]}: ${siteCount[edge]}`);
}
// load: 1
// save: 1
// show: 2
Time: O(1).
node ID in 0..nodeCount-1
start of the range
Returns the out-nodes of node in ascending order. These are the targets of the edges with
node as source.
The result is a view of this graph's own array, not a copy.
Time: O(1).
node ID in 0..nodeCount-1
out-nodes
Returns the node and edge counts and the out-nodes of the first nodes, for debuggers and logs. For the control flow of a method with a loop:
const text = cfg.toString();
// Graph[nodes=6, edges=7] {0 -> [1], 1 -> [2, 5], 2 -> [3, 4], 3 -> [4], 4 -> [1],
// 5 -> []}
The text stays short however large the graph: it lists at most 16 nodes and 64 out-nodes in
total. ... marks where it is cut - after the 16th node, or inside the list that would go
beyond 64 out-nodes. A BiAdjacentGraph is named as such and lists its out-nodes.
Graphs.adjacencyListOf gives the whole graph as text.
Time: O(1), as the text is bounded.
Returns a graph with the same nodes and edges, without in-adjacency. A plain Graph returns
itself, and a BiAdjacentGraph returns a Graph over its out-adjacency.
Time: O(1).
this graph, or a graph over the out-adjacency of a BiAdjacentGraph
Writes this graph to out, for Graph.readFrom to read back with the same node IDs and
edge indices.
Only the structure is written. Data in arrays indexed by node ID or edge index stays valid for the graph read back and can follow it in the same file or stream. Here, the names of the methods of a program follow the calls between them:
const fd = openSync(file, "w");
try {
calls.writeTo({ write: (chunk) => writeSync(fd, chunk) });
writeSync(fd, JSON.stringify(names));
} finally {
closeSync(fd);
}
A BiAdjacentGraph writes its out-adjacency only.
The format is the two arrays that Graph.of takes, preceded by the node count and the edge count. Every field is a signed 32-bit integer in little-endian byte order:
| offset in bytes | size in bytes | field |
|---|---|---|
| 0 | 4 | nodeCount |
| 4 | 4 | edgeCount |
| 8 | 4 x (nodeCount + 1) | outAdjacencyStart |
| 8 + 4 x (nodeCount + 1) | 4 x edgeCount | outAdjacency |
The arrays follow the rules of Graph.of. Nothing else is written: no padding, checksum, magic number or version. So a graph takes exactly 8 + 4 x (nodeCount + 1 + edgeCount) bytes, and Graph.readFrom recognizes foreign data only where it does not form a valid graph. Every implementation of Graphity reads and writes this format, so a graph written in one language can be read in another.
Passes the header and then the arrays. Where the platform is little-endian, each array goes in one chunk, which is a view of the graph's own array, so nothing is copied. Otherwise the chunks are little-endian copies in blocks of 64 KiB. A file stream therefore needs no buffering of its own.
Time: O(nodeCount + edgeCount). Memory: 8 bytes for the header; where the platform is big-endian, 4 x (nodeCount + 1 + edgeCount) bytes for the copies.
where the bytes go, for instance a file stream of Node.js; not closed
Error what out.write throws (Java: IOException); only a write that throws fails
the call. A stream reports its errors through its own event or promise, which the caller
must watch (see ByteSink)
StaticofReturns a graph from the two arrays of its CSR layout, as Csr returns them and Graph.writeTo writes them.
The out-nodes of node v are
outAdjacency[outAdjacencyStart[v]..outAdjacencyStart[v + 1] - 1], and the position of an
out-node in outAdjacency is the edge index of its edge. For example, the control flow of a
method with a loop:
// blocks 0..5: entry, header, body, then, next, exit
const outAdjacencyStart = [0, 1, 3, 5, 6, 7, 7];
const outAdjacency = [
1, // entry -> header
2, 5, // header -> body, exit
3, 4, // body -> then, next
4, // then -> next
1]; // next -> header
const cfg = Graph.of(outAdjacencyStart, outAdjacency);
Both arrays are copied and checked: the offsets start at 0, never decrease and end at
outAdjacency.length, and the out-nodes of each node are strictly ascending. For edges in any
order or with duplicates, use GraphBuilder.
Time: O(nodeCount + edgeCount). Memory: 4 x (nodeCount + edgeCount) bytes for the copies, which the result keeps.
where the out-nodes of each node start in outAdjacency, followed by
edgeCount; length nodeCount + 1; any array of numbers
the out-nodes of all nodes, node after node; length edgeCount
the graph
StaticreadReads a graph that Graph.writeTo wrote, with the same node IDs and edge indices.
Reads the graph at the start of bytes. Data written after the graph follows it at offset 8 +
4 x (nodeCount + 1 + edgeCount); the bytes from there on are not read. Here, that data is the
names of the methods of a program:
const bytes = readFileSync(file);
// equal to the graph written, edge indices included
const calls = Graph.readFrom(bytes);
// The names follow the graph, which takes 8 + 4 x (nodeCount + 1 + edgeCount) bytes.
const namesStart = 8 + 4 * (calls.nodeCount + 1 + calls.edgeCount);
const names: string[] = JSON.parse(bytes.subarray(namesStart).toString("utf8"));
The result has no in-adjacency, even if the data was written from a BiAdjacentGraph and
even when called as BiAdjacentGraph.readFrom(bytes); BiAdjacentGraph.of builds it
again.
The arrays are checked as by Graph.of, but not copied. Where the platform is
little-endian and an array lies 4-byte aligned in the buffer of bytes, the graph keeps a view
of that buffer, which must not change afterwards. Otherwise it keeps a converted copy. The
length of the data is checked against the header before anything is allocated, so a header that
announces more than the data holds ends in an Error, not in an allocation of the announced
size.
Time: O(nodeCount + edgeCount).
Memory: none for views, which keep the whole buffer of bytes reachable as long as the
graph is; otherwise 4 x (nodeCount + edgeCount) bytes for the result. To keep only the graph,
copy it with Graph.of, or pass a copy of its bytes:
bytes.slice(0, 8 + 4 * (nodeCount + 1 + edgeCount)).
the data to read, for instance the content of a file
the graph
Error if the data ends before the graph does (Java: EOFException), a count in the
header is negative or larger than a graph can have, or the arrays do not form a graph that
Graph.of accepts, whose error is then the cause (Java: IOException)
A directed graph, which is the input of the algorithms in this library.
Nodes are the integer IDs
0..nodeCount-1. Edges are numbered by edge index0..edgeCount-1, ordered by source and then by target. No two edges have the same source and target; self-loops are allowed. The graph holds nothing but this structure: names, weights and any other data live in arrays of the caller, indexed by node ID or by edge index.GraphBuilder builds a graph from edges in any order. Graph.of takes the two arrays of a CSR (compressed sparse row) layout. A graph keeps its edges in this form, and Csr hands the arrays to algorithm code. Graph.writeTo and Graph.readFrom store a graph and load it again.
A
Graphholds the out-nodes of every node, called its out-adjacency. Algorithms that also need the in-nodes take a BiAdjacentGraph, which BiAdjacentGraph.of builds and which also gives the transposed graph. Algorithms are not methods of the graph but types of their own, such as Components, TopOrder and Dominators; the documentation of the package lists them by topic.Instances are immutable.
Memory: 4 x (nodeCount + edgeCount) bytes.
A graph has at most 2^31 - 10 nodes and 2^31 - 9 edges.