Skip to content

connectedComponents(adjacency): ConnectedComponentsResult

Defined in: packages/graph/src/connected-components.ts:33

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.

Adjacency

ConnectedComponentsResult

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.

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