gridAdjacency(
options):GridAdjacency
Defined in: packages/graph/src/grid-adjacency.ts:74
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
Section titled “Parameters”options
Section titled “options”Returns
Section titled “Returns”Throws
Section titled “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
Section titled “Example”const grid = [[1, 1, 0], [0, 1, 0], [1, 0, 1]]const open = (cell: number) => grid[Math.floor(cell / 3)][cell % 3] === 1const 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