twokeys 2.0.2 → 3.0.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/dist/index.d.ts CHANGED
@@ -1,3 +1,603 @@
1
+ /**
2
+ * Vector Distance & Similarity Functions
3
+ *
4
+ * Standalone functions for vector math — zero dependencies.
5
+ * These are the workhorses that Points, graph algorithms, and consumers all use.
6
+ */
7
+ /**
8
+ * Cosine similarity between two dense vectors.
9
+ * Returns a value in [-1, 1] where 1 means identical direction.
10
+ */
11
+ declare function cosineSimilarity(a: number[], b: number[]): number;
12
+ /**
13
+ * Squared Euclidean distance between two dense vectors.
14
+ * Avoids the sqrt for performance-sensitive comparisons.
15
+ */
16
+ declare function squaredEuclideanDistance(a: number[], b: number[]): number;
17
+ /**
18
+ * Euclidean (L2) distance between two dense vectors.
19
+ */
20
+ declare function euclideanDistance(a: number[], b: number[]): number;
21
+ /**
22
+ * Manhattan (L1) distance between two dense vectors.
23
+ */
24
+ declare function manhattanDistance(a: number[], b: number[]): number;
25
+ /**
26
+ * Mahalanobis distance of a point from a distribution described by
27
+ * per-dimension means and variances.
28
+ *
29
+ * This is the diagonal-covariance special case (dimensions are independent),
30
+ * which is what most embedding-space consumers need.
31
+ *
32
+ * d = sqrt( Σ (x_i - μ_i)² / max(σ²_i, ε) )
33
+ */
34
+ declare function mahalanobisDistance(point: number[], means: number[], variances: number[], epsilon?: number): number;
35
+ /**
36
+ * L2-normalize a dense vector (unit vector in same direction).
37
+ * Returns the zero vector if the input has zero magnitude.
38
+ */
39
+ declare function normalizeL2(vector: number[]): number[];
40
+ /**
41
+ * Cosine similarity between two sparse vectors represented as Map<string, number>.
42
+ */
43
+ declare function cosineSimilaritySparse(a: Map<string, number>, b: Map<string, number>): number;
44
+ /**
45
+ * Jaccard similarity between two sets: |A ∩ B| / |A ∪ B|.
46
+ * Returns 0 for empty sets.
47
+ */
48
+ declare function jaccardSimilarity<T>(a: Set<T>, b: Set<T>): number;
49
+ /**
50
+ * Overlap coefficient: |A ∩ B| / min(|A|, |B|).
51
+ * Returns 0 for empty sets.
52
+ */
53
+ declare function overlapCoefficient<T>(a: Set<T>, b: Set<T>): number;
54
+
55
+ type GraphNodeInput<NodeId extends string = string> = NodeId | {
56
+ id: NodeId;
57
+ };
58
+ interface GraphEdge<NodeId extends string = string> {
59
+ from: NodeId;
60
+ to: NodeId;
61
+ weight?: number;
62
+ }
63
+ interface GraphBuildOptions {
64
+ directed?: boolean;
65
+ }
66
+ interface GraphNeighbor<NodeId extends string = string> {
67
+ id: NodeId;
68
+ weight: number;
69
+ }
70
+ interface GraphAdjacency<NodeId extends string = string> {
71
+ nodes: NodeId[];
72
+ neighborsByNode: Map<NodeId, GraphNeighbor<NodeId>[]>;
73
+ incomingByNode: Map<NodeId, GraphNeighbor<NodeId>[]>;
74
+ edges: Array<Required<GraphEdge<NodeId>>>;
75
+ directed: boolean;
76
+ }
77
+ type GraphAlgorithmErrorCode = 'NEGATIVE_WEIGHT' | 'NEGATIVE_CYCLE' | 'INVALID_ARGUMENT';
78
+ declare class GraphAlgorithmError extends Error {
79
+ readonly code: GraphAlgorithmErrorCode;
80
+ constructor(code: GraphAlgorithmErrorCode, message: string);
81
+ }
82
+ interface TopologicalSortOptions<NodeId extends string = string> {
83
+ tieBreaker?: (left: NodeId, right: NodeId) => number;
84
+ priority?: (nodeId: NodeId) => number;
85
+ priorityByNode?: Map<NodeId, number> | Partial<Record<NodeId, number>>;
86
+ }
87
+ interface TopologicalSortResult<NodeId extends string = string> {
88
+ order: NodeId[];
89
+ cycleNodes: NodeId[];
90
+ isDag: boolean;
91
+ }
92
+ interface StronglyConnectedComponentsOptions extends GraphBuildOptions {
93
+ }
94
+ interface StronglyConnectedComponentsResult<NodeId extends string = string> {
95
+ components: NodeId[][];
96
+ componentByNode: Map<NodeId, number>;
97
+ }
98
+ interface WeaklyConnectedComponentsResult<NodeId extends string = string> {
99
+ components: NodeId[][];
100
+ componentByNode: Map<NodeId, number>;
101
+ }
102
+ interface DegreeCentralityRecord {
103
+ inDegree: number;
104
+ outDegree: number;
105
+ degree: number;
106
+ normalized: number;
107
+ }
108
+ type ClosenessCentralityMode = 'classic' | 'harmonic';
109
+ interface ClosenessCentralityRecord {
110
+ reachableCount: number;
111
+ distanceSum: number;
112
+ score: number;
113
+ normalized: number;
114
+ mode: ClosenessCentralityMode;
115
+ negativeCycle: boolean;
116
+ }
117
+ interface BetweennessCentralityRecord {
118
+ raw: number;
119
+ normalized: number;
120
+ }
121
+ interface PageRankOptions extends GraphBuildOptions {
122
+ dampingFactor?: number;
123
+ tolerance?: number;
124
+ maxIterations?: number;
125
+ }
126
+ interface PageRankRecord {
127
+ score: number;
128
+ normalized: number;
129
+ rank: number;
130
+ }
131
+ interface PageRankResult<NodeId extends string = string> {
132
+ byNode: Map<NodeId, PageRankRecord>;
133
+ order: NodeId[];
134
+ iterations: number;
135
+ converged: boolean;
136
+ dampingFactor: number;
137
+ }
138
+ interface MinimumSpanningTreeOptions<NodeId extends string = string> {
139
+ tieBreaker?: (left: NodeId, right: NodeId) => number;
140
+ }
141
+ interface MinimumSpanningTreeResult<NodeId extends string = string> {
142
+ edges: Array<Required<GraphEdge<NodeId>>>;
143
+ totalWeight: number;
144
+ componentCount: number;
145
+ spanning: boolean;
146
+ }
147
+ interface ArticulationBridgeResult<NodeId extends string = string> {
148
+ articulationPoints: NodeId[];
149
+ bridges: Array<Required<GraphEdge<NodeId>>>;
150
+ }
151
+ interface GraphAnalysisResult<NodeId extends string = string> {
152
+ degree: Map<NodeId, DegreeCentralityRecord>;
153
+ closeness: Map<NodeId, ClosenessCentralityRecord>;
154
+ betweenness: Map<NodeId, BetweennessCentralityRecord>;
155
+ pageRank: Map<NodeId, PageRankRecord>;
156
+ stronglyConnectedComponents: NodeId[][];
157
+ weaklyConnectedComponents: NodeId[][];
158
+ articulationPoints: NodeId[];
159
+ bridges: Array<Required<GraphEdge<NodeId>>>;
160
+ }
161
+ interface GraphAnalysisOptions extends GraphBuildOptions {
162
+ closenessMode?: ClosenessCentralityMode;
163
+ shortestPathAlgorithm?: ShortestPathAlgorithm;
164
+ pageRankOptions?: Omit<PageRankOptions, 'directed'>;
165
+ }
166
+ type ShortestPathAlgorithm = 'auto' | 'dijkstra' | 'bellman-ford';
167
+ interface ShortestPathOptions extends GraphBuildOptions {
168
+ algorithm?: ShortestPathAlgorithm;
169
+ failOnNegativeCycle?: boolean;
170
+ }
171
+ interface ShortestPathResult<NodeId extends string = string> {
172
+ source: NodeId;
173
+ target: NodeId;
174
+ path: NodeId[];
175
+ distance: number;
176
+ reachable: boolean;
177
+ explored: number;
178
+ algorithm: ShortestPathAlgorithm;
179
+ hasNegativeWeights: boolean;
180
+ negativeCycle: boolean;
181
+ }
182
+ interface LabelPropagationOptions<NodeId extends string = string> extends GraphBuildOptions {
183
+ maxIterations?: number;
184
+ tieBreaker?: (left: NodeId, right: NodeId) => number;
185
+ }
186
+ interface LouvainOptions<NodeId extends string = string> extends GraphBuildOptions {
187
+ maxPasses?: number;
188
+ tolerance?: number;
189
+ tieBreaker?: (left: NodeId, right: NodeId) => number;
190
+ }
191
+ interface CommunityDetectionResult<NodeId extends string = string> {
192
+ communities: NodeId[][];
193
+ communityByNode: Map<NodeId, number>;
194
+ iterations: number;
195
+ converged: boolean;
196
+ algorithm: 'label-propagation' | 'louvain';
197
+ modularity: number;
198
+ }
199
+ type SimilarityMetric = 'common-neighbors' | 'jaccard' | 'cosine' | 'overlap' | 'adamic-adar' | 'resource-allocation' | 'preferential-attachment';
200
+ interface SimilarityOptions extends GraphBuildOptions {
201
+ metric?: SimilarityMetric;
202
+ minScore?: number;
203
+ }
204
+ interface NodeSimilarityRecord<NodeId extends string = string> {
205
+ left: NodeId;
206
+ right: NodeId;
207
+ score: number;
208
+ metric: SimilarityMetric;
209
+ rank: number;
210
+ }
211
+ interface NodeSimilarityResult<NodeId extends string = string> {
212
+ metric: SimilarityMetric;
213
+ pairs: NodeSimilarityRecord<NodeId>[];
214
+ }
215
+ interface KNearestNeighborsOptions extends GraphBuildOptions {
216
+ metric?: SimilarityMetric;
217
+ k?: number;
218
+ minScore?: number;
219
+ }
220
+ interface KNearestNeighbor<NodeId extends string = string> {
221
+ nodeId: NodeId;
222
+ score: number;
223
+ }
224
+ interface KNearestNeighborsResult<NodeId extends string = string> {
225
+ metric: SimilarityMetric;
226
+ k: number;
227
+ neighborsByNode: Map<NodeId, KNearestNeighbor<NodeId>[]>;
228
+ }
229
+ interface LinkPredictionOptions<NodeId extends string = string> extends GraphBuildOptions {
230
+ metric?: SimilarityMetric;
231
+ limit?: number;
232
+ minScore?: number;
233
+ allowExistingEdges?: boolean;
234
+ sourceFilter?: NodeId[];
235
+ targetFilter?: NodeId[];
236
+ }
237
+ interface LinkPredictionRecord<NodeId extends string = string> {
238
+ from: NodeId;
239
+ to: NodeId;
240
+ score: number;
241
+ metric: SimilarityMetric;
242
+ rank: number;
243
+ }
244
+ interface LinkPredictionResult<NodeId extends string = string> {
245
+ metric: SimilarityMetric;
246
+ predictions: LinkPredictionRecord<NodeId>[];
247
+ }
248
+ interface AStarOptions<NodeId extends string = string> extends GraphBuildOptions {
249
+ heuristic?: (nodeId: NodeId, targetId: NodeId) => number;
250
+ }
251
+ interface AStarResult<NodeId extends string = string> {
252
+ source: NodeId;
253
+ target: NodeId;
254
+ path: NodeId[];
255
+ distance: number;
256
+ reachable: boolean;
257
+ explored: number;
258
+ estimatedDistance: number;
259
+ }
260
+ interface PathRecord<NodeId extends string = string> {
261
+ path: NodeId[];
262
+ distance: number;
263
+ }
264
+ interface YenKShortestPathsOptions extends GraphBuildOptions {
265
+ k?: number;
266
+ shortestPathAlgorithm?: ShortestPathAlgorithm;
267
+ }
268
+ interface YenKShortestPathsResult<NodeId extends string = string> {
269
+ source: NodeId;
270
+ target: NodeId;
271
+ paths: PathRecord<NodeId>[];
272
+ complete: boolean;
273
+ }
274
+ interface AllPairsShortestPathsOptions extends GraphBuildOptions {
275
+ algorithm?: ShortestPathAlgorithm;
276
+ failOnNegativeCycle?: boolean;
277
+ }
278
+ interface AllPairsShortestPathsResult<NodeId extends string = string> {
279
+ nodes: NodeId[];
280
+ distanceBySource: Map<NodeId, Map<NodeId, number>>;
281
+ previousBySource: Map<NodeId, Map<NodeId, NodeId>>;
282
+ algorithm: ShortestPathAlgorithm;
283
+ hasNegativeWeights: boolean;
284
+ negativeCycle: boolean;
285
+ }
286
+ interface FlowEdge<NodeId extends string = string> {
287
+ from: NodeId;
288
+ to: NodeId;
289
+ capacity: number;
290
+ cost?: number;
291
+ }
292
+ interface FlowEdgeResult<NodeId extends string = string> {
293
+ from: NodeId;
294
+ to: NodeId;
295
+ flow: number;
296
+ capacity: number;
297
+ }
298
+ interface MaximumFlowOptions extends GraphBuildOptions {
299
+ }
300
+ interface MaximumFlowResult<NodeId extends string = string> {
301
+ source: NodeId;
302
+ sink: NodeId;
303
+ maxFlow: number;
304
+ augmentations: number;
305
+ flowByEdge: FlowEdgeResult<NodeId>[];
306
+ sourcePartition: NodeId[];
307
+ sinkPartition: NodeId[];
308
+ cutEdges: Array<Required<GraphEdge<NodeId>>>;
309
+ }
310
+ interface MinCostMaxFlowOptions extends GraphBuildOptions {
311
+ targetFlow?: number;
312
+ }
313
+ interface MinCostFlowEdgeResult<NodeId extends string = string> extends FlowEdgeResult<NodeId> {
314
+ cost: number;
315
+ }
316
+ interface MinCostMaxFlowResult<NodeId extends string = string> {
317
+ source: NodeId;
318
+ sink: NodeId;
319
+ flow: number;
320
+ cost: number;
321
+ complete: boolean;
322
+ augmentations: number;
323
+ flowByEdge: MinCostFlowEdgeResult<NodeId>[];
324
+ }
325
+ type KMeansNormalization = 'none' | 'zscore' | 'minmax';
326
+ interface KMeansOptions {
327
+ maxIterations?: number;
328
+ tolerance?: number;
329
+ seed?: number;
330
+ nInit?: number;
331
+ normalization?: KMeansNormalization;
332
+ useKMeansPlusPlus?: boolean;
333
+ }
334
+ interface KMeansCluster {
335
+ centroid: number[];
336
+ indices: number[];
337
+ }
338
+ interface KMeansResult {
339
+ assignments: number[];
340
+ clusters: KMeansCluster[];
341
+ iterations: number;
342
+ inertia: number;
343
+ converged: boolean;
344
+ silhouette: number | null;
345
+ selectedSeed: number;
346
+ }
347
+ interface KMeansAutoOptions extends KMeansOptions {
348
+ kMin?: number;
349
+ kMax?: number;
350
+ }
351
+ interface KMeansAutoCandidate {
352
+ k: number;
353
+ silhouette: number | null;
354
+ inertia: number;
355
+ }
356
+ interface KMeansAutoResult extends KMeansResult {
357
+ selectedK: number;
358
+ candidates: KMeansAutoCandidate[];
359
+ }
360
+ interface TravelingSalesmanOptions<NodeId extends string = string> extends GraphBuildOptions {
361
+ start?: NodeId;
362
+ returnToStart?: boolean;
363
+ twoOptPasses?: number;
364
+ multiStartCount?: number;
365
+ startCandidates?: NodeId[];
366
+ seed?: number;
367
+ shortestPathAlgorithm?: ShortestPathAlgorithm;
368
+ }
369
+ interface TravelingSalesmanSegment<NodeId extends string = string> {
370
+ from: NodeId;
371
+ to: NodeId;
372
+ distance: number;
373
+ path: NodeId[];
374
+ }
375
+ interface TravelingSalesmanResult<NodeId extends string = string> {
376
+ order: NodeId[];
377
+ distance: number;
378
+ segments: TravelingSalesmanSegment<NodeId>[];
379
+ visitedCount: number;
380
+ complete: boolean;
381
+ unreachableNodes: NodeId[];
382
+ lowerBound: number;
383
+ optimalityGap: number | null;
384
+ }
385
+ declare function buildGraphAdjacency<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: GraphBuildOptions): GraphAdjacency<NodeId>;
386
+ declare function stronglyConnectedComponents<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: StronglyConnectedComponentsOptions): StronglyConnectedComponentsResult<NodeId>;
387
+ declare function weaklyConnectedComponents<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[]): WeaklyConnectedComponentsResult<NodeId>;
388
+ declare function topologicalSort<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: TopologicalSortOptions<NodeId>): TopologicalSortResult<NodeId>;
389
+ declare function degreeCentrality<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: GraphBuildOptions): Map<NodeId, DegreeCentralityRecord>;
390
+ interface ClosenessCentralityOptions extends GraphBuildOptions {
391
+ mode?: ClosenessCentralityMode;
392
+ shortestPathAlgorithm?: ShortestPathAlgorithm;
393
+ }
394
+ declare function closenessCentrality<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: ClosenessCentralityOptions): Map<NodeId, ClosenessCentralityRecord>;
395
+ declare function betweennessCentrality<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: GraphBuildOptions): Map<NodeId, BetweennessCentralityRecord>;
396
+ declare function pageRank<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: PageRankOptions): PageRankResult<NodeId>;
397
+ declare function minimumSpanningTree<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: MinimumSpanningTreeOptions<NodeId>): MinimumSpanningTreeResult<NodeId>;
398
+ declare function articulationPointsAndBridges<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[]): ArticulationBridgeResult<NodeId>;
399
+ declare function analyzeGraph<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: GraphAnalysisOptions): GraphAnalysisResult<NodeId>;
400
+ declare function shortestPath<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], source: NodeId, target: NodeId, options?: ShortestPathOptions): ShortestPathResult<NodeId>;
401
+ declare function labelPropagationCommunities<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: LabelPropagationOptions<NodeId>): CommunityDetectionResult<NodeId>;
402
+ declare function louvainCommunities<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: LouvainOptions<NodeId>): CommunityDetectionResult<NodeId>;
403
+ declare function nodeSimilarity<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: SimilarityOptions): NodeSimilarityResult<NodeId>;
404
+ declare function kNearestNeighbors<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: KNearestNeighborsOptions): KNearestNeighborsResult<NodeId>;
405
+ declare function predictLinks<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: LinkPredictionOptions<NodeId>): LinkPredictionResult<NodeId>;
406
+ declare function linkPrediction<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: LinkPredictionOptions<NodeId>): LinkPredictionResult<NodeId>;
407
+ declare function aStarShortestPath<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], source: NodeId, target: NodeId, options?: AStarOptions<NodeId>): AStarResult<NodeId>;
408
+ declare function allPairsShortestPaths<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: AllPairsShortestPathsOptions): AllPairsShortestPathsResult<NodeId>;
409
+ declare function yenKShortestPaths<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], source: NodeId, target: NodeId, options?: YenKShortestPathsOptions): YenKShortestPathsResult<NodeId>;
410
+ declare function maximumFlow<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], source: NodeId, sink: NodeId, options?: MaximumFlowOptions): MaximumFlowResult<NodeId>;
411
+ declare function minCostMaxFlow<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: FlowEdge<NodeId>[], source: NodeId, sink: NodeId, options?: MinCostMaxFlowOptions): MinCostMaxFlowResult<NodeId>;
412
+ declare function kMeansClustering(points: number[][], k: number, options?: KMeansOptions): KMeansResult;
413
+ declare function kMeansAuto(points: number[][], options?: KMeansAutoOptions): KMeansAutoResult;
414
+ declare function travelingSalesmanApprox<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: TravelingSalesmanOptions<NodeId>): TravelingSalesmanResult<NodeId>;
415
+ type LinkageMethod = 'single' | 'complete' | 'average' | 'ward';
416
+ interface DendrogramNode {
417
+ left: number;
418
+ right: number;
419
+ distance: number;
420
+ size: number;
421
+ }
422
+ interface HierarchicalClusterResult {
423
+ clusters: number[][];
424
+ assignments: number[];
425
+ dendrogram: DendrogramNode[];
426
+ silhouette: number | null;
427
+ }
428
+ declare function hierarchicalClustering(points: number[][], k: number, options?: {
429
+ linkage?: LinkageMethod;
430
+ distanceMetric?: 'euclidean' | 'cosine' | 'manhattan';
431
+ }): HierarchicalClusterResult;
432
+ interface DbscanResult {
433
+ clusters: number[][];
434
+ assignments: number[];
435
+ noise: number[];
436
+ clusterCount: number;
437
+ }
438
+ declare function dbscan(points: number[][], epsilon: number, minPoints: number, options?: {
439
+ distanceMetric?: 'euclidean' | 'cosine' | 'manhattan';
440
+ }): DbscanResult;
441
+
442
+ /**
443
+ * Graph EDA — What Tukey Would Have Done With Graphs
444
+ *
445
+ * Treats graph structural properties as data series that deserve
446
+ * full exploratory data analysis treatment.
447
+ */
448
+
449
+ interface GraphEdaSummary<NodeId extends string = string> {
450
+ nodeCount: number;
451
+ edgeCount: number;
452
+ density: number;
453
+ degreeDistribution: SeriesDescription;
454
+ inDegreeDistribution: SeriesDescription;
455
+ outDegreeDistribution: SeriesDescription;
456
+ clusteringCoefficients: Map<NodeId, number>;
457
+ globalClusteringCoefficient: number;
458
+ clusteringDistribution: SeriesDescription;
459
+ averagePathLength: number;
460
+ diameter: number;
461
+ reciprocity: number;
462
+ degreeAssortativity: number;
463
+ }
464
+ interface GraphOutlierResult<NodeId extends string = string> {
465
+ nodeId: NodeId;
466
+ score: number;
467
+ reason: string;
468
+ }
469
+ /**
470
+ * Compute the local clustering coefficient for each node.
471
+ * For undirected graphs: the fraction of pairs of neighbors that are connected.
472
+ * For directed graphs: uses the total number of directed triangles.
473
+ */
474
+ declare function clusteringCoefficient<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: {
475
+ directed?: boolean;
476
+ }): Map<NodeId, number>;
477
+ /**
478
+ * Full exploratory data analysis of a graph's structural properties.
479
+ * Returns distributions as Series descriptions for full Tukey-style EDA.
480
+ */
481
+ declare function graphEda<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: {
482
+ directed?: boolean;
483
+ samplePathLength?: number;
484
+ }): GraphEdaSummary<NodeId>;
485
+ /**
486
+ * Graph-based outlier detection: nodes with unusual structural signatures.
487
+ */
488
+ declare function graphOutliers<NodeId extends string>(nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: {
489
+ method?: 'degree' | 'clustering' | 'combined';
490
+ threshold?: number;
491
+ }): GraphOutlierResult<NodeId>[];
492
+
493
+ interface GdsProjectOptions {
494
+ directed?: boolean;
495
+ replace?: boolean;
496
+ metadata?: Record<string, unknown>;
497
+ }
498
+ interface GdsGraphProjection<NodeId extends string = string> {
499
+ name: string;
500
+ nodes: NodeId[];
501
+ edges: Array<Required<GraphEdge<NodeId>>>;
502
+ directed: boolean;
503
+ createdAt: number;
504
+ metadata: Record<string, unknown>;
505
+ }
506
+ interface GdsCatalogEntry {
507
+ name: string;
508
+ nodeCount: number;
509
+ edgeCount: number;
510
+ directed: boolean;
511
+ createdAt: number;
512
+ metadata: Record<string, unknown>;
513
+ }
514
+ type GdsPipelineStep<NodeId extends string = string> = {
515
+ id: string;
516
+ kind: 'page-rank';
517
+ options?: PageRankOptions;
518
+ } | {
519
+ id: string;
520
+ kind: 'louvain';
521
+ options?: LouvainOptions<NodeId>;
522
+ } | {
523
+ id: string;
524
+ kind: 'label-propagation';
525
+ options?: LabelPropagationOptions<NodeId>;
526
+ } | {
527
+ id: string;
528
+ kind: 'similarity';
529
+ options?: SimilarityOptions;
530
+ } | {
531
+ id: string;
532
+ kind: 'knn';
533
+ options?: KNearestNeighborsOptions;
534
+ } | {
535
+ id: string;
536
+ kind: 'link-prediction';
537
+ options?: LinkPredictionOptions<NodeId>;
538
+ } | {
539
+ id: string;
540
+ kind: 'shortest-path';
541
+ source: NodeId;
542
+ target: NodeId;
543
+ options?: ShortestPathOptions;
544
+ } | {
545
+ id: string;
546
+ kind: 'a-star';
547
+ source: NodeId;
548
+ target: NodeId;
549
+ options?: AStarOptions<NodeId>;
550
+ } | {
551
+ id: string;
552
+ kind: 'yen-k-shortest-paths';
553
+ source: NodeId;
554
+ target: NodeId;
555
+ options?: YenKShortestPathsOptions;
556
+ } | {
557
+ id: string;
558
+ kind: 'max-flow';
559
+ source: NodeId;
560
+ sink: NodeId;
561
+ options?: MaximumFlowOptions;
562
+ } | {
563
+ id: string;
564
+ kind: 'min-cost-max-flow';
565
+ source: NodeId;
566
+ sink: NodeId;
567
+ edges?: FlowEdge<NodeId>[] | null;
568
+ options?: MinCostMaxFlowOptions;
569
+ } | {
570
+ id: string;
571
+ kind: 'all-pairs-shortest-paths';
572
+ options?: AllPairsShortestPathsOptions;
573
+ };
574
+ type GdsPipelineOutput<NodeId extends string = string> = PageRankResult<NodeId> | CommunityDetectionResult<NodeId> | NodeSimilarityResult<NodeId> | KNearestNeighborsResult<NodeId> | LinkPredictionResult<NodeId> | ShortestPathResult<NodeId> | AStarResult<NodeId> | YenKShortestPathsResult<NodeId> | MaximumFlowResult<NodeId> | MinCostMaxFlowResult<NodeId> | AllPairsShortestPathsResult<NodeId>;
575
+ declare class GraphCatalog<NodeId extends string = string> {
576
+ private projections;
577
+ project(name: string, nodes: GraphNodeInput<NodeId>[], edges: GraphEdge<NodeId>[], options?: GdsProjectOptions): GdsGraphProjection<NodeId>;
578
+ drop(name: string): boolean;
579
+ clear(): void;
580
+ exists(name: string): boolean;
581
+ get(name: string): GdsGraphProjection<NodeId> | null;
582
+ list(): GdsCatalogEntry[];
583
+ pageRank(name: string, options?: PageRankOptions): PageRankResult<NodeId>;
584
+ louvain(name: string, options?: LouvainOptions<NodeId>): CommunityDetectionResult<NodeId>;
585
+ labelPropagation(name: string, options?: LabelPropagationOptions<NodeId>): CommunityDetectionResult<NodeId>;
586
+ similarity(name: string, options?: SimilarityOptions): NodeSimilarityResult<NodeId>;
587
+ knn(name: string, options?: KNearestNeighborsOptions): KNearestNeighborsResult<NodeId>;
588
+ linkPrediction(name: string, options?: LinkPredictionOptions<NodeId>): LinkPredictionResult<NodeId>;
589
+ shortestPath(name: string, source: NodeId, target: NodeId, options?: ShortestPathOptions): ShortestPathResult<NodeId>;
590
+ aStar(name: string, source: NodeId, target: NodeId, options?: AStarOptions<NodeId>): AStarResult<NodeId>;
591
+ yen(name: string, source: NodeId, target: NodeId, options?: YenKShortestPathsOptions): YenKShortestPathsResult<NodeId>;
592
+ allPairs(name: string, options?: AllPairsShortestPathsOptions): AllPairsShortestPathsResult<NodeId>;
593
+ maxFlow(name: string, source: NodeId, sink: NodeId, options?: MaximumFlowOptions): MaximumFlowResult<NodeId>;
594
+ minCostMaxFlow(name: string, source: NodeId, sink: NodeId, edges?: FlowEdge<NodeId>[] | null, options?: MinCostMaxFlowOptions): MinCostMaxFlowResult<NodeId>;
595
+ runPipeline(name: string, steps: GdsPipelineStep<NodeId>[]): Map<string, GdsPipelineOutput<NodeId>>;
596
+ private requireProjection;
597
+ }
598
+ declare function createGraphCatalog<NodeId extends string = string>(): GraphCatalog<NodeId>;
599
+ declare const gds: GraphCatalog<string>;
600
+
1
601
  /**
2
602
  * Twokeys - A small data exploration and manipulation library
3
603
  * Named after John Tukey, pioneer of exploratory data analysis (EDA)
@@ -79,6 +679,12 @@ interface SeriesDescription {
79
679
  }
80
680
  interface PointsDescription {
81
681
  original: number[][];
682
+ centroid: number[];
683
+ variances: number[];
684
+ correlationMatrix: number[][];
685
+ mahalanobisDistances: number[];
686
+ outlierCount: number;
687
+ dimensionSummaries: SeriesDescription[];
82
688
  }
83
689
  declare function randomInteger(max?: number): number;
84
690
  declare function randomSeries(count?: number, max?: number): number[];
@@ -97,6 +703,12 @@ declare class Series {
97
703
  private getMedian;
98
704
  mean(): number;
99
705
  private getMean;
706
+ variance(): number;
707
+ stddev(): number;
708
+ ema(alpha: number): number[];
709
+ zscore(): number[];
710
+ skewness(): number;
711
+ kurtosis(): number;
100
712
  mode(): ModeResult;
101
713
  private getMode;
102
714
  extremes(): number[];
@@ -168,6 +780,16 @@ declare class Points {
168
780
  private dimension;
169
781
  private count;
170
782
  constructor(options?: PointsOptions | number);
783
+ centroid(): number[];
784
+ variances(): number[];
785
+ standardDeviations(): number[];
786
+ covarianceMatrix(): number[][];
787
+ correlationMatrix(): number[][];
788
+ mahalanobis(point: number[]): number;
789
+ mahalanobisAll(): number[];
790
+ outliersByMahalanobis(threshold?: number): number[][];
791
+ normalizeL2(): Points;
792
+ normalizeZscore(): Points;
171
793
  describe(): PointsDescription;
172
794
  }
173
795
  /**
@@ -190,4 +812,4 @@ declare class Twokeys {
190
812
  static randomPoints: typeof randomPoints;
191
813
  }
192
814
 
193
- export { type BinnedResult, type MedianResult, type ModeResult, Points, type PointsDescription, type PointsOptions, type RankInfo, type RankedResult, Series, type SeriesDescription, type SeriesOptions, Twokeys, Twokeys as default };
815
+ export { type AStarOptions, type AStarResult, type AllPairsShortestPathsOptions, type AllPairsShortestPathsResult, type ArticulationBridgeResult, type BetweennessCentralityRecord, type BinnedResult, type ClosenessCentralityMode, type ClosenessCentralityOptions, type ClosenessCentralityRecord, type CommunityDetectionResult, type DbscanResult, type DegreeCentralityRecord, type DendrogramNode, type FlowEdge, type FlowEdgeResult, type GdsCatalogEntry, type GdsGraphProjection, type GdsPipelineOutput, type GdsPipelineStep, type GdsProjectOptions, type GraphAdjacency, GraphAlgorithmError, type GraphAlgorithmErrorCode, type GraphAnalysisOptions, type GraphAnalysisResult, type GraphBuildOptions, GraphCatalog, type GraphEdaSummary, type GraphEdge, type GraphNeighbor, type GraphNodeInput, type GraphOutlierResult, type HierarchicalClusterResult, type KMeansAutoCandidate, type KMeansAutoOptions, type KMeansAutoResult, type KMeansCluster, type KMeansNormalization, type KMeansOptions, type KMeansResult, type KNearestNeighbor, type KNearestNeighborsOptions, type KNearestNeighborsResult, type LabelPropagationOptions, type LinkPredictionOptions, type LinkPredictionRecord, type LinkPredictionResult, type LinkageMethod, type LouvainOptions, type MaximumFlowOptions, type MaximumFlowResult, type MedianResult, type MinCostFlowEdgeResult, type MinCostMaxFlowOptions, type MinCostMaxFlowResult, type MinimumSpanningTreeOptions, type MinimumSpanningTreeResult, type ModeResult, type NodeSimilarityRecord, type NodeSimilarityResult, type PageRankOptions, type PageRankRecord, type PageRankResult, type PathRecord, Points, type PointsDescription, type PointsOptions, type RankInfo, type RankedResult, Series, type SeriesDescription, type SeriesOptions, type ShortestPathAlgorithm, type ShortestPathOptions, type ShortestPathResult, type SimilarityMetric, type SimilarityOptions, type StronglyConnectedComponentsOptions, type StronglyConnectedComponentsResult, type TopologicalSortOptions, type TopologicalSortResult, type TravelingSalesmanOptions, type TravelingSalesmanResult, type TravelingSalesmanSegment, Twokeys, type WeaklyConnectedComponentsResult, type YenKShortestPathsOptions, type YenKShortestPathsResult, aStarShortestPath, allPairsShortestPaths, analyzeGraph, articulationPointsAndBridges, betweennessCentrality, buildGraphAdjacency, closenessCentrality, clusteringCoefficient, cosineSimilarity, cosineSimilaritySparse, createGraphCatalog, dbscan, Twokeys as default, degreeCentrality, euclideanDistance, gds, graphEda, graphOutliers, hierarchicalClustering, jaccardSimilarity, kMeansAuto, kMeansClustering, kNearestNeighbors, labelPropagationCommunities, linkPrediction, louvainCommunities, mahalanobisDistance, manhattanDistance, maximumFlow, minCostMaxFlow, minimumSpanningTree, nodeSimilarity, normalizeL2, overlapCoefficient, pageRank, predictLinks, shortestPath, squaredEuclideanDistance, stronglyConnectedComponents, topologicalSort, travelingSalesmanApprox, weaklyConnectedComponents, yenKShortestPaths };