queue-typed 1.48.3 → 1.48.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/data-structures/base/iterable-base.js +3 -3
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/data-structures/binary-tree/binary-tree.js +16 -13
- package/dist/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/data-structures/binary-tree/bst.js +17 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +53 -52
- package/dist/data-structures/graph/abstract-graph.js +82 -78
- package/dist/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/data-structures/graph/directed-graph.js +111 -65
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/data-structures/graph/undirected-graph.js +117 -54
- package/dist/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/base/base.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +8 -5
- package/src/data-structures/binary-tree/binary-tree.ts +23 -19
- package/src/data-structures/binary-tree/bst.ts +19 -14
- package/src/data-structures/binary-tree/rb-tree.ts +20 -14
- package/src/data-structures/binary-tree/tree-multimap.ts +27 -19
- package/src/data-structures/graph/abstract-graph.ts +87 -82
- package/src/data-structures/graph/directed-graph.ts +114 -65
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +124 -56
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/base/base.ts +3 -3
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
import { uuidV4 } from '../../utils';
|
|
9
9
|
import { PriorityQueue } from '../priority-queue';
|
|
10
10
|
import type { DijkstraResult, VertexKey } from '../../types';
|
|
11
|
-
import {
|
|
11
|
+
import { EntryCallback } from "../../types";
|
|
12
12
|
import { IGraph } from '../../interfaces';
|
|
13
13
|
import { Queue } from '../queue';
|
|
14
|
-
import {
|
|
14
|
+
import { IterableEntryBase } from "../base";
|
|
15
15
|
|
|
16
16
|
export abstract class AbstractVertex<V = any> {
|
|
17
17
|
key: VertexKey;
|
|
@@ -66,15 +66,15 @@ export abstract class AbstractGraph<
|
|
|
66
66
|
E = any,
|
|
67
67
|
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
68
68
|
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
69
|
-
> extends
|
|
69
|
+
> extends IterableEntryBase<VertexKey, V | undefined> implements IGraph<V, E, VO, EO> {
|
|
70
70
|
constructor() {
|
|
71
71
|
super();
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
protected
|
|
74
|
+
protected _vertexMap: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
75
75
|
|
|
76
|
-
get
|
|
77
|
-
return this.
|
|
76
|
+
get vertexMap(): Map<VertexKey, VO> {
|
|
77
|
+
return this._vertexMap;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/**
|
|
@@ -120,12 +120,12 @@ export abstract class AbstractGraph<
|
|
|
120
120
|
*
|
|
121
121
|
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
122
122
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
123
|
-
* the `
|
|
124
|
-
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `
|
|
123
|
+
* the `_vertexMap` map.
|
|
124
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertexMap`
|
|
125
125
|
* map. If the vertex does not exist, it returns `undefined`.
|
|
126
126
|
*/
|
|
127
127
|
getVertex(vertexKey: VertexKey): VO | undefined {
|
|
128
|
-
return this.
|
|
128
|
+
return this._vertexMap.get(vertexKey) || undefined;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
/**
|
|
@@ -143,7 +143,7 @@ export abstract class AbstractGraph<
|
|
|
143
143
|
* @returns a boolean value.
|
|
144
144
|
*/
|
|
145
145
|
hasVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
146
|
-
return this.
|
|
146
|
+
return this._vertexMap.has(this._getVertexKey(vertexOrKey));
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
addVertex(vertex: VO): boolean;
|
|
@@ -164,6 +164,11 @@ export abstract class AbstractGraph<
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
isVertexKey(potentialKey: any): potentialKey is VertexKey {
|
|
168
|
+
const potentialKeyType = typeof potentialKey;
|
|
169
|
+
return potentialKeyType === "string" || potentialKeyType === "number"
|
|
170
|
+
}
|
|
171
|
+
|
|
167
172
|
/**
|
|
168
173
|
* Time Complexity: O(1) - Constant time for Map operations.
|
|
169
174
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -180,27 +185,27 @@ export abstract class AbstractGraph<
|
|
|
180
185
|
*/
|
|
181
186
|
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
182
187
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
183
|
-
return this.
|
|
188
|
+
return this._vertexMap.delete(vertexKey);
|
|
184
189
|
}
|
|
185
190
|
|
|
186
191
|
/**
|
|
187
|
-
* Time Complexity: O(K), where K is the number of
|
|
192
|
+
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
|
|
188
193
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
189
194
|
*/
|
|
190
195
|
|
|
191
196
|
/**
|
|
192
|
-
* Time Complexity: O(K), where K is the number of
|
|
197
|
+
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
|
|
193
198
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
194
199
|
*
|
|
195
|
-
* The function removes all
|
|
196
|
-
* @param {VO[] | VertexKey[]}
|
|
200
|
+
* The function removes all vertexMap from a graph and returns a boolean indicating if any vertexMap were removed.
|
|
201
|
+
* @param {VO[] | VertexKey[]} vertexMap - The `vertexMap` parameter can be either an array of vertexMap (`VO[]`) or an array
|
|
197
202
|
* of vertex IDs (`VertexKey[]`).
|
|
198
|
-
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no
|
|
203
|
+
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertexMap
|
|
199
204
|
* were removed.
|
|
200
205
|
*/
|
|
201
|
-
removeManyVertices(
|
|
206
|
+
removeManyVertices(vertexMap: VO[] | VertexKey[]): boolean {
|
|
202
207
|
const removed: boolean[] = [];
|
|
203
|
-
for (const v of
|
|
208
|
+
for (const v of vertexMap) {
|
|
204
209
|
removed.push(this.deleteVertex(v));
|
|
205
210
|
}
|
|
206
211
|
return removed.length > 0;
|
|
@@ -215,7 +220,7 @@ export abstract class AbstractGraph<
|
|
|
215
220
|
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
216
221
|
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
217
222
|
*
|
|
218
|
-
* The function checks if there is an edge between two
|
|
223
|
+
* The function checks if there is an edge between two vertexMap and returns a boolean value indicating the result.
|
|
219
224
|
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
|
|
220
225
|
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
221
226
|
* @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
@@ -261,14 +266,14 @@ export abstract class AbstractGraph<
|
|
|
261
266
|
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
262
267
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
263
268
|
*
|
|
264
|
-
* The function sets the weight of an edge between two
|
|
269
|
+
* The function sets the weight of an edge between two vertexMap in a graph.
|
|
265
270
|
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
266
271
|
* the source vertex of the edge.
|
|
267
272
|
* @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
268
273
|
* either a `VertexKey` or a vertex object `VO`.
|
|
269
274
|
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
270
275
|
* and the destination vertex (destOrKey).
|
|
271
|
-
* @returns a boolean value. If the edge exists between the source and destination
|
|
276
|
+
* @returns a boolean value. If the edge exists between the source and destination vertexMap, the function will update
|
|
272
277
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
273
278
|
*/
|
|
274
279
|
setEdgeWeight(srcOrKey: VertexKey | VO, destOrKey: VertexKey | VO, weight: number): boolean {
|
|
@@ -290,12 +295,12 @@ export abstract class AbstractGraph<
|
|
|
290
295
|
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
291
296
|
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
292
297
|
*
|
|
293
|
-
* The function `getAllPathsBetween` finds all paths between two
|
|
298
|
+
* The function `getAllPathsBetween` finds all paths between two vertexMap in a graph using depth-first search.
|
|
294
299
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
295
300
|
* It is the starting vertex for finding paths.
|
|
296
301
|
* @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
297
302
|
* @param limit - The count of limitation of result array.
|
|
298
|
-
* @returns The function `getAllPathsBetween` returns an array of arrays of
|
|
303
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
|
|
299
304
|
*/
|
|
300
305
|
getAllPathsBetween(v1: VO | VertexKey, v2: VO | VertexKey, limit = 1000): VO[][] {
|
|
301
306
|
const paths: VO[][] = [];
|
|
@@ -338,8 +343,8 @@ export abstract class AbstractGraph<
|
|
|
338
343
|
* Space Complexity: O(1) - Constant space.
|
|
339
344
|
*
|
|
340
345
|
* The function calculates the sum of weights along a given path.
|
|
341
|
-
* @param {VO[]} path - An array of
|
|
342
|
-
* @returns The function `getPathSumWeight` returns the sum of the weights of the
|
|
346
|
+
* @param {VO[]} path - An array of vertexMap (VO) representing a path in a graph.
|
|
347
|
+
* @returns The function `getPathSumWeight` returns the sum of the weights of the edgeMap in the given path.
|
|
343
348
|
*/
|
|
344
349
|
getPathSumWeight(path: VO[]): number {
|
|
345
350
|
let sum = 0;
|
|
@@ -358,17 +363,17 @@ export abstract class AbstractGraph<
|
|
|
358
363
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
359
364
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
360
365
|
*
|
|
361
|
-
* The function `getMinCostBetween` calculates the minimum cost between two
|
|
366
|
+
* The function `getMinCostBetween` calculates the minimum cost between two vertexMap in a graph, either based on edge
|
|
362
367
|
* weights or using a breadth-first search algorithm.
|
|
363
368
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
364
369
|
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
365
370
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
366
|
-
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph
|
|
371
|
+
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
|
|
367
372
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
368
|
-
* the
|
|
369
|
-
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two
|
|
373
|
+
* the edgeMap. If isWeight is set to false or not provided, the function will calculate the
|
|
374
|
+
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertexMap (`v1`
|
|
370
375
|
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
371
|
-
*
|
|
376
|
+
* vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
372
377
|
* minimum number of
|
|
373
378
|
*/
|
|
374
379
|
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | undefined {
|
|
@@ -425,20 +430,20 @@ export abstract class AbstractGraph<
|
|
|
425
430
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
426
431
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
427
432
|
*
|
|
428
|
-
* The function `getMinPathBetween` returns the minimum path between two
|
|
433
|
+
* The function `getMinPathBetween` returns the minimum path between two vertexMap in a graph, either based on weight or
|
|
429
434
|
* using a breadth-first search algorithm.
|
|
430
435
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
431
436
|
* object (`VO`) or a vertex ID (`VertexKey`).
|
|
432
437
|
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
433
438
|
* path.
|
|
434
|
-
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of
|
|
439
|
+
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
|
|
435
440
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
436
441
|
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
437
442
|
* @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
|
|
438
443
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
439
444
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
440
|
-
* @returns The function `getMinPathBetween` returns an array of
|
|
441
|
-
* two
|
|
445
|
+
* @returns The function `getMinPathBetween` returns an array of vertexMap (`VO[]`) representing the minimum path between
|
|
446
|
+
* two vertexMap (`v1` and `v2`). If there is no path between the vertexMap, it returns `undefined`.
|
|
442
447
|
*/
|
|
443
448
|
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS = false): VO[] | undefined {
|
|
444
449
|
if (isWeight === undefined) isWeight = false;
|
|
@@ -505,7 +510,7 @@ export abstract class AbstractGraph<
|
|
|
505
510
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
506
511
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
507
512
|
*
|
|
508
|
-
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two
|
|
513
|
+
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertexMap in
|
|
509
514
|
* a graph without using a heap data structure.
|
|
510
515
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
511
516
|
* vertex object or a vertex ID.
|
|
@@ -517,7 +522,7 @@ export abstract class AbstractGraph<
|
|
|
517
522
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
518
523
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
519
524
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
520
|
-
* shortest paths from the source vertex to all other
|
|
525
|
+
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
521
526
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
522
527
|
*/
|
|
523
528
|
dijkstraWithoutHeap(
|
|
@@ -535,7 +540,7 @@ export abstract class AbstractGraph<
|
|
|
535
540
|
let minPath: VO[] = [];
|
|
536
541
|
const paths: VO[][] = [];
|
|
537
542
|
|
|
538
|
-
const
|
|
543
|
+
const vertexMap = this._vertexMap;
|
|
539
544
|
const distMap: Map<VO, number> = new Map();
|
|
540
545
|
const seen: Set<VO> = new Set();
|
|
541
546
|
const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
|
|
@@ -547,7 +552,7 @@ export abstract class AbstractGraph<
|
|
|
547
552
|
return undefined;
|
|
548
553
|
}
|
|
549
554
|
|
|
550
|
-
for (const vertex of
|
|
555
|
+
for (const vertex of vertexMap) {
|
|
551
556
|
const vertexOrKey = vertex[1];
|
|
552
557
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
553
558
|
}
|
|
@@ -569,7 +574,7 @@ export abstract class AbstractGraph<
|
|
|
569
574
|
};
|
|
570
575
|
|
|
571
576
|
const getPaths = (minV: VO | undefined) => {
|
|
572
|
-
for (const vertex of
|
|
577
|
+
for (const vertex of vertexMap) {
|
|
573
578
|
const vertexOrKey = vertex[1];
|
|
574
579
|
|
|
575
580
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
@@ -586,7 +591,7 @@ export abstract class AbstractGraph<
|
|
|
586
591
|
}
|
|
587
592
|
};
|
|
588
593
|
|
|
589
|
-
for (let i = 1; i <
|
|
594
|
+
for (let i = 1; i < vertexMap.size; i++) {
|
|
590
595
|
const cur = getMinOfNoSeen();
|
|
591
596
|
if (cur) {
|
|
592
597
|
seen.add(cur);
|
|
@@ -638,7 +643,7 @@ export abstract class AbstractGraph<
|
|
|
638
643
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
639
644
|
*
|
|
640
645
|
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
641
|
-
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight
|
|
646
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edgeMap.
|
|
642
647
|
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
643
648
|
*
|
|
644
649
|
* /
|
|
@@ -659,13 +664,13 @@ export abstract class AbstractGraph<
|
|
|
659
664
|
* start. It can be either a vertex object or a vertex ID.
|
|
660
665
|
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
661
666
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
662
|
-
* will calculate the shortest paths to all other
|
|
667
|
+
* will calculate the shortest paths to all other vertexMap from the source vertex.
|
|
663
668
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
664
669
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
665
670
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
666
671
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
667
672
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
668
|
-
* shortest paths from the source vertex to all other
|
|
673
|
+
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
669
674
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
670
675
|
*/
|
|
671
676
|
dijkstra(
|
|
@@ -682,7 +687,7 @@ export abstract class AbstractGraph<
|
|
|
682
687
|
let minDest: VO | undefined = undefined;
|
|
683
688
|
let minPath: VO[] = [];
|
|
684
689
|
const paths: VO[][] = [];
|
|
685
|
-
const
|
|
690
|
+
const vertexMap = this._vertexMap;
|
|
686
691
|
const distMap: Map<VO, number> = new Map();
|
|
687
692
|
const seen: Set<VO> = new Set();
|
|
688
693
|
const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
|
|
@@ -692,7 +697,7 @@ export abstract class AbstractGraph<
|
|
|
692
697
|
|
|
693
698
|
if (!srcVertex) return undefined;
|
|
694
699
|
|
|
695
|
-
for (const vertex of
|
|
700
|
+
for (const vertex of vertexMap) {
|
|
696
701
|
const vertexOrKey = vertex[1];
|
|
697
702
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
698
703
|
}
|
|
@@ -704,12 +709,12 @@ export abstract class AbstractGraph<
|
|
|
704
709
|
preMap.set(srcVertex, undefined);
|
|
705
710
|
|
|
706
711
|
/**
|
|
707
|
-
* The function `getPaths` retrieves all paths from
|
|
712
|
+
* The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
|
|
708
713
|
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
709
714
|
* undefined.
|
|
710
715
|
*/
|
|
711
716
|
const getPaths = (minV: VO | undefined) => {
|
|
712
|
-
for (const vertex of
|
|
717
|
+
for (const vertex of vertexMap) {
|
|
713
718
|
const vertexOrKey = vertex[1];
|
|
714
719
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
715
720
|
const path: VO[] = [vertexOrKey];
|
|
@@ -790,16 +795,16 @@ export abstract class AbstractGraph<
|
|
|
790
795
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
791
796
|
*
|
|
792
797
|
* one to rest pairs
|
|
793
|
-
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all
|
|
798
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
794
799
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
795
|
-
* all other
|
|
800
|
+
* all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
796
801
|
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
797
802
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
798
803
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
799
804
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
800
|
-
* calculate the minimum distance from the source vertex to all other
|
|
805
|
+
* calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
|
|
801
806
|
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
802
|
-
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all
|
|
807
|
+
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
|
|
803
808
|
* vertex.
|
|
804
809
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
805
810
|
*/
|
|
@@ -818,12 +823,12 @@ export abstract class AbstractGraph<
|
|
|
818
823
|
if (scanNegativeCycle) hasNegativeCycle = false;
|
|
819
824
|
if (!srcVertex) return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
820
825
|
|
|
821
|
-
const
|
|
822
|
-
const numOfVertices =
|
|
823
|
-
const
|
|
824
|
-
const numOfEdges =
|
|
826
|
+
const vertexMap = this._vertexMap;
|
|
827
|
+
const numOfVertices = vertexMap.size;
|
|
828
|
+
const edgeMap = this.edgeSet();
|
|
829
|
+
const numOfEdges = edgeMap.length;
|
|
825
830
|
|
|
826
|
-
this.
|
|
831
|
+
this._vertexMap.forEach(vertex => {
|
|
827
832
|
distMap.set(vertex, Infinity);
|
|
828
833
|
});
|
|
829
834
|
|
|
@@ -831,10 +836,10 @@ export abstract class AbstractGraph<
|
|
|
831
836
|
|
|
832
837
|
for (let i = 1; i < numOfVertices; ++i) {
|
|
833
838
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
834
|
-
const ends = this.getEndsOfEdge(
|
|
839
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
835
840
|
if (ends) {
|
|
836
841
|
const [s, d] = ends;
|
|
837
|
-
const weight =
|
|
842
|
+
const weight = edgeMap[j].weight;
|
|
838
843
|
const sWeight = distMap.get(s);
|
|
839
844
|
const dWeight = distMap.get(d);
|
|
840
845
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
@@ -860,7 +865,7 @@ export abstract class AbstractGraph<
|
|
|
860
865
|
}
|
|
861
866
|
|
|
862
867
|
if (genPath) {
|
|
863
|
-
for (const vertex of
|
|
868
|
+
for (const vertex of vertexMap) {
|
|
864
869
|
const vertexOrKey = vertex[1];
|
|
865
870
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
866
871
|
const path: VO[] = [vertexOrKey];
|
|
@@ -877,10 +882,10 @@ export abstract class AbstractGraph<
|
|
|
877
882
|
}
|
|
878
883
|
|
|
879
884
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
880
|
-
const ends = this.getEndsOfEdge(
|
|
885
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
881
886
|
if (ends) {
|
|
882
887
|
const [s] = ends;
|
|
883
|
-
const weight =
|
|
888
|
+
const weight = edgeMap[j].weight;
|
|
884
889
|
const sWeight = distMap.get(s);
|
|
885
890
|
if (sWeight) {
|
|
886
891
|
if (sWeight !== Infinity && sWeight + weight < sWeight) hasNegativeCycle = true;
|
|
@@ -903,7 +908,7 @@ export abstract class AbstractGraph<
|
|
|
903
908
|
/**
|
|
904
909
|
* BellmanFord time:O(VE) space:O(VO)
|
|
905
910
|
* one to rest pairs
|
|
906
|
-
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all
|
|
911
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
907
912
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
908
913
|
*/
|
|
909
914
|
|
|
@@ -912,7 +917,7 @@ export abstract class AbstractGraph<
|
|
|
912
917
|
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
913
918
|
* Not support graph with negative weight cycle
|
|
914
919
|
* all pairs
|
|
915
|
-
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight
|
|
920
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
|
|
916
921
|
* /
|
|
917
922
|
|
|
918
923
|
/**
|
|
@@ -921,16 +926,16 @@ export abstract class AbstractGraph<
|
|
|
921
926
|
*
|
|
922
927
|
* Not support graph with negative weight cycle
|
|
923
928
|
* all pairs
|
|
924
|
-
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight
|
|
925
|
-
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of
|
|
929
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
|
|
930
|
+
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
|
|
926
931
|
* graph.
|
|
927
932
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
928
|
-
* property is a 2D array of numbers representing the shortest path costs between
|
|
929
|
-
* `predecessor` property is a 2D array of
|
|
930
|
-
* path between
|
|
933
|
+
* property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
|
|
934
|
+
* `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
|
|
935
|
+
* path between vertexMap in the
|
|
931
936
|
*/
|
|
932
937
|
floydWarshall(): { costs: number[][]; predecessor: (VO | undefined)[][] } {
|
|
933
|
-
const idAndVertices = [...this.
|
|
938
|
+
const idAndVertices = [...this._vertexMap];
|
|
934
939
|
const n = idAndVertices.length;
|
|
935
940
|
|
|
936
941
|
const costs: number[][] = [];
|
|
@@ -969,7 +974,7 @@ export abstract class AbstractGraph<
|
|
|
969
974
|
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
970
975
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
971
976
|
* Tarjan can find cycles in directed or undirected graph
|
|
972
|
-
* Tarjan can find the articulation points and bridges(critical
|
|
977
|
+
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
973
978
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
974
979
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
975
980
|
* /
|
|
@@ -980,22 +985,22 @@ export abstract class AbstractGraph<
|
|
|
980
985
|
*
|
|
981
986
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
982
987
|
* Tarjan can find cycles in directed or undirected graph
|
|
983
|
-
* Tarjan can find the articulation points and bridges(critical
|
|
988
|
+
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
984
989
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
985
990
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
986
991
|
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
987
992
|
* strongly connected components (SCCs), and cycles in a graph.
|
|
988
993
|
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
989
|
-
* articulation points in the graph. Articulation points are the
|
|
994
|
+
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
990
995
|
* number of connected components in the graph.
|
|
991
996
|
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
992
|
-
* (
|
|
997
|
+
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
993
998
|
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
994
999
|
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
995
1000
|
* SCCs will not be calculated or returned.
|
|
996
1001
|
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
997
1002
|
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
998
|
-
* are arrays of
|
|
1003
|
+
* are arrays of vertexMap that form cycles within the SCCs.
|
|
999
1004
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
1000
1005
|
*/
|
|
1001
1006
|
tarjan(
|
|
@@ -1016,13 +1021,13 @@ export abstract class AbstractGraph<
|
|
|
1016
1021
|
|
|
1017
1022
|
const dfnMap: Map<VO, number> = new Map();
|
|
1018
1023
|
const lowMap: Map<VO, number> = new Map();
|
|
1019
|
-
const
|
|
1020
|
-
|
|
1024
|
+
const vertexMap = this._vertexMap;
|
|
1025
|
+
vertexMap.forEach(v => {
|
|
1021
1026
|
dfnMap.set(v, -1);
|
|
1022
1027
|
lowMap.set(v, Infinity);
|
|
1023
1028
|
});
|
|
1024
1029
|
|
|
1025
|
-
const [root] =
|
|
1030
|
+
const [root] = vertexMap.values();
|
|
1026
1031
|
|
|
1027
1032
|
const cutVertexes: VO[] = [];
|
|
1028
1033
|
const bridges: EO[] = [];
|
|
@@ -1186,7 +1191,7 @@ export abstract class AbstractGraph<
|
|
|
1186
1191
|
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
|
|
1187
1192
|
* that satisfy the given predicate function.
|
|
1188
1193
|
*/
|
|
1189
|
-
filter(predicate:
|
|
1194
|
+
filter(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?: any): [VertexKey, V | undefined][] {
|
|
1190
1195
|
const filtered: [VertexKey, V | undefined][] = [];
|
|
1191
1196
|
let index = 0;
|
|
1192
1197
|
for (const [key, value] of this) {
|
|
@@ -1216,7 +1221,7 @@ export abstract class AbstractGraph<
|
|
|
1216
1221
|
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
1217
1222
|
* @returns The `map` function is returning an array of type `T[]`.
|
|
1218
1223
|
*/
|
|
1219
|
-
map<T>(callback:
|
|
1224
|
+
map<T>(callback: EntryCallback<VertexKey, V | undefined, T>, thisArg?: any): T[] {
|
|
1220
1225
|
const mapped: T[] = [];
|
|
1221
1226
|
let index = 0;
|
|
1222
1227
|
for (const [key, value] of this) {
|
|
@@ -1227,7 +1232,7 @@ export abstract class AbstractGraph<
|
|
|
1227
1232
|
}
|
|
1228
1233
|
|
|
1229
1234
|
protected* _getIterator(): IterableIterator<[VertexKey, V | undefined]> {
|
|
1230
|
-
for (const vertex of this.
|
|
1235
|
+
for (const vertex of this._vertexMap.values()) {
|
|
1231
1236
|
yield [vertex.key, vertex.value];
|
|
1232
1237
|
}
|
|
1233
1238
|
}
|
|
@@ -1239,13 +1244,13 @@ export abstract class AbstractGraph<
|
|
|
1239
1244
|
return false;
|
|
1240
1245
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
1241
1246
|
}
|
|
1242
|
-
this.
|
|
1247
|
+
this._vertexMap.set(newVertex.key, newVertex);
|
|
1243
1248
|
return true;
|
|
1244
1249
|
}
|
|
1245
1250
|
|
|
1246
1251
|
protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined {
|
|
1247
1252
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1248
|
-
return this.
|
|
1253
|
+
return this._vertexMap.get(vertexKey) || undefined;
|
|
1249
1254
|
}
|
|
1250
1255
|
|
|
1251
1256
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey {
|