data-structure-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/CHANGELOG.md +1 -1
- package/README.md +6 -6
- package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/cjs/data-structures/base/iterable-base.js +3 -3
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +16 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/cjs/data-structures/binary-tree/bst.js +17 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +53 -52
- package/dist/cjs/data-structures/graph/abstract-graph.js +82 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/cjs/data-structures/graph/directed-graph.js +111 -65
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/cjs/data-structures/graph/map-graph.js +8 -8
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/cjs/data-structures/graph/undirected-graph.js +117 -54
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +3 -3
- package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/mjs/data-structures/base/iterable-base.js +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +17 -14
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +53 -52
- package/dist/mjs/data-structures/graph/abstract-graph.js +83 -79
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/mjs/data-structures/graph/directed-graph.js +111 -65
- package/dist/mjs/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/mjs/data-structures/graph/map-graph.js +8 -8
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/mjs/data-structures/graph/undirected-graph.js +119 -56
- package/dist/mjs/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/mjs/data-structures/hash/hash-map.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/base/base.d.ts +3 -3
- package/dist/umd/data-structure-typed.js +413 -285
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +3 -3
- package/test/unit/data-structures/graph/directed-graph.test.ts +52 -15
- package/test/unit/data-structures/graph/map-graph.test.ts +3 -3
- package/test/unit/data-structures/graph/undirected-graph.test.ts +42 -5
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { uuidV4 } from '../../utils';
|
|
9
9
|
import { PriorityQueue } from '../priority-queue';
|
|
10
10
|
import { Queue } from '../queue';
|
|
11
|
-
import {
|
|
11
|
+
import { IterableEntryBase } from "../base";
|
|
12
12
|
export class AbstractVertex {
|
|
13
13
|
key;
|
|
14
14
|
value;
|
|
@@ -46,13 +46,13 @@ export class AbstractEdge {
|
|
|
46
46
|
return this._hashCode;
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
export class AbstractGraph extends
|
|
49
|
+
export class AbstractGraph extends IterableEntryBase {
|
|
50
50
|
constructor() {
|
|
51
51
|
super();
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
get
|
|
55
|
-
return this.
|
|
53
|
+
_vertexMap = new Map();
|
|
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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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
|
let sum = 0;
|
|
@@ -277,17 +281,17 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
277
281
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
278
282
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
279
283
|
*
|
|
280
|
-
* The function `getMinCostBetween` calculates the minimum cost between two
|
|
284
|
+
* The function `getMinCostBetween` calculates the minimum cost between two vertexMap in a graph, either based on edge
|
|
281
285
|
* weights or using a breadth-first search algorithm.
|
|
282
286
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
283
287
|
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
284
288
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
285
|
-
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph
|
|
289
|
+
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
|
|
286
290
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
287
|
-
* the
|
|
288
|
-
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two
|
|
291
|
+
* the edgeMap. If isWeight is set to false or not provided, the function will calculate the
|
|
292
|
+
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertexMap (`v1`
|
|
289
293
|
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
290
|
-
*
|
|
294
|
+
* vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
291
295
|
* minimum number of
|
|
292
296
|
*/
|
|
293
297
|
getMinCostBetween(v1, v2, isWeight) {
|
|
@@ -342,20 +346,20 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
342
346
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
343
347
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
344
348
|
*
|
|
345
|
-
* The function `getMinPathBetween` returns the minimum path between two
|
|
349
|
+
* The function `getMinPathBetween` returns the minimum path between two vertexMap in a graph, either based on weight or
|
|
346
350
|
* using a breadth-first search algorithm.
|
|
347
351
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
348
352
|
* object (`VO`) or a vertex ID (`VertexKey`).
|
|
349
353
|
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
350
354
|
* path.
|
|
351
|
-
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of
|
|
355
|
+
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
|
|
352
356
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
353
357
|
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
354
358
|
* @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
|
|
355
359
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
356
360
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
357
|
-
* @returns The function `getMinPathBetween` returns an array of
|
|
358
|
-
* two
|
|
361
|
+
* @returns The function `getMinPathBetween` returns an array of vertexMap (`VO[]`) representing the minimum path between
|
|
362
|
+
* two vertexMap (`v1` and `v2`). If there is no path between the vertexMap, it returns `undefined`.
|
|
359
363
|
*/
|
|
360
364
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
361
365
|
if (isWeight === undefined)
|
|
@@ -419,7 +423,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
419
423
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
420
424
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
421
425
|
*
|
|
422
|
-
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two
|
|
426
|
+
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertexMap in
|
|
423
427
|
* a graph without using a heap data structure.
|
|
424
428
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
425
429
|
* vertex object or a vertex ID.
|
|
@@ -431,7 +435,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
431
435
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
432
436
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
433
437
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
434
|
-
* shortest paths from the source vertex to all other
|
|
438
|
+
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
435
439
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
436
440
|
*/
|
|
437
441
|
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
@@ -445,7 +449,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
445
449
|
let minDest = undefined;
|
|
446
450
|
let minPath = [];
|
|
447
451
|
const paths = [];
|
|
448
|
-
const
|
|
452
|
+
const vertexMap = this._vertexMap;
|
|
449
453
|
const distMap = new Map();
|
|
450
454
|
const seen = new Set();
|
|
451
455
|
const preMap = new Map(); // predecessor
|
|
@@ -454,7 +458,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
454
458
|
if (!srcVertex) {
|
|
455
459
|
return undefined;
|
|
456
460
|
}
|
|
457
|
-
for (const vertex of
|
|
461
|
+
for (const vertex of vertexMap) {
|
|
458
462
|
const vertexOrKey = vertex[1];
|
|
459
463
|
if (vertexOrKey instanceof AbstractVertex)
|
|
460
464
|
distMap.set(vertexOrKey, Infinity);
|
|
@@ -475,7 +479,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
475
479
|
return minV;
|
|
476
480
|
};
|
|
477
481
|
const getPaths = (minV) => {
|
|
478
|
-
for (const vertex of
|
|
482
|
+
for (const vertex of vertexMap) {
|
|
479
483
|
const vertexOrKey = vertex[1];
|
|
480
484
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
481
485
|
const path = [vertexOrKey];
|
|
@@ -491,7 +495,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
491
495
|
}
|
|
492
496
|
}
|
|
493
497
|
};
|
|
494
|
-
for (let i = 1; i <
|
|
498
|
+
for (let i = 1; i < vertexMap.size; i++) {
|
|
495
499
|
const cur = getMinOfNoSeen();
|
|
496
500
|
if (cur) {
|
|
497
501
|
seen.add(cur);
|
|
@@ -540,7 +544,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
540
544
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
541
545
|
*
|
|
542
546
|
* 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.
|
|
543
|
-
* 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
|
|
547
|
+
* 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.
|
|
544
548
|
* 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.
|
|
545
549
|
*
|
|
546
550
|
* /
|
|
@@ -560,13 +564,13 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
560
564
|
* start. It can be either a vertex object or a vertex ID.
|
|
561
565
|
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
562
566
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
563
|
-
* will calculate the shortest paths to all other
|
|
567
|
+
* will calculate the shortest paths to all other vertexMap from the source vertex.
|
|
564
568
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
565
569
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
566
570
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
567
571
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
568
572
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
569
|
-
* shortest paths from the source vertex to all other
|
|
573
|
+
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
570
574
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
571
575
|
*/
|
|
572
576
|
dijkstra(src, dest, getMinDist, genPaths) {
|
|
@@ -580,7 +584,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
580
584
|
let minDest = undefined;
|
|
581
585
|
let minPath = [];
|
|
582
586
|
const paths = [];
|
|
583
|
-
const
|
|
587
|
+
const vertexMap = this._vertexMap;
|
|
584
588
|
const distMap = new Map();
|
|
585
589
|
const seen = new Set();
|
|
586
590
|
const preMap = new Map(); // predecessor
|
|
@@ -588,7 +592,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
588
592
|
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
589
593
|
if (!srcVertex)
|
|
590
594
|
return undefined;
|
|
591
|
-
for (const vertex of
|
|
595
|
+
for (const vertex of vertexMap) {
|
|
592
596
|
const vertexOrKey = vertex[1];
|
|
593
597
|
if (vertexOrKey instanceof AbstractVertex)
|
|
594
598
|
distMap.set(vertexOrKey, Infinity);
|
|
@@ -598,12 +602,12 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
598
602
|
distMap.set(srcVertex, 0);
|
|
599
603
|
preMap.set(srcVertex, undefined);
|
|
600
604
|
/**
|
|
601
|
-
* The function `getPaths` retrieves all paths from
|
|
605
|
+
* The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
|
|
602
606
|
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
603
607
|
* undefined.
|
|
604
608
|
*/
|
|
605
609
|
const getPaths = (minV) => {
|
|
606
|
-
for (const vertex of
|
|
610
|
+
for (const vertex of vertexMap) {
|
|
607
611
|
const vertexOrKey = vertex[1];
|
|
608
612
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
609
613
|
const path = [vertexOrKey];
|
|
@@ -681,16 +685,16 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
681
685
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
682
686
|
*
|
|
683
687
|
* one to rest pairs
|
|
684
|
-
* 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
|
|
688
|
+
* 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.
|
|
685
689
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
686
|
-
* all other
|
|
690
|
+
* all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
687
691
|
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
688
692
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
689
693
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
690
694
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
691
|
-
* calculate the minimum distance from the source vertex to all other
|
|
695
|
+
* calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
|
|
692
696
|
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
693
|
-
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all
|
|
697
|
+
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
|
|
694
698
|
* vertex.
|
|
695
699
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
696
700
|
*/
|
|
@@ -711,20 +715,20 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
711
715
|
hasNegativeCycle = false;
|
|
712
716
|
if (!srcVertex)
|
|
713
717
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
714
|
-
const
|
|
715
|
-
const numOfVertices =
|
|
716
|
-
const
|
|
717
|
-
const numOfEdges =
|
|
718
|
-
this.
|
|
718
|
+
const vertexMap = this._vertexMap;
|
|
719
|
+
const numOfVertices = vertexMap.size;
|
|
720
|
+
const edgeMap = this.edgeSet();
|
|
721
|
+
const numOfEdges = edgeMap.length;
|
|
722
|
+
this._vertexMap.forEach(vertex => {
|
|
719
723
|
distMap.set(vertex, Infinity);
|
|
720
724
|
});
|
|
721
725
|
distMap.set(srcVertex, 0);
|
|
722
726
|
for (let i = 1; i < numOfVertices; ++i) {
|
|
723
727
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
724
|
-
const ends = this.getEndsOfEdge(
|
|
728
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
725
729
|
if (ends) {
|
|
726
730
|
const [s, d] = ends;
|
|
727
|
-
const weight =
|
|
731
|
+
const weight = edgeMap[j].weight;
|
|
728
732
|
const sWeight = distMap.get(s);
|
|
729
733
|
const dWeight = distMap.get(d);
|
|
730
734
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
@@ -749,7 +753,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
749
753
|
});
|
|
750
754
|
}
|
|
751
755
|
if (genPath) {
|
|
752
|
-
for (const vertex of
|
|
756
|
+
for (const vertex of vertexMap) {
|
|
753
757
|
const vertexOrKey = vertex[1];
|
|
754
758
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
755
759
|
const path = [vertexOrKey];
|
|
@@ -766,10 +770,10 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
766
770
|
}
|
|
767
771
|
}
|
|
768
772
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
769
|
-
const ends = this.getEndsOfEdge(
|
|
773
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
770
774
|
if (ends) {
|
|
771
775
|
const [s] = ends;
|
|
772
|
-
const weight =
|
|
776
|
+
const weight = edgeMap[j].weight;
|
|
773
777
|
const sWeight = distMap.get(s);
|
|
774
778
|
if (sWeight) {
|
|
775
779
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
@@ -790,7 +794,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
790
794
|
/**
|
|
791
795
|
* BellmanFord time:O(VE) space:O(VO)
|
|
792
796
|
* 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
|
|
797
|
+
* 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
798
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
795
799
|
*/
|
|
796
800
|
/**
|
|
@@ -798,7 +802,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
798
802
|
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
799
803
|
* Not support graph with negative weight cycle
|
|
800
804
|
* all pairs
|
|
801
|
-
* 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
|
|
805
|
+
* 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.
|
|
802
806
|
* /
|
|
803
807
|
|
|
804
808
|
/**
|
|
@@ -807,16 +811,16 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
807
811
|
*
|
|
808
812
|
* Not support graph with negative weight cycle
|
|
809
813
|
* all pairs
|
|
810
|
-
* 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
|
|
811
|
-
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of
|
|
814
|
+
* 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.
|
|
815
|
+
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
|
|
812
816
|
* graph.
|
|
813
817
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
814
|
-
* property is a 2D array of numbers representing the shortest path costs between
|
|
815
|
-
* `predecessor` property is a 2D array of
|
|
816
|
-
* path between
|
|
818
|
+
* property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
|
|
819
|
+
* `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
|
|
820
|
+
* path between vertexMap in the
|
|
817
821
|
*/
|
|
818
822
|
floydWarshall() {
|
|
819
|
-
const idAndVertices = [...this.
|
|
823
|
+
const idAndVertices = [...this._vertexMap];
|
|
820
824
|
const n = idAndVertices.length;
|
|
821
825
|
const costs = [];
|
|
822
826
|
const predecessor = [];
|
|
@@ -850,7 +854,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
850
854
|
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
851
855
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
852
856
|
* Tarjan can find cycles in directed or undirected graph
|
|
853
|
-
* Tarjan can find the articulation points and bridges(critical
|
|
857
|
+
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
854
858
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
855
859
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
856
860
|
* /
|
|
@@ -861,22 +865,22 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
861
865
|
*
|
|
862
866
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
863
867
|
* Tarjan can find cycles in directed or undirected graph
|
|
864
|
-
* Tarjan can find the articulation points and bridges(critical
|
|
868
|
+
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
865
869
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
866
870
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
867
871
|
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
868
872
|
* strongly connected components (SCCs), and cycles in a graph.
|
|
869
873
|
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
870
|
-
* articulation points in the graph. Articulation points are the
|
|
874
|
+
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
871
875
|
* number of connected components in the graph.
|
|
872
876
|
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
873
|
-
* (
|
|
877
|
+
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
874
878
|
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
875
879
|
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
876
880
|
* SCCs will not be calculated or returned.
|
|
877
881
|
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
878
882
|
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
879
|
-
* are arrays of
|
|
883
|
+
* are arrays of vertexMap that form cycles within the SCCs.
|
|
880
884
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
881
885
|
*/
|
|
882
886
|
tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
|
|
@@ -894,12 +898,12 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
894
898
|
needCycles = defaultConfig;
|
|
895
899
|
const dfnMap = new Map();
|
|
896
900
|
const lowMap = new Map();
|
|
897
|
-
const
|
|
898
|
-
|
|
901
|
+
const vertexMap = this._vertexMap;
|
|
902
|
+
vertexMap.forEach(v => {
|
|
899
903
|
dfnMap.set(v, -1);
|
|
900
904
|
lowMap.set(v, Infinity);
|
|
901
905
|
});
|
|
902
|
-
const [root] =
|
|
906
|
+
const [root] = vertexMap.values();
|
|
903
907
|
const cutVertexes = [];
|
|
904
908
|
const bridges = [];
|
|
905
909
|
let dfn = 0;
|
|
@@ -1083,7 +1087,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
1083
1087
|
return mapped;
|
|
1084
1088
|
}
|
|
1085
1089
|
*_getIterator() {
|
|
1086
|
-
for (const vertex of this.
|
|
1090
|
+
for (const vertex of this._vertexMap.values()) {
|
|
1087
1091
|
yield [vertex.key, vertex.value];
|
|
1088
1092
|
}
|
|
1089
1093
|
}
|
|
@@ -1092,12 +1096,12 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
1092
1096
|
return false;
|
|
1093
1097
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
1094
1098
|
}
|
|
1095
|
-
this.
|
|
1099
|
+
this._vertexMap.set(newVertex.key, newVertex);
|
|
1096
1100
|
return true;
|
|
1097
1101
|
}
|
|
1098
1102
|
_getVertex(vertexOrKey) {
|
|
1099
1103
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1100
|
-
return this.
|
|
1104
|
+
return this._vertexMap.get(vertexKey) || undefined;
|
|
1101
1105
|
}
|
|
1102
1106
|
_getVertexKey(vertexOrKey) {
|
|
1103
1107
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|