Skip to content

@graphrs/core

核心包提供 Graph 类、类型定义、错误类和 WASM 加载器。所有其他包都依赖于它。

bash
npm install @graphrs/core

Graph 类

构造函数

typescript
new Graph<N extends NodeData, E extends EdgeData>(options?: GraphOptions)
选项类型默认值说明
directedbooleanfalse边是否有方向

静态工厂方法

Graph.fromEdges(edges, options?)

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

[source, target] 对数组创建图。节点自动创建。

Graph.fromAdjacencyMatrix(matrix, options?)

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

从邻接矩阵创建图。非零值会成为边的权重。

Graph.fromJSON(data)

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

从序列化的 JSON 对象创建图。

实例方法

方法返回值说明
addNode(id, data?)this添加节点
addEdge(source, target, data?)this添加边(自动创建节点)
removeNode(id)this删除节点及其所有边
removeEdge(source, target)this删除一条边
nodeCount()number节点数量
edgeCount()number边数量
hasNode(id)boolean检查节点是否存在
hasEdge(source, target)boolean检查边是否存在
neighbors(id)VertexId[]相邻节点 ID
degree(id)number相邻边的数量
nodes()VertexId[]所有节点 ID
edges()Array<{source, target, data}>所有边
nodeData(id)N节点的自定义数据
subgraph(nodeIds)Graph<N, E>提取子图
toJSON()SerializedGraph序列化为 JSON
toG6Format(layout?)G6GraphData转换为 AntV G6 格式
toReactFlowFormat(layout?)ReactFlowData转换为 React Flow 格式
toCytoscapeFormat(layout?)CytoscapeData转换为 Cytoscape.js 格式

在线体验

图创建与查询

序列化往返

类型

图类型

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> }>;
}

算法结果类型

typescript
interface CommunityResult {
  membership: number[];  // 每个节点的社区索引
  modularity: number;    // 模块度分数
  clusters: number;      // 社区数量
}

interface CentralityResult {
  scores: number[];  // 每个节点的中心性分数
}

interface LayoutResult {
  positions: [number, number][];  // 每个节点的 [x, y] 坐标
}

interface Layout3DResult {
  positions: [number, number, number][];  // 每个节点的 [x, y, z] 坐标
}

interface PathResult {
  path: number[];     // 最短路径上的节点 ID
  distance: number;   // 总距离(不可达时为 Infinity)
}

interface FlowResult {
  value: number;   // 最大流总量
  flow: number[];  // 每条边的流量值
}

可视化格式类型

这些类型描述 toG6Format()toReactFlowFormat()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

其他结果类型(BfsResultDfsResultHitsResultMinCutResult)从各自的包中导出。请参阅 pathcentralityflow API 参考。

错误

错误类错误码触发时机
GraphError不定所有错误的基类
NodeNotFoundErrorNODE_NOT_FOUND节点不存在
EdgeNotFoundErrorEDGE_NOT_FOUND边不存在
WasmNotInitializedErrorWASM_NOT_INITIALIZED在初始化前访问 WASM
WasmErrorWASM_ERRORWASM 运行时错误

WASM 加载器

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)