# gridAdjacency

> **gridAdjacency**(`options`): [`GridAdjacency`](/reference/knotkit/graph/interfaces/gridadjacency/)

Defined in: [packages/graph/src/grid-adjacency.ts:74](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/graph/src/grid-adjacency.ts#L74)

Treat a board of cells as a graph without storing it: each cell is a vertex, and each move to a neighboring cell is an edge of weight `1`.

Every algorithm that takes an `Adjacency` accepts the result, which is all but `floydWarshall`: the fewest moves through a maze is one `breadthFirstSearch`, and the islands are `connectedComponents`. A closed cell is still a vertex, an isolated one with a component of its own, so count islands as the distinct labels of the open cells, as in the example. Moves are visited in ascending cell order. Memory is `O(1)`; a walk over one cell is `O(1)` plus the callbacks.

## Parameters

### options

[`GridAdjacencyOptions`](/reference/knotkit/graph/interfaces/gridadjacencyoptions/)

## Returns

[`GridAdjacency`](/reference/knotkit/graph/interfaces/gridadjacency/)

## Throws

With code `INVALID_GRID` when `rows` or `columns` is not a positive integer, when the board would hold more than `GRAPH_MAX_VERTEX_COUNT` cells, or when `connectivity` is not `4` or `8`.

## Example

```ts
const grid = [[1, 1, 0], [0, 1, 0], [1, 0, 1]]
const open = (cell: number) => grid[Math.floor(cell / 3)][cell % 3] === 1
const board = gridAdjacency({ rows: 3, columns: 3, open })
board.cell(1, 1) // 4

const { component } = connectedComponents(board)
const islands = new Set<number>()
for (let cell = 0; cell < board.vertexCount; cell += 1) if (open(cell)) islands.add(component[cell])
islands.size // 3; connectedComponents(board).count is 7, one per water cell as well
```
