# adjacencyMatrixGraph

> **adjacencyMatrixGraph**(`options`): [`AdjacencyMatrixGraph`](/reference/knotkit/graph/interfaces/adjacencymatrixgraph/)

Defined in: [packages/graph/src/adjacency-matrix-graph.ts:40](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/graph/src/adjacency-matrix-graph.ts#L40)

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.

## Parameters

### options

[`GraphOptions`](/reference/knotkit/graph/interfaces/graphoptions/)

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

## Returns

[`AdjacencyMatrixGraph`](/reference/knotkit/graph/interfaces/adjacencymatrixgraph/)

## Throws

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.

## Example

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