data-structure-typed 2.4.4 → 2.5.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/CHANGELOG.md +22 -1
- package/README.md +34 -1
- package/dist/cjs/index.cjs +10639 -2151
- package/dist/cjs-legacy/index.cjs +10694 -2195
- package/dist/esm/index.mjs +10639 -2150
- package/dist/esm-legacy/index.mjs +10694 -2194
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -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 +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -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 +313 -66
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/data-structure-typed.js +10725 -2221
- package/dist/umd/data-structure-typed.min.js +4 -2
- package/package.json +5 -4
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +146 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
- package/src/data-structures/binary-tree/binary-tree.ts +567 -121
- package/src/data-structures/binary-tree/bst.ts +370 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
- package/src/data-structures/binary-tree/segment-tree.ts +378 -248
- package/src/data-structures/binary-tree/tree-map.ts +1411 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
- package/src/data-structures/binary-tree/tree-set.ts +1257 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +233 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +308 -59
- package/src/data-structures/hash/hash-map.ts +254 -79
- package/src/data-structures/heap/heap.ts +305 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
- package/src/data-structures/matrix/matrix.ts +433 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- 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 +358 -68
- package/src/data-structures/queue/queue.ts +223 -42
- package/src/data-structures/stack/stack.ts +184 -32
- package/src/data-structures/trie/trie.ts +227 -44
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -337,4 +337,48 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
337
337
|
* @remarks Time O(1), Space O(1)
|
|
338
338
|
*/
|
|
339
339
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
|
|
340
|
+
/**
|
|
341
|
+
* The edge connector string used in visual output.
|
|
342
|
+
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
343
|
+
*/
|
|
344
|
+
protected get _edgeConnector(): string;
|
|
345
|
+
/**
|
|
346
|
+
* Generate a text-based visual representation of the graph.
|
|
347
|
+
*
|
|
348
|
+
* **Adjacency list format:**
|
|
349
|
+
* ```
|
|
350
|
+
* Graph (5 vertices, 6 edges):
|
|
351
|
+
* A -> B (1), C (2)
|
|
352
|
+
* B -> D (3)
|
|
353
|
+
* C -> (no outgoing edges)
|
|
354
|
+
* D -> A (1)
|
|
355
|
+
* E (isolated)
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* @param options - Optional display settings.
|
|
359
|
+
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
360
|
+
* @returns The visual string.
|
|
361
|
+
*/
|
|
362
|
+
toVisual(options?: {
|
|
363
|
+
showWeight?: boolean;
|
|
364
|
+
}): string;
|
|
365
|
+
/**
|
|
366
|
+
* Generate DOT language representation for Graphviz.
|
|
367
|
+
*
|
|
368
|
+
* @param options - Optional display settings.
|
|
369
|
+
* @param options.name - Graph name (default: 'G').
|
|
370
|
+
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
371
|
+
* @returns DOT format string.
|
|
372
|
+
*/
|
|
373
|
+
toDot(options?: {
|
|
374
|
+
name?: string;
|
|
375
|
+
showWeight?: boolean;
|
|
376
|
+
}): string;
|
|
377
|
+
/**
|
|
378
|
+
* Print the graph to console.
|
|
379
|
+
* @param options - Display settings passed to `toVisual`.
|
|
380
|
+
*/
|
|
381
|
+
print(options?: {
|
|
382
|
+
showWeight?: boolean;
|
|
383
|
+
}): void;
|
|
340
384
|
}
|
|
@@ -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>();
|
|
@@ -157,6 +110,7 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
157
110
|
* @remarks Time O(1), Space O(1)
|
|
158
111
|
*/
|
|
159
112
|
constructor(options?: Partial<GraphOptions<V>>);
|
|
113
|
+
protected get _edgeConnector(): string;
|
|
160
114
|
protected _outEdgeMap: Map<VO, EO[]>;
|
|
161
115
|
get outEdgeMap(): Map<VO, EO[]>;
|
|
162
116
|
set outEdgeMap(v: Map<VO, EO[]>);
|
|
@@ -203,6 +157,22 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
203
157
|
* @param destOrKey - Destination vertex or key.
|
|
204
158
|
* @returns Edge instance or `undefined`.
|
|
205
159
|
* @remarks Time O(1) avg, Space O(1)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
* @example
|
|
169
|
+
* // Get edge between vertices
|
|
170
|
+
* const g = new DirectedGraph();
|
|
171
|
+
* g.addVertex('A');
|
|
172
|
+
* g.addVertex('B');
|
|
173
|
+
* g.addEdge('A', 'B', 5);
|
|
174
|
+
* const edge = g.getEdge('A', 'B');
|
|
175
|
+
* console.log(edge?.weight); // 5;
|
|
206
176
|
*/
|
|
207
177
|
getEdge(srcOrKey: VO | VertexKey | undefined, destOrKey: VO | VertexKey | undefined): EO | undefined;
|
|
208
178
|
/**
|
|
@@ -219,8 +189,64 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
219
189
|
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
220
190
|
* @returns Removed edge or `undefined`.
|
|
221
191
|
* @remarks Time O(1) avg, Space O(1)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
* @example
|
|
204
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
205
|
+
* const graph = new DirectedGraph<string>();
|
|
206
|
+
*
|
|
207
|
+
* // Build a small graph
|
|
208
|
+
* graph.addVertex('X');
|
|
209
|
+
* graph.addVertex('Y');
|
|
210
|
+
* graph.addVertex('Z');
|
|
211
|
+
* graph.addEdge('X', 'Y', 1);
|
|
212
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
213
|
+
*
|
|
214
|
+
* // Delete an edge
|
|
215
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
216
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
217
|
+
*
|
|
218
|
+
* // Edge in other direction should not exist
|
|
219
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
220
|
+
*
|
|
221
|
+
* // Other edges should remain
|
|
222
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
223
|
+
*
|
|
224
|
+
* // Delete a vertex
|
|
225
|
+
* graph.deleteVertex('Y');
|
|
226
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
227
|
+
* console.log(graph.size); // 2;
|
|
222
228
|
*/
|
|
223
229
|
deleteEdge(edgeOrSrcVertexKey: EO | VertexKey, destVertexKey?: VertexKey): EO | undefined;
|
|
230
|
+
/**
|
|
231
|
+
* Remove a vertex
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
* @example
|
|
241
|
+
* // Remove a vertex
|
|
242
|
+
* const g = new DirectedGraph();
|
|
243
|
+
* g.addVertex('A');
|
|
244
|
+
* g.addVertex('B');
|
|
245
|
+
* g.addEdge('A', 'B');
|
|
246
|
+
* g.deleteVertex('A');
|
|
247
|
+
* console.log(g.hasVertex('A')); // false;
|
|
248
|
+
* console.log(g.hasEdge('A', 'B')); // false;
|
|
249
|
+
*/
|
|
224
250
|
deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
225
251
|
deleteEdgesBetween(v1: VertexKey | VO, v2: VertexKey | VO): EO[];
|
|
226
252
|
/**
|
|
@@ -228,6 +254,23 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
228
254
|
* @param vertexOrKey - Vertex or key.
|
|
229
255
|
* @returns Array of incoming edges.
|
|
230
256
|
* @remarks Time O(deg_in), Space O(deg_in)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
* @example
|
|
266
|
+
* // Get incoming edges
|
|
267
|
+
* const g = new DirectedGraph();
|
|
268
|
+
* g.addVertex('A');
|
|
269
|
+
* g.addVertex('B');
|
|
270
|
+
* g.addVertex('C');
|
|
271
|
+
* g.addEdge('A', 'C');
|
|
272
|
+
* g.addEdge('B', 'C');
|
|
273
|
+
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
231
274
|
*/
|
|
232
275
|
incomingEdgesOf(vertexOrKey: VO | VertexKey): EO[];
|
|
233
276
|
/**
|
|
@@ -235,6 +278,23 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
235
278
|
* @param vertexOrKey - Vertex or key.
|
|
236
279
|
* @returns Array of outgoing edges.
|
|
237
280
|
* @remarks Time O(deg_out), Space O(deg_out)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
* @example
|
|
290
|
+
* // Get outgoing edges
|
|
291
|
+
* const g = new DirectedGraph();
|
|
292
|
+
* g.addVertex('A');
|
|
293
|
+
* g.addVertex('B');
|
|
294
|
+
* g.addVertex('C');
|
|
295
|
+
* g.addEdge('A', 'B');
|
|
296
|
+
* g.addEdge('A', 'C');
|
|
297
|
+
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
238
298
|
*/
|
|
239
299
|
outgoingEdgesOf(vertexOrKey: VO | VertexKey): EO[];
|
|
240
300
|
/**
|
|
@@ -267,9 +327,82 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
267
327
|
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
268
328
|
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
269
329
|
* @remarks Time O(V + E), Space O(V)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
* @example
|
|
342
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
343
|
+
* const graph = new DirectedGraph<string>();
|
|
344
|
+
*
|
|
345
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
346
|
+
* graph.addVertex('Design');
|
|
347
|
+
* graph.addVertex('Implement');
|
|
348
|
+
* graph.addVertex('Test');
|
|
349
|
+
* graph.addVertex('Deploy');
|
|
350
|
+
*
|
|
351
|
+
* // Add dependency edges
|
|
352
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
353
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
354
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
355
|
+
*
|
|
356
|
+
* // Topological sort gives valid execution order
|
|
357
|
+
* const executionOrder = graph.topologicalSort();
|
|
358
|
+
* console.log(executionOrder); // defined;
|
|
359
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
360
|
+
*
|
|
361
|
+
* // All vertices should be included
|
|
362
|
+
* console.log(executionOrder?.length); // 4;
|
|
270
363
|
*/
|
|
271
364
|
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | undefined;
|
|
365
|
+
/**
|
|
366
|
+
* Get all edges
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
* @example
|
|
376
|
+
* // Get all edges
|
|
377
|
+
* const g = new DirectedGraph();
|
|
378
|
+
* g.addVertex('A');
|
|
379
|
+
* g.addVertex('B');
|
|
380
|
+
* g.addEdge('A', 'B', 3);
|
|
381
|
+
* console.log(g.edgeSet().length); // 1;
|
|
382
|
+
*/
|
|
272
383
|
edgeSet(): EO[];
|
|
384
|
+
/**
|
|
385
|
+
* Get outgoing neighbors
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
* @example
|
|
396
|
+
* // Get outgoing neighbors
|
|
397
|
+
* const g = new DirectedGraph();
|
|
398
|
+
* g.addVertex('A');
|
|
399
|
+
* g.addVertex('B');
|
|
400
|
+
* g.addVertex('C');
|
|
401
|
+
* g.addEdge('A', 'B');
|
|
402
|
+
* g.addEdge('A', 'C');
|
|
403
|
+
* const neighbors = g.getNeighbors('A');
|
|
404
|
+
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
405
|
+
*/
|
|
273
406
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
274
407
|
/**
|
|
275
408
|
* Resolve an edge's `[src, dest]` endpoints to vertex instances.
|
|
@@ -298,6 +431,27 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
298
431
|
* Tarjan's algorithm for strongly connected components.
|
|
299
432
|
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
300
433
|
* @remarks Time O(V + E), Space O(V + E)
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
* @example
|
|
443
|
+
* // Find strongly connected components
|
|
444
|
+
* const g = new DirectedGraph();
|
|
445
|
+
* g.addVertex('A');
|
|
446
|
+
* g.addVertex('B');
|
|
447
|
+
* g.addVertex('C');
|
|
448
|
+
* g.addEdge('A', 'B');
|
|
449
|
+
* g.addEdge('B', 'C');
|
|
450
|
+
* g.addEdge('C', 'A');
|
|
451
|
+
* const { SCCs } = g.tarjan();
|
|
452
|
+
* // A→B→C→A forms one SCC with 3 members
|
|
453
|
+
* const sccArrays = [...SCCs.values()];
|
|
454
|
+
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
301
455
|
*/
|
|
302
456
|
tarjan(): {
|
|
303
457
|
dfnMap: Map<VO, number>;
|
|
@@ -320,6 +474,25 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
320
474
|
* Strongly connected components computed by `tarjan()`.
|
|
321
475
|
* @returns Map from SCC id to vertices.
|
|
322
476
|
* @remarks Time O(#SCC + V), Space O(V)
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
* @example
|
|
486
|
+
* // Get strongly connected components
|
|
487
|
+
* const g = new DirectedGraph();
|
|
488
|
+
* g.addVertex(1);
|
|
489
|
+
* g.addVertex(2);
|
|
490
|
+
* g.addVertex(3);
|
|
491
|
+
* g.addEdge(1, 2);
|
|
492
|
+
* g.addEdge(2, 3);
|
|
493
|
+
* g.addEdge(3, 1);
|
|
494
|
+
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
495
|
+
* console.log(sccs.size); // >= 1;
|
|
323
496
|
*/
|
|
324
497
|
getSCCs(): Map<number, VO[]>;
|
|
325
498
|
/**
|
|
@@ -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
|
/**
|