Skip to content

@graphty/algorithms / core/graph / Graph

Class: Graph ​

Defined in: core/graph.ts:9

Core Graph data structure for the Graphty Algorithms library

Provides efficient graph representation with support for both directed and undirected graphs. Uses adjacency lists for optimal performance with sparse graphs.

Constructors ​

Constructor ​

new Graph(config): Graph

Defined in: core/graph.ts:20

Creates a new Graph instance.

Parameters ​

config ​

Partial<GraphConfig> = {}

Configuration options for the graph

Returns ​

Graph

Accessors ​

isDirected ​

Get Signature ​

get isDirected(): boolean

Defined in: core/graph.ts:238

Check if the graph is directed

Returns ​

boolean

True if the graph is directed, false otherwise


nodeCount ​

Get Signature ​

get nodeCount(): number

Defined in: core/graph.ts:222

Get the number of nodes in the graph

Returns ​

number

The total count of nodes


totalEdgeCount ​

Get Signature ​

get totalEdgeCount(): number

Defined in: core/graph.ts:230

Get the number of edges in the graph

Returns ​

number

The total count of edges


uniqueEdgeCount ​

Get Signature ​

get uniqueEdgeCount(): number

Defined in: core/graph.ts:381

Get the number of unique edges in the graph For undirected graphs, each edge is counted once

Returns ​

number

The count of unique edges

Methods ​

addEdge() ​

addEdge(source, target, weight, data?): void

Defined in: core/graph.ts:101

Add an edge to the graph

Parameters ​

source ​

NodeId

The source node identifier

target ​

NodeId

The target node identifier

weight ​

number = 1

The weight of the edge (defaults to 1)

data? ​

Record<string, unknown>

Optional key-value data to attach to the edge

Returns ​

void


addNode() ​

addNode(id, data?): void

Defined in: core/graph.ts:38

Add a node to the graph

Parameters ​

id ​

NodeId

The unique identifier for the node

data? ​

Record<string, unknown>

Optional key-value data to attach to the node

Returns ​

void


clear() ​

clear(): void

Defined in: core/graph.ts:369

Clear all nodes and edges from the graph

Returns ​

void


clone() ​

clone(): Graph

Defined in: core/graph.ts:342

Create a copy of the graph

Returns ​

Graph

A new Graph instance with the same nodes and edges


degree() ​

degree(nodeId): number

Defined in: core/graph.ts:305

Get the degree of a node

Parameters ​

nodeId ​

NodeId

The node identifier to get the degree for

Returns ​

number

The total degree of the node


edges() ​

edges(): IterableIterator<Edge>

Defined in: core/graph.ts:254

Get all edges in the graph

Returns ​

IterableIterator<Edge>

Yields ​

Each unique edge in the graph


getConfig() ​

getConfig(): GraphConfig

Defined in: core/graph.ts:362

Get graph configuration

Returns ​

GraphConfig

A copy of the graph configuration object


getEdge() ​

getEdge(source, target): Edge | undefined

Defined in: core/graph.ts:213

Get an edge by source and target

Parameters ​

source ​

NodeId

The source node identifier

target ​

NodeId

The target node identifier

Returns ​

Edge | undefined

The edge if found, undefined otherwise


getNode() ​

getNode(id): Node | undefined

Defined in: core/graph.ts:203

Get a node by ID

Parameters ​

id ​

NodeId

The unique identifier of the node to retrieve

Returns ​

Node | undefined

The node if found, undefined otherwise


hasEdge() ​

hasEdge(source, target): boolean

Defined in: core/graph.ts:193

Check if an edge exists in the graph

Parameters ​

source ​

NodeId

The source node identifier

target ​

NodeId

The target node identifier

Returns ​

boolean

True if the edge exists, false otherwise


hasNode() ​

hasNode(id): boolean

Defined in: core/graph.ts:183

Check if a node exists in the graph

Parameters ​

id ​

NodeId

The unique identifier of the node to check

Returns ​

boolean

True if the node exists, false otherwise


inDegree() ​

inDegree(nodeId): number

Defined in: core/graph.ts:319

Get the in-degree of a node

Parameters ​

nodeId ​

NodeId

The node identifier to get the in-degree for

Returns ​

number

The number of incoming edges to the node


inNeighbors() ​

inNeighbors(nodeId): IterableIterator<NodeId>

Defined in: core/graph.ts:282

Get incoming neighbors of a node (directed graphs only)

Parameters ​

nodeId ​

NodeId

The node identifier to get incoming neighbors for

Returns ​

IterableIterator<NodeId>

An iterator over the incoming neighbor node identifiers


neighbors() ​

neighbors(nodeId): IterableIterator<NodeId>

Defined in: core/graph.ts:272

Get neighbors of a node (outgoing edges)

Parameters ​

nodeId ​

NodeId

The node identifier to get neighbors for

Returns ​

IterableIterator<NodeId>

An iterator over the neighbor node identifiers


nodes() ​

nodes(): IterableIterator<Node>

Defined in: core/graph.ts:246

Get all nodes in the graph

Returns ​

IterableIterator<Node>

An iterator over all nodes


outDegree() ​

outDegree(nodeId): number

Defined in: core/graph.ts:333

Get the out-degree of a node

Parameters ​

nodeId ​

NodeId

The node identifier to get the out-degree for

Returns ​

number

The number of outgoing edges from the node


outNeighbors() ​

outNeighbors(nodeId): IterableIterator<NodeId>

Defined in: core/graph.ts:296

Get outgoing neighbors of a node

Parameters ​

nodeId ​

NodeId

The node identifier to get outgoing neighbors for

Returns ​

IterableIterator<NodeId>

An iterator over the outgoing neighbor node identifiers


removeEdge() ​

removeEdge(source, target): boolean

Defined in: core/graph.ts:153

Remove an edge from the graph

Parameters ​

source ​

NodeId

The source node identifier

target ​

NodeId

The target node identifier

Returns ​

boolean

True if the edge was removed, false if it did not exist


removeNode() ​

removeNode(id): boolean

Defined in: core/graph.ts:54

Remove a node from the graph

Parameters ​

id ​

NodeId

The unique identifier of the node to remove

Returns ​

boolean

True if the node was removed, false if it did not exist