binary-tree-typed 2.4.5 → 2.5.1
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 +0 -84
- package/dist/cjs/index.cjs +1476 -404
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1473 -401
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1476 -404
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1473 -401
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/binary-tree-typed.js +1469 -397
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +5 -5
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -131,7 +131,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
131
131
|
* @returns `true` if string/number; else `false`.
|
|
132
132
|
* @remarks Time O(1), Space O(1)
|
|
133
133
|
*/
|
|
134
|
-
isVertexKey(potentialKey:
|
|
134
|
+
isVertexKey(potentialKey: unknown): potentialKey is VertexKey;
|
|
135
135
|
/**
|
|
136
136
|
* Delete a vertex and its incident edges.
|
|
137
137
|
* @param vertexOrKey - Vertex or key.
|
|
@@ -252,13 +252,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
252
252
|
* @returns A new graph of the same concrete class (`this` type).
|
|
253
253
|
* @remarks Time O(V + E), Space O(V + E)
|
|
254
254
|
*/
|
|
255
|
-
filter(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?:
|
|
255
|
+
filter(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?: unknown): this;
|
|
256
256
|
/**
|
|
257
257
|
* Preserve the old behavior: return filtered entries as an array.
|
|
258
258
|
* @remarks Time O(V), Space O(V)
|
|
259
259
|
*/
|
|
260
|
-
filterEntries(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?:
|
|
261
|
-
map<T>(callback: EntryCallback<VertexKey, V | undefined, T>, thisArg?:
|
|
260
|
+
filterEntries(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?: unknown): [VertexKey, V | undefined][];
|
|
261
|
+
map<T>(callback: EntryCallback<VertexKey, V | undefined, T>, thisArg?: unknown): T[];
|
|
262
262
|
/**
|
|
263
263
|
* Create a deep clone of the graph with the same species.
|
|
264
264
|
* @remarks Time O(V + E), Space O(V + E)
|
|
@@ -65,53 +65,6 @@ export declare class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
65
65
|
* console.log(neighborsA[0].key); // 'B';
|
|
66
66
|
* console.log(neighborsA[1].key); // 'C';
|
|
67
67
|
* @example
|
|
68
|
-
* // DirectedGraph deleteEdge and vertex operations
|
|
69
|
-
* const graph = new DirectedGraph<string>();
|
|
70
|
-
*
|
|
71
|
-
* // Build a small graph
|
|
72
|
-
* graph.addVertex('X');
|
|
73
|
-
* graph.addVertex('Y');
|
|
74
|
-
* graph.addVertex('Z');
|
|
75
|
-
* graph.addEdge('X', 'Y', 1);
|
|
76
|
-
* graph.addEdge('Y', 'Z', 2);
|
|
77
|
-
*
|
|
78
|
-
* // Delete an edge
|
|
79
|
-
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
80
|
-
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
81
|
-
*
|
|
82
|
-
* // Edge in other direction should not exist
|
|
83
|
-
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
84
|
-
*
|
|
85
|
-
* // Other edges should remain
|
|
86
|
-
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
87
|
-
*
|
|
88
|
-
* // Delete a vertex
|
|
89
|
-
* graph.deleteVertex('Y');
|
|
90
|
-
* console.log(graph.hasVertex('Y')); // false;
|
|
91
|
-
* console.log(graph.size); // 2;
|
|
92
|
-
* @example
|
|
93
|
-
* // DirectedGraph topologicalSort for task scheduling
|
|
94
|
-
* const graph = new DirectedGraph<string>();
|
|
95
|
-
*
|
|
96
|
-
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
97
|
-
* graph.addVertex('Design');
|
|
98
|
-
* graph.addVertex('Implement');
|
|
99
|
-
* graph.addVertex('Test');
|
|
100
|
-
* graph.addVertex('Deploy');
|
|
101
|
-
*
|
|
102
|
-
* // Add dependency edges
|
|
103
|
-
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
104
|
-
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
105
|
-
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
106
|
-
*
|
|
107
|
-
* // Topological sort gives valid execution order
|
|
108
|
-
* const executionOrder = graph.topologicalSort();
|
|
109
|
-
* console.log(executionOrder); // defined;
|
|
110
|
-
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
111
|
-
*
|
|
112
|
-
* // All vertices should be included
|
|
113
|
-
* console.log(executionOrder?.length); // 4;
|
|
114
|
-
* @example
|
|
115
68
|
* // DirectedGraph dijkstra shortest path for network routing
|
|
116
69
|
* // Build a weighted directed graph representing network nodes and costs
|
|
117
70
|
* const network = new DirectedGraph<string>();
|
|
@@ -204,6 +157,43 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
204
157
|
* @param destOrKey - Destination vertex or key.
|
|
205
158
|
* @returns Edge instance or `undefined`.
|
|
206
159
|
* @remarks Time O(1) avg, Space O(1)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
* @example
|
|
190
|
+
* // Get edge between vertices
|
|
191
|
+
* const g = new DirectedGraph();
|
|
192
|
+
* g.addVertex('A');
|
|
193
|
+
* g.addVertex('B');
|
|
194
|
+
* g.addEdge('A', 'B', 5);
|
|
195
|
+
* const edge = g.getEdge('A', 'B');
|
|
196
|
+
* console.log(edge?.weight); // 5;
|
|
207
197
|
*/
|
|
208
198
|
getEdge(srcOrKey: VO | VertexKey | undefined, destOrKey: VO | VertexKey | undefined): EO | undefined;
|
|
209
199
|
/**
|
|
@@ -220,8 +210,106 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
220
210
|
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
221
211
|
* @returns Removed edge or `undefined`.
|
|
222
212
|
* @remarks Time O(1) avg, Space O(1)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
* @example
|
|
246
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
247
|
+
* const graph = new DirectedGraph<string>();
|
|
248
|
+
*
|
|
249
|
+
* // Build a small graph
|
|
250
|
+
* graph.addVertex('X');
|
|
251
|
+
* graph.addVertex('Y');
|
|
252
|
+
* graph.addVertex('Z');
|
|
253
|
+
* graph.addEdge('X', 'Y', 1);
|
|
254
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
255
|
+
*
|
|
256
|
+
* // Delete an edge
|
|
257
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
258
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
259
|
+
*
|
|
260
|
+
* // Edge in other direction should not exist
|
|
261
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
262
|
+
*
|
|
263
|
+
* // Other edges should remain
|
|
264
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
265
|
+
*
|
|
266
|
+
* // Delete a vertex
|
|
267
|
+
* graph.deleteVertex('Y');
|
|
268
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
269
|
+
* console.log(graph.size); // 2;
|
|
223
270
|
*/
|
|
224
271
|
deleteEdge(edgeOrSrcVertexKey: EO | VertexKey, destVertexKey?: VertexKey): EO | undefined;
|
|
272
|
+
/**
|
|
273
|
+
* Remove a vertex
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
* @example
|
|
304
|
+
* // Remove a vertex
|
|
305
|
+
* const g = new DirectedGraph();
|
|
306
|
+
* g.addVertex('A');
|
|
307
|
+
* g.addVertex('B');
|
|
308
|
+
* g.addEdge('A', 'B');
|
|
309
|
+
* g.deleteVertex('A');
|
|
310
|
+
* console.log(g.hasVertex('A')); // false;
|
|
311
|
+
* console.log(g.hasEdge('A', 'B')); // false;
|
|
312
|
+
*/
|
|
225
313
|
deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
226
314
|
deleteEdgesBetween(v1: VertexKey | VO, v2: VertexKey | VO): EO[];
|
|
227
315
|
/**
|
|
@@ -229,6 +317,44 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
229
317
|
* @param vertexOrKey - Vertex or key.
|
|
230
318
|
* @returns Array of incoming edges.
|
|
231
319
|
* @remarks Time O(deg_in), Space O(deg_in)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
* @example
|
|
350
|
+
* // Get incoming edges
|
|
351
|
+
* const g = new DirectedGraph();
|
|
352
|
+
* g.addVertex('A');
|
|
353
|
+
* g.addVertex('B');
|
|
354
|
+
* g.addVertex('C');
|
|
355
|
+
* g.addEdge('A', 'C');
|
|
356
|
+
* g.addEdge('B', 'C');
|
|
357
|
+
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
232
358
|
*/
|
|
233
359
|
incomingEdgesOf(vertexOrKey: VO | VertexKey): EO[];
|
|
234
360
|
/**
|
|
@@ -236,6 +362,44 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
236
362
|
* @param vertexOrKey - Vertex or key.
|
|
237
363
|
* @returns Array of outgoing edges.
|
|
238
364
|
* @remarks Time O(deg_out), Space O(deg_out)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
* @example
|
|
395
|
+
* // Get outgoing edges
|
|
396
|
+
* const g = new DirectedGraph();
|
|
397
|
+
* g.addVertex('A');
|
|
398
|
+
* g.addVertex('B');
|
|
399
|
+
* g.addVertex('C');
|
|
400
|
+
* g.addEdge('A', 'B');
|
|
401
|
+
* g.addEdge('A', 'C');
|
|
402
|
+
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
239
403
|
*/
|
|
240
404
|
outgoingEdgesOf(vertexOrKey: VO | VertexKey): EO[];
|
|
241
405
|
/**
|
|
@@ -268,9 +432,145 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
268
432
|
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
269
433
|
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
270
434
|
* @remarks Time O(V + E), Space O(V)
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
* @example
|
|
468
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
469
|
+
* const graph = new DirectedGraph<string>();
|
|
470
|
+
*
|
|
471
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
472
|
+
* graph.addVertex('Design');
|
|
473
|
+
* graph.addVertex('Implement');
|
|
474
|
+
* graph.addVertex('Test');
|
|
475
|
+
* graph.addVertex('Deploy');
|
|
476
|
+
*
|
|
477
|
+
* // Add dependency edges
|
|
478
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
479
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
480
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
481
|
+
*
|
|
482
|
+
* // Topological sort gives valid execution order
|
|
483
|
+
* const executionOrder = graph.topologicalSort();
|
|
484
|
+
* console.log(executionOrder); // defined;
|
|
485
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
486
|
+
*
|
|
487
|
+
* // All vertices should be included
|
|
488
|
+
* console.log(executionOrder?.length); // 4;
|
|
271
489
|
*/
|
|
272
490
|
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | undefined;
|
|
491
|
+
/**
|
|
492
|
+
* Get all edges
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
* @example
|
|
523
|
+
* // Get all edges
|
|
524
|
+
* const g = new DirectedGraph();
|
|
525
|
+
* g.addVertex('A');
|
|
526
|
+
* g.addVertex('B');
|
|
527
|
+
* g.addEdge('A', 'B', 3);
|
|
528
|
+
* console.log(g.edgeSet().length); // 1;
|
|
529
|
+
*/
|
|
273
530
|
edgeSet(): EO[];
|
|
531
|
+
/**
|
|
532
|
+
* Get outgoing neighbors
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
* @example
|
|
564
|
+
* // Get outgoing neighbors
|
|
565
|
+
* const g = new DirectedGraph();
|
|
566
|
+
* g.addVertex('A');
|
|
567
|
+
* g.addVertex('B');
|
|
568
|
+
* g.addVertex('C');
|
|
569
|
+
* g.addEdge('A', 'B');
|
|
570
|
+
* g.addEdge('A', 'C');
|
|
571
|
+
* const neighbors = g.getNeighbors('A');
|
|
572
|
+
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
573
|
+
*/
|
|
274
574
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
275
575
|
/**
|
|
276
576
|
* Resolve an edge's `[src, dest]` endpoints to vertex instances.
|
|
@@ -299,6 +599,48 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
299
599
|
* Tarjan's algorithm for strongly connected components.
|
|
300
600
|
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
301
601
|
* @remarks Time O(V + E), Space O(V + E)
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
* @example
|
|
632
|
+
* // Find strongly connected components
|
|
633
|
+
* const g = new DirectedGraph();
|
|
634
|
+
* g.addVertex('A');
|
|
635
|
+
* g.addVertex('B');
|
|
636
|
+
* g.addVertex('C');
|
|
637
|
+
* g.addEdge('A', 'B');
|
|
638
|
+
* g.addEdge('B', 'C');
|
|
639
|
+
* g.addEdge('C', 'A');
|
|
640
|
+
* const { SCCs } = g.tarjan();
|
|
641
|
+
* // A→B→C→A forms one SCC with 3 members
|
|
642
|
+
* const sccArrays = [...SCCs.values()];
|
|
643
|
+
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
302
644
|
*/
|
|
303
645
|
tarjan(): {
|
|
304
646
|
dfnMap: Map<VO, number>;
|
|
@@ -321,6 +663,46 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
321
663
|
* Strongly connected components computed by `tarjan()`.
|
|
322
664
|
* @returns Map from SCC id to vertices.
|
|
323
665
|
* @remarks Time O(#SCC + V), Space O(V)
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
* @example
|
|
696
|
+
* // Get strongly connected components
|
|
697
|
+
* const g = new DirectedGraph();
|
|
698
|
+
* g.addVertex(1);
|
|
699
|
+
* g.addVertex(2);
|
|
700
|
+
* g.addVertex(3);
|
|
701
|
+
* g.addEdge(1, 2);
|
|
702
|
+
* g.addEdge(2, 3);
|
|
703
|
+
* g.addEdge(3, 1);
|
|
704
|
+
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
705
|
+
* console.log(sccs.size); // >= 1;
|
|
324
706
|
*/
|
|
325
707
|
getSCCs(): Map<number, VO[]>;
|
|
326
708
|
/**
|
|
@@ -22,7 +22,65 @@ export declare class MapEdge<E = any> extends DirectedEdge<E> {
|
|
|
22
22
|
* @template VO - Concrete vertex class (MapVertex<V>).
|
|
23
23
|
* @template EO - Concrete edge class (MapEdge<E>).
|
|
24
24
|
* @remarks Time O(1), Space O(1)
|
|
25
|
-
* @example
|
|
25
|
+
* @example
|
|
26
|
+
* // City navigation with shortest path
|
|
27
|
+
* const map = new MapGraph([0, 0], [10, 10]);
|
|
28
|
+
*
|
|
29
|
+
* map.addVertex(new MapVertex('Home', '', 0, 0));
|
|
30
|
+
* map.addVertex(new MapVertex('Office', '', 3, 4));
|
|
31
|
+
* map.addVertex(new MapVertex('Cafe', '', 1, 2));
|
|
32
|
+
* map.addVertex(new MapVertex('Park', '', 2, 1));
|
|
33
|
+
*
|
|
34
|
+
* map.addEdge('Home', 'Cafe', 2.2);
|
|
35
|
+
* map.addEdge('Cafe', 'Office', 3.5);
|
|
36
|
+
* map.addEdge('Home', 'Park', 2.0);
|
|
37
|
+
* map.addEdge('Park', 'Office', 4.0);
|
|
38
|
+
* map.addEdge('Home', 'Office', 7.0);
|
|
39
|
+
*
|
|
40
|
+
* // Find shortest path
|
|
41
|
+
* const result = map.dijkstra('Home', 'Office', true, true);
|
|
42
|
+
* console.log(result?.minDist); // 5.7; // Home → Cafe → Office
|
|
43
|
+
* console.log(result?.minPath.map(v => v.key)); // ['Home', 'Cafe', 'Office'];
|
|
44
|
+
* @example
|
|
45
|
+
* // Delivery route optimization
|
|
46
|
+
* const routes = new MapGraph([0, 0], [10, 10]);
|
|
47
|
+
*
|
|
48
|
+
* routes.addVertex(new MapVertex('Warehouse', '', 0, 0));
|
|
49
|
+
* routes.addVertex(new MapVertex('Customer A', '', 2, 3));
|
|
50
|
+
* routes.addVertex(new MapVertex('Customer B', '', 5, 1));
|
|
51
|
+
* routes.addVertex(new MapVertex('Customer C', '', 3, 5));
|
|
52
|
+
*
|
|
53
|
+
* routes.addEdge('Warehouse', 'Customer A', 3.6);
|
|
54
|
+
* routes.addEdge('Warehouse', 'Customer B', 5.1);
|
|
55
|
+
* routes.addEdge('Customer A', 'Customer C', 2.2);
|
|
56
|
+
* routes.addEdge('Customer A', 'Customer B', 3.6);
|
|
57
|
+
* routes.addEdge('Customer B', 'Customer C', 4.5);
|
|
58
|
+
*
|
|
59
|
+
* // Check outgoing neighbors of Customer A
|
|
60
|
+
* const neighbors = routes.getNeighbors('Customer A');
|
|
61
|
+
* console.log(neighbors.map(n => n.key).sort()); // ['Customer B', 'Customer C'];
|
|
62
|
+
*
|
|
63
|
+
* // Shortest path from Warehouse to Customer C
|
|
64
|
+
* const path = routes.getMinPathBetween('Warehouse', 'Customer C', true);
|
|
65
|
+
* console.log(path?.map(v => v.key)); // ['Warehouse', 'Customer A', 'Customer C'];
|
|
66
|
+
* @example
|
|
67
|
+
* // Campus map with building connections
|
|
68
|
+
* const campus = new MapGraph([0, 0], [5, 5]);
|
|
69
|
+
*
|
|
70
|
+
* campus.addVertex(new MapVertex('Library', '', 0, 0));
|
|
71
|
+
* campus.addVertex(new MapVertex('Lab', '', 1, 1));
|
|
72
|
+
* campus.addVertex(new MapVertex('Cafeteria', '', 2, 0));
|
|
73
|
+
*
|
|
74
|
+
* campus.addEdge('Library', 'Lab', 5);
|
|
75
|
+
* campus.addEdge('Lab', 'Cafeteria', 3);
|
|
76
|
+
* campus.addEdge('Library', 'Cafeteria', 10);
|
|
77
|
+
*
|
|
78
|
+
* console.log(campus.hasVertex('Library')); // true;
|
|
79
|
+
* console.log(campus.hasVertex('Gym')); // false;
|
|
80
|
+
*
|
|
81
|
+
* // Direct distance vs shortest path
|
|
82
|
+
* const direct = campus.dijkstra('Library', 'Cafeteria', true, true);
|
|
83
|
+
* console.log(direct?.minDist); // 8;
|
|
26
84
|
*/
|
|
27
85
|
export declare class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVertex<V>, EO extends MapEdge<E> = MapEdge<E>> extends DirectedGraph<V, E, VO, EO> {
|
|
28
86
|
/**
|