@statelyai/graph 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +121 -44
  2. package/dist/{adjacency-list-VsUaH9SJ.mjs → adjacency-list-DQ32Mmhx.mjs} +3 -1
  3. package/dist/algorithms-D1cgly0g.d.mts +452 -0
  4. package/dist/algorithms-DBpH74hR.mjs +3309 -0
  5. package/dist/algorithms.d.mts +2 -2
  6. package/dist/algorithms.mjs +2 -2
  7. package/dist/config-Dt5u1gSf.mjs +793 -0
  8. package/dist/{converter-udLITX36.mjs → converter-DB6Rg6Vd.mjs} +2 -2
  9. package/dist/format-support.mjs +38 -11
  10. package/dist/formats/adjacency-list/index.d.mts +1 -1
  11. package/dist/formats/adjacency-list/index.mjs +1 -1
  12. package/dist/formats/converter/index.d.mts +1 -1
  13. package/dist/formats/converter/index.mjs +1 -1
  14. package/dist/formats/cytoscape/index.d.mts +4 -4
  15. package/dist/formats/cytoscape/index.mjs +10 -4
  16. package/dist/formats/d2/index.d.mts +1 -1
  17. package/dist/formats/d2/index.mjs +26 -12
  18. package/dist/formats/d3/index.d.mts +4 -4
  19. package/dist/formats/d3/index.mjs +10 -4
  20. package/dist/formats/dot/index.d.mts +1 -1
  21. package/dist/formats/dot/index.mjs +22 -6
  22. package/dist/formats/edge-list/index.d.mts +1 -1
  23. package/dist/formats/edge-list/index.mjs +1 -1
  24. package/dist/formats/elk/index.d.mts +1 -1
  25. package/dist/formats/elk/index.mjs +63 -24
  26. package/dist/formats/gexf/index.d.mts +1 -1
  27. package/dist/formats/gexf/index.mjs +43 -16
  28. package/dist/formats/gml/index.d.mts +4 -4
  29. package/dist/formats/gml/index.mjs +28 -15
  30. package/dist/formats/graphml/index.d.mts +1 -1
  31. package/dist/formats/graphml/index.mjs +96 -23
  32. package/dist/formats/jgf/index.d.mts +4 -4
  33. package/dist/formats/jgf/index.mjs +12 -5
  34. package/dist/formats/mermaid/index.d.mts +1 -1
  35. package/dist/formats/mermaid/index.mjs +49 -12
  36. package/dist/formats/tgf/index.d.mts +4 -4
  37. package/dist/formats/tgf/index.mjs +4 -4
  38. package/dist/formats/xyflow/index.d.mts +12 -6
  39. package/dist/formats/xyflow/index.mjs +42 -10
  40. package/dist/{index-D9Kj6Fe3.d.mts → index-BlbSWUvH.d.mts} +1 -1
  41. package/dist/{index-CHoriXZD.d.mts → index-CNvqxPLJ.d.mts} +157 -30
  42. package/dist/index.d.mts +6 -6
  43. package/dist/index.mjs +290 -307
  44. package/dist/layout/cytoscape.d.mts +66 -0
  45. package/dist/layout/cytoscape.mjs +114 -0
  46. package/dist/layout/d3-force.d.mts +52 -0
  47. package/dist/layout/d3-force.mjs +127 -0
  48. package/dist/layout/d3-hierarchy.d.mts +39 -0
  49. package/dist/layout/d3-hierarchy.mjs +135 -0
  50. package/dist/layout/dagre.d.mts +32 -0
  51. package/dist/layout/dagre.mjs +99 -0
  52. package/dist/layout/elk.d.mts +47 -0
  53. package/dist/layout/elk.mjs +73 -0
  54. package/dist/layout/forceatlas2.d.mts +48 -0
  55. package/dist/layout/forceatlas2.mjs +100 -0
  56. package/dist/layout/graphviz.d.mts +50 -0
  57. package/dist/layout/graphviz.mjs +179 -0
  58. package/dist/layout/index.d.mts +185 -0
  59. package/dist/layout/index.mjs +181 -0
  60. package/dist/layout/webcola.d.mts +40 -0
  61. package/dist/layout/webcola.mjs +104 -0
  62. package/dist/{queries-BlkA1HAN.d.mts → queries-B6quF529.d.mts} +43 -12
  63. package/dist/queries-BMM0XAv_.mjs +986 -0
  64. package/dist/queries.d.mts +1 -1
  65. package/dist/queries.mjs +1 -768
  66. package/dist/schemas.d.mts +19 -1
  67. package/dist/schemas.mjs +32 -84
  68. package/dist/{types-3-FS9NV2.d.mts → types-BAEQTwK_.d.mts} +99 -7
  69. package/dist/validate-BsfSOv0S.mjs +190 -0
  70. package/package.json +59 -7
  71. package/schemas/edge.schema.json +27 -0
  72. package/schemas/graph.schema.json +27 -0
  73. package/dist/algorithms-Ba7o7niK.mjs +0 -2394
  74. package/dist/algorithms-fTqmvhzP.d.mts +0 -178
  75. package/dist/indexing-DR8M1vBy.mjs +0 -137
  76. /package/dist/{edge-list-DP4otyPU.mjs → edge-list-CA9UTvn2.mjs} +0 -0
  77. /package/dist/{mode-D8OnHFBk.mjs → mode-gu_mhKKs.mjs} +0 -0
