Skip to content

@graphrs/core

The core package provides the Graph class, type definitions, error classes, and the WASM loader. All other packages depend on it.

bash
npm install @graphrs/core

Graph Class

Constructor

typescript
new Graph<N extends NodeData, E extends EdgeData>(options?: GraphOptions)
OptionTypeDefaultDescription
directedbooleanfalseWhether edges are directed

Static Factories

Graph.fromEdges(edges, options?)

typescript
static fromEdges<N, E>(
  edges: [number, number][],
  options?: GraphOptions,
): Graph<N, E>

Creates a graph from an array of [source, target] pairs. Nodes are auto-created.

Graph.fromAdjacencyMatrix(matrix, options?)

typescript
static fromAdjacencyMatrix<N, E>(
  matrix: number[][],
  options?: GraphOptions,
): Graph<N, E>

Creates a graph from an adjacency matrix. Non-zero values become edge weights.

Graph.fromJSON(data)

typescript
static fromJSON<N, E>(data: SerializedGraph): Graph<N, E>

Creates a graph from a serialized JSON object.

Instance Methods

MethodReturnsDescription
addNode(id, data?)thisAdd a node
addEdge(source, target, data?)thisAdd an edge (auto-creates nodes)
removeNode(id)thisRemove a node and its edges
removeEdge(source, target)thisRemove an edge
nodeCount()numberNumber of nodes
edgeCount()numberNumber of edges
hasNode(id)booleanCheck if node exists
hasEdge(source, target)booleanCheck if edge exists
neighbors(id)VertexId[]Adjacent node IDs
degree(id)numberNumber of adjacent edges
nodes()VertexId[]All node IDs
edges()Array<{source, target, data}>All edges
nodeData(id)NNode's custom data
subgraph(nodeIds)Graph<N, E>Extract a subgraph
toJSON()SerializedGraphSerialize to JSON
toG6Format(layout?)G6GraphDataConvert for AntV G6
toReactFlowFormat(layout?)ReactFlowDataConvert for React Flow
toCytoscapeFormat(layout?)CytoscapeDataConvert for Cytoscape.js

Try It

Graph Creation & Querying

Serialization Round-trip

Types

Graph Types

typescript
type VertexId = number;
type EdgeId = number;

interface GraphOptions {
  directed?: boolean;
}

interface NodeData {
  [key: string]: unknown;
}

interface EdgeData {
  weight?: number;
  [key: string]: unknown;
}

interface SerializedGraph {
  directed: boolean;
  nodes: Array<{ id: VertexId; data?: Record<string, unknown> }>;
  edges: Array<{ source: VertexId; target: VertexId; data?: Record<string, unknown> }>;
}

Algorithm Result Types

typescript
interface CommunityResult {
  membership: number[];  // community index per node
  modularity: number;    // modularity score
  clusters: number;      // number of communities
}

interface CentralityResult {
  scores: number[];  // centrality score per node
}

interface LayoutResult {
  positions: [number, number][];  // [x, y] per node
}

interface Layout3DResult {
  positions: [number, number, number][];  // [x, y, z] per node
}

interface PathResult {
  path: number[];     // node IDs along the shortest path
  distance: number;   // total distance (Infinity if unreachable)
}

interface FlowResult {
  value: number;   // total maximum flow value
  flow: number[];  // flow value per edge
}

Visualization Format Types

These types describe the output of toG6Format(), toReactFlowFormat(), and toCytoscapeFormat().

typescript
interface G6GraphData {
  nodes: Array<{ id: string; x?: number; y?: number; [key: string]: unknown }>;
  edges: Array<{ source: string; target: string; [key: string]: unknown }>;
}

interface ReactFlowData {
  nodes: Array<{
    id: string;
    position: { x: number; y: number };
    data: Record<string, unknown>;
  }>;
  edges: Array<{ id: string; source: string; target: string }>;
}

interface CytoscapeData {
  elements: {
    nodes: Array<{ data: { id: string; [key: string]: unknown } }>;
    edges: Array<{ data: { source: string; target: string; [key: string]: unknown } }>;
  };
}

TIP

Additional result types (BfsResult, DfsResult, HitsResult, MinCutResult) are exported from their respective packages. See the path, centrality, and flow API references.

Errors

Error ClassCodeWhen
GraphErrorvariesBase class for all errors
NodeNotFoundErrorNODE_NOT_FOUNDNode doesn't exist
EdgeNotFoundErrorEDGE_NOT_FOUNDEdge doesn't exist
WasmNotInitializedErrorWASM_NOT_INITIALIZEDWASM accessed before init
WasmErrorWASM_ERRORWASM runtime error

WASM Loader

typescript
import { getWasm, getWasmSync, isWasmInitialized } from '@graphrs/core';

await getWasm();         // Load WASM (lazy singleton)
getWasmSync();           // Get WASM sync (null if not loaded)
isWasmInitialized();     // Check if WASM is loaded

MIT License (TS code) · GPL-2.0 (WASM binary)