twokeys 2.0.1 → 2.2.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 +422 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +470 -1
- package/dist/index.d.ts +470 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
# Twokeys
|
|
2
|
+
|
|
3
|
+
> A small data exploration and manipulation library, named after **John Tukey** — the legendary statistician who pioneered exploratory data analysis (EDA).
|
|
4
|
+
|
|
5
|
+
[](https://github.com/buley/twokeys/actions/workflows/ci.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/twokeys)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
|
|
9
|
+
## About John Tukey
|
|
10
|
+
|
|
11
|
+
John Wilder Tukey (1915–2000) revolutionized how we look at data. He invented the box plot, coined the terms "bit" and "software," and championed the idea that **looking at data** is just as important as modeling it. His book *Exploratory Data Analysis* (1977) changed statistics forever.
|
|
12
|
+
|
|
13
|
+
This library is named after him — a founding mind in [data exploration and analysis](http://en.wikipedia.org/wiki/Exploratory_data_analysis) and a personal hero of the author.
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- **Summary Statistics**: Mean, median, mode, trimean, quartiles (hinges)
|
|
18
|
+
- **Outlier Detection**: Tukey fences (inner and outer)
|
|
19
|
+
- **Letter Values**: Extended quartiles (eighths, sixteenths, etc.)
|
|
20
|
+
- **Stem-and-Leaf**: Text-based distribution visualization
|
|
21
|
+
- **Ranking**: Full ranking with tie handling
|
|
22
|
+
- **Binning**: Histogram-style grouping
|
|
23
|
+
- **Smoothing**: Hanning filter, Tukey's 3RSSH smoothing
|
|
24
|
+
- **Transforms**: Logarithms, square roots, reciprocals
|
|
25
|
+
- **Graph Analytics**: Centrality, communities, paths, flow, clustering, TSP approximation
|
|
26
|
+
- **GDS-style Catalog**: In-memory graph projections with procedure wrappers and pipelines
|
|
27
|
+
- **WASM Support**: Optional WebAssembly for maximum performance
|
|
28
|
+
- **Zero Dependencies**: Pure TypeScript, works everywhere
|
|
29
|
+
- **Tiny**: <3KB minified and gzipped
|
|
30
|
+
|
|
31
|
+
## Packages
|
|
32
|
+
|
|
33
|
+
| Package | Description |
|
|
34
|
+
|---------|-------------|
|
|
35
|
+
| `twokeys` | Core TypeScript library |
|
|
36
|
+
| `@buley/twokeys-wasm` | WebAssembly implementation with TypeScript fallback |
|
|
37
|
+
| `@buley/twokeys-types` | Shared Zod schemas for runtime validation |
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install twokeys
|
|
43
|
+
# or
|
|
44
|
+
bun add twokeys
|
|
45
|
+
# or
|
|
46
|
+
yarn add twokeys
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
For WASM acceleration (optional):
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install @buley/twokeys-wasm
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Quick Start
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Series } from 'twokeys';
|
|
59
|
+
|
|
60
|
+
// Create a series from your data
|
|
61
|
+
const series = new Series({ data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 100] });
|
|
62
|
+
|
|
63
|
+
// Get summary statistics
|
|
64
|
+
console.log(series.mean()); // 14.5
|
|
65
|
+
console.log(series.median()); // { datum: 5.5, depth: 5.5 }
|
|
66
|
+
console.log(series.trimean()); // Tukey's trimean
|
|
67
|
+
|
|
68
|
+
// Detect outliers (using Tukey fences)
|
|
69
|
+
console.log(series.outliers()); // [100]
|
|
70
|
+
|
|
71
|
+
// Get a full description
|
|
72
|
+
const desc = series.describe();
|
|
73
|
+
console.log(desc.summary);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Using WASM (Optional)
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { loadWasm, analyze, isWasmLoaded } from '@buley/twokeys-wasm';
|
|
80
|
+
|
|
81
|
+
// Load WASM (falls back to TypeScript if unavailable)
|
|
82
|
+
await loadWasm();
|
|
83
|
+
|
|
84
|
+
console.log(isWasmLoaded()); // true if WASM loaded
|
|
85
|
+
|
|
86
|
+
// Use the same API - automatically uses WASM when available
|
|
87
|
+
const result = analyze([1, 2, 3, 4, 5, 6, 7, 8, 9, 100]);
|
|
88
|
+
console.log(result.summary.outliers); // [100]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Graph Analytics + GDS-style Catalog
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import {
|
|
95
|
+
createGraphCatalog,
|
|
96
|
+
topologicalSort,
|
|
97
|
+
louvainCommunities,
|
|
98
|
+
kNearestNeighbors,
|
|
99
|
+
linkPrediction,
|
|
100
|
+
aStarShortestPath,
|
|
101
|
+
yenKShortestPaths,
|
|
102
|
+
allPairsShortestPaths,
|
|
103
|
+
maximumFlow,
|
|
104
|
+
minCostMaxFlow,
|
|
105
|
+
} from 'twokeys';
|
|
106
|
+
|
|
107
|
+
const nodes = ['root', 'plan', 'build', 'ship'] as const;
|
|
108
|
+
const edges = [
|
|
109
|
+
{ from: 'root', to: 'plan', weight: 1 },
|
|
110
|
+
{ from: 'plan', to: 'build', weight: 2 },
|
|
111
|
+
{ from: 'build', to: 'ship', weight: 1 },
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
// Vote-weighted linearization for "abstract -> concrete" flows
|
|
115
|
+
const linear = topologicalSort(nodes, edges, {
|
|
116
|
+
priorityByNode: { ship: 10, build: 6, plan: 3 },
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Community + similarity/link prediction family
|
|
120
|
+
const communities = louvainCommunities(nodes, edges);
|
|
121
|
+
const knn = kNearestNeighbors(nodes, edges, { k: 2 });
|
|
122
|
+
const links = linkPrediction(nodes, edges, { limit: 5 });
|
|
123
|
+
|
|
124
|
+
// Path + flow family
|
|
125
|
+
const aStar = aStarShortestPath(nodes, edges, 'root', 'ship');
|
|
126
|
+
const yen = yenKShortestPaths(nodes, edges, 'root', 'ship', { k: 3 });
|
|
127
|
+
const apsp = allPairsShortestPaths(nodes, edges);
|
|
128
|
+
const maxFlow = maximumFlow(nodes, edges, 'root', 'ship');
|
|
129
|
+
const minCost = minCostMaxFlow(
|
|
130
|
+
nodes,
|
|
131
|
+
edges.map((edge) => ({
|
|
132
|
+
from: edge.from,
|
|
133
|
+
to: edge.to,
|
|
134
|
+
capacity: edge.weight ?? 1,
|
|
135
|
+
cost: edge.weight ?? 1,
|
|
136
|
+
})),
|
|
137
|
+
'root',
|
|
138
|
+
'ship',
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// GDS-style catalog/procedure wrapper
|
|
142
|
+
const gds = createGraphCatalog<string>();
|
|
143
|
+
gds.project('tasks', [...nodes], edges, { directed: true });
|
|
144
|
+
const rank = gds.pageRank('tasks');
|
|
145
|
+
const pipeline = gds.runPipeline('tasks', [
|
|
146
|
+
{ id: 'rank', kind: 'page-rank' },
|
|
147
|
+
{ id: 'sim', kind: 'similarity', options: { metric: 'jaccard' } },
|
|
148
|
+
{ id: 'links', kind: 'link-prediction', options: { limit: 10 } },
|
|
149
|
+
]);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Benchmarks
|
|
153
|
+
|
|
154
|
+
Performance on different dataset sizes (operations per second, higher is better):
|
|
155
|
+
|
|
156
|
+
### TypeScript Implementation
|
|
157
|
+
|
|
158
|
+
| Method | 100 elements | 1,000 elements | 10,000 elements |
|
|
159
|
+
|--------|-------------:|---------------:|----------------:|
|
|
160
|
+
| `sorted()` | 224,599 | 14,121 | 874 |
|
|
161
|
+
| `median()` | 199,397 | 14,127 | 876 |
|
|
162
|
+
| `mean()` | 550,610 | 413,551 | 68,399 |
|
|
163
|
+
| `mode()` | 87,665 | 6,738 | 431 |
|
|
164
|
+
| `fences()` | 238,486 | 13,270 | 864 |
|
|
165
|
+
| `outliers()` | 210,058 | 12,584 | 854 |
|
|
166
|
+
| `smooth()` | 61,268 | 1,599 | 31 |
|
|
167
|
+
| `describe()` | 15,642 | 952 | 29 |
|
|
168
|
+
|
|
169
|
+
### v2.0 Performance Improvements
|
|
170
|
+
|
|
171
|
+
Compared to v1.x (CoffeeScript), v2.0 TypeScript is dramatically faster:
|
|
172
|
+
|
|
173
|
+
| Method | v1.x (10K) | v2.0 (10K) | Improvement |
|
|
174
|
+
|--------|------------|------------|-------------|
|
|
175
|
+
| `median()` | 6 ops/sec | 876 ops/sec | **146x faster** |
|
|
176
|
+
| `counts()` | 1 ops/sec | 606 ops/sec | **606x faster** |
|
|
177
|
+
| `fences()` | 5 ops/sec | 864 ops/sec | **173x faster** |
|
|
178
|
+
| `describe()` | 1 ops/sec | 29 ops/sec | **29x faster** |
|
|
179
|
+
|
|
180
|
+
Key optimizations:
|
|
181
|
+
- O(1) index-based median (was O(n²) recursive)
|
|
182
|
+
- Map-based frequency counting (was O(n²) recursive)
|
|
183
|
+
- Eliminated unnecessary array copying in smoothing
|
|
184
|
+
|
|
185
|
+
## Example Output
|
|
186
|
+
|
|
187
|
+
Applying `describe()` to a Series returns a complete analysis:
|
|
188
|
+
|
|
189
|
+
```javascript
|
|
190
|
+
const series = new Series({ data: [48, 59, 63, 30, 57, 92, 73, 47, 31, 5] });
|
|
191
|
+
const analysis = series.describe();
|
|
192
|
+
|
|
193
|
+
// Result:
|
|
194
|
+
{
|
|
195
|
+
"original": [48, 59, 63, 30, 57, 92, 73, 47, 31, 5],
|
|
196
|
+
"summary": {
|
|
197
|
+
"median": { "datum": 52.5, "depth": 5.5 },
|
|
198
|
+
"mean": 50.5,
|
|
199
|
+
"hinges": [{ "datum": 31, "depth": 3 }, { "datum": 63, "depth": 8 }],
|
|
200
|
+
"adjacent": [30, 92],
|
|
201
|
+
"outliers": [],
|
|
202
|
+
"extremes": [5, 92],
|
|
203
|
+
"iqr": 32,
|
|
204
|
+
"fences": [4.5, 100.5]
|
|
205
|
+
},
|
|
206
|
+
"smooths": {
|
|
207
|
+
"smooth": [48, 30, 57, 57, 57, 47, 31, 5, 5, 5],
|
|
208
|
+
"hanning": [48, 61, 46.5, 43.5, 74.5, 82.5, 60, 39, 18, 5]
|
|
209
|
+
},
|
|
210
|
+
"transforms": {
|
|
211
|
+
"logs": [3.87, 4.08, 4.14, ...],
|
|
212
|
+
"roots": [6.93, 7.68, 7.94, ...],
|
|
213
|
+
"inverse": [0.021, 0.017, 0.016, ...]
|
|
214
|
+
},
|
|
215
|
+
"sorted": [5, 30, 31, 47, 48, 57, 59, 63, 73, 92],
|
|
216
|
+
"ranked": { "up": {...}, "down": {...}, "groups": {...} },
|
|
217
|
+
"binned": { "bins": 4, "width": 26, "binned": {...} }
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## API
|
|
222
|
+
|
|
223
|
+
### Series
|
|
224
|
+
|
|
225
|
+
The `Series` class provides methods for exploring 1-dimensional numerical data.
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { Series } from 'twokeys';
|
|
229
|
+
|
|
230
|
+
const series = new Series({ data: [1, 2, 3, 4, 5] });
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
#### Summary Statistics
|
|
234
|
+
|
|
235
|
+
| Method | Description |
|
|
236
|
+
|--------|-------------|
|
|
237
|
+
| `mean()` | Arithmetic mean |
|
|
238
|
+
| `median()` | Median value and depth |
|
|
239
|
+
| `mode()` | Most frequent value(s) |
|
|
240
|
+
| `trimean()` | Tukey's trimean: (Q1 + 2×median + Q3) / 4 |
|
|
241
|
+
| `extremes()` | [min, max] values |
|
|
242
|
+
| `hinges()` | Quartile boundaries (Q1, Q3) |
|
|
243
|
+
| `iqr()` | Interquartile range |
|
|
244
|
+
|
|
245
|
+
#### Outlier Detection
|
|
246
|
+
|
|
247
|
+
| Method | Description |
|
|
248
|
+
|--------|-------------|
|
|
249
|
+
| `fences()` | Inner fence boundaries (1.5 × IQR) |
|
|
250
|
+
| `outer()` | Outer fence boundaries (3 × IQR) |
|
|
251
|
+
| `outliers()` | Values outside inner fences |
|
|
252
|
+
| `inside()` | Values within fences |
|
|
253
|
+
| `outside()` | Values outside outer fences |
|
|
254
|
+
| `adjacent()` | Most extreme non-outlier values |
|
|
255
|
+
|
|
256
|
+
#### Letter Values & Visualization
|
|
257
|
+
|
|
258
|
+
| Method | Description |
|
|
259
|
+
|--------|-------------|
|
|
260
|
+
| `letterValues()` | Extended quartiles (M, F, E, D, C, B, A...) |
|
|
261
|
+
| `stemLeaf()` | Stem-and-leaf text display |
|
|
262
|
+
| `midSummaries()` | Symmetric quantile pair averages |
|
|
263
|
+
|
|
264
|
+
#### Ranking & Counting
|
|
265
|
+
|
|
266
|
+
| Method | Description |
|
|
267
|
+
|--------|-------------|
|
|
268
|
+
| `sorted()` | Sorted copy of data |
|
|
269
|
+
| `ranked()` | Rank information with tie handling |
|
|
270
|
+
| `counts()` | Frequency of each value |
|
|
271
|
+
| `binned()` | Histogram-style bins |
|
|
272
|
+
|
|
273
|
+
#### Transforms
|
|
274
|
+
|
|
275
|
+
| Method | Description |
|
|
276
|
+
|--------|-------------|
|
|
277
|
+
| `logs()` | Natural logarithm of each value |
|
|
278
|
+
| `roots()` | Square root of each value |
|
|
279
|
+
| `inverse()` | Reciprocal (1/x) of each value |
|
|
280
|
+
|
|
281
|
+
#### Smoothing
|
|
282
|
+
|
|
283
|
+
| Method | Description |
|
|
284
|
+
|--------|-------------|
|
|
285
|
+
| `hanning()` | Hanning filter (running averages) |
|
|
286
|
+
| `smooth()` | Tukey's 3RSSH smoothing |
|
|
287
|
+
| `rough()` | Residuals (original - smooth) |
|
|
288
|
+
|
|
289
|
+
#### Full Description
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
series.describe();
|
|
293
|
+
// Returns complete analysis including all of the above
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Points
|
|
297
|
+
|
|
298
|
+
The `Points` class handles n-dimensional point data.
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import { Points } from 'twokeys';
|
|
302
|
+
|
|
303
|
+
// 100 random 2D points
|
|
304
|
+
const points = new Points({ count: 100, dimensionality: 2 });
|
|
305
|
+
|
|
306
|
+
// Or provide your own data
|
|
307
|
+
const myPoints = new Points({ data: [[1, 2], [3, 4], [5, 6]] });
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Twokeys
|
|
311
|
+
|
|
312
|
+
The main class provides factory methods and utilities.
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import Twokeys from 'twokeys';
|
|
316
|
+
|
|
317
|
+
// Generate random data
|
|
318
|
+
const randomData = Twokeys.randomSeries(100, 50); // 100 values, max 50
|
|
319
|
+
const randomPoints = Twokeys.randomPoints(50, 3); // 50 3D points
|
|
320
|
+
|
|
321
|
+
// Access classes
|
|
322
|
+
const series = new Twokeys.Series({ data: [1, 2, 3] });
|
|
323
|
+
const points = new Twokeys.Points(100);
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## Examples
|
|
327
|
+
|
|
328
|
+
### Box Plot Data
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
331
|
+
const series = new Series({ data: myData });
|
|
332
|
+
|
|
333
|
+
const boxPlot = {
|
|
334
|
+
min: series.extremes()[0],
|
|
335
|
+
q1: series.hinges()[0].datum,
|
|
336
|
+
median: series.median().datum,
|
|
337
|
+
q3: series.hinges()[1].datum,
|
|
338
|
+
max: series.extremes()[1],
|
|
339
|
+
outliers: series.outliers(),
|
|
340
|
+
};
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Outlier Detection
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
const series = new Series({ data: measurements });
|
|
347
|
+
|
|
348
|
+
// Inner fences: 1.5 × IQR from hinges
|
|
349
|
+
const suspicious = series.outliers();
|
|
350
|
+
|
|
351
|
+
// Outer fences: 3 × IQR from hinges
|
|
352
|
+
const extreme = series.outside();
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Letter Values Display
|
|
356
|
+
|
|
357
|
+
```typescript
|
|
358
|
+
const series = new Series({ data: myData });
|
|
359
|
+
|
|
360
|
+
// Get extended quartiles
|
|
361
|
+
const lv = series.letterValues();
|
|
362
|
+
// [
|
|
363
|
+
// { letter: 'M', depth: 10.5, lower: 52.5, upper: 52.5, mid: 52.5, spread: 0 },
|
|
364
|
+
// { letter: 'F', depth: 5, lower: 31, upper: 73, mid: 52, spread: 42 },
|
|
365
|
+
// { letter: 'E', depth: 3, lower: 30, upper: 82, mid: 56, spread: 52 },
|
|
366
|
+
// ...
|
|
367
|
+
// ]
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### Stem-and-Leaf Display
|
|
371
|
+
|
|
372
|
+
```typescript
|
|
373
|
+
const series = new Series({ data: myData });
|
|
374
|
+
|
|
375
|
+
const { display } = series.stemLeaf();
|
|
376
|
+
// [
|
|
377
|
+
// " 0 | 5",
|
|
378
|
+
// " 3 | 0 1",
|
|
379
|
+
// " 4 | 7 8",
|
|
380
|
+
// " 5 | 7 9",
|
|
381
|
+
// " 6 | 3",
|
|
382
|
+
// " 7 | 3",
|
|
383
|
+
// " 9 | 2"
|
|
384
|
+
// ]
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### Data Transformation
|
|
388
|
+
|
|
389
|
+
```typescript
|
|
390
|
+
const series = new Series({ data: skewedData });
|
|
391
|
+
|
|
392
|
+
// Try different transforms to normalize
|
|
393
|
+
const logTransformed = series.logs();
|
|
394
|
+
const sqrtTransformed = series.roots();
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
## Development
|
|
398
|
+
|
|
399
|
+
```bash
|
|
400
|
+
# Install dependencies
|
|
401
|
+
bun install
|
|
402
|
+
|
|
403
|
+
# Run tests
|
|
404
|
+
bun test
|
|
405
|
+
|
|
406
|
+
# Run tests with coverage
|
|
407
|
+
bun test --coverage
|
|
408
|
+
|
|
409
|
+
# Build all packages
|
|
410
|
+
bun run build
|
|
411
|
+
|
|
412
|
+
# Run benchmark
|
|
413
|
+
bun run bench.ts
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
## License
|
|
417
|
+
|
|
418
|
+
MIT
|
|
419
|
+
|
|
420
|
+
---
|
|
421
|
+
|
|
422
|
+
*"The best thing about being a statistician is that you get to play in everyone's backyard."* — John Tukey
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';Object.defineProperty(exports,'__esModule',{value:true});function R(b=100){return Math.floor(Math.random()*b)}function E(b=1e3,t=100){let n=[];for(let e=0;e<b;e++)n.push(R(t));return n}function _(b=2,t=100){let n=[];for(let e=0;e<b;e++)n.push(Math.floor(Math.random()*(t/10)%t));return n}function f(b=1e3,t=2,n=100){let e=[];for(let r=0;r<b;r++)e.push(_(t,n));return e}var g=class{data;constructor(t={}){this.data={original:t.data??E()};}sorted(){return this.data.sorted||(this.data.sorted=this.getSorted(this.data.original)),this.data.sorted}getSorted(t){return [...t].sort((n,e)=>n>e?1:n===e?0:-1)}median(){return this.sorted(),this.data.median===void 0&&(this.data.median=this.getMedian(this.data.sorted)),this.data.medianDepth===void 0&&(this.data.medianDepth=this.getMedianDepth(this.data.sorted)),{datum:this.data.median,depth:this.data.medianDepth}}getMedianDepth(t,n=0){return t.length?n+(t.length+1)/2:NaN}getMedian(t){let n=t.length;if(!n)return NaN;if(n===1)return t[0];let e=Math.floor(n/2);return n%2===0?(t[e-1]+t[e])/2:t[e]}mean(){return this.data.mean===void 0&&(this.data.mean=this.getMean(this.data.original)),this.data.mean}getMean(t){if(!t.length)return NaN;let n=0;for(let e of t)n+=e;return n/t.length}mode(){return this.data.mode||(this.sorted(),this.data.mode=this.getMode(this.data.sorted)),this.data.mode}getMode(t){if(!t.length)return {count:0,data:[]};let n={},e=0;for(let i of t)n[i]=(n[i]||0)+1,n[i]>e&&(e=n[i]);let r=[];for(let[i,s]of Object.entries(n))s===e&&r.push(Number(i));return {count:e,data:r.sort((i,s)=>i-s)}}extremes(){return this.data.extremes||(this.sorted(),this.data.extremes=this.getExtremes(this.data.sorted)),this.data.extremes}getExtremes(t){return t.length?[t[0],t[t.length-1]]:[]}counts(){return this.data.counts||(this.sorted(),this.data.counts=this.getCounts(this.data.sorted)),this.data.counts}getCounts(t){let n=new Map;for(let r of t)n.set(r,(n.get(r)||0)+1);let e=[];for(let[r,i]of n)e.push([r,i]);return e.sort((r,i)=>r[0]-i[0])}hinges(){return this.data.hinges||(this.sorted(),this.data.hinges=this.getHinges(this.data.sorted)),this.data.hinges}getHinges(t,n=2,e=[]){let r=[...t],i=r.length,s=n;if(s%2!==0&&s++,i<=s||s<=0)return e;let m=Math.floor(i/s),h=Math.floor(i/m)-1;for(let a=0;a<=h;a++){let o=r.slice(a*m,a*m+m);e.push({datum:this.getMedian(o),depth:this.getMedianDepth(o,a*m)});}return e}iqr(){return this.data.iqr===void 0&&(this.hinges(),this.data.iqr=this.getIQR(this.data.hinges)),this.data.iqr}getIQR(t){let n=t[0]?.datum,e=t[1]?.datum;return n===void 0||e===void 0?NaN:Math.abs(n-e)}fences(){return this.data.fences||(this.median(),this.iqr(),this.data.fences=this.getFences()),this.data.fences}getFences(t=1.5){let n=this.data.median,e=this.data.iqr;if(n===void 0||e===void 0||isNaN(e))return [];let r=e*t;return [n-r,n+r]}outer(){return this.data.outer||(this.median(),this.iqr(),this.data.outer=this.getOuter()),this.data.outer}getOuter(t=1.5){let n=this.data.median,e=this.data.iqr;if(n===void 0||e===void 0||isNaN(e))return [];let r=2*e*t;return [n-r,n+r]}outside(){return this.data.outside||(this.outer(),this.data.outside=this.getOutside()),this.data.outside}getOutside(){let t=[],n=this.data.sorted,e=this.data.outer;if(!e||e.length===0)return [];let r=Math.min(...e),i=Math.max(...e);for(let s of n)(s>i||s<r)&&t.push(s);return t}inside(){return this.data.inside||(this.fences(),this.data.inside=this.getInside()),this.data.inside}getInside(){let t=[],n=this.data.sorted,e=this.data.fences;if(!e||e.length===0)return [];let r=Math.min(...e),i=Math.max(...e);for(let s of n)s<i&&s>r&&t.push(s);return t}outliers(){return this.data.outliers||(this.fences(),this.data.outliers=this.getOutliers()),this.data.outliers}getOutliers(){let t=[],n=this.data.sorted,e=this.data.fences;if(e.length===0)return [];let r=Math.min(...e),i=Math.max(...e);for(let s of n)(s>i||s<r)&&t.push(s);return t}ranked(){return this.data.ranked||(this.sorted(),this.data.ranked=this.getRanked(this.data.sorted)),this.data.ranked}getRanked(t,n=true){let e={},r={},i=t.length,s=[],m=NaN,h=[],a=()=>{m=NaN,h=[];};for(let d=0;d<t.length;d++){let u=t[d];if(!n)e[u]={rank:d+1,peers:0},r[u]={rank:i-d,peers:0};else {let c=d+1,l=d-1;u===t[l]?(!isNaN(m)&&h.length===0?(h.push(u),s.push(h),a()):(h.push(u),m=l),u!==t[c]&&(s.push(h),a())):u!==t[c]?h.length>0?(s.push(h),a()):s.push(u):h.push(u);}}let o=0;for(let d=0;d<s.length;d++){let u=s[d];if(typeof u=="number")r[u]={rank:d+1+o,peers:0},e[u]={rank:i-d-o,peers:0};else if(Array.isArray(u)){o+=u.length;let c=u[0];r[c]={rank:d+1+o,peers:u.length},e[c]={rank:i-d-o,peers:u.length};}else o+=1;}return {up:e,down:r,groups:{down:[...s],up:[...s].reverse()}}}adjacent(){return this.data.adjacent||(this.fences(),this.data.adjacent=this.getAdjacent(this.data.sorted,this.data.fences)),this.data.adjacent}getAdjacent(t,n){if(n.length===0)return [];let e=n[0],r=[],i=n[1],s=[];for(let m of t)m>e&&r.push(m),m<i&&s.push(m);return r.sort((m,h)=>m-h),s.sort((m,h)=>m-h),[r[0],s[s.length-1]]}binned(t=NaN){return this.data.binned||(this.sorted(),this.extremes(),this.data.binned=this.getBinned(this.data.sorted,t)),this.data.binned}getBinned(t,n=10,e=NaN,r=true){let i={},s=t.length,m=r?0:1;if(s===0)return {bins:0,width:NaN,binned:{}};let h=this.data.extremes,a=e;if(h&&isNaN(a)&&h.length===2){a=(h[1]-h[0])/(Math.log(t.length)/Math.LN2),a=Math.floor(a);let d=true;for(let u of t)if(u%1!==0){d=false;break}d&&(a=Math.floor(a));}let o=Math.floor(h[1]/a)+1;(!o||o<1)&&(o=1);for(let d of t){let u=Math.floor((d-m)/a);i[u]||(i[u]={from:u*a+m,to:(u+1)*a+m-1,data:[]}),i[u].data.push(d);}return {bins:o,width:a,binned:i}}logs(){return this.data.logs||(this.data.logs=this.getLogs(this.data.original)),this.data.logs}getLogs(t){return t.map(n=>Math.log(n))}roots(){return this.data.roots||(this.data.roots=this.getRoots(this.data.original)),this.data.roots}getRoots(t){return t.map(n=>Math.sqrt(n))}inverse(){return this.data.inverse||(this.data.inverse=this.getInverse(this.data.original)),this.data.inverse}getInverse(t){return t.map(n=>1/n)}hanning(){return this.data.hanning||(this.data.hanning=this.getSkipMeans(this.data.original)),this.data.hanning}getSkipMeans(t){let n=[];for(let e=0;e<t.length;e++)e!==0&&e!==t.length-1&&n.push((t[e]+t[e+1])/2);return n.unshift(t[0]),n.push(t[t.length-1]),n}smooth(){return this.data.smooth||(this.sorted(),this.data.smooth=this.getSmooth(this.data.original)),this.data.rough=this.getRough(this.data.original,this.data.smooth),this.data.smooth}getRough(t,n){let e=[];for(let r=0;r<t.length;r++)e.push(t[r]-n[r]);return e}getSmooth(t,n=3){let e=[...t];return e=this.smoothMedian(e,n),e=this.smoothExtremes(e,-1),e=this.smoothSplit(e,2),e=this.smoothMedian(e,n),e=this.smoothExtremes(e,-1),e=this.smoothMedian(e,n),e}smoothExtremes(t,n=1,e=0,r="both"){let i=t.length;if(i<=2)return [...t];let s=[...t];for(let m=e;m<n||n===-1;m++){let h=false;if(r==="both"||r==="head"){let a=s[0],o=s[1],d=s[2],u=o-2*(d-o),c=a<=o?o<=u?o:a<=u?u:a:a<=u?a:o<=u?u:o;s[0]!==c&&(s[0]=c,h=true);}if(r==="both"||r==="tail"){let a=s[i-3],o=s[i-2],d=s[i-1],u=o-2*(a-o),c=d<=o?o<=u?o:d<=u?u:d:d<=u?d:o<=u?u:o;s[i-1]!==c&&(s[i-1]=c,h=true);}if(n===-1&&!h)break}return s}smoothSplit(t,n=2,e=0){let r=[...t],i=t.length;for(let s=e;s<n||n===-1;s++){let m=false;for(let h=2;h<i-1;h++){let a=r[h],o=r[h-1],d=r[h-2],u=r[h+1];if(a===o&&(o>d&&a>u||o<d&&a<u)){let c=this.smoothExtremes(r.slice(0,h)),l=this.smoothExtremes(r.slice(h));r=c.concat(l),m=true;}}if(n===-1&&!m)return r}return r}smoothMedian(t,n=1,e=0){let r=t,i=t.length;if(i<=2)return [...t];for(let s=e;s<n||n===-1;s++){let m=new Array(i);m[0]=r[0],m[i-1]=r[i-1];let h=false;for(let a=1;a<i-1;a++){let o=r[a],d=Math.min(Math.max(r[a-1],o),r[a+1]);m[a]=d,d!==o&&(h=true);}if(n===-1&&!h)return r;r=m;}return r}jitter(t,n=1,e=NaN,r=1,i=NaN,s=0){let m=s+1,h=[...t];if(m<=n){let a=[];for(let o of h){let d=i;!d&&!isNaN(d)&&(d=(1+Math.floor(o/10))*(Math.random()>.5?1:-1));let u=o+Math.floor(Math.random()*r*d);!isNaN(e)&&u<e&&(u=e),a.push(u);}return this.jitter(a,n,e,r,i,m)}return h}trimean(){let t=this.median(),n=this.hinges();return n.length<2?t.datum:(n[0].datum+2*t.datum+n[1].datum)/4}letterValues(){this.sorted();let t=this.data.sorted.length;if(t<2)return [];let n=["M","F","E","D","C","B","A","Z","Y","X","W","V","U","T","S"],e=[],r=(t+1)/2,i=this.median().datum;e.push({letter:"M",depth:r,lower:i,upper:i,mid:i,spread:0});let s=r,m=1;for(;s>1&&m<n.length&&(s=Math.floor((Math.floor(s)+1)/2),!(s<1));){let h=Math.ceil(s)-1,a=t-Math.ceil(s);if(h<0||a>=t||h>=a)break;let o=this.data.sorted[h],d=this.data.sorted[a],u=(o+d)/2,c=d-o;e.push({letter:n[m],depth:s,lower:o,upper:d,mid:u,spread:c}),m++;}return e}rough(){return this.data.rough||this.smooth(),this.data.rough||[]}stemLeaf(t=1){this.sorted();let n=this.data.sorted;if(!n.length)return {stems:[],leaves:{},display:[]};let e=Math.pow(10,t),r=new Map;for(let a of n){let o=Math.floor(a/e),d=Math.abs(Math.round(a%e));r.has(o)||r.set(o,[]),r.get(o).push(d);}let i=Array.from(r.keys()).sort((a,o)=>a-o),s=[],m={},h=[];for(let a of i){let o=String(a);s.push(o);let d=r.get(a).sort((u,c)=>u-c).map(String);m[o]=d,h.push(`${o.padStart(4)} | ${d.join(" ")}`);}return {stems:s,leaves:m,display:h}}midSummaries(){return this.letterValues().map(({depth:n,mid:e,spread:r})=>({depth:n,mid:e,spread:r}))}describe(){return this.data.description={original:this.data.original,summary:{median:this.median(),mean:this.mean(),mode:this.mode(),hinges:this.hinges(),adjacent:this.adjacent(),outliers:this.outliers(),outer:this.outer(),outside:this.outside(),inside:this.inside(),extremes:this.extremes(),iqr:this.iqr(),fences:this.fences()},smooths:{smooth:this.smooth(),hanning:this.hanning()},transforms:{logs:this.logs(),roots:this.roots(),inverse:this.inverse()},counts:this.counts(),sorted:this.sorted(),ranked:this.ranked(),binned:this.binned()},this.data.description}},p=class{data;dimension;count;constructor(t={}){typeof t=="number"?(this.count=t,this.dimension=2,this.data={original:f(this.count,this.dimension)}):(this.dimension=t.dimensionality??2,this.count=t.count??100,this.data={original:t.data??f(this.count,this.dimension)});}describe(){return this.data.description={original:this.data.original},this.data.description}},M=class{smoothed=false;static DEFAULT_MAX_RANDOM_INTEGER=100;static DEFAULT_MIN_RANDOM_INTEGER=0;static DEFAULT_RANDOM_SERIES_COUNT=1e3;static DEFAULT_OUTLIER_MULTIPLE=1.5;static DEFAULT_JITTER_MULTIPLIER=1;static DEFAULT_SPLIT_PASSES=2;static DEFAULT_MAX_RANDOM_DIMENSIONALITY=2;static Series=g;static Points=p;static randomInteger=R;static randomSeries=E;static randomPoint=_;static randomPoints=f},A=M;exports.Points=p;exports.Series=g;exports.Twokeys=M;exports.default=A;//# sourceMappingURL=index.cjs.map
|
|
1
|
+
'use strict';Object.defineProperty(exports,'__esModule',{value:true});var E=class extends Error{code;constructor(n,t){super(t),this.name="GraphAlgorithmError",this.code=n;}},C=class{heap=[];get size(){return this.heap.length}push(n,t){this.heap.push({priority:t,value:n}),this.siftUp(this.heap.length-1);}pop(){if(this.heap.length===0)return;let n=this.heap[0],t=this.heap.pop();return this.heap.length>0&&t&&(this.heap[0]=t,this.siftDown(0)),n}siftUp(n){let t=n;for(;t>0;){let e=Math.floor((t-1)/2),o=this.heap[t],r=this.heap[e];if(!o||!r||r.priority<=o.priority)return;this.heap[e]=o,this.heap[t]=r,t=e;}}siftDown(n){let t=n;for(;;){let e=t*2+1,o=t*2+2,r=t,s=this.heap[r],c=this.heap[e],i=this.heap[o];s&&c&&c.priority<s.priority&&(r=e);let d=this.heap[r];if(d&&i&&i.priority<d.priority&&(r=o),r===t)return;let a=this.heap[t],h=this.heap[r];if(!a||!h)return;this.heap[t]=h,this.heap[r]=a,t=r;}}};function P(u,n){return u.localeCompare(n)}function V(u,n){return P(u,n)<=0?{from:u,to:n}:{from:n,to:u}}function F(u,n){let t=V(u,n);return `${t.from}\0${t.to}`}function Pe(u){return typeof u=="string"?u:u.id}var Y=class{parent=new Map;rank=new Map;constructor(n){for(let t of n)this.parent.set(t,t),this.rank.set(t,0);}find(n){let t=this.parent.get(n);if(!t||t===n)return n;let e=this.find(t);return this.parent.set(n,e),e}union(n,t){let e=this.find(n),o=this.find(t);if(e===o)return false;let r=this.rank.get(e)??0,s=this.rank.get(o)??0;return r<s?(this.parent.set(e,o),true):r>s?(this.parent.set(o,e),true):(this.parent.set(o,e),this.rank.set(e,r+1),true)}};function q(u,n){let t=new Set,e=[];for(let o of u){let r=Pe(o);t.has(r)||(t.add(r),e.push(r));}for(let o of n)t.has(o.from)||(t.add(o.from),e.push(o.from)),t.has(o.to)||(t.add(o.to),e.push(o.to));return e}function Ae(u){if(u===void 0)return 1;if(!Number.isFinite(u))throw new E("INVALID_ARGUMENT",`Graph edge weight must be finite, received ${u}`);return u}function ve(u){let n=new Map;for(let t of u)n.set(t,new Map);return n}function se(u,n,t,e){let o=u.get(n);if(!o)return;let r=o.get(t);(r===void 0||e<r)&&o.set(t,e);}function ke(u,n,t){let e=ve(u);for(let o of n){if(o.from===o.to)continue;let r=Ae(o.weight);!e.has(o.from)||!e.has(o.to)||(se(e,o.from,o.to,r),t||se(e,o.to,o.from,r));}return e}function Ge(u){let n=new Map;for(let[t,e]of u.entries()){let o=[];for(let[r,s]of e.entries())o.push({id:r,weight:s});n.set(t,o);}return n}function Te(u,n){let t=new Map;for(let e of u)t.set(e,[]);for(let[e,o]of n.entries())for(let r of o){let s=t.get(r.id);s&&s.push({id:e,weight:r.weight});}return t}function Oe(u){let n=[];for(let[t,e]of u.entries())for(let o of e)n.push({from:t,to:o.id,weight:o.weight});return n}function G(u){for(let n of u.edges)if(n.weight<0)return true;return false}function $(u,n,t){if(u===n)return [u];let e=[n],o=n;for(;o&&o!==u;){let r=t.get(o);if(!r)return [];e.push(r),o=r;}return e.reverse(),e[0]===u?e:[]}function ae(u){return u.reduce((n,t)=>n+t.weight,0)}function Ce(u){let n=new Map;for(let t of u.edges){let e=V(t.from,t.to),o=F(e.from,e.to),r=n.get(o);(!r||t.weight<r.weight-1e-12)&&n.set(o,{from:e.from,to:e.to,weight:t.weight});}return Array.from(n.values())}function Fe(u,n){let t=Ce(u),e=0;for(let i of t)e+=i.weight;if(!Number.isFinite(e)||e<=0)return 0;let o=new Map;for(let i of u.nodes)o.set(i,ae(u.neighborsByNode.get(i)??[]));let r=new Map,s=new Map;for(let i of u.nodes){let d=n.get(i);if(d===void 0)continue;let a=o.get(i)??0;r.set(d,(r.get(d)??0)+a);}for(let i of t){let d=n.get(i.from),a=n.get(i.to);d===void 0||a===void 0||d===a&&s.set(d,(s.get(d)??0)+i.weight);}let c=0;for(let[i,d]of r.entries()){let a=s.get(i)??0;c+=a/e-Math.pow(d/(2*e),2);}return c}function U(u,n,t,e,o){let r=new Map;for(let i of u.nodes){let d=n.get(i);if(d===void 0)continue;let a=r.get(d)??[];a.push(i),r.set(d,a);}let s=Array.from(r.values()).map(i=>[...i].sort(P));s.sort((i,d)=>P(i[0]??"",d[0]??""));let c=new Map;for(let i=0;i<s.length;i+=1)for(let d of s[i]??[])c.set(d,i);return {communities:s,communityByNode:c,iterations:t,converged:e,algorithm:o,modularity:Fe(u,c)}}function H(u){let n=new Map;for(let t of u.nodes)n.set(t,new Set);for(let[t,e]of u.neighborsByNode.entries()){let o=n.get(t);if(o)for(let r of e)o.add(r.id);}return n}function L(u,n,t,e){let o=t.get(u)??new Set,r=t.get(n)??new Set,s=o.size,c=r.size,i=s<=c?o:r,d=i===o?r:o,a=0,h=0,l=0;for(let N of i){if(!d.has(N))continue;a+=1;let b=t.get(N)?.size??0;b>1&&(h+=1/Math.log(b)),b>0&&(l+=1/b);}if(e==="common-neighbors")return a;if(e==="preferential-attachment")return s*c;if(e==="adamic-adar")return h;if(e==="resource-allocation")return l;if(e==="jaccard"){let N=s+c-a;return N>0?a/N:0}if(e==="cosine")return s>0&&c>0?a/Math.sqrt(s*c):0;let m=Math.min(s,c);return m>0?a/m:0}function Be(u,n){if(u.length<=1)return 0;let t=0;for(let e=1;e<u.length;e+=1){let o=u[e-1],r=u[e];if(!o||!r)return Number.POSITIVE_INFINITY;let s=n.neighborsByNode.get(o)?.find(c=>c.id===r)?.weight;if(s===void 0||!Number.isFinite(s))return Number.POSITIVE_INFINITY;t+=s;}return t}function _(u){return u.join("\0")}function De(u,n){if(n.length>u.length)return false;for(let t=0;t<n.length;t+=1)if(u[t]!==n[t])return false;return true}function ce(u,n){return u==="auto"?n?"bellman-ford":"dijkstra":u}function _e(u,n){if(G(u))throw new E("NEGATIVE_WEIGHT","Dijkstra cannot run on negative graph weights.");let t=new Map,e=new Map,o=new Set,r=new C;for(let s of u.nodes)t.set(s,Number.POSITIVE_INFINITY);for(t.set(n,0),r.push(n,0);r.size>0;){let s=r.pop();if(!s)break;let c=s.value;if(o.has(c))continue;o.add(c);let i=t.get(c);if(i===void 0||!Number.isFinite(i))continue;let d=u.neighborsByNode.get(c)??[];for(let a of d){let h=i+a.weight,l=t.get(a.id);(l===void 0||h<l-1e-12)&&(t.set(a.id,h),e.set(a.id,c),r.push(a.id,h));}}return {distanceByNode:t,previousByNode:e,explored:o.size,negativeCycleNodes:new Set}}function Le(u,n){let t=new Map,e=new Map;for(let d of u.nodes)t.set(d,Number.POSITIVE_INFINITY);t.set(n,0);let o=u.nodes.length;for(let d=0;d<o-1;d+=1){let a=false;for(let h of u.edges){let l=t.get(h.from);if(l===void 0||!Number.isFinite(l))continue;let m=l+h.weight,N=t.get(h.to);(N===void 0||m<N-1e-12)&&(t.set(h.to,m),e.set(h.to,h.from),a=true);}if(!a)break}let r=new Set,s=[];for(let d of u.edges){let a=t.get(d.from),h=t.get(d.to);a!==void 0&&Number.isFinite(a)&&h!==void 0&&a+d.weight<h-1e-12&&(r.has(d.to)||(r.add(d.to),s.push(d.to)),r.has(d.from)||(r.add(d.from),s.push(d.from)));}let c=0;for(;c<s.length;){let d=s[c];if(c+=1,!!d)for(let a of u.neighborsByNode.get(d)??[])r.has(a.id)||(r.add(a.id),s.push(a.id));}if(r.size>0)for(let d of r)t.set(d,Number.NEGATIVE_INFINITY),e.delete(d);let i=0;for(let d of u.nodes){let a=t.get(d);a!==void 0&&Number.isFinite(a)&&(i+=1);}return {distanceByNode:t,previousByNode:e,explored:i,negativeCycleNodes:r}}function z(u,n,t){let e=G(u),o=ce(t,e);return o==="dijkstra"?{algorithm:o,result:_e(u,n),hasNegativeWeights:e}:{algorithm:o,result:Le(u,n),hasNegativeWeights:e}}function ue(u){if(u===void 0||!Number.isFinite(u))return Math.random;let n=Math.floor(u)>>>0||1;return ()=>(n=n*1664525+1013904223>>>0,n/4294967296)}function O(u,n){let t=0;for(let e=0;e<u.length;e+=1){let o=(u[e]??0)-(n[e]??0);t+=o*o;}return t}function de(u,n){return Math.sqrt(O(u,n))}function je(u,n){let t=u[0]?.length??0;if(n==="none")return {normalize:i=>[...i],denormalize:i=>[...i]};let e=new Array(t).fill(0),o=new Array(t).fill(Number.POSITIVE_INFINITY),r=new Array(t).fill(Number.NEGATIVE_INFINITY);for(let i of u)for(let d=0;d<t;d+=1){let a=i[d]??0;e[d]=(e[d]??0)+a,o[d]=Math.min(o[d]??a,a),r[d]=Math.max(r[d]??a,a);}for(let i=0;i<t;i+=1)e[i]=(e[i]??0)/u.length;if(n==="minmax"){let i=r.map((d,a)=>{let h=o[a]??0;return d-h});return {normalize:d=>d.map((a,h)=>{let l=o[h]??0,m=i[h]??0;return Math.abs(m)<1e-12?0:(a-l)/m}),denormalize:d=>d.map((a,h)=>{let l=o[h]??0,m=i[h]??0;return a*m+l})}}let s=new Array(t).fill(0);for(let i of u)for(let d=0;d<t;d+=1){let a=(i[d]??0)-(e[d]??0);s[d]=(s[d]??0)+a*a;}let c=s.map(i=>Math.sqrt(i/u.length));return {normalize:i=>i.map((d,a)=>{let h=c[a]??0;return h<1e-12?0:(d-(e[a]??0))/h}),denormalize:i=>i.map((d,a)=>{let h=c[a]??0;return d*h+(e[a]??0)})}}function Ve(u,n,t,e){if(u.length===0)return [];if(!e){let c=[],i=new Set;for(;c.length<n;){let d=Math.floor(t()*u.length);i.has(d)||(i.add(d),c.push([...u[d]??u[0]??[]]));}return c}let o=[],r=new Set,s=Math.floor(t()*u.length);for(r.add(s),o.push([...u[s]??u[0]??[]]);o.length<n;){let c=new Array(u.length).fill(0),i=0;for(let l=0;l<u.length;l+=1){if(r.has(l))continue;let m=u[l];if(!m)continue;let N=Number.POSITIVE_INFINITY;for(let b of o)N=Math.min(N,O(m,b));c[l]=N,i+=N;}if(!Number.isFinite(i)||i<=0){for(let l=0;l<u.length;l+=1)if(!r.has(l)){r.add(l),o.push([...u[l]??u[0]??[]]);break}continue}let d=t()*i,a=0,h=-1;for(let l=0;l<u.length;l+=1)if(!r.has(l)&&(a+=c[l]??0,a>=d)){h=l;break}if(h<0){for(let l=0;l<u.length;l+=1)if(!r.has(l)){h=l;break}}if(h<0)break;r.add(h),o.push([...u[h]??u[0]??[]]);}return o}function qe(u,n,t,e,o,r){let s=u[0]?.length??0,c=Ve(u,n,t,r);if(c.length!==n)throw new E("INVALID_ARGUMENT","Unable to initialize centroids for k-means.");let i=new Array(u.length).fill(-1),d=0,a=false,h=Number.POSITIVE_INFINITY;for(;d<e;){d+=1;let l=false;h=0;for(let p=0;p<u.length;p+=1){let g=u[p];if(!g)continue;let f=0,I=Number.POSITIVE_INFINITY;for(let x=0;x<c.length;x+=1){let M=c[x];if(!M)continue;let w=O(g,M);w<I&&(I=w,f=x);}h+=I,i[p]!==f&&(i[p]=f,l=true);}let m=new Array(n),N=new Array(n).fill(0);for(let p=0;p<n;p+=1)m[p]=new Array(s).fill(0);for(let p=0;p<u.length;p+=1){let g=i[p];if(g<0)continue;let f=u[p],I=m[g];if(!(!f||!I)){N[g]=(N[g]??0)+1;for(let x=0;x<s;x+=1)I[x]=(I[x]??0)+(f[x]??0);}}for(let p=0;p<n;p+=1){let g=N[p]??0;if(g>0){let f=m[p];if(!f)continue;for(let I=0;I<s;I+=1)f[I]=(f[I]??0)/g;}else {let f=0,I=-1;for(let x=0;x<u.length;x+=1){let M=i[x],w=c[M],y=u[x];if(!w||!y)continue;let R=O(y,w);R>I&&(I=R,f=x);}m[p]=[...u[f]??u[0]??[]];}}let b=0;for(let p=0;p<n;p+=1){let g=c[p],f=m[p];!g||!f||(b=Math.max(b,Math.sqrt(O(g,f))));}if(c=m,!l||b<=o){a=true;break}}return {assignments:i,centroidsNormalized:c,iterations:d,inertia:h,converged:a}}function ze(u,n,t){if(u.length<=1||t<=1)return null;let e=Array.from({length:t},()=>[]);for(let s=0;s<n.length;s+=1){let c=n[s]??-1;if(c>=0&&c<t){let i=e[c];i&&i.push(s);}}let o=0,r=0;for(let s=0;s<u.length;s+=1){let c=u[s],i=n[s]??-1;if(!c||i<0||i>=t)continue;let d=e[i]??[];if(d.length<=1)continue;let a=0;for(let m of d){if(m===s)continue;let N=u[m];N&&(a+=de(c,N));}a/=d.length-1;let h=Number.POSITIVE_INFINITY;for(let m=0;m<t;m+=1){if(m===i)continue;let N=e[m]??[];if(N.length===0)continue;let b=0;for(let p of N){let g=u[p];g&&(b+=de(c,g));}b/=N.length,h=Math.min(h,b);}if(!Number.isFinite(h))continue;let l=Math.max(a,h);l<=0||(o+=(h-a)/l,r+=1);}return r===0?null:o/r}function W(u,n){let t=0;for(let e=1;e<u.length;e+=1){let o=u[e-1],r=u[e];if(!o||!r)return Number.POSITIVE_INFINITY;let s=n.get(o)?.get(r);if(s===void 0||!Number.isFinite(s))return Number.POSITIVE_INFINITY;t+=s;}return t}function Ke(u,n,t){if(u.length<4||t<=0)return u;let e=[...u],o=W(e,n);if(!Number.isFinite(o))return e;for(let r=0;r<t;r+=1){let s=false;for(let c=1;c<e.length-2;c+=1)for(let i=c+1;i<e.length-1;i+=1){let d=e.slice(0,c),a=e.slice(c,i+1).reverse(),h=e.slice(i+1),l=d.concat(a,h),m=W(l,n);m+1e-9<o&&(e=l,o=m,s=true);}if(!s)break}return e}function Ye(u,n){let t=[],e=new Set;n.start&&u.includes(n.start)&&(t.push(n.start),e.add(n.start));for(let c of n.startCandidates??[])u.includes(c)&&!e.has(c)&&(t.push(c),e.add(c));let o=ue(n.seed),r=u.filter(c=>!e.has(c));for(let c=r.length-1;c>0;c-=1){let i=Math.floor(o()*(c+1)),d=r[c],a=r[i];d!==void 0&&a!==void 0&&(r[c]=a,r[i]=d);}let s=Math.max(1,Math.min(u.length,n.multiStartCount??Math.min(8,u.length)));for(let c of r){if(t.length>=s)break;t.push(c);}return t.length===0&&u.length>0&&t.push(u[0]),t}function Ue(u,n,t,e){let o=[u],r=new Set(n.filter(c=>c!==u)),s=u;for(;r.size>0;){let c=null,i=Number.POSITIVE_INFINITY,d=t.get(s);for(let a of r){let h=d?.get(a)??Number.POSITIVE_INFINITY;h<i&&(i=h,c=a);}if(!c||!Number.isFinite(i))break;o.push(c),r.delete(c),s=c;}return e&&o.length>1&&o.push(u),o}function We(u,n){if(u.length<=1)return 0;let t=new Set;t.add(u[0]);let e=0;for(;t.size<u.length;){let o=Number.POSITIVE_INFINITY,r=null;for(let s of t){let c=n.get(s);for(let i of u){if(t.has(i))continue;let d=c?.get(i)??Number.POSITIVE_INFINITY;d<o&&(o=d,r=i);}}if(!r||!Number.isFinite(o))return Number.POSITIVE_INFINITY;t.add(r),e+=o;}return e}function v(u,n,t={}){let e=t.directed??true,o=q(u,n),r=ke(o,n,e),s=Ge(r),c=Te(o,s);return {nodes:o,neighborsByNode:s,incomingByNode:c,edges:Oe(s),directed:e}}function le(u,n,t={}){let e=v(u,n,t),o=[];if(e.directed){let s=0,c=new Map,i=new Map,d=[],a=new Set,h=l=>{c.set(l,s),i.set(l,s),s+=1,d.push(l),a.add(l);for(let m of e.neighborsByNode.get(l)??[])if(c.has(m.id)){if(a.has(m.id)){let N=i.get(l)??0,b=c.get(m.id)??0;i.set(l,Math.min(N,b));}}else {h(m.id);let N=i.get(l)??0,b=i.get(m.id)??0;i.set(l,Math.min(N,b));}if((i.get(l)??-1)===(c.get(l)??-2)){let m=[];for(;d.length>0;){let N=d.pop();if(!N||(a.delete(N),m.push(N),N===l))break}m.sort(P),o.push(m);}};for(let l of e.nodes)c.has(l)||h(l);}else {let s=new Set;for(let c of e.nodes){if(s.has(c))continue;let i=[c];s.add(c);let d=[],a=0;for(;a<i.length;){let h=i[a];if(a+=1,!!h){d.push(h);for(let l of e.neighborsByNode.get(h)??[])s.has(l.id)||(s.add(l.id),i.push(l.id));}}d.sort(P),o.push(d);}}o.sort((s,c)=>{let i=s[0]??"",d=c[0]??"";return P(i,d)});let r=new Map;for(let s=0;s<o.length;s+=1)for(let c of o[s]??[])r.set(c,s);return {components:o,componentByNode:r}}function $e(u,n){let t=v(u,n,{directed:false}),e=[],o=new Map,r=new Set;for(let s of t.nodes){if(r.has(s))continue;let c=[s];r.add(s);let i=[],d=0;for(;d<c.length;){let h=c[d];if(d+=1,!!h){i.push(h);for(let l of t.neighborsByNode.get(h)??[])r.has(l.id)||(r.add(l.id),c.push(l.id));}}i.sort(P);let a=e.length;e.push(i);for(let h of i)o.set(h,a);}e.sort((s,c)=>{let i=s[0]??"",d=c[0]??"";return P(i,d)}),o.clear();for(let s=0;s<e.length;s+=1){let c=e[s]??[];for(let i of c)o.set(i,s);}return {components:e,componentByNode:o}}function nt(u,n,t={}){let e=q(u,n),o=t.tieBreaker??((g,f)=>P(g,f)),r=t.priorityByNode,s=t.priority??(g=>r instanceof Map?r.get(g)??0:r?r[g]??0:0),c=(g,f)=>{let I=s(f)-s(g);return Math.abs(I)>1e-12?I:o(g,f)},i=new Map,d=new Map;for(let g of e)i.set(g,new Set),d.set(g,0);for(let g of n){if(g.from===g.to||!i.has(g.from)||!i.has(g.to))continue;let f=i.get(g.from);!f||f.has(g.to)||(f.add(g.to),d.set(g.to,(d.get(g.to)??0)+1));}let a=e.filter(g=>(d.get(g)??0)===0).sort(c),h=[];for(;a.length>0;){let g=a.shift();if(!g)continue;h.push(g);let f=Array.from(i.get(g)??[]).sort(c);for(let I of f){let x=(d.get(I)??0)-1;d.set(I,x),x===0&&a.push(I);}a.sort(c);}let l=new Set(h),m=e.filter(g=>!l.has(g));m.sort(c);let N=le(e,n,{directed:true}),b=new Set(n.filter(g=>g.from===g.to).map(g=>g.from)),p=new Set;for(let g of N.components)if(g.length>1)for(let f of g)p.add(f);else if(g.length===1){let f=g[0];f&&b.has(f)&&p.add(f);}for(let g of m)p.add(g);for(let g of m)l.has(g)||(h.push(g),l.add(g));return {order:h,cycleNodes:Array.from(p).sort(c),isDag:p.size===0}}function He(u,n,t={}){let e=v(u,n,t),o=e.nodes.length,r=o<=1?1:e.directed?2*(o-1):Math.max(1,o-1),s=new Map;for(let c of e.nodes){let i=e.neighborsByNode.get(c)?.length??0,d=e.incomingByNode.get(c)?.length??0,a=e.directed?d+i:i;s.set(c,{inDegree:d,outDegree:i,degree:a,normalized:a/r});}return s}function Xe(u,n,t={}){let e=v(u,n,t),o=e.nodes.length,r=Math.max(0,o-1),s=t.mode??"harmonic",c=new Map;for(let i of e.nodes){let d=z(e,i,t.shortestPathAlgorithm??"auto"),a=d.result.distanceByNode,h=d.result.negativeCycleNodes.size>0,l=0,m=0,N=0;for(let g of e.nodes){if(g===i)continue;let f=a.get(g);f===void 0||!Number.isFinite(f)||f<=0||(l+=1,m+=f,N+=1/f);}let b=0,p=0;h||(s==="classic"?(b=l>0&&m>0?l/m:0,p=r>0?b*(l/r):0):(b=N,p=r>0?N/r:0)),c.set(i,{reachableCount:l,distanceSum:m,score:b,normalized:p,mode:s,negativeCycle:h});}return c}function Je(u,n,t={}){let e=v(u,n,t);if(G(e))throw new E("NEGATIVE_WEIGHT","Betweenness centrality requires non-negative graph weights.");let o=new Map;for(let i of e.nodes)o.set(i,0);for(let i of e.nodes){let d=[],a=new Map,h=new Map,l=new Map,m=new C,N=new Set;for(let p of e.nodes)a.set(p,[]),h.set(p,0),l.set(p,Number.POSITIVE_INFINITY);for(h.set(i,1),l.set(i,0),m.push(i,0);m.size>0;){let p=m.pop();if(!p)break;let g=p.value;if(N.has(g))continue;N.add(g),d.push(g);let f=l.get(g);if(f===void 0||!Number.isFinite(f))continue;let I=h.get(g)??0;for(let x of e.neighborsByNode.get(g)??[]){let M=f+x.weight,w=l.get(x.id);w===void 0||M<w-1e-12?(l.set(x.id,M),m.push(x.id,M),h.set(x.id,I),a.set(x.id,[g])):w!==void 0&&Math.abs(M-w)<=1e-12&&(h.set(x.id,(h.get(x.id)??0)+I),a.get(x.id)?.push(g));}}let b=new Map;for(let p of e.nodes)b.set(p,0);for(;d.length>0;){let p=d.pop();if(!p)continue;let g=h.get(p)??0,f=b.get(p)??0;for(let I of a.get(p)??[]){let x=h.get(I)??0;if(g<=0)continue;let M=x/g*(1+f);b.set(I,(b.get(I)??0)+M);}p!==i&&o.set(p,(o.get(p)??0)+f);}}if(!e.directed)for(let i of e.nodes)o.set(i,(o.get(i)??0)/2);let r=e.nodes.length,s=r<=2?Number.POSITIVE_INFINITY:e.directed?(r-1)*(r-2):(r-1)*(r-2)/2,c=new Map;for(let i of e.nodes){let d=o.get(i)??0;c.set(i,{raw:d,normalized:Number.isFinite(s)&&s>0?d/s:0});}return c}function X(u,n,t={}){let e=v(u,n,t);if(G(e))throw new E("NEGATIVE_WEIGHT","PageRank requires non-negative graph weights.");let o=e.nodes.length;if(o===0)return {byNode:new Map,order:[],iterations:0,converged:true,dampingFactor:t.dampingFactor??.85};let r=t.dampingFactor??.85;if(!Number.isFinite(r)||r<0||r>=1)throw new E("INVALID_ARGUMENT",`PageRank dampingFactor must be in [0, 1), received ${r}.`);let s=Math.max(0,t.tolerance??1e-9),c=Math.max(1,Math.floor(t.maxIterations??200)),i=1/o,d=new Map;for(let g of e.nodes){let f=(e.neighborsByNode.get(g)??[]).reduce((I,x)=>I+x.weight,0);d.set(g,f);}let a=new Map;for(let g of e.nodes)a.set(g,i);let h=0,l=false;for(let g=0;g<c;g+=1){h=g+1;let f=new Map,I=(1-r)/o,x=0;for(let y of e.nodes)(d.get(y)??0)<=1e-12&&(x+=a.get(y)??0),f.set(y,I);let M=r*x/o;if(M>0)for(let y of e.nodes)f.set(y,(f.get(y)??0)+M);for(let y of e.nodes){let R=d.get(y)??0;if(R<=1e-12)continue;let S=a.get(y)??0;for(let A of e.neighborsByNode.get(y)??[]){let k=r*S*A.weight/R;f.set(A.id,(f.get(A.id)??0)+k);}}let w=0;for(let y of e.nodes){let R=a.get(y)??0,S=f.get(y)??0;w+=Math.abs(R-S);}if(a=f,w<=s){l=true;break}}let m=0;for(let g of e.nodes)m+=a.get(g)??0;if(!Number.isFinite(m)||m<=0){a=new Map;for(let g of e.nodes)a.set(g,i);m=1;}else if(Math.abs(m-1)>1e-12)for(let g of e.nodes)a.set(g,(a.get(g)??0)/m);let N=[...e.nodes].sort((g,f)=>{let I=(a.get(f)??0)-(a.get(g)??0);return Math.abs(I)>1e-12?I:P(g,f)}),b=N.length>0?a.get(N[0])??0:0,p=new Map;for(let g=0;g<N.length;g+=1){let f=N[g];if(!f)continue;let I=a.get(f)??0;p.set(f,{score:I,normalized:b>0?I/b:0,rank:g+1});}return {byNode:p,order:N,iterations:h,converged:l,dampingFactor:r}}function ot(u,n,t={}){let e=v(u,n,{directed:false}),o=t.tieBreaker??((l,m)=>P(l,m)),r=new Map;for(let l of e.edges){if(l.from===l.to)continue;let m=V(l.from,l.to),N=F(m.from,m.to),b=r.get(N);(!b||l.weight<b.weight-1e-12)&&r.set(N,{from:m.from,to:m.to,weight:l.weight});}let s=Array.from(r.values());s.sort((l,m)=>{let N=l.weight-m.weight;if(Math.abs(N)>1e-12)return N;let b=o(l.from,m.from);return b!==0?b:o(l.to,m.to)});let c=new Y(e.nodes),i=[],d=0;for(let l of s)c.union(l.from,l.to)&&(i.push(l),d+=l.weight);let a=new Set;for(let l of e.nodes)a.add(c.find(l));let h=a.size;return {edges:i,totalWeight:d,componentCount:h,spanning:e.nodes.length<=1||h===1&&i.length===e.nodes.length-1}}function Qe(u,n){let t=v(u,n,{directed:false}),e=new Map,o=new Map,r=new Map,s=new Set,c=new Map,i=0,d=h=>{e.set(h,i),o.set(h,i),i+=1;let l=0,m=r.get(h)??null;for(let N of t.neighborsByNode.get(h)??[]){let b=e.get(N.id);if(b===void 0){r.set(N.id,h),l+=1,d(N.id);let p=o.get(h)??0,g=o.get(N.id)??0;o.set(h,Math.min(p,g));let f=e.get(h)??0;if(m===null&&l>1&&s.add(h),m!==null&&g>=f&&s.add(h),g>f){let I=V(h,N.id),x=F(I.from,I.to),M=c.get(x);(!M||N.weight<M.weight-1e-12)&&c.set(x,{from:I.from,to:I.to,weight:N.weight});}}else if(N.id!==m){let p=o.get(h)??0;o.set(h,Math.min(p,b));}}};for(let h of t.nodes)e.has(h)||(r.set(h,null),d(h));let a=Array.from(c.values());return a.sort((h,l)=>{let m=P(h.from,l.from);if(m!==0)return m;let N=P(h.to,l.to);return N!==0?N:h.weight-l.weight}),{articulationPoints:Array.from(s).sort(P),bridges:a}}function rt(u,n,t={}){let e=le(u,n,t),o=$e(u,n),r=Qe(u,n),s=q(u,n),c=[...s].sort(P),i=new Map,d=new Map;try{i=Je(u,n,t);}catch(a){if(!(a instanceof E)||a.code!=="NEGATIVE_WEIGHT")throw a;i=new Map;for(let h of c)i.set(h,{raw:0,normalized:0});}try{d=X(u,n,{directed:t.directed??!0,...t.pageRankOptions??{}}).byNode;}catch(a){if(!(a instanceof E)||a.code!=="NEGATIVE_WEIGHT")throw a;let h=s.length>0?1/s.length:0;d=new Map;for(let l=0;l<c.length;l+=1){let m=c[l];m&&d.set(m,{score:h,normalized:1,rank:l+1});}}return {degree:He(u,n,t),closeness:Xe(u,n,{...t,mode:t.closenessMode??"harmonic",shortestPathAlgorithm:t.shortestPathAlgorithm}),betweenness:i,pageRank:d,stronglyConnectedComponents:e.components,weaklyConnectedComponents:o.components,articulationPoints:r.articulationPoints,bridges:r.bridges}}function j(u,n,t,e,o={}){let r=v(u,n,o);if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,target:e,path:[],distance:Number.POSITIVE_INFINITY,reachable:false,explored:0,algorithm:o.algorithm??"auto",hasNegativeWeights:G(r),negativeCycle:false};let s=z(r,t,o.algorithm??"auto");if(s.result.negativeCycleNodes.has(e)){if(o.failOnNegativeCycle??false)throw new E("NEGATIVE_CYCLE",`Negative cycle reaches target node "${e}".`);return {source:t,target:e,path:[],distance:Number.NEGATIVE_INFINITY,reachable:false,explored:s.result.explored,algorithm:s.algorithm,hasNegativeWeights:s.hasNegativeWeights,negativeCycle:true}}let c=s.result.distanceByNode.get(e)??Number.POSITIVE_INFINITY,i=Number.isFinite(c),d=i?$(t,e,s.result.previousByNode):[];return {source:t,target:e,path:d,distance:c,reachable:i&&d.length>0,explored:s.result.explored,algorithm:s.algorithm,hasNegativeWeights:s.hasNegativeWeights,negativeCycle:s.result.negativeCycleNodes.size>0}}function he(u,n,t={}){let e=v(u,n,{directed:t.directed??false});if(G(e))throw new E("NEGATIVE_WEIGHT","Label propagation requires non-negative graph weights.");let o=t.tieBreaker??((a,h)=>P(a,h)),r=[...e.nodes].sort(o),s=new Map;for(let a=0;a<r.length;a+=1){let h=r[a];h&&s.set(h,a);}let c=Math.max(1,t.maxIterations??100),i=0,d=false;for(let a=0;a<c;a+=1){i=a+1;let h=false;for(let l of r){let m=e.neighborsByNode.get(l)??[];if(m.length===0)continue;let N=new Map;for(let f of m){let I=s.get(f.id);I!==void 0&&N.set(I,(N.get(I)??0)+f.weight);}let b=s.get(l);if(b===void 0||N.size===0)continue;let p=b,g=Number.NEGATIVE_INFINITY;for(let[f,I]of N.entries()){if(I>g+1e-12){g=I,p=f;continue}Math.abs(I-g)<=1e-12&&f<p&&(p=f);}p!==b&&(s.set(l,p),h=true);}if(!h){d=true;break}}return U(e,s,i,d,"label-propagation")}function me(u,n,t={}){let e=v(u,n,{directed:t.directed??false});if(G(e))throw new E("NEGATIVE_WEIGHT","Louvain requires non-negative graph weights.");let o=t.tieBreaker??((N,b)=>P(N,b)),r=[...e.nodes].sort(o),s=Math.max(1,t.maxPasses??32),c=Math.max(0,t.tolerance??1e-9),i=new Map;for(let N of e.nodes)i.set(N,ae(e.neighborsByNode.get(N)??[]));let d=0;for(let N of i.values())d+=N;let a=new Map,h=new Map;for(let N=0;N<r.length;N+=1){let b=r[N];if(!b)continue;a.set(b,N);let p=i.get(b)??0;h.set(N,p);}if(d<=1e-12)return U(e,a,0,true,"louvain");let l=0,m=false;for(let N=0;N<s;N+=1){l=N+1;let b=false;for(let p of r){let g=a.get(p);if(g===void 0)continue;let f=i.get(p)??0;h.set(g,(h.get(g)??0)-f);let I=new Map;for(let w of e.neighborsByNode.get(p)??[]){let y=a.get(w.id);y!==void 0&&I.set(y,(I.get(y)??0)+w.weight);}let x=g,M=0;for(let[w,y]of I.entries()){let R=h.get(w)??0,S=y-R*f/d;if(S>M+c){M=S,x=w;continue}Math.abs(S-M)<=c&&w<x&&(x=w);}x!==g&&M>c?(a.set(p,x),h.set(x,(h.get(x)??0)+f),b=true):h.set(g,(h.get(g)??0)+f);}if(!b){m=true;break}}return U(e,a,l,m,"louvain")}function ge(u,n,t={}){let e=v(u,n,{directed:t.directed??false}),o=t.metric??"jaccard",r=t.minScore??0,s=H(e),c=[...e.nodes].sort(P),i=[];for(let d=0;d<c.length;d+=1){let a=c[d];if(a)for(let h=d+1;h<c.length;h+=1){let l=c[h];if(!l)continue;let m=L(a,l,s,o);m<r-1e-12||i.push({left:a,right:l,score:m,metric:o});}}return i.sort((d,a)=>{let h=a.score-d.score;if(Math.abs(h)>1e-12)return h;let l=P(d.left,a.left);return l!==0?l:P(d.right,a.right)}),{metric:o,pairs:i.map((d,a)=>({...d,rank:a+1}))}}function fe(u,n,t={}){let e=v(u,n,{directed:t.directed??false}),o=t.metric??"jaccard",r=t.minScore??0,s=Math.max(1,Math.floor(t.k??5)),c=H(e),i=[...e.nodes].sort(P),d=new Map;for(let a of i){let h=[];for(let l of i){if(a===l)continue;let m=L(a,l,c,o);m<r-1e-12||h.push({nodeId:l,score:m});}h.sort((l,m)=>{let N=m.score-l.score;return Math.abs(N)>1e-12?N:P(l.nodeId,m.nodeId)}),d.set(a,h.slice(0,s));}return {metric:o,k:s,neighborsByNode:d}}function J(u,n,t={}){let e=t.directed??false,o=v(u,n,{directed:e}),r=t.metric??"jaccard",s=t.minScore??0,c=Math.max(1,Math.floor(t.limit??20)),i=t.allowExistingEdges??false,d=H(o),a=[...o.nodes].sort(P),h=t.sourceFilter?new Set(t.sourceFilter):null,l=t.targetFilter?new Set(t.targetFilter):null,m=new Set;for(let b of o.edges)e?m.add(`${b.from}\0${b.to}`):m.add(F(b.from,b.to));let N=[];if(e){for(let b of a)if(!(h&&!h.has(b)))for(let p of a){if(b===p||l&&!l.has(p)||!i&&m.has(`${b}\0${p}`))continue;let g=L(b,p,d,r);g<s-1e-12||N.push({from:b,to:p,score:g,metric:r});}}else for(let b=0;b<a.length;b+=1){let p=a[b];if(p)for(let g=b+1;g<a.length;g+=1){let f=a[g];if(!f||h&&!h.has(p)&&!h.has(f)||l&&!l.has(p)&&!l.has(f)||!i&&m.has(F(p,f)))continue;let I=L(p,f,d,r);I<s-1e-12||N.push({from:p,to:f,score:I,metric:r});}}return N.sort((b,p)=>{let g=p.score-b.score;if(Math.abs(g)>1e-12)return g;let f=P(b.from,p.from);return f!==0?f:P(b.to,p.to)}),{metric:r,predictions:N.slice(0,c).map((b,p)=>({...b,rank:p+1}))}}function it(u,n,t={}){return J(u,n,t)}function Ie(u,n,t,e,o={}){let r=v(u,n,o);if(G(r))throw new E("NEGATIVE_WEIGHT","A* requires non-negative graph weights.");if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,target:e,path:[],distance:Number.POSITIVE_INFINITY,reachable:false,explored:0,estimatedDistance:Number.POSITIVE_INFINITY};let s=o.heuristic??((p,g)=>0),c=new C,i=new Map,d=new Map,a=new Set;for(let p of r.nodes)i.set(p,Number.POSITIVE_INFINITY);i.set(t,0);let h=s(t,e);if(!Number.isFinite(h))throw new E("INVALID_ARGUMENT","A* heuristic must return finite values.");c.push(t,h);let l=0;for(;c.size>0;){let p=c.pop();if(!p)break;let g=p.value;if(a.has(g))continue;if(a.add(g),l+=1,g===e)break;let f=i.get(g);if(!(f===void 0||!Number.isFinite(f)))for(let I of r.neighborsByNode.get(g)??[]){let x=f+I.weight,M=i.get(I.id)??Number.POSITIVE_INFINITY;if(x>=M-1e-12)continue;let w=s(I.id,e);if(!Number.isFinite(w))throw new E("INVALID_ARGUMENT","A* heuristic must return finite values.");i.set(I.id,x),d.set(I.id,g),c.push(I.id,x+w);}}let m=i.get(e)??Number.POSITIVE_INFINITY,N=Number.isFinite(m),b=N?$(t,e,d):[];return {source:t,target:e,path:b,distance:m,reachable:N&&b.length>0,explored:l,estimatedDistance:m}}function Ne(u,n,t={}){let e=v(u,n,t),o=t.algorithm??"auto",r=ce(o,G(e)),s=new Map,c=new Map,i=false;for(let d of e.nodes){let a=z(e,d,r);if(a.result.negativeCycleNodes.size>0&&(i=true,t.failOnNegativeCycle??false))throw new E("NEGATIVE_CYCLE",`Negative cycle reaches source node "${d}".`);let h=new Map,l=new Map;for(let m of e.nodes){h.set(m,a.result.distanceByNode.get(m)??Number.POSITIVE_INFINITY);let N=a.result.previousByNode.get(m);N&&l.set(m,N);}s.set(d,h),c.set(d,l);}return {nodes:e.nodes,distanceBySource:s,previousBySource:c,algorithm:r,hasNegativeWeights:G(e),negativeCycle:i}}function pe(u,n,t,e,o={}){let r=o.directed??true,s=Math.max(1,Math.floor(o.k??3)),c=o.shortestPathAlgorithm??"auto",i=v(u,n,{directed:r});if(!i.neighborsByNode.has(t)||!i.neighborsByNode.has(e))return {source:t,target:e,paths:[],complete:false};let d=j(i.nodes,i.edges,t,e,{directed:r,algorithm:c});if(!d.reachable||d.path.length===0)return {source:t,target:e,paths:[],complete:false};let a=[{path:d.path,distance:d.distance}],h=new Map;for(let l=1;l<s;l+=1){let m=a[l-1]?.path??[];if(m.length<2)break;for(let b=0;b<m.length-1;b+=1){let p=m[b];if(!p)continue;let g=m.slice(0,b+1),f=new Set(g.slice(0,-1)),I=new Set;for(let S of a)if(S.path.length>b+1&&De(S.path,g)){let A=S.path[b],k=S.path[b+1];A&&k&&I.add(`${A}\0${k}`);}let x=i.edges.filter(S=>!(I.has(`${S.from}\0${S.to}`)||f.has(S.from)||f.has(S.to))),M=j(i.nodes,x,p,e,{directed:r,algorithm:c});if(!M.reachable||M.path.length===0)continue;let w=g.slice(0,-1).concat(M.path),y=Be(w,i);if(!Number.isFinite(y))continue;let R=_(w);h.has(R)||h.set(R,{path:w,distance:y});}if(h.size===0)break;let N=Array.from(h.values()).sort((b,p)=>{let g=b.distance-p.distance;return Math.abs(g)>1e-12?g:P(_(b.path),_(p.path))})[0];if(!N)break;a.push(N),h.delete(_(N.path));}return {source:t,target:e,paths:a,complete:a.length>=s}}function be(u,n,t,e,o={}){let r=v(u,n,o);if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,sink:e,maxFlow:0,augmentations:0,flowByEdge:[],sourcePartition:[],sinkPartition:[],cutEdges:[]};let s=[];for(let I of r.edges){if(I.weight<0)throw new E("NEGATIVE_WEIGHT","Maximum flow requires non-negative capacities.");s.push(I);}let c=new Map;for(let I of r.nodes)c.set(I,[]);let i=(I,x,M,w)=>{let y=c.get(I),R=c.get(x);if(!y||!R)return;let S=y.length,A=R.length;y.push({to:x,reverseIndex:A,capacity:M,originalIndex:w}),R.push({to:I,reverseIndex:S,capacity:0,originalIndex:null});};for(let I=0;I<s.length;I+=1){let x=s[I];x&&i(x.from,x.to,x.weight,I);}let d=0,a=0,h=new Array(s.length).fill(0);for(;;){let I=new Map,x=[t];I.set(t,{from:t,edgeIndex:-1});let M=0;for(;M<x.length&&!I.has(e);){let R=x[M];if(M+=1,!R)continue;let S=c.get(R)??[];for(let A=0;A<S.length;A+=1){let k=S[A];!k||k.capacity<=1e-12||I.has(k.to)||(I.set(k.to,{from:R,edgeIndex:A}),x.push(k.to));}}if(!I.has(e))break;let w=Number.POSITIVE_INFINITY,y=e;for(;y!==t;){let R=I.get(y);if(!R){w=0;break}let S=c.get(R.from)?.[R.edgeIndex];if(!S){w=0;break}w=Math.min(w,S.capacity),y=R.from;}if(!Number.isFinite(w)||w<=1e-12)break;for(y=e;y!==t;){let R=I.get(y);if(!R)break;let S=c.get(R.from)?.[R.edgeIndex],A=S?c.get(S.to)?.[S.reverseIndex]:void 0;if(!S||!A)break;S.capacity-=w,A.capacity+=w,S.originalIndex!==null?h[S.originalIndex]=(h[S.originalIndex]??0)+w:A.originalIndex!==null&&(h[A.originalIndex]=(h[A.originalIndex]??0)-w),y=R.from;}d+=w,a+=1;}let l=new Set,m=[t];l.add(t);let N=0;for(;N<m.length;){let I=m[N];if(N+=1,!!I)for(let x of c.get(I)??[])x.capacity<=1e-12||l.has(x.to)||(l.add(x.to),m.push(x.to));}let b=r.nodes.filter(I=>l.has(I)).sort(P),p=r.nodes.filter(I=>!l.has(I)).sort(P),g=s.filter(I=>l.has(I.from)&&!l.has(I.to)).sort((I,x)=>{let M=P(I.from,x.from);return M!==0?M:P(I.to,x.to)}),f=s.map((I,x)=>({from:I.from,to:I.to,flow:Math.max(0,h[x]??0),capacity:I.weight})).sort((I,x)=>{let M=P(I.from,x.from);return M!==0?M:P(I.to,x.to)});return {source:t,sink:e,maxFlow:d,augmentations:a,flowByEdge:f,sourcePartition:b,sinkPartition:p,cutEdges:g}}function xe(u,n,t,e,o={}){let r=n.map(f=>({from:f.from,to:f.to,weight:f.capacity})),s=q(u,r),c=o.directed??true;if(!s.includes(t)||!s.includes(e))return {source:t,sink:e,flow:0,cost:0,complete:false,augmentations:0,flowByEdge:[]};let i=[];for(let f of n){if(!Number.isFinite(f.capacity)||f.capacity<0)throw new E("INVALID_ARGUMENT",`Flow edge capacity must be non-negative and finite, received ${f.capacity}.`);let I=f.cost??0;if(!Number.isFinite(I))throw new E("INVALID_ARGUMENT",`Flow edge cost must be finite, received ${I}.`);i.push({from:f.from,to:f.to,capacity:f.capacity,cost:I}),c||i.push({from:f.to,to:f.from,capacity:f.capacity,cost:I});}let d=new Map;for(let f of s)d.set(f,[]);let a=(f,I,x,M,w)=>{let y=d.get(f),R=d.get(I);if(!y||!R)return;let S=y.length,A=R.length;y.push({to:I,reverseIndex:A,capacity:x,cost:M,originalIndex:w}),R.push({to:f,reverseIndex:S,capacity:0,cost:-M,originalIndex:null});};for(let f=0;f<i.length;f+=1){let I=i[f];I&&a(I.from,I.to,I.capacity,I.cost,f);}let h=o.targetFlow,l=h===void 0?Number.POSITIVE_INFINITY:h;if(!Number.isFinite(l)&&l!==Number.POSITIVE_INFINITY)throw new E("INVALID_ARGUMENT",`targetFlow must be finite or omitted, received ${l}.`);if(l<0)throw new E("INVALID_ARGUMENT",`targetFlow must be non-negative, received ${l}.`);let m=0,N=0,b=0,p=new Array(i.length).fill(0);for(;m<l-1e-12;){let f=new Map,I=new Map;for(let y of s)f.set(y,Number.POSITIVE_INFINITY);f.set(t,0);for(let y=0;y<s.length-1;y+=1){let R=false;for(let S of s){let A=f.get(S);if(A===void 0||!Number.isFinite(A))continue;let k=d.get(S)??[];for(let D=0;D<k.length;D+=1){let T=k[D];if(!T||T.capacity<=1e-12)continue;let ie=A+T.cost,Ee=f.get(T.to)??Number.POSITIVE_INFINITY;ie<Ee-1e-12&&(f.set(T.to,ie),I.set(T.to,{from:S,edgeIndex:D}),R=true);}}if(!R)break}let x=f.get(e)??Number.POSITIVE_INFINITY;if(!Number.isFinite(x))break;let M=Math.min(l-m,Number.POSITIVE_INFINITY),w=e;for(;w!==t;){let y=I.get(w);if(!y){M=0;break}let R=d.get(y.from)?.[y.edgeIndex];if(!R){M=0;break}M=Math.min(M,R.capacity),w=y.from;}if(!Number.isFinite(M)||M<=1e-12)break;for(w=e;w!==t;){let y=I.get(w);if(!y)break;let R=d.get(y.from)?.[y.edgeIndex],S=R?d.get(R.to)?.[R.reverseIndex]:void 0;if(!R||!S)break;R.capacity-=M,S.capacity+=M,R.originalIndex!==null?p[R.originalIndex]=(p[R.originalIndex]??0)+M:S.originalIndex!==null&&(p[S.originalIndex]=(p[S.originalIndex]??0)-M),w=y.from;}m+=M,N+=M*x,b+=1;}let g=i.map((f,I)=>({from:f.from,to:f.to,flow:Math.max(0,p[I]??0),capacity:f.capacity,cost:f.cost})).sort((f,I)=>{let x=P(f.from,I.from);return x!==0?x:P(f.to,I.to)});return {source:t,sink:e,flow:m,cost:N,complete:l===Number.POSITIVE_INFINITY?true:m>=l-1e-12,augmentations:b,flowByEdge:g}}function Ze(u,n,t={}){if(!Array.isArray(u)||u.length===0)throw new E("INVALID_ARGUMENT","kMeansClustering requires at least one point.");let e=u[0]?.length??0;if(e===0)throw new E("INVALID_ARGUMENT","kMeansClustering requires points with at least one axis.");for(let g of u)if(g.length!==e)throw new E("INVALID_ARGUMENT","kMeansClustering requires all points to share dimensionality.");let o=Math.max(1,Math.min(u.length,Math.floor(n))),r=Math.max(1,t.maxIterations??120),s=Math.max(0,t.tolerance??1e-5),c=Math.max(1,t.nInit??8),i=t.normalization??"zscore",d=t.useKMeansPlusPlus??true,a=t.seed===void 0||!Number.isFinite(t.seed)?Math.floor(Math.random()*4294967295):Math.floor(t.seed),h=je(u,i),l=u.map(g=>h.normalize(g)),m=null,N=a;for(let g=0;g<c;g+=1){let f=a+g*2654435761>>>0,I=ue(f),x=qe(l,o,I,r,s,d);(!m||x.inertia<m.inertia-1e-9)&&(m=x,N=f);}if(!m)throw new E("INVALID_ARGUMENT","kMeansClustering failed to produce a valid run.");let b=[];for(let g=0;g<o;g+=1){let f=[];for(let I=0;I<m.assignments.length;I+=1)m.assignments[I]===g&&f.push(I);b.push({centroid:h.denormalize(m.centroidsNormalized[g]??[]),indices:f});}let p=ze(l,m.assignments,o);return {assignments:m.assignments,clusters:b,iterations:m.iterations,inertia:m.inertia,converged:m.converged,silhouette:p,selectedSeed:N}}function st(u,n={}){if(!Array.isArray(u)||u.length===0)throw new E("INVALID_ARGUMENT","kMeansAuto requires at least one point.");let t=Math.max(2,n.kMin??2),e=Math.max(t,Math.min(u.length,n.kMax??Math.min(10,Math.ceil(Math.sqrt(u.length))+2))),o=null,r=t,s=[];for(let c=t;c<=e;c+=1){let i=Ze(u,c,{...n,seed:n.seed===void 0||!Number.isFinite(n.seed)?void 0:n.seed+c*997});if(s.push({k:c,silhouette:i.silhouette,inertia:i.inertia}),!o){o=i,r=c;continue}let d=i.silhouette??Number.NEGATIVE_INFINITY,a=o.silhouette??Number.NEGATIVE_INFINITY;if(d>a+1e-9){o=i,r=c;continue}Math.abs(d-a)<=1e-9&&i.inertia<o.inertia-1e-9&&(o=i,r=c);}if(!o)throw new E("INVALID_ARGUMENT","kMeansAuto failed to choose a cluster configuration.");return {...o,selectedK:r,candidates:s}}function dt(u,n,t={}){let e=v(u,n,t);if(e.nodes.length===0)return {order:[],distance:0,segments:[],visitedCount:0,complete:true,unreachableNodes:[],lowerBound:0,optimalityGap:0};if(G(e))throw new E("NEGATIVE_WEIGHT","TSP approximation requires non-negative graph weights.");let o=t.returnToStart??true,r=Math.max(0,t.twoOptPasses??3),s=Ye(e.nodes,t),c=new Map,i=new Map;for(let f of e.nodes){let I=z(e,f,t.shortestPathAlgorithm??"auto");if(I.result.negativeCycleNodes.size>0)throw new E("NEGATIVE_CYCLE","TSP approximation cannot run when negative cycles are reachable.");c.set(f,I.result.distanceByNode),i.set(f,I.result.previousByNode);}let d=[],a=Number.POSITIVE_INFINITY;for(let f of s){let x=Ue(f,e.nodes,c,o);x.length>2&&(x=Ke(x,c,r));let M=W(x,c);M<a&&(a=M,d=x);}let h=new Set(d),l=e.nodes.filter(f=>!h.has(f)),m=l.length===0&&Number.isFinite(a),N=[],b=0;for(let f=1;f<d.length;f+=1){let I=d[f-1],x=d[f];if(!I||!x)continue;let M=c.get(I)?.get(x)??Number.POSITIVE_INFINITY,w=i.get(I)??new Map,y=$(I,x,w);!Number.isFinite(M)||y.length===0||(b+=M,N.push({from:I,to:x,distance:M,path:y}));}m||(b=Number.POSITIVE_INFINITY);let p=We(e.nodes,c),g=m&&Number.isFinite(b)&&Number.isFinite(p)&&p>0?(b-p)/p:null;return {order:d,distance:b,segments:N,visitedCount:h.size,complete:m,unreachableNodes:l,lowerBound:p,optimalityGap:g}}var Q=class{projections=new Map;project(n,t,e,o={}){let r=n.trim();if(!r)throw new E("INVALID_ARGUMENT","Projection name must be non-empty.");if(this.projections.has(r)&&!(o.replace??false))throw new E("INVALID_ARGUMENT",`Projection "${r}" already exists.`);let s=o.directed??true,c=v(t,e,{directed:s}),i={name:r,nodes:c.nodes,edges:c.edges,directed:s,createdAt:Date.now(),metadata:o.metadata??{}};return this.projections.set(r,i),i}drop(n){return this.projections.delete(n)}clear(){this.projections.clear();}exists(n){return this.projections.has(n)}get(n){return this.projections.get(n)??null}list(){return Array.from(this.projections.values()).map(n=>({name:n.name,nodeCount:n.nodes.length,edgeCount:n.edges.length,directed:n.directed,createdAt:n.createdAt,metadata:n.metadata})).sort((n,t)=>n.name.localeCompare(t.name))}pageRank(n,t={}){let e=this.requireProjection(n);return X(e.nodes,e.edges,{directed:e.directed,...t})}louvain(n,t={}){let e=this.requireProjection(n);return me(e.nodes,e.edges,{directed:e.directed,...t})}labelPropagation(n,t={}){let e=this.requireProjection(n);return he(e.nodes,e.edges,{directed:e.directed,...t})}similarity(n,t={}){let e=this.requireProjection(n);return ge(e.nodes,e.edges,{directed:e.directed,...t})}knn(n,t={}){let e=this.requireProjection(n);return fe(e.nodes,e.edges,{directed:e.directed,...t})}linkPrediction(n,t={}){let e=this.requireProjection(n);return J(e.nodes,e.edges,{directed:e.directed,...t})}shortestPath(n,t,e,o={}){let r=this.requireProjection(n);return j(r.nodes,r.edges,t,e,{directed:r.directed,...o})}aStar(n,t,e,o={}){let r=this.requireProjection(n);return Ie(r.nodes,r.edges,t,e,{directed:r.directed,...o})}yen(n,t,e,o={}){let r=this.requireProjection(n);return pe(r.nodes,r.edges,t,e,{directed:r.directed,...o})}allPairs(n,t={}){let e=this.requireProjection(n);return Ne(e.nodes,e.edges,{directed:e.directed,...t})}maxFlow(n,t,e,o={}){let r=this.requireProjection(n);return be(r.nodes,r.edges,t,e,{directed:r.directed,...o})}minCostMaxFlow(n,t,e,o=null,r={}){let s=this.requireProjection(n),c=o??s.edges.map(i=>({from:i.from,to:i.to,capacity:i.weight,cost:i.weight}));return xe(s.nodes,c,t,e,{directed:s.directed,...r})}runPipeline(n,t){let e=new Map;for(let o of t){let r=o.id.trim();if(!r)throw new E("INVALID_ARGUMENT","Pipeline step id must be non-empty.");if(e.has(r))throw new E("INVALID_ARGUMENT",`Duplicate pipeline step id "${r}".`);if(o.kind==="page-rank"){e.set(r,this.pageRank(n,o.options));continue}if(o.kind==="louvain"){e.set(r,this.louvain(n,o.options));continue}if(o.kind==="label-propagation"){e.set(r,this.labelPropagation(n,o.options));continue}if(o.kind==="similarity"){e.set(r,this.similarity(n,o.options));continue}if(o.kind==="knn"){e.set(r,this.knn(n,o.options));continue}if(o.kind==="link-prediction"){e.set(r,this.linkPrediction(n,o.options));continue}if(o.kind==="shortest-path"){e.set(r,this.shortestPath(n,o.source,o.target,o.options));continue}if(o.kind==="a-star"){e.set(r,this.aStar(n,o.source,o.target,o.options));continue}if(o.kind==="yen-k-shortest-paths"){e.set(r,this.yen(n,o.source,o.target,o.options));continue}if(o.kind==="max-flow"){e.set(r,this.maxFlow(n,o.source,o.sink,o.options));continue}if(o.kind==="min-cost-max-flow"){e.set(r,this.minCostMaxFlow(n,o.source,o.sink,o.edges??null,o.options));continue}if(o.kind==="all-pairs-shortest-paths"){e.set(r,this.allPairs(n,o.options));continue}let s=o;throw new E("INVALID_ARGUMENT",`Unsupported pipeline step kind "${String(s)}".`)}return e}requireProjection(n){let t=this.projections.get(n);if(!t)throw new E("INVALID_ARGUMENT",`Projection "${n}" does not exist.`);return t}};function et(){return new Q}var ut=et();var B=100,tt=0,re=1e3,Z=1.5,Me=1,ye=2,K=2;function we(u=B){return Math.floor(Math.random()*u)}function Re(u=re,n=B){let t=[];for(let e=0;e<u;e++)t.push(we(n));return t}function Se(u=K,n=B){let t=[];for(let e=0;e<u;e++)t.push(Math.floor(Math.random()*(n/10)%n));return t}function ee(u=re,n=K,t=B){let e=[];for(let o=0;o<u;o++)e.push(Se(n,t));return e}var te=class{data;constructor(n={}){this.data={original:n.data??Re()};}sorted(){return this.data.sorted||(this.data.sorted=this.getSorted(this.data.original)),this.data.sorted}getSorted(n){return [...n].sort((t,e)=>t>e?1:t===e?0:-1)}median(){return this.sorted(),this.data.median===void 0&&(this.data.median=this.getMedian(this.data.sorted)),this.data.medianDepth===void 0&&(this.data.medianDepth=this.getMedianDepth(this.data.sorted)),{datum:this.data.median,depth:this.data.medianDepth}}getMedianDepth(n,t=0){return n.length?t+(n.length+1)/2:NaN}getMedian(n){let t=n.length;if(!t)return NaN;if(t===1)return n[0];let e=Math.floor(t/2);return t%2===0?(n[e-1]+n[e])/2:n[e]}mean(){return this.data.mean===void 0&&(this.data.mean=this.getMean(this.data.original)),this.data.mean}getMean(n){if(!n.length)return NaN;let t=0;for(let e of n)t+=e;return t/n.length}mode(){return this.data.mode||(this.sorted(),this.data.mode=this.getMode(this.data.sorted)),this.data.mode}getMode(n){if(!n.length)return {count:0,data:[]};let t={},e=0;for(let r of n)t[r]=(t[r]||0)+1,t[r]>e&&(e=t[r]);let o=[];for(let[r,s]of Object.entries(t))s===e&&o.push(Number(r));return {count:e,data:o.sort((r,s)=>r-s)}}extremes(){return this.data.extremes||(this.sorted(),this.data.extremes=this.getExtremes(this.data.sorted)),this.data.extremes}getExtremes(n){return n.length?[n[0],n[n.length-1]]:[]}counts(){return this.data.counts||(this.sorted(),this.data.counts=this.getCounts(this.data.sorted)),this.data.counts}getCounts(n){let t=new Map;for(let o of n)t.set(o,(t.get(o)||0)+1);let e=[];for(let[o,r]of t)e.push([o,r]);return e.sort((o,r)=>o[0]-r[0])}hinges(){return this.data.hinges||(this.sorted(),this.data.hinges=this.getHinges(this.data.sorted)),this.data.hinges}getHinges(n,t=2,e=[]){let o=[...n],r=o.length,s=t;if(s%2!==0&&s++,r<=s||s<=0)return e;let c=Math.floor(r/s),i=Math.floor(r/c)-1;for(let d=0;d<=i;d++){let a=o.slice(d*c,d*c+c);e.push({datum:this.getMedian(a),depth:this.getMedianDepth(a,d*c)});}return e}iqr(){return this.data.iqr===void 0&&(this.hinges(),this.data.iqr=this.getIQR(this.data.hinges)),this.data.iqr}getIQR(n){let t=n[0]?.datum,e=n[1]?.datum;return t===void 0||e===void 0?NaN:Math.abs(t-e)}fences(){return this.data.fences||(this.median(),this.iqr(),this.data.fences=this.getFences()),this.data.fences}getFences(n=Z){let t=this.data.median,e=this.data.iqr;if(t===void 0||e===void 0||isNaN(e))return [];let o=e*n;return [t-o,t+o]}outer(){return this.data.outer||(this.median(),this.iqr(),this.data.outer=this.getOuter()),this.data.outer}getOuter(n=Z){let t=this.data.median,e=this.data.iqr;if(t===void 0||e===void 0||isNaN(e))return [];let o=2*e*n;return [t-o,t+o]}outside(){return this.data.outside||(this.outer(),this.data.outside=this.getOutside()),this.data.outside}getOutside(){let n=[],t=this.data.sorted,e=this.data.outer;if(!e||e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let s of t)(s>r||s<o)&&n.push(s);return n}inside(){return this.data.inside||(this.fences(),this.data.inside=this.getInside()),this.data.inside}getInside(){let n=[],t=this.data.sorted,e=this.data.fences;if(!e||e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let s of t)s<r&&s>o&&n.push(s);return n}outliers(){return this.data.outliers||(this.fences(),this.data.outliers=this.getOutliers()),this.data.outliers}getOutliers(){let n=[],t=this.data.sorted,e=this.data.fences;if(e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let s of t)(s>r||s<o)&&n.push(s);return n}ranked(){return this.data.ranked||(this.sorted(),this.data.ranked=this.getRanked(this.data.sorted)),this.data.ranked}getRanked(n,t=true){let e={},o={},r=n.length,s=[],c=NaN,i=[],d=()=>{c=NaN,i=[];};for(let h=0;h<n.length;h++){let l=n[h];if(!t)e[l]={rank:h+1,peers:0},o[l]={rank:r-h,peers:0};else {let m=h+1,N=h-1;l===n[N]?(!isNaN(c)&&i.length===0?(i.push(l),s.push(i),d()):(i.push(l),c=N),l!==n[m]&&(s.push(i),d())):l!==n[m]?i.length>0?(s.push(i),d()):s.push(l):i.push(l);}}let a=0;for(let h=0;h<s.length;h++){let l=s[h];if(typeof l=="number")o[l]={rank:h+1+a,peers:0},e[l]={rank:r-h-a,peers:0};else if(Array.isArray(l)){a+=l.length;let m=l[0];o[m]={rank:h+1+a,peers:l.length},e[m]={rank:r-h-a,peers:l.length};}else a+=1;}return {up:e,down:o,groups:{down:[...s],up:[...s].reverse()}}}adjacent(){return this.data.adjacent||(this.fences(),this.data.adjacent=this.getAdjacent(this.data.sorted,this.data.fences)),this.data.adjacent}getAdjacent(n,t){if(t.length===0)return [];let e=t[0],o=[],r=t[1],s=[];for(let c of n)c>e&&o.push(c),c<r&&s.push(c);return o.sort((c,i)=>c-i),s.sort((c,i)=>c-i),[o[0],s[s.length-1]]}binned(n=NaN){return this.data.binned||(this.sorted(),this.extremes(),this.data.binned=this.getBinned(this.data.sorted,n)),this.data.binned}getBinned(n,t=10,e=NaN,o=true){let r={},s=n.length,c=o?0:1;if(s===0)return {bins:0,width:NaN,binned:{}};let i=this.data.extremes,d=e;if(i&&isNaN(d)&&i.length===2){d=(i[1]-i[0])/(Math.log(n.length)/Math.LN2),d=Math.floor(d);let h=true;for(let l of n)if(l%1!==0){h=false;break}h&&(d=Math.floor(d));}let a=Math.floor(i[1]/d)+1;(!a||a<1)&&(a=1);for(let h of n){let l=Math.floor((h-c)/d);r[l]||(r[l]={from:l*d+c,to:(l+1)*d+c-1,data:[]}),r[l].data.push(h);}return {bins:a,width:d,binned:r}}logs(){return this.data.logs||(this.data.logs=this.getLogs(this.data.original)),this.data.logs}getLogs(n){return n.map(t=>Math.log(t))}roots(){return this.data.roots||(this.data.roots=this.getRoots(this.data.original)),this.data.roots}getRoots(n){return n.map(t=>Math.sqrt(t))}inverse(){return this.data.inverse||(this.data.inverse=this.getInverse(this.data.original)),this.data.inverse}getInverse(n){return n.map(t=>1/t)}hanning(){return this.data.hanning||(this.data.hanning=this.getSkipMeans(this.data.original)),this.data.hanning}getSkipMeans(n){let t=[];for(let e=0;e<n.length;e++)e!==0&&e!==n.length-1&&t.push((n[e]+n[e+1])/2);return t.unshift(n[0]),t.push(n[n.length-1]),t}smooth(){return this.data.smooth||(this.sorted(),this.data.smooth=this.getSmooth(this.data.original)),this.data.rough=this.getRough(this.data.original,this.data.smooth),this.data.smooth}getRough(n,t){let e=[];for(let o=0;o<n.length;o++)e.push(n[o]-t[o]);return e}getSmooth(n,t=3){let e=[...n];return e=this.smoothMedian(e,t),e=this.smoothExtremes(e,-1),e=this.smoothSplit(e,2),e=this.smoothMedian(e,t),e=this.smoothExtremes(e,-1),e=this.smoothMedian(e,t),e}smoothExtremes(n,t=1,e=0,o="both"){let r=n.length;if(r<=2)return [...n];let s=[...n];for(let c=e;c<t||t===-1;c++){let i=false;if(o==="both"||o==="head"){let d=s[0],a=s[1],h=s[2],l=a-2*(h-a),m=d<=a?a<=l?a:d<=l?l:d:d<=l?d:a<=l?l:a;s[0]!==m&&(s[0]=m,i=true);}if(o==="both"||o==="tail"){let d=s[r-3],a=s[r-2],h=s[r-1],l=a-2*(d-a),m=h<=a?a<=l?a:h<=l?l:h:h<=l?h:a<=l?l:a;s[r-1]!==m&&(s[r-1]=m,i=true);}if(t===-1&&!i)break}return s}smoothSplit(n,t=ye,e=0){let o=[...n],r=n.length;for(let s=e;s<t||t===-1;s++){let c=false;for(let i=2;i<r-1;i++){let d=o[i],a=o[i-1],h=o[i-2],l=o[i+1];if(d===a&&(a>h&&d>l||a<h&&d<l)){let m=this.smoothExtremes(o.slice(0,i)),N=this.smoothExtremes(o.slice(i));o=m.concat(N),c=true;}}if(t===-1&&!c)return o}return o}smoothMedian(n,t=1,e=0){let o=n,r=n.length;if(r<=2)return [...n];for(let s=e;s<t||t===-1;s++){let c=new Array(r);c[0]=o[0],c[r-1]=o[r-1];let i=false;for(let d=1;d<r-1;d++){let a=o[d],h=Math.min(Math.max(o[d-1],a),o[d+1]);c[d]=h,h!==a&&(i=true);}if(t===-1&&!i)return o;o=c;}return o}jitter(n,t=1,e=NaN,o=Me,r=NaN,s=0){let c=s+1,i=[...n];if(c<=t){let d=[];for(let a of i){let h=r;!h&&!isNaN(h)&&(h=(1+Math.floor(a/10))*(Math.random()>.5?1:-1));let l=a+Math.floor(Math.random()*o*h);!isNaN(e)&&l<e&&(l=e),d.push(l);}return this.jitter(d,t,e,o,r,c)}return i}trimean(){let n=this.median(),t=this.hinges();return t.length<2?n.datum:(t[0].datum+2*n.datum+t[1].datum)/4}letterValues(){this.sorted();let n=this.data.sorted.length;if(n<2)return [];let t=["M","F","E","D","C","B","A","Z","Y","X","W","V","U","T","S"],e=[],o=(n+1)/2,r=this.median().datum;e.push({letter:"M",depth:o,lower:r,upper:r,mid:r,spread:0});let s=o,c=1;for(;s>1&&c<t.length&&(s=Math.floor((Math.floor(s)+1)/2),!(s<1));){let i=Math.ceil(s)-1,d=n-Math.ceil(s);if(i<0||d>=n||i>=d)break;let a=this.data.sorted[i],h=this.data.sorted[d],l=(a+h)/2,m=h-a;e.push({letter:t[c],depth:s,lower:a,upper:h,mid:l,spread:m}),c++;}return e}rough(){return this.data.rough||this.smooth(),this.data.rough||[]}stemLeaf(n=1){this.sorted();let t=this.data.sorted;if(!t.length)return {stems:[],leaves:{},display:[]};let e=Math.pow(10,n),o=new Map;for(let d of t){let a=Math.floor(d/e),h=Math.abs(Math.round(d%e));o.has(a)||o.set(a,[]),o.get(a).push(h);}let r=Array.from(o.keys()).sort((d,a)=>d-a),s=[],c={},i=[];for(let d of r){let a=String(d);s.push(a);let h=o.get(d).sort((l,m)=>l-m).map(String);c[a]=h,i.push(`${a.padStart(4)} | ${h.join(" ")}`);}return {stems:s,leaves:c,display:i}}midSummaries(){return this.letterValues().map(({depth:t,mid:e,spread:o})=>({depth:t,mid:e,spread:o}))}describe(){return this.data.description={original:this.data.original,summary:{median:this.median(),mean:this.mean(),mode:this.mode(),hinges:this.hinges(),adjacent:this.adjacent(),outliers:this.outliers(),outer:this.outer(),outside:this.outside(),inside:this.inside(),extremes:this.extremes(),iqr:this.iqr(),fences:this.fences()},smooths:{smooth:this.smooth(),hanning:this.hanning()},transforms:{logs:this.logs(),roots:this.roots(),inverse:this.inverse()},counts:this.counts(),sorted:this.sorted(),ranked:this.ranked(),binned:this.binned()},this.data.description}},ne=class{data;dimension;count;constructor(n={}){typeof n=="number"?(this.count=n,this.dimension=K,this.data={original:ee(this.count,this.dimension)}):(this.dimension=n.dimensionality??2,this.count=n.count??100,this.data={original:n.data??ee(this.count,this.dimension)});}describe(){return this.data.description={original:this.data.original},this.data.description}},oe=class{smoothed=false;static DEFAULT_MAX_RANDOM_INTEGER=B;static DEFAULT_MIN_RANDOM_INTEGER=tt;static DEFAULT_RANDOM_SERIES_COUNT=re;static DEFAULT_OUTLIER_MULTIPLE=Z;static DEFAULT_JITTER_MULTIPLIER=Me;static DEFAULT_SPLIT_PASSES=ye;static DEFAULT_MAX_RANDOM_DIMENSIONALITY=K;static Series=te;static Points=ne;static randomInteger=we;static randomSeries=Re;static randomPoint=Se;static randomPoints=ee},ht=oe;exports.GraphAlgorithmError=E;exports.GraphCatalog=Q;exports.Points=ne;exports.Series=te;exports.Twokeys=oe;exports.aStarShortestPath=Ie;exports.allPairsShortestPaths=Ne;exports.analyzeGraph=rt;exports.articulationPointsAndBridges=Qe;exports.betweennessCentrality=Je;exports.buildGraphAdjacency=v;exports.closenessCentrality=Xe;exports.createGraphCatalog=et;exports.default=ht;exports.degreeCentrality=He;exports.gds=ut;exports.kMeansAuto=st;exports.kMeansClustering=Ze;exports.kNearestNeighbors=fe;exports.labelPropagationCommunities=he;exports.linkPrediction=it;exports.louvainCommunities=me;exports.maximumFlow=be;exports.minCostMaxFlow=xe;exports.minimumSpanningTree=ot;exports.nodeSimilarity=ge;exports.pageRank=X;exports.predictLinks=J;exports.shortestPath=j;exports.stronglyConnectedComponents=le;exports.topologicalSort=nt;exports.travelingSalesmanApprox=dt;exports.weaklyConnectedComponents=$e;exports.yenKShortestPaths=pe;//# sourceMappingURL=index.cjs.map
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|