Skip to content

adjacencyMatrixGraph(options): AdjacencyMatrixGraph

Defined in: packages/graph/src/adjacency-matrix-graph.ts:40

An immutable graph stored as a dense n × n matrix: one bit per vertex pair when unweighted, or one Float64Array cell per pair when weighted, with Infinity for “no edge”.

hasEdge is O(1), and an unweighted dense graph costs n² / 8 bytes whatever its edge count. A row walk costs O(n / 32 + d) unweighted and O(n) weighted, so on a sparse graph every traversal pays for the absent edges. Storage is checked against GRAPH_MAX_MATRIX_BYTES before it is allocated. One cell per pair means the matrix cannot hold parallel edges.

GraphOptions

The vertex count, the two required flags, selfLoops, and the edges.

AdjacencyMatrixGraph

With code PARALLEL_EDGES_UNSUPPORTED for parallelEdges: true; MAX_CAPACITY_EXCEEDED when the matrix would pass GRAPH_MAX_MATRIX_BYTES or the runtime refuses it; or 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.

const graph = adjacencyMatrixGraph({ vertexCount: 3, directed: true, weighted: true, edges: [[0, 2, 4]] })
graph.hasEdge(0, 2) // true