Skip to content

Graph

Defined in: packages/data/src/graph/graph.ts:32

An undirected graph held as a sparse adjacency list.

The backing is Map<N, Map<N, E>> — an adjacency list, as the sparse case this package favours wants, rather than an adjacency matrix that would cost O(n²) regardless of how few edges exist. The inner Map rather than a set is what carries the edge label and keeps hasEdge/edgeLabel O(1).

An edge is stored under both endpoints, so addEdge(a, b) makes b a neighbor of a and a a neighbor of b; edgeCount() still counts it once. A self-loop is stored once and counts once toward degree.

Edge weights are just labels here. The ordering a weighted algorithm needs belongs to that algorithm (@ac-kit/algo), not to the representation — which is why this package carries no numeric dependency.

Time complexity: O(1) for every node and edge operation; O(degree) to delete a node, since each neighbor’s own adjacency has to drop it. Space is O(nodes

  • Edges).

Nodes are compared by SameValueZero, native Map semantics.

N

The node type.

E extends DefinedValue | void = void

The edge label type. void for an unlabeled graph, in which case addEdge’s third argument may be omitted.

new Graph<N, E>(nodes?): Graph<N, E>

Defined in: packages/data/src/graph/graph.ts:39

Iterable<N, any, any>

Graph<N, E>

addEdge(from, to, label): void

Defined in: packages/data/src/graph/graph.ts:105

Adds an undirected edge, creating either endpoint if it is unknown. Re-adding an existing edge replaces its label rather than duplicating it. O(1).

N

N

E

void

IGraph.addEdge


addNode(node): void

Defined in: packages/data/src/graph/graph.ts:56

O(1). A node already present keeps its edges.

N

void

IGraph.addNode


addNodes(nodes): void

Defined in: packages/data/src/graph/graph.ts:63

O(m) in the number of nodes.

Iterable<N>

void

IGraph.addNodes


degree(node): number

Defined in: packages/data/src/graph/graph.ts:177

The number of adjacent nodes; a self-loop counts once. O(1).

N

number

IGraph.degree


deleteEdge(from, to): boolean

Defined in: packages/data/src/graph/graph.ts:120

O(1).

N

N

boolean

IGraph.deleteEdge


deleteNode(node): boolean

Defined in: packages/data/src/graph/graph.ts:70

Removes node and every edge touching it. O(degree).

N

boolean

IGraph.deleteNode


edgeCount(): number

Defined in: packages/data/src/graph/graph.ts:51

Each undirected edge once, not twice. O(1).

number

IGraph.edgeCount


edgeLabel(from, to): E | undefined

Defined in: packages/data/src/graph/graph.ts:146

O(1).

N

N

E | undefined

The label, or undefined when there is no such edge. An unlabeled graph (E = void) returns undefined either way — ask hasEdge instead.

IGraph.edgeLabel


edges(): IterableIterator<readonly [N, N, E]>

Defined in: packages/data/src/graph/graph.ts:151

Each undirected edge once, oriented as first encountered.

IterableIterator<readonly [N, N, E]>

IGraph.edges


hasEdge(from, to): boolean

Defined in: packages/data/src/graph/graph.ts:135

O(1). Symmetric: the edge is undirected.

N

N

boolean

IGraph.hasEdge


hasNode(node): boolean

Defined in: packages/data/src/graph/graph.ts:91

O(1).

N

boolean

IGraph.hasNode


neighbors(node): IterableIterator<N>

Defined in: packages/data/src/graph/graph.ts:168

Empty for an unknown node.

N

IterableIterator<N>

IGraph.neighbors


nodeCount(): number

Defined in: packages/data/src/graph/graph.ts:46

O(1).

number

IGraph.nodeCount


nodes(): IterableIterator<N>

Defined in: packages/data/src/graph/graph.ts:96

In first-insertion order.

IterableIterator<N>

IGraph.nodes