adjacencyListGraph(
options):MutableGraph
Defined in: packages/graph/src/adjacency-list-graph.ts:50
A mutable graph stored as one sorted row per vertex, each row a slot with
spare capacity in one shared Int32Array of neighbor ids, plus a parallel
Float64Array of weights when the graph is weighted.
It is the one layout that grows after construction, through addVertex,
addEdge, and deleteEdge. A row that outgrows its slot moves to the end
of the shared array with doubled capacity, and the array is compacted once
dead slots and spare capacity outweigh the stored arcs, so a graph that
shrinks gives its memory back. An isolated vertex costs 12 bytes. A directed
graph also keeps a reverse row per vertex for predecessor queries, which
doubles its memory.
addEdge keeps the row sorted, which costs O(log d + d) for a vertex of
degree d. Building from options.edges is O(n + m), so a large edge set
known up front belongs in options.edges rather than in a loop of
addEdge calls.
Parameters
Section titled “Parameters”options
Section titled “options”The vertex count, the two required flags, the multigraph flags, and the edges.
Returns
Section titled “Returns”Throws
Section titled “Throws”With code INVALID_VERTEX_COUNT, INVALID_FLAG, INVALID_EDGE,
INVALID_VERTEX, MISSING_WEIGHT, UNEXPECTED_WEIGHT, INVALID_WEIGHT, SELF_LOOP, or PARALLEL_EDGE
when an option or an edge breaks the contract, or MAX_CAPACITY_EXCEEDED when storage cannot be allocated.
Example
Section titled “Example”const graph = adjacencyListGraph({ vertexCount: 2, directed: false, weighted: true })const city = graph.addVertex() // 2graph.addEdge(0, city, 7)graph.hasEdge(city, 0) // true