graph-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
|
@@ -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.
|
|
@@ -100,6 +100,10 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
100
100
|
return this._addVertexOnly(newVertex);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
+
isVertexKey(potentialKey) {
|
|
104
|
+
const potentialKeyType = typeof potentialKey;
|
|
105
|
+
return potentialKeyType === "string" || potentialKeyType === "number";
|
|
106
|
+
}
|
|
103
107
|
/**
|
|
104
108
|
* Time Complexity: O(1) - Constant time for Map operations.
|
|
105
109
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -115,25 +119,25 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
115
119
|
*/
|
|
116
120
|
deleteVertex(vertexOrKey) {
|
|
117
121
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
118
|
-
return this.
|
|
122
|
+
return this._vertexMap.delete(vertexKey);
|
|
119
123
|
}
|
|
120
124
|
/**
|
|
121
|
-
* 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.
|
|
122
126
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
123
127
|
*/
|
|
124
128
|
/**
|
|
125
|
-
* 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.
|
|
126
130
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
127
131
|
*
|
|
128
|
-
* The function removes all
|
|
129
|
-
* @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
|
|
130
134
|
* of vertex IDs (`VertexKey[]`).
|
|
131
|
-
* @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
|
|
132
136
|
* were removed.
|
|
133
137
|
*/
|
|
134
|
-
removeManyVertices(
|
|
138
|
+
removeManyVertices(vertexMap) {
|
|
135
139
|
const removed = [];
|
|
136
|
-
for (const v of
|
|
140
|
+
for (const v of vertexMap) {
|
|
137
141
|
removed.push(this.deleteVertex(v));
|
|
138
142
|
}
|
|
139
143
|
return removed.length > 0;
|
|
@@ -146,7 +150,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
146
150
|
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
147
151
|
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
148
152
|
*
|
|
149
|
-
* 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.
|
|
150
154
|
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
|
|
151
155
|
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
152
156
|
* @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
@@ -189,14 +193,14 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
189
193
|
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
190
194
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
191
195
|
*
|
|
192
|
-
* 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.
|
|
193
197
|
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
194
198
|
* the source vertex of the edge.
|
|
195
199
|
* @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
196
200
|
* either a `VertexKey` or a vertex object `VO`.
|
|
197
201
|
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
198
202
|
* and the destination vertex (destOrKey).
|
|
199
|
-
* @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
|
|
200
204
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
201
205
|
*/
|
|
202
206
|
setEdgeWeight(srcOrKey, destOrKey, weight) {
|
|
@@ -217,12 +221,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
217
221
|
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
218
222
|
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
219
223
|
*
|
|
220
|
-
* 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.
|
|
221
225
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
222
226
|
* It is the starting vertex for finding paths.
|
|
223
227
|
* @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
224
228
|
* @param limit - The count of limitation of result array.
|
|
225
|
-
* @returns The function `getAllPathsBetween` returns an array of arrays of
|
|
229
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
|
|
226
230
|
*/
|
|
227
231
|
getAllPathsBetween(v1, v2, limit = 1000) {
|
|
228
232
|
const paths = [];
|
|
@@ -259,8 +263,8 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
259
263
|
* Space Complexity: O(1) - Constant space.
|
|
260
264
|
*
|
|
261
265
|
* The function calculates the sum of weights along a given path.
|
|
262
|
-
* @param {VO[]} path - An array of
|
|
263
|
-
* @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.
|
|
264
268
|
*/
|
|
265
269
|
getPathSumWeight(path) {
|
|
266
270
|
var _a;
|
|
@@ -278,17 +282,17 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
278
282
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
279
283
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
280
284
|
*
|
|
281
|
-
* 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
|
|
282
286
|
* weights or using a breadth-first search algorithm.
|
|
283
287
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
284
288
|
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
285
289
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
286
|
-
* @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.
|
|
287
291
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
288
|
-
* the
|
|
289
|
-
* @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`
|
|
290
294
|
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
291
|
-
*
|
|
295
|
+
* vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
292
296
|
* minimum number of
|
|
293
297
|
*/
|
|
294
298
|
getMinCostBetween(v1, v2, isWeight) {
|
|
@@ -343,20 +347,20 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
343
347
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
344
348
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
345
349
|
*
|
|
346
|
-
* 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
|
|
347
351
|
* using a breadth-first search algorithm.
|
|
348
352
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
349
353
|
* object (`VO`) or a vertex ID (`VertexKey`).
|
|
350
354
|
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
351
355
|
* path.
|
|
352
|
-
* @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
|
|
353
357
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
354
358
|
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
355
359
|
* @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
|
|
356
360
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
357
361
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
358
|
-
* @returns The function `getMinPathBetween` returns an array of
|
|
359
|
-
* 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`.
|
|
360
364
|
*/
|
|
361
365
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
362
366
|
var _a, _b;
|
|
@@ -421,7 +425,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
421
425
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
422
426
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
423
427
|
*
|
|
424
|
-
* 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
|
|
425
429
|
* a graph without using a heap data structure.
|
|
426
430
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
427
431
|
* vertex object or a vertex ID.
|
|
@@ -433,7 +437,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
433
437
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
434
438
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
435
439
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
436
|
-
* 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
|
|
437
441
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
438
442
|
*/
|
|
439
443
|
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
@@ -447,7 +451,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
447
451
|
let minDest = undefined;
|
|
448
452
|
let minPath = [];
|
|
449
453
|
const paths = [];
|
|
450
|
-
const
|
|
454
|
+
const vertexMap = this._vertexMap;
|
|
451
455
|
const distMap = new Map();
|
|
452
456
|
const seen = new Set();
|
|
453
457
|
const preMap = new Map(); // predecessor
|
|
@@ -456,7 +460,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
456
460
|
if (!srcVertex) {
|
|
457
461
|
return undefined;
|
|
458
462
|
}
|
|
459
|
-
for (const vertex of
|
|
463
|
+
for (const vertex of vertexMap) {
|
|
460
464
|
const vertexOrKey = vertex[1];
|
|
461
465
|
if (vertexOrKey instanceof AbstractVertex)
|
|
462
466
|
distMap.set(vertexOrKey, Infinity);
|
|
@@ -477,7 +481,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
477
481
|
return minV;
|
|
478
482
|
};
|
|
479
483
|
const getPaths = (minV) => {
|
|
480
|
-
for (const vertex of
|
|
484
|
+
for (const vertex of vertexMap) {
|
|
481
485
|
const vertexOrKey = vertex[1];
|
|
482
486
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
483
487
|
const path = [vertexOrKey];
|
|
@@ -493,7 +497,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
493
497
|
}
|
|
494
498
|
}
|
|
495
499
|
};
|
|
496
|
-
for (let i = 1; i <
|
|
500
|
+
for (let i = 1; i < vertexMap.size; i++) {
|
|
497
501
|
const cur = getMinOfNoSeen();
|
|
498
502
|
if (cur) {
|
|
499
503
|
seen.add(cur);
|
|
@@ -542,7 +546,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
542
546
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
543
547
|
*
|
|
544
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.
|
|
545
|
-
* 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.
|
|
546
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.
|
|
547
551
|
*
|
|
548
552
|
* /
|
|
@@ -562,13 +566,13 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
562
566
|
* start. It can be either a vertex object or a vertex ID.
|
|
563
567
|
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
564
568
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
565
|
-
* will calculate the shortest paths to all other
|
|
569
|
+
* will calculate the shortest paths to all other vertexMap from the source vertex.
|
|
566
570
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
567
571
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
568
572
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
569
573
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
570
574
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
571
|
-
* 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
|
|
572
576
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
573
577
|
*/
|
|
574
578
|
dijkstra(src, dest, getMinDist, genPaths) {
|
|
@@ -583,7 +587,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
583
587
|
let minDest = undefined;
|
|
584
588
|
let minPath = [];
|
|
585
589
|
const paths = [];
|
|
586
|
-
const
|
|
590
|
+
const vertexMap = this._vertexMap;
|
|
587
591
|
const distMap = new Map();
|
|
588
592
|
const seen = new Set();
|
|
589
593
|
const preMap = new Map(); // predecessor
|
|
@@ -591,7 +595,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
591
595
|
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
592
596
|
if (!srcVertex)
|
|
593
597
|
return undefined;
|
|
594
|
-
for (const vertex of
|
|
598
|
+
for (const vertex of vertexMap) {
|
|
595
599
|
const vertexOrKey = vertex[1];
|
|
596
600
|
if (vertexOrKey instanceof AbstractVertex)
|
|
597
601
|
distMap.set(vertexOrKey, Infinity);
|
|
@@ -601,12 +605,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
601
605
|
distMap.set(srcVertex, 0);
|
|
602
606
|
preMap.set(srcVertex, undefined);
|
|
603
607
|
/**
|
|
604
|
-
* The function `getPaths` retrieves all paths from
|
|
608
|
+
* The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
|
|
605
609
|
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
606
610
|
* undefined.
|
|
607
611
|
*/
|
|
608
612
|
const getPaths = (minV) => {
|
|
609
|
-
for (const vertex of
|
|
613
|
+
for (const vertex of vertexMap) {
|
|
610
614
|
const vertexOrKey = vertex[1];
|
|
611
615
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
612
616
|
const path = [vertexOrKey];
|
|
@@ -684,16 +688,16 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
684
688
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
685
689
|
*
|
|
686
690
|
* one to rest pairs
|
|
687
|
-
* 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.
|
|
688
692
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
689
|
-
* all other
|
|
693
|
+
* all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
690
694
|
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
691
695
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
692
696
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
693
697
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
694
|
-
* 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
|
|
695
699
|
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
696
|
-
* @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
|
|
697
701
|
* vertex.
|
|
698
702
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
699
703
|
*/
|
|
@@ -714,20 +718,20 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
714
718
|
hasNegativeCycle = false;
|
|
715
719
|
if (!srcVertex)
|
|
716
720
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
717
|
-
const
|
|
718
|
-
const numOfVertices =
|
|
719
|
-
const
|
|
720
|
-
const numOfEdges =
|
|
721
|
-
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 => {
|
|
722
726
|
distMap.set(vertex, Infinity);
|
|
723
727
|
});
|
|
724
728
|
distMap.set(srcVertex, 0);
|
|
725
729
|
for (let i = 1; i < numOfVertices; ++i) {
|
|
726
730
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
727
|
-
const ends = this.getEndsOfEdge(
|
|
731
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
728
732
|
if (ends) {
|
|
729
733
|
const [s, d] = ends;
|
|
730
|
-
const weight =
|
|
734
|
+
const weight = edgeMap[j].weight;
|
|
731
735
|
const sWeight = distMap.get(s);
|
|
732
736
|
const dWeight = distMap.get(d);
|
|
733
737
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
@@ -752,7 +756,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
752
756
|
});
|
|
753
757
|
}
|
|
754
758
|
if (genPath) {
|
|
755
|
-
for (const vertex of
|
|
759
|
+
for (const vertex of vertexMap) {
|
|
756
760
|
const vertexOrKey = vertex[1];
|
|
757
761
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
758
762
|
const path = [vertexOrKey];
|
|
@@ -769,10 +773,10 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
769
773
|
}
|
|
770
774
|
}
|
|
771
775
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
772
|
-
const ends = this.getEndsOfEdge(
|
|
776
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
773
777
|
if (ends) {
|
|
774
778
|
const [s] = ends;
|
|
775
|
-
const weight =
|
|
779
|
+
const weight = edgeMap[j].weight;
|
|
776
780
|
const sWeight = distMap.get(s);
|
|
777
781
|
if (sWeight) {
|
|
778
782
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
@@ -793,7 +797,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
793
797
|
/**
|
|
794
798
|
* BellmanFord time:O(VE) space:O(VO)
|
|
795
799
|
* one to rest pairs
|
|
796
|
-
* 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.
|
|
797
801
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
798
802
|
*/
|
|
799
803
|
/**
|
|
@@ -801,7 +805,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
801
805
|
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
802
806
|
* Not support graph with negative weight cycle
|
|
803
807
|
* all pairs
|
|
804
|
-
* 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.
|
|
805
809
|
* /
|
|
806
810
|
|
|
807
811
|
/**
|
|
@@ -810,17 +814,17 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
810
814
|
*
|
|
811
815
|
* Not support graph with negative weight cycle
|
|
812
816
|
* all pairs
|
|
813
|
-
* 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
|
|
814
|
-
* 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
|
|
815
819
|
* graph.
|
|
816
820
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
817
|
-
* property is a 2D array of numbers representing the shortest path costs between
|
|
818
|
-
* `predecessor` property is a 2D array of
|
|
819
|
-
* 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
|
|
820
824
|
*/
|
|
821
825
|
floydWarshall() {
|
|
822
826
|
var _a;
|
|
823
|
-
const idAndVertices = [...this.
|
|
827
|
+
const idAndVertices = [...this._vertexMap];
|
|
824
828
|
const n = idAndVertices.length;
|
|
825
829
|
const costs = [];
|
|
826
830
|
const predecessor = [];
|
|
@@ -854,7 +858,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
854
858
|
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
855
859
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
856
860
|
* Tarjan can find cycles in directed or undirected graph
|
|
857
|
-
* 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,
|
|
858
862
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
859
863
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
860
864
|
* /
|
|
@@ -865,22 +869,22 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
865
869
|
*
|
|
866
870
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
867
871
|
* Tarjan can find cycles in directed or undirected graph
|
|
868
|
-
* 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,
|
|
869
873
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
870
874
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
871
875
|
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
872
876
|
* strongly connected components (SCCs), and cycles in a graph.
|
|
873
877
|
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
874
|
-
* 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
|
|
875
879
|
* number of connected components in the graph.
|
|
876
880
|
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
877
|
-
* (
|
|
881
|
+
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
878
882
|
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
879
883
|
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
880
884
|
* SCCs will not be calculated or returned.
|
|
881
885
|
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
882
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
|
|
883
|
-
* are arrays of
|
|
887
|
+
* are arrays of vertexMap that form cycles within the SCCs.
|
|
884
888
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
885
889
|
*/
|
|
886
890
|
tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
|
|
@@ -898,12 +902,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
898
902
|
needCycles = defaultConfig;
|
|
899
903
|
const dfnMap = new Map();
|
|
900
904
|
const lowMap = new Map();
|
|
901
|
-
const
|
|
902
|
-
|
|
905
|
+
const vertexMap = this._vertexMap;
|
|
906
|
+
vertexMap.forEach(v => {
|
|
903
907
|
dfnMap.set(v, -1);
|
|
904
908
|
lowMap.set(v, Infinity);
|
|
905
909
|
});
|
|
906
|
-
const [root] =
|
|
910
|
+
const [root] = vertexMap.values();
|
|
907
911
|
const cutVertexes = [];
|
|
908
912
|
const bridges = [];
|
|
909
913
|
let dfn = 0;
|
|
@@ -1088,7 +1092,7 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
1088
1092
|
return mapped;
|
|
1089
1093
|
}
|
|
1090
1094
|
*_getIterator() {
|
|
1091
|
-
for (const vertex of this.
|
|
1095
|
+
for (const vertex of this._vertexMap.values()) {
|
|
1092
1096
|
yield [vertex.key, vertex.value];
|
|
1093
1097
|
}
|
|
1094
1098
|
}
|
|
@@ -1097,12 +1101,12 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
1097
1101
|
return false;
|
|
1098
1102
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
1099
1103
|
}
|
|
1100
|
-
this.
|
|
1104
|
+
this._vertexMap.set(newVertex.key, newVertex);
|
|
1101
1105
|
return true;
|
|
1102
1106
|
}
|
|
1103
1107
|
_getVertex(vertexOrKey) {
|
|
1104
1108
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1105
|
-
return this.
|
|
1109
|
+
return this._vertexMap.get(vertexKey) || undefined;
|
|
1106
1110
|
}
|
|
1107
1111
|
_getVertexKey(vertexOrKey) {
|
|
1108
1112
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|