# connectedComponents

> **connectedComponents**(`adjacency`): [`ConnectedComponentsResult`](/reference/knotkit/graph/interfaces/connectedcomponentsresult/)

Defined in: [packages/graph/src/connected-components.ts:33](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/graph/src/connected-components.ts#L33)

Label the connected components of an undirected graph. `O(n + m)` on `compressed-sparse-row` and `adjacency-list`; it walks every row once, so an `adjacency-matrix` costs `O(n² / 32 + m)` unweighted and `O(n²)` weighted.

Labels are assigned by breadth-first search from each unlabelled vertex in ascending id, so vertex `0` is always in component `0` and the labels are the same on every layout.

## Parameters

### adjacency

[`Adjacency`](/reference/knotkit/graph/interfaces/adjacency/)

## Returns

[`ConnectedComponentsResult`](/reference/knotkit/graph/interfaces/connectedcomponentsresult/)

## Throws

With code `UNDIRECTED_GRAPH_REQUIRED` for a directed graph, whose weak and strong components are different questions; or, from a hand-written `adjacency`, `INVALID_VERTEX` or `INVALID_WEIGHT` for a neighbor or weight it reports, and `INVALID_VERTEX_COUNT` or `INVALID_FLAG` when it misstates its shape.

## Example

```ts
const { count, component } = connectedComponents(graph)
component[3] === component[7] // whether 3 and 7 are connected
```
