linked-list-typed 1.48.4 → 1.48.6
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 +24 -22
- package/dist/data-structures/binary-tree/binary-tree.js +35 -22
- package/dist/data-structures/binary-tree/bst.d.ts +30 -22
- package/dist/data-structures/binary-tree/bst.js +38 -26
- 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 +52 -52
- package/dist/data-structures/graph/abstract-graph.js +78 -78
- package/dist/data-structures/graph/directed-graph.d.ts +47 -47
- package/dist/data-structures/graph/directed-graph.js +56 -56
- 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 +29 -29
- package/dist/data-structures/graph/undirected-graph.js +57 -57
- 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 +2 -2
- 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 +7 -4
- package/src/data-structures/binary-tree/binary-tree.ts +47 -27
- package/src/data-structures/binary-tree/bst.ts +52 -32
- package/src/data-structures/binary-tree/rb-tree.ts +20 -14
- package/src/data-structures/binary-tree/tree-multimap.ts +26 -18
- package/src/data-structures/graph/abstract-graph.ts +82 -82
- package/src/data-structures/graph/directed-graph.ts +56 -56
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +59 -59
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/base/base.ts +3 -3
|
@@ -46,13 +46,13 @@ class AbstractEdge {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
exports.AbstractEdge = AbstractEdge;
|
|
49
|
-
class AbstractGraph extends base_1.
|
|
49
|
+
class AbstractGraph extends base_1.IterableEntryBase {
|
|
50
50
|
constructor() {
|
|
51
51
|
super();
|
|
52
|
-
this.
|
|
52
|
+
this._vertexMap = new Map();
|
|
53
53
|
}
|
|
54
|
-
get
|
|
55
|
-
return this.
|
|
54
|
+
get vertexMap() {
|
|
55
|
+
return this._vertexMap;
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
58
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
@@ -64,12 +64,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
64
64
|
*
|
|
65
65
|
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
66
66
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
67
|
-
* the `
|
|
68
|
-
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `
|
|
67
|
+
* the `_vertexMap` map.
|
|
68
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertexMap`
|
|
69
69
|
* map. If the vertex does not exist, it returns `undefined`.
|
|
70
70
|
*/
|
|
71
71
|
getVertex(vertexKey) {
|
|
72
|
-
return this.
|
|
72
|
+
return this._vertexMap.get(vertexKey) || undefined;
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
75
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
@@ -85,7 +85,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
85
85
|
* @returns a boolean value.
|
|
86
86
|
*/
|
|
87
87
|
hasVertex(vertexOrKey) {
|
|
88
|
-
return this.
|
|
88
|
+
return this._vertexMap.has(this._getVertexKey(vertexOrKey));
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
91
91
|
* Time Complexity: O(1) - Constant time for Map operations.
|
|
@@ -119,25 +119,25 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
119
119
|
*/
|
|
120
120
|
deleteVertex(vertexOrKey) {
|
|
121
121
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
122
|
-
return this.
|
|
122
|
+
return this._vertexMap.delete(vertexKey);
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
125
|
-
* Time Complexity: O(K), where K is the number of
|
|
125
|
+
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
|
|
126
126
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
127
127
|
*/
|
|
128
128
|
/**
|
|
129
|
-
* Time Complexity: O(K), where K is the number of
|
|
129
|
+
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
|
|
130
130
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
131
131
|
*
|
|
132
|
-
* The function removes all
|
|
133
|
-
* @param {VO[] | VertexKey[]}
|
|
132
|
+
* The function removes all vertexMap from a graph and returns a boolean indicating if any vertexMap were removed.
|
|
133
|
+
* @param {VO[] | VertexKey[]} vertexMap - The `vertexMap` parameter can be either an array of vertexMap (`VO[]`) or an array
|
|
134
134
|
* of vertex IDs (`VertexKey[]`).
|
|
135
|
-
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no
|
|
135
|
+
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertexMap
|
|
136
136
|
* were removed.
|
|
137
137
|
*/
|
|
138
|
-
removeManyVertices(
|
|
138
|
+
removeManyVertices(vertexMap) {
|
|
139
139
|
const removed = [];
|
|
140
|
-
for (const v of
|
|
140
|
+
for (const v of vertexMap) {
|
|
141
141
|
removed.push(this.deleteVertex(v));
|
|
142
142
|
}
|
|
143
143
|
return removed.length > 0;
|
|
@@ -150,7 +150,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
150
150
|
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
151
151
|
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
152
152
|
*
|
|
153
|
-
* The function checks if there is an edge between two
|
|
153
|
+
* The function checks if there is an edge between two vertexMap and returns a boolean value indicating the result.
|
|
154
154
|
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
|
|
155
155
|
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
156
156
|
* @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
@@ -193,14 +193,14 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
193
193
|
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
194
194
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
195
195
|
*
|
|
196
|
-
* The function sets the weight of an edge between two
|
|
196
|
+
* The function sets the weight of an edge between two vertexMap in a graph.
|
|
197
197
|
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
198
198
|
* the source vertex of the edge.
|
|
199
199
|
* @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
200
200
|
* either a `VertexKey` or a vertex object `VO`.
|
|
201
201
|
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
202
202
|
* and the destination vertex (destOrKey).
|
|
203
|
-
* @returns a boolean value. If the edge exists between the source and destination
|
|
203
|
+
* @returns a boolean value. If the edge exists between the source and destination vertexMap, the function will update
|
|
204
204
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
205
205
|
*/
|
|
206
206
|
setEdgeWeight(srcOrKey, destOrKey, weight) {
|
|
@@ -221,12 +221,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
221
221
|
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
222
222
|
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
223
223
|
*
|
|
224
|
-
* The function `getAllPathsBetween` finds all paths between two
|
|
224
|
+
* The function `getAllPathsBetween` finds all paths between two vertexMap in a graph using depth-first search.
|
|
225
225
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
226
226
|
* It is the starting vertex for finding paths.
|
|
227
227
|
* @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
228
228
|
* @param limit - The count of limitation of result array.
|
|
229
|
-
* @returns The function `getAllPathsBetween` returns an array of arrays of
|
|
229
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
|
|
230
230
|
*/
|
|
231
231
|
getAllPathsBetween(v1, v2, limit = 1000) {
|
|
232
232
|
const paths = [];
|
|
@@ -263,8 +263,8 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
263
263
|
* Space Complexity: O(1) - Constant space.
|
|
264
264
|
*
|
|
265
265
|
* The function calculates the sum of weights along a given path.
|
|
266
|
-
* @param {VO[]} path - An array of
|
|
267
|
-
* @returns The function `getPathSumWeight` returns the sum of the weights of the
|
|
266
|
+
* @param {VO[]} path - An array of vertexMap (VO) representing a path in a graph.
|
|
267
|
+
* @returns The function `getPathSumWeight` returns the sum of the weights of the edgeMap in the given path.
|
|
268
268
|
*/
|
|
269
269
|
getPathSumWeight(path) {
|
|
270
270
|
var _a;
|
|
@@ -282,17 +282,17 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
282
282
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
283
283
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
284
284
|
*
|
|
285
|
-
* The function `getMinCostBetween` calculates the minimum cost between two
|
|
285
|
+
* The function `getMinCostBetween` calculates the minimum cost between two vertexMap in a graph, either based on edge
|
|
286
286
|
* weights or using a breadth-first search algorithm.
|
|
287
287
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
288
288
|
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
289
289
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
290
|
-
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph
|
|
290
|
+
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
|
|
291
291
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
292
|
-
* the
|
|
293
|
-
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two
|
|
292
|
+
* the edgeMap. If isWeight is set to false or not provided, the function will calculate the
|
|
293
|
+
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertexMap (`v1`
|
|
294
294
|
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
295
|
-
*
|
|
295
|
+
* vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
296
296
|
* minimum number of
|
|
297
297
|
*/
|
|
298
298
|
getMinCostBetween(v1, v2, isWeight) {
|
|
@@ -347,20 +347,20 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
347
347
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
348
348
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
349
349
|
*
|
|
350
|
-
* The function `getMinPathBetween` returns the minimum path between two
|
|
350
|
+
* The function `getMinPathBetween` returns the minimum path between two vertexMap in a graph, either based on weight or
|
|
351
351
|
* using a breadth-first search algorithm.
|
|
352
352
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
353
353
|
* object (`VO`) or a vertex ID (`VertexKey`).
|
|
354
354
|
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
355
355
|
* path.
|
|
356
|
-
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of
|
|
356
|
+
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
|
|
357
357
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
358
358
|
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
359
359
|
* @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
|
|
360
360
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
361
361
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
362
|
-
* @returns The function `getMinPathBetween` returns an array of
|
|
363
|
-
* two
|
|
362
|
+
* @returns The function `getMinPathBetween` returns an array of vertexMap (`VO[]`) representing the minimum path between
|
|
363
|
+
* two vertexMap (`v1` and `v2`). If there is no path between the vertexMap, it returns `undefined`.
|
|
364
364
|
*/
|
|
365
365
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
366
366
|
var _a, _b;
|
|
@@ -425,7 +425,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
425
425
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
426
426
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
427
427
|
*
|
|
428
|
-
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two
|
|
428
|
+
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertexMap in
|
|
429
429
|
* a graph without using a heap data structure.
|
|
430
430
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
431
431
|
* vertex object or a vertex ID.
|
|
@@ -437,7 +437,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
437
437
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
438
438
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
439
439
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
440
|
-
* shortest paths from the source vertex to all other
|
|
440
|
+
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
441
441
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
442
442
|
*/
|
|
443
443
|
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
@@ -451,7 +451,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
451
451
|
let minDest = undefined;
|
|
452
452
|
let minPath = [];
|
|
453
453
|
const paths = [];
|
|
454
|
-
const
|
|
454
|
+
const vertexMap = this._vertexMap;
|
|
455
455
|
const distMap = new Map();
|
|
456
456
|
const seen = new Set();
|
|
457
457
|
const preMap = new Map(); // predecessor
|
|
@@ -460,7 +460,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
460
460
|
if (!srcVertex) {
|
|
461
461
|
return undefined;
|
|
462
462
|
}
|
|
463
|
-
for (const vertex of
|
|
463
|
+
for (const vertex of vertexMap) {
|
|
464
464
|
const vertexOrKey = vertex[1];
|
|
465
465
|
if (vertexOrKey instanceof AbstractVertex)
|
|
466
466
|
distMap.set(vertexOrKey, Infinity);
|
|
@@ -481,7 +481,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
481
481
|
return minV;
|
|
482
482
|
};
|
|
483
483
|
const getPaths = (minV) => {
|
|
484
|
-
for (const vertex of
|
|
484
|
+
for (const vertex of vertexMap) {
|
|
485
485
|
const vertexOrKey = vertex[1];
|
|
486
486
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
487
487
|
const path = [vertexOrKey];
|
|
@@ -497,7 +497,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
497
497
|
}
|
|
498
498
|
}
|
|
499
499
|
};
|
|
500
|
-
for (let i = 1; i <
|
|
500
|
+
for (let i = 1; i < vertexMap.size; i++) {
|
|
501
501
|
const cur = getMinOfNoSeen();
|
|
502
502
|
if (cur) {
|
|
503
503
|
seen.add(cur);
|
|
@@ -546,7 +546,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
546
546
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
547
547
|
*
|
|
548
548
|
* 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.
|
|
549
|
-
* 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
|
|
549
|
+
* 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.
|
|
550
550
|
* 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.
|
|
551
551
|
*
|
|
552
552
|
* /
|
|
@@ -566,13 +566,13 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
566
566
|
* start. It can be either a vertex object or a vertex ID.
|
|
567
567
|
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
568
568
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
569
|
-
* will calculate the shortest paths to all other
|
|
569
|
+
* will calculate the shortest paths to all other vertexMap from the source vertex.
|
|
570
570
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
571
571
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
572
572
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
573
573
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
574
574
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
575
|
-
* shortest paths from the source vertex to all other
|
|
575
|
+
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
576
576
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
577
577
|
*/
|
|
578
578
|
dijkstra(src, dest, getMinDist, genPaths) {
|
|
@@ -587,7 +587,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
587
587
|
let minDest = undefined;
|
|
588
588
|
let minPath = [];
|
|
589
589
|
const paths = [];
|
|
590
|
-
const
|
|
590
|
+
const vertexMap = this._vertexMap;
|
|
591
591
|
const distMap = new Map();
|
|
592
592
|
const seen = new Set();
|
|
593
593
|
const preMap = new Map(); // predecessor
|
|
@@ -595,7 +595,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
595
595
|
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
596
596
|
if (!srcVertex)
|
|
597
597
|
return undefined;
|
|
598
|
-
for (const vertex of
|
|
598
|
+
for (const vertex of vertexMap) {
|
|
599
599
|
const vertexOrKey = vertex[1];
|
|
600
600
|
if (vertexOrKey instanceof AbstractVertex)
|
|
601
601
|
distMap.set(vertexOrKey, Infinity);
|
|
@@ -605,12 +605,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
605
605
|
distMap.set(srcVertex, 0);
|
|
606
606
|
preMap.set(srcVertex, undefined);
|
|
607
607
|
/**
|
|
608
|
-
* The function `getPaths` retrieves all paths from
|
|
608
|
+
* The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
|
|
609
609
|
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
610
610
|
* undefined.
|
|
611
611
|
*/
|
|
612
612
|
const getPaths = (minV) => {
|
|
613
|
-
for (const vertex of
|
|
613
|
+
for (const vertex of vertexMap) {
|
|
614
614
|
const vertexOrKey = vertex[1];
|
|
615
615
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
616
616
|
const path = [vertexOrKey];
|
|
@@ -688,16 +688,16 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
688
688
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
689
689
|
*
|
|
690
690
|
* one to rest pairs
|
|
691
|
-
* 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
|
|
691
|
+
* 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.
|
|
692
692
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
693
|
-
* all other
|
|
693
|
+
* all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
694
694
|
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
695
695
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
696
696
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
697
697
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
698
|
-
* calculate the minimum distance from the source vertex to all other
|
|
698
|
+
* calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
|
|
699
699
|
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
700
|
-
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all
|
|
700
|
+
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
|
|
701
701
|
* vertex.
|
|
702
702
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
703
703
|
*/
|
|
@@ -718,20 +718,20 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
718
718
|
hasNegativeCycle = false;
|
|
719
719
|
if (!srcVertex)
|
|
720
720
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
721
|
-
const
|
|
722
|
-
const numOfVertices =
|
|
723
|
-
const
|
|
724
|
-
const numOfEdges =
|
|
725
|
-
this.
|
|
721
|
+
const vertexMap = this._vertexMap;
|
|
722
|
+
const numOfVertices = vertexMap.size;
|
|
723
|
+
const edgeMap = this.edgeSet();
|
|
724
|
+
const numOfEdges = edgeMap.length;
|
|
725
|
+
this._vertexMap.forEach(vertex => {
|
|
726
726
|
distMap.set(vertex, Infinity);
|
|
727
727
|
});
|
|
728
728
|
distMap.set(srcVertex, 0);
|
|
729
729
|
for (let i = 1; i < numOfVertices; ++i) {
|
|
730
730
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
731
|
-
const ends = this.getEndsOfEdge(
|
|
731
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
732
732
|
if (ends) {
|
|
733
733
|
const [s, d] = ends;
|
|
734
|
-
const weight =
|
|
734
|
+
const weight = edgeMap[j].weight;
|
|
735
735
|
const sWeight = distMap.get(s);
|
|
736
736
|
const dWeight = distMap.get(d);
|
|
737
737
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
@@ -756,7 +756,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
756
756
|
});
|
|
757
757
|
}
|
|
758
758
|
if (genPath) {
|
|
759
|
-
for (const vertex of
|
|
759
|
+
for (const vertex of vertexMap) {
|
|
760
760
|
const vertexOrKey = vertex[1];
|
|
761
761
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
762
762
|
const path = [vertexOrKey];
|
|
@@ -773,10 +773,10 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
773
773
|
}
|
|
774
774
|
}
|
|
775
775
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
776
|
-
const ends = this.getEndsOfEdge(
|
|
776
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
777
777
|
if (ends) {
|
|
778
778
|
const [s] = ends;
|
|
779
|
-
const weight =
|
|
779
|
+
const weight = edgeMap[j].weight;
|
|
780
780
|
const sWeight = distMap.get(s);
|
|
781
781
|
if (sWeight) {
|
|
782
782
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
@@ -797,7 +797,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
797
797
|
/**
|
|
798
798
|
* BellmanFord time:O(VE) space:O(VO)
|
|
799
799
|
* one to rest pairs
|
|
800
|
-
* 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
|
|
800
|
+
* 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.
|
|
801
801
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
802
802
|
*/
|
|
803
803
|
/**
|
|
@@ -805,7 +805,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
805
805
|
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
806
806
|
* Not support graph with negative weight cycle
|
|
807
807
|
* all pairs
|
|
808
|
-
* 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
|
|
808
|
+
* 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.
|
|
809
809
|
* /
|
|
810
810
|
|
|
811
811
|
/**
|
|
@@ -814,17 +814,17 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
814
814
|
*
|
|
815
815
|
* Not support graph with negative weight cycle
|
|
816
816
|
* all pairs
|
|
817
|
-
* 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
|
|
818
|
-
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of
|
|
817
|
+
* 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.
|
|
818
|
+
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
|
|
819
819
|
* graph.
|
|
820
820
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
821
|
-
* property is a 2D array of numbers representing the shortest path costs between
|
|
822
|
-
* `predecessor` property is a 2D array of
|
|
823
|
-
* path between
|
|
821
|
+
* property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
|
|
822
|
+
* `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
|
|
823
|
+
* path between vertexMap in the
|
|
824
824
|
*/
|
|
825
825
|
floydWarshall() {
|
|
826
826
|
var _a;
|
|
827
|
-
const idAndVertices = [...this.
|
|
827
|
+
const idAndVertices = [...this._vertexMap];
|
|
828
828
|
const n = idAndVertices.length;
|
|
829
829
|
const costs = [];
|
|
830
830
|
const predecessor = [];
|
|
@@ -858,7 +858,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
858
858
|
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
859
859
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
860
860
|
* Tarjan can find cycles in directed or undirected graph
|
|
861
|
-
* Tarjan can find the articulation points and bridges(critical
|
|
861
|
+
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
862
862
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
863
863
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
864
864
|
* /
|
|
@@ -869,22 +869,22 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
869
869
|
*
|
|
870
870
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
871
871
|
* Tarjan can find cycles in directed or undirected graph
|
|
872
|
-
* Tarjan can find the articulation points and bridges(critical
|
|
872
|
+
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
873
873
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
874
874
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
875
875
|
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
876
876
|
* strongly connected components (SCCs), and cycles in a graph.
|
|
877
877
|
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
878
|
-
* articulation points in the graph. Articulation points are the
|
|
878
|
+
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
879
879
|
* number of connected components in the graph.
|
|
880
880
|
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
881
|
-
* (
|
|
881
|
+
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
882
882
|
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
883
883
|
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
884
884
|
* SCCs will not be calculated or returned.
|
|
885
885
|
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
886
886
|
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
887
|
-
* are arrays of
|
|
887
|
+
* are arrays of vertexMap that form cycles within the SCCs.
|
|
888
888
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
889
889
|
*/
|
|
890
890
|
tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
|
|
@@ -902,12 +902,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
902
902
|
needCycles = defaultConfig;
|
|
903
903
|
const dfnMap = new Map();
|
|
904
904
|
const lowMap = new Map();
|
|
905
|
-
const
|
|
906
|
-
|
|
905
|
+
const vertexMap = this._vertexMap;
|
|
906
|
+
vertexMap.forEach(v => {
|
|
907
907
|
dfnMap.set(v, -1);
|
|
908
908
|
lowMap.set(v, Infinity);
|
|
909
909
|
});
|
|
910
|
-
const [root] =
|
|
910
|
+
const [root] = vertexMap.values();
|
|
911
911
|
const cutVertexes = [];
|
|
912
912
|
const bridges = [];
|
|
913
913
|
let dfn = 0;
|
|
@@ -1092,7 +1092,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
1092
1092
|
return mapped;
|
|
1093
1093
|
}
|
|
1094
1094
|
*_getIterator() {
|
|
1095
|
-
for (const vertex of this.
|
|
1095
|
+
for (const vertex of this._vertexMap.values()) {
|
|
1096
1096
|
yield [vertex.key, vertex.value];
|
|
1097
1097
|
}
|
|
1098
1098
|
}
|
|
@@ -1101,12 +1101,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
1101
1101
|
return false;
|
|
1102
1102
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
1103
1103
|
}
|
|
1104
|
-
this.
|
|
1104
|
+
this._vertexMap.set(newVertex.key, newVertex);
|
|
1105
1105
|
return true;
|
|
1106
1106
|
}
|
|
1107
1107
|
_getVertex(vertexOrKey) {
|
|
1108
1108
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1109
|
-
return this.
|
|
1109
|
+
return this._vertexMap.get(vertexKey) || undefined;
|
|
1110
1110
|
}
|
|
1111
1111
|
_getVertexKey(vertexOrKey) {
|
|
1112
1112
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|