@@ -1,178 +0,0 @@
1
- import { A as TraversalOptions, O as SinglePathOptions, S as MSTOptions, T as PathOptions, _ as GraphNode, n as AllPairsShortestPathsOptions, p as GraphEdge, t as AStarOptions, u as Graph, y as GraphPath } from "./types-3-FS9NV2.mjs";
2
-
3
- //#region src/algorithms/traversal.d.ts
4
- declare function bfs<N>(graph: Graph<N>, startId: string): Generator<GraphNode<N>>;
5
- declare function dfs<N>(graph: Graph<N>, startId: string): Generator<GraphNode<N>>;
6
- declare function isAcyclic(graph: Graph): boolean;
7
- declare function getConnectedComponents<N>(graph: Graph<N>): GraphNode<N>[][];
8
- declare function getTopologicalSort<N>(graph: Graph<N>): GraphNode<N>[] | null;
9
- declare function hasPath(graph: Graph, sourceId: string, targetId: string): boolean;
10
- declare function isConnected(graph: Graph): boolean;
11
- declare function isTree(graph: Graph): boolean;
12
- //#endregion
13
- //#region src/algorithms/paths.d.ts
14
- declare function genShortestPaths<N, E>(graph: Graph<N, E>, opts?: PathOptions<E>): Generator<GraphPath<N, E>>;
15
- declare function getShortestPaths<N, E>(graph: Graph<N, E>, opts?: PathOptions<E>): GraphPath<N, E>[];
16
- declare function getShortestPath<N, E>(graph: Graph<N, E>, opts: SinglePathOptions<E>): GraphPath<N, E> | undefined;
17
- declare function getSimplePaths<N, E>(graph: Graph<N, E>, opts?: PathOptions<E>): GraphPath<N, E>[];
18
- declare function genSimplePaths<N, E>(graph: Graph<N, E>, opts?: PathOptions<E>): Generator<GraphPath<N, E>>;
19
- declare function getSimplePath<N, E>(graph: Graph<N, E>, opts: SinglePathOptions<E>): GraphPath<N, E> | undefined;
20
- declare function getStronglyConnectedComponents<N>(graph: Graph<N>): GraphNode<N>[][];
21
- declare function getCycles<N, E>(graph: Graph<N, E>): GraphPath<N, E>[];
22
- declare function genCycles<N, E>(graph: Graph<N, E>): Generator<GraphPath<N, E>>;
23
- declare function getAllPairsShortestPaths<N, E>(graph: Graph<N, E>, opts?: AllPairsShortestPathsOptions<E>): GraphPath<N, E>[];
24
- declare function getAStarPath<N, E>(graph: Graph<N, E>, opts: AStarOptions<E>): GraphPath<N, E> | undefined;
25
- declare function joinPaths<N, E>(headPath: GraphPath<N, E>, tailPath: GraphPath<N, E>): GraphPath<N, E>;
26
- //#endregion
27
- //#region src/algorithms/ordering.d.ts
28
- declare function getPreorder<N>(graph: Graph<N>, opts?: TraversalOptions): GraphNode<N>[];
29
- declare function getPostorder<N>(graph: Graph<N>, opts?: TraversalOptions): GraphNode<N>[];
30
- declare function getPreorders<N>(graph: Graph<N>, opts?: TraversalOptions): GraphNode<N>[][];
31
- declare function getPostorders<N>(graph: Graph<N>, opts?: TraversalOptions): GraphNode<N>[][];
32
- declare function genPreorders<N>(graph: Graph<N>, opts?: TraversalOptions): Generator<GraphNode<N>[]>;
33
- declare function genPostorders<N>(graph: Graph<N>, opts?: TraversalOptions): Generator<GraphNode<N>[]>;
34
- //#endregion
35
- //#region src/algorithms/spanning-tree.d.ts
36
- declare function getMinimumSpanningTree<N, E>(graph: Graph<N, E>, opts?: MSTOptions<E>): Graph<N, E>;
37
- //#endregion
38
- //#region src/algorithms/centrality.d.ts
39
- interface IterativeCentralityOptions {
40
- alpha?: number;
41
- maxIterations?: number;
42
- tolerance?: number;
43
- }
44
- interface HITSResult {
45
- hubs: Record<string, number>;
46
- authorities: Record<string, number>;
47
- }
48
- /**
49
- * Returns degree centrality scores for all nodes.
50
- *
51
- * Degree centrality is the node degree normalized by `n - 1`.
52
- *
53
- * @example
54
- * ```ts
55
- * const scores = getDegreeCentrality(graph);
56
- * console.log(scores.a); // 0.5
57
- * ```
58
- */
59
- declare function getDegreeCentrality(graph: Graph): Record<string, number>;
60
- /**
61
- * Returns in-degree centrality scores for all nodes.
62
- *
63
- * In-degree centrality is the incoming degree normalized by `n - 1`.
64
- */
65
- declare function getInDegreeCentrality(graph: Graph): Record<string, number>;
66
- /**
67
- * Returns out-degree centrality scores for all nodes.
68
- *
69
- * Out-degree centrality is the outgoing degree normalized by `n - 1`.
70
- */
71
- declare function getOutDegreeCentrality(graph: Graph): Record<string, number>;
72
- /**
73
- * Returns closeness centrality scores for all nodes.
74
- *
75
- * Distances are computed over unweighted shortest paths using the graph's
76
- * existing directed or undirected edge semantics.
77
- */
78
- declare function getClosenessCentrality(graph: Graph): Record<string, number>;
79
- /**
80
- * Returns betweenness centrality scores for all nodes.
81
- *
82
- * Uses Brandes' algorithm over unweighted shortest paths and returns
83
- * normalized scores.
84
- */
85
- declare function getBetweennessCentrality(graph: Graph): Record<string, number>;
86
- /**
87
- * Returns PageRank scores for all nodes.
88
- *
89
- * Uses power iteration with damping factor `alpha`.
90
- */
91
- declare function getPageRank(graph: Graph, options?: IterativeCentralityOptions): Record<string, number>;
92
- /**
93
- * Returns HITS hub and authority scores for all nodes.
94
- *
95
- * Uses power iteration and L2 normalization per iteration.
96
- */
97
- declare function getHITS(graph: Graph, options?: IterativeCentralityOptions): HITSResult;
98
- /**
99
- * Returns eigenvector centrality scores for all nodes.
100
- *
101
- * Uses power iteration over incoming neighbors for directed graphs and
102
- * undirected adjacency for undirected graphs.
103
- */
104
- declare function getEigenvectorCentrality(graph: Graph, options?: IterativeCentralityOptions): Record<string, number>;
105
- //#endregion
106
- //#region src/algorithms/community.d.ts
107
- interface GirvanNewmanOptions {
108
- level?: number;
109
- maxLevels?: number;
110
- }
111
- interface LabelPropagationOptions {
112
- maxIterations?: number;
113
- }
114
- type Community<N = any> = GraphNode<N>[];
115
- /**
116
- * Returns label-propagation communities for the graph.
117
- *
118
- * The implementation is deterministic: ties are broken by lexicographic label
119
- * order so test results remain stable.
120
- */
121
- declare function getLabelPropagationCommunities<N>(graph: Graph<N>, options?: LabelPropagationOptions): Community<N>[];
122
- /**
123
- * Lazily yields Girvan-Newman community splits as edge betweenness removes
124
- * bridge-like edges from the graph.
125
- */
126
- declare function genGirvanNewmanCommunities<N>(graph: Graph<N>, options?: GirvanNewmanOptions): Generator<Community<N>[]>;
127
- /**
128
- * Returns the requested Girvan-Newman split level eagerly.
129
- *
130
- * `level: 1` returns the first split yielded by `genGirvanNewmanCommunities`.
131
- */
132
- declare function getGirvanNewmanCommunities<N>(graph: Graph<N>, options?: GirvanNewmanOptions): Community<N>[];
133
- /**
134
- * Returns the modularity score for a partition of communities.
135
- *
136
- * Community algorithms in this module treat the graph as undirected.
137
- */
138
- declare function getModularity<N>(graph: Graph<N>, communities: Community<N>[]): number;
139
- /**
140
- * Returns communities found by greedily merging partitions that improve
141
- * modularity the most at each step.
142
- */
143
- declare function getGreedyModularityCommunities<N>(graph: Graph<N>): Community<N>[];
144
- //#endregion
145
- //#region src/algorithms/connectivity.d.ts
146
- /**
147
- * Returns bridge edges whose removal disconnects the graph.
148
- *
149
- * Connectivity algorithms in this module treat the graph as undirected.
150
- */
151
- declare function getBridges<N, E>(graph: Graph<N, E>): GraphEdge<E>[];
152
- /**
153
- * Returns articulation points (cut vertices) for the graph.
154
- *
155
- * Connectivity algorithms in this module treat the graph as undirected.
156
- */
157
- declare function getArticulationPoints<N, E>(graph: Graph<N, E>): GraphNode<N>[];
158
- /**
159
- * Returns biconnected components as arrays of nodes.
160
- *
161
- * Articulation points may appear in multiple returned components.
162
- */
163
- declare function getBiconnectedComponents<N, E>(graph: Graph<N, E>): GraphNode<N>[][];
164
- //#endregion
165
- //#region src/algorithms/isomorphism.d.ts
166
- interface IsomorphismOptions<N = any, E = any> {
167
- nodeMatch?: (a: GraphNode<N>, b: GraphNode<N>) => boolean;
168
- edgeMatch?: (a: GraphEdge<E>, b: GraphEdge<E>) => boolean;
169
- }
170
- /**
171
- * Returns whether two graphs are structurally isomorphic.
172
- *
173
- * Optional `nodeMatch` and `edgeMatch` predicates can refine the match using
174
- * node and edge payloads.
175
- */
176
- declare function isIsomorphic<N, E>(graphA: Graph<N, E>, graphB: Graph<N, E>, options?: IsomorphismOptions<N, E>): boolean;
177
- //#endregion
178
- export { genCycles as A, getStronglyConnectedComponents as B, getMinimumSpanningTree as C, getPostorders as D, getPostorder as E, getCycles as F, getTopologicalSort as G, bfs as H, getShortestPath as I, isConnected as J, hasPath as K, getShortestPaths as L, genSimplePaths as M, getAStarPath as N, getPreorder as O, getAllPairsShortestPaths as P, getSimplePath as R, getPageRank as S, genPreorders as T, dfs as U, joinPaths as V, getConnectedComponents as W, isTree as Y, getDegreeCentrality as _, getBridges as a, getInDegreeCentrality as b, genGirvanNewmanCommunities as c, getLabelPropagationCommunities as d, getModularity as f, getClosenessCentrality as g, getBetweennessCentrality as h, getBiconnectedComponents as i, genShortestPaths as j, getPreorders as k, getGirvanNewmanCommunities as l, IterativeCentralityOptions as m, isIsomorphic as n, GirvanNewmanOptions as o, HITSResult as p, isAcyclic as q, getArticulationPoints as r, LabelPropagationOptions as s, IsomorphismOptions as t, getGreedyModularityCommunities as u, getEigenvectorCentrality as v, genPostorders as w, getOutDegreeCentrality as x, getHITS as y, getSimplePaths as z };
@@ -1,137 +0,0 @@
1
- //#region src/indexing.ts
2
- const indexes = /* @__PURE__ */ new WeakMap();
3
- /**
4
- * Get or lazily build the index for a graph.
5
- * Auto-rebuilds when node/edge count changes.
6
- *
7
- * @example
8
- * ```ts
9
- * import { createGraph, getIndex } from '@statelyai/graph';
10
- *
11
- * const graph = createGraph({
12
- * nodes: [{ id: 'a' }, { id: 'b' }],
13
- * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
14
- * });
15
- *
16
- * const idx = getIndex(graph);
17
- * idx.nodeById.get('a'); // 0
18
- * idx.outEdges.get('a'); // ['e1']
19
- * ```
20
- */
21
- function getIndex(graph) {
22
- let idx = indexes.get(graph);
23
- const sameStructure = idx && idx.nodeCount === graph.nodes.length && idx.edgeCount === graph.edges.length;
24
- const signature = idx !== void 0 && sameStructure && idx.nodesRef === graph.nodes && idx.edgesRef === graph.edges ? getIndexSignature(graph) : void 0;
25
- if (!idx || idx.nodeCount !== graph.nodes.length || idx.edgeCount !== graph.edges.length || signature !== void 0 && idx.signature !== signature) {
26
- idx = buildIndex(graph, signature ?? getIndexSignature(graph));
27
- indexes.set(graph, idx);
28
- }
29
- return idx;
30
- }
31
- /**
32
- * Clear the cached index. Call this if you mutate graph.nodes/edges directly.
33
- *
34
- * @example
35
- * ```ts
36
- * import { createGraph, invalidateIndex, getIndex } from '@statelyai/graph';
37
- *
38
- * const graph = createGraph({ nodes: [{ id: 'a' }], edges: [] });
39
- * // manually mutate nodes array
40
- * graph.nodes.push({ type: 'node', id: 'b', parentId: null, initialNodeId: null, label: '', data: undefined });
41
- * invalidateIndex(graph); // forces rebuild on next getIndex()
42
- * ```
43
- */
44
- function invalidateIndex(graph) {
45
- indexes.delete(graph);
46
- }
47
- function getIndexSignature(graph) {
48
- const nodeParts = graph.nodes.map((node) => `${node.id}\u0000${node.parentId ?? ""}`);
49
- const edgeParts = graph.edges.map((edge) => `${edge.id}\u0000${edge.sourceId}\u0000${edge.targetId}`);
50
- return `${nodeParts.join("")}\u0002${edgeParts.join("")}`;
51
- }
52
- function buildIndex(graph, signature) {
53
- const nodeById = /* @__PURE__ */ new Map();
54
- const edgeById = /* @__PURE__ */ new Map();
55
- const outEdges = /* @__PURE__ */ new Map();
56
- const inEdges = /* @__PURE__ */ new Map();
57
- const childNodes = /* @__PURE__ */ new Map();
58
- for (let i = 0; i < graph.nodes.length; i++) {
59
- const n = graph.nodes[i];
60
- nodeById.set(n.id, i);
61
- outEdges.set(n.id, []);
62
- inEdges.set(n.id, []);
63
- const parent = n.parentId ?? null;
64
- if (!childNodes.has(parent)) childNodes.set(parent, []);
65
- childNodes.get(parent).push(n.id);
66
- }
67
- for (let i = 0; i < graph.edges.length; i++) {
68
- const e = graph.edges[i];
69
- edgeById.set(e.id, i);
70
- outEdges.get(e.sourceId)?.push(e.id);
71
- inEdges.get(e.targetId)?.push(e.id);
72
- }
73
- return {
74
- nodeById,
75
- edgeById,
76
- outEdges,
77
- inEdges,
78
- childNodes,
79
- nodeCount: graph.nodes.length,
80
- edgeCount: graph.edges.length,
81
- signature,
82
- nodesRef: graph.nodes,
83
- edgesRef: graph.edges
84
- };
85
- }
86
- function indexAddNode(idx, node, arrayIndex) {
87
- idx.nodeById.set(node.id, arrayIndex);
88
- idx.outEdges.set(node.id, []);
89
- idx.inEdges.set(node.id, []);
90
- const parent = node.parentId ?? null;
91
- if (!idx.childNodes.has(parent)) idx.childNodes.set(parent, []);
92
- idx.childNodes.get(parent).push(node.id);
93
- idx.nodeCount++;
94
- idx.signature = "";
95
- }
96
- function indexAddEdge(idx, edge, arrayIndex) {
97
- idx.edgeById.set(edge.id, arrayIndex);
98
- idx.outEdges.get(edge.sourceId)?.push(edge.id);
99
- idx.inEdges.get(edge.targetId)?.push(edge.id);
100
- idx.edgeCount++;
101
- idx.signature = "";
102
- }
103
- /** Update childNodes index when a node's parentId changes. */
104
- function indexReparentNode(idx, nodeId, oldParentId, newParentId) {
105
- const oldSiblings = idx.childNodes.get(oldParentId ?? null);
106
- if (oldSiblings) {
107
- const pos = oldSiblings.indexOf(nodeId);
108
- if (pos !== -1) oldSiblings.splice(pos, 1);
109
- }
110
- const np = newParentId ?? null;
111
- if (!idx.childNodes.has(np)) idx.childNodes.set(np, []);
112
- idx.childNodes.get(np).push(nodeId);
113
- idx.signature = "";
114
- }
115
- /** Update adjacency lists when an edge's sourceId/targetId changes. */
116
- function indexUpdateEdgeEndpoints(idx, edgeId, oldSourceId, oldTargetId, newSourceId, newTargetId) {
117
- if (oldSourceId !== newSourceId) {
118
- const oldOut = idx.outEdges.get(oldSourceId);
119
- if (oldOut) {
120
- const pos = oldOut.indexOf(edgeId);
121
- if (pos !== -1) oldOut.splice(pos, 1);
122
- }
123
- idx.outEdges.get(newSourceId)?.push(edgeId);
124
- }
125
- if (oldTargetId !== newTargetId) {
126
- const oldIn = idx.inEdges.get(oldTargetId);
127
- if (oldIn) {
128
- const pos = oldIn.indexOf(edgeId);
129
- if (pos !== -1) oldIn.splice(pos, 1);
130
- }
131
- idx.inEdges.get(newTargetId)?.push(edgeId);
132
- }
133
- idx.signature = "";
134
- }
135
-
136
- //#endregion
137
- export { indexUpdateEdgeEndpoints as a, indexReparentNode as i, indexAddEdge as n, invalidateIndex as o, indexAddNode as r, getIndex as t };
File without changes