@statelyai/graph 0.8.0 → 0.9.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.
package/README.md CHANGED
@@ -10,6 +10,28 @@ Made from our experience at [stately.ai](https://stately.ai), where we build vis
10
10
  npm install @statelyai/graph
11
11
  ```
12
12
 
13
+ Optional peers are only needed for specific adapters:
14
+
15
+ | Package | Needed for |
16
+ | --- | --- |
17
+ | `fast-xml-parser` | `@statelyai/graph/gexf`, `@statelyai/graph/graphml` |
18
+ | `dotparser` | `@statelyai/graph/dot` parsing |
19
+ | `cytoscape` | Cytoscape integration tests and consumer typing |
20
+ | `d3-force` | D3 force integration tests and consumer typing |
21
+ | `elkjs` | `@statelyai/graph/elk` |
22
+ | `zod` | `@statelyai/graph/schemas` |
23
+
24
+ ## Highlights
25
+
26
+ - Plain JSON graphs with no runtime wrappers required
27
+ - Standalone functions with a consistent `get*`/`gen*`/`is*`/`add*` naming model
28
+ - Directed, undirected, hierarchical, and visual graph support
29
+ - Ports for node-editor and dataflow-style graphs
30
+ - Algorithms for traversal, paths, centrality, communities, connectivity, isomorphism, ordering, MST, and walks
31
+ - Diff/patch utilities for graph state changes
32
+ - Multi-format conversion via package subpaths
33
+ - Small, fast test suite with broad format coverage
34
+
13
35
  ## Quick Start
14
36
 
15
37
  Graphs are plain JSON-serializable objects. All operations are standalone functions — no classes, no DOM, no rendering engine.
@@ -78,15 +100,50 @@ const children = getChildren(graph, 'b'); // [b1, b2]
78
100
  const flat = flatten(graph); // only leaf nodes, edges resolved
79
101
  ```
80
102
 
103
+ ## Ports
104
+
105
+ Ports are optional named connection points on nodes. They are useful for flow-based systems, node editors, and dataflow graphs where edges need to target a specific input or output.
106
+
107
+ ```ts
108
+ import { createGraph, getEdgesByPort, getPorts } from '@statelyai/graph';
109
+
110
+ const graph = createGraph({
111
+ nodes: [
112
+ {
113
+ id: 'fetch',
114
+ ports: [{ name: 'result', direction: 'out' }],
115
+ },
116
+ {
117
+ id: 'render',
118
+ ports: [{ name: 'input', direction: 'in' }],
119
+ },
120
+ ],
121
+ edges: [
122
+ {
123
+ id: 'e1',
124
+ sourceId: 'fetch',
125
+ sourcePort: 'result',
126
+ targetId: 'render',
127
+ targetPort: 'input',
128
+ },
129
+ ],
130
+ });
131
+
132
+ getPorts(graph, 'fetch'); // [{ name: 'result', ... }]
133
+ getEdgesByPort(graph, 'render', 'input'); // [e1]
134
+ ```
135
+
81
136
  ## Algorithms
82
137
 
83
- Includes traversal (BFS, DFS), pathfinding (shortest path, simple paths, all-pairs shortest paths), cycle detection, connected/strongly-connected components, topological sort, minimum spanning tree, and more. Many algorithms have lazy generator variants (`gen*`) for early exit.
138
+ Includes traversal (BFS, DFS), pathfinding (shortest path, simple paths, all-pairs shortest paths), centrality/link analysis (degree, closeness, betweenness, PageRank, HITS, eigenvector), community detection (label propagation, Girvan-Newman, greedy modularity, modularity scoring), cycle detection, connected/strongly-connected components, bridges, articulation points, biconnected components, isomorphism, topological sort, minimum spanning tree, and more. Many algorithms have lazy generator variants (`gen*`) for early exit.
84
139
 
85
140
  ```ts
86
141
  import {
87
142
  bfs, dfs, hasPath, isAcyclic,
88
143
  getShortestPath, getCycles, getTopologicalSort,
89
144
  getConnectedComponents, getMinimumSpanningTree,
145
+ getPageRank, getLabelPropagationCommunities,
146
+ genGirvanNewmanCommunities, getBridges, isIsomorphic,
90
147
  } from '@statelyai/graph';
91
148
 
92
149
  for (const node of bfs(graph, 'a')) { /* breadth-first */ }
@@ -98,8 +155,21 @@ getShortestPath(graph, { from: 'a', to: 'c' }); // single shortest path
98
155
  getTopologicalSort(graph); // topological order (or null)
99
156
  getConnectedComponents(graph); // connected components
100
157
  getMinimumSpanningTree(graph, { weight: e => e.data?.weight ?? 1 }); // MST
158
+ getPageRank(graph); // link analysis scores
159
+ getLabelPropagationCommunities(graph); // community detection
160
+ [...genGirvanNewmanCommunities(graph)]; // lazy community splits
161
+ getBridges(graph); // bridge edges
162
+ isIsomorphic(graph, otherGraph); // structural equivalence
101
163
  ```
102
164
 
165
+ ## Diff & Walks
166
+
167
+ Beyond classic graph algorithms, the library also includes utilities for evolving and exploring graph state:
168
+
169
+ - `getDiff()`, `getPatches()`, `applyPatches()` for graph change tracking
170
+ - `genRandomWalk()`, `genWeightedRandomWalk()`, and coverage helpers for model-based testing and simulation
171
+ - `getSubgraph()` and `reverseGraph()` for structural transforms
172
+
103
173
  ## Visual Graphs
104
174
 
105
175
  `createVisualGraph()` guarantees `x`, `y`, `width`, `height` on all nodes and edges (default `0`).
@@ -146,6 +216,35 @@ const back = cytoscapeConverter.from(cyto);
146
216
 
147
217
  Some formats have optional peer dependencies: `fast-xml-parser` (GEXF, GraphML) and `dotparser` (DOT). All other formats are dependency-free.
148
218
 
219
+ Format-specific docs live alongside the source:
220
+
221
+ - [DOT](./src/formats/dot/README.md)
222
+ - [GraphML](./src/formats/graphml/README.md)
223
+ - [GEXF](./src/formats/gexf/README.md)
224
+ - [GML](./src/formats/gml/README.md)
225
+ - [JGF](./src/formats/jgf/README.md)
226
+ - [TGF](./src/formats/tgf/README.md)
227
+ - [Cytoscape](./src/formats/cytoscape/README.md)
228
+ - [D3](./src/formats/d3/README.md)
229
+ - [Mermaid](./src/formats/mermaid/README.md)
230
+ - [Converter helpers](./src/formats/converter/README.md)
231
+
232
+ ## Examples
233
+
234
+ The repo includes runnable examples under [`examples/`](./examples):
235
+
236
+ - [Flow-based math](./examples/flow-based-math.ts) shows ports, topological ordering, and value propagation.
237
+ - [Async workflow](./examples/async-workflow.ts) models an n8n/Zapier-style workflow with ports and dependency-aware execution.
238
+
239
+ ## Development
240
+
241
+ ```bash
242
+ pnpm install
243
+ pnpm verify
244
+ ```
245
+
246
+ See [CONTRIBUTING.md](./CONTRIBUTING.md) for contributor conventions, format-module checklist, and release notes guidance.
247
+
149
248
  ## Why this library?
150
249
 
151
250
  Graph file formats define how to _store_ graphs. Visualization libraries define how to _render_ them. This library is the computational layer in between: plain JSON objects in, algorithms and mutations, plain JSON objects out.