# compressedSparseRowGraph

> **compressedSparseRowGraph**(`options`): [`CompressedSparseRowGraph`](/reference/knotkit/graph/interfaces/compressedsparserowgraph/)

Defined in: [packages/graph/src/compressed-sparse-row-graph.ts:35](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/graph/src/compressed-sparse-row-graph.ts#L35)

An immutable graph stored as compressed sparse rows: one `Int32Array` of row offsets and one flat array of neighbor ids, plus a flat `Float64Array` of weights when the graph is weighted.

It is the layout for an edge set known up front: `4(n + 1) + 4m'` bytes, plus `8m'` when weighted, where `m'` is the stored arcs (`2m` undirected). Rows are contiguous, so a walk reads memory in order. A directed graph counts its in-degrees into one `Int32Array` on the first `inDegree` call, and builds its transposed rows only on the first `forEachPredecessor` call, keeping them and dropping the counts.

## Parameters

### options

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

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

## Returns

[`CompressedSparseRowGraph`](/reference/knotkit/graph/interfaces/compressedsparserowgraph/)

## 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

```ts
const graph = compressedSparseRowGraph({
  vertexCount: 4,
  directed: true,
  weighted: false,
  edges: [[0, 1], [0, 2], [1, 3], [2, 3]],
})
graph.neighbors(0) // Int32Array [1, 2]
```
