data-structure-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/CHANGELOG.md +1 -1
- package/README.md +109 -59
- package/README_zh-CN.md +1028 -0
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +204 -174
- 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 +24 -22
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +35 -22
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +30 -22
- package/dist/cjs/data-structures/binary-tree/bst.js +38 -26
- 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 +52 -52
- package/dist/cjs/data-structures/graph/abstract-graph.js +78 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +47 -47
- package/dist/cjs/data-structures/graph/directed-graph.js +56 -56
- 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 +29 -29
- package/dist/cjs/data-structures/graph/undirected-graph.js +57 -57
- 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 +2 -2
- 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 +24 -22
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +36 -23
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +30 -22
- package/dist/mjs/data-structures/binary-tree/bst.js +38 -26
- 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 +52 -52
- package/dist/mjs/data-structures/graph/abstract-graph.js +79 -79
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +47 -47
- package/dist/mjs/data-structures/graph/directed-graph.js +56 -56
- 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 +29 -29
- package/dist/mjs/data-structures/graph/undirected-graph.js +59 -59
- 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 +2 -2
- package/dist/mjs/types/data-structures/base/base.d.ts +3 -3
- package/dist/umd/data-structure-typed.js +336 -295
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- 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
- package/test/integration/bst.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/performance/data-structures/hash/hash-map.test.ts +8 -8
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +12 -1
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/performance/data-structures/queue/deque.test.ts +27 -15
- package/test/performance/data-structures/queue/queue.test.ts +27 -4
- package/test/performance/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +29 -29
- package/test/unit/data-structures/binary-tree/overall.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 +15 -15
- package/test/unit/data-structures/graph/map-graph.test.ts +3 -3
- package/test/unit/data-structures/graph/undirected-graph.test.ts +12 -12
|
@@ -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.
|
|
@@ -119,25 +119,25 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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 @@ export class AbstractGraph extends 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
|
let sum = 0;
|
|
@@ -281,17 +281,17 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
281
281
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
282
282
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
283
283
|
*
|
|
284
|
-
* 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
|
|
285
285
|
* weights or using a breadth-first search algorithm.
|
|
286
286
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
287
287
|
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
288
288
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
289
|
-
* @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.
|
|
290
290
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
291
|
-
* the
|
|
292
|
-
* @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`
|
|
293
293
|
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
294
|
-
*
|
|
294
|
+
* vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
295
295
|
* minimum number of
|
|
296
296
|
*/
|
|
297
297
|
getMinCostBetween(v1, v2, isWeight) {
|
|
@@ -346,20 +346,20 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
346
346
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
347
347
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
348
348
|
*
|
|
349
|
-
* 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
|
|
350
350
|
* using a breadth-first search algorithm.
|
|
351
351
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
352
352
|
* object (`VO`) or a vertex ID (`VertexKey`).
|
|
353
353
|
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
354
354
|
* path.
|
|
355
|
-
* @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
|
|
356
356
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
357
357
|
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
358
358
|
* @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
|
|
359
359
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
360
360
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
361
|
-
* @returns The function `getMinPathBetween` returns an array of
|
|
362
|
-
* 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`.
|
|
363
363
|
*/
|
|
364
364
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
365
365
|
if (isWeight === undefined)
|
|
@@ -423,7 +423,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
423
423
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
424
424
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
425
425
|
*
|
|
426
|
-
* 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
|
|
427
427
|
* a graph without using a heap data structure.
|
|
428
428
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
429
429
|
* vertex object or a vertex ID.
|
|
@@ -435,7 +435,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
435
435
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
436
436
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
437
437
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
438
|
-
* 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
|
|
439
439
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
440
440
|
*/
|
|
441
441
|
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
@@ -449,7 +449,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
449
449
|
let minDest = undefined;
|
|
450
450
|
let minPath = [];
|
|
451
451
|
const paths = [];
|
|
452
|
-
const
|
|
452
|
+
const vertexMap = this._vertexMap;
|
|
453
453
|
const distMap = new Map();
|
|
454
454
|
const seen = new Set();
|
|
455
455
|
const preMap = new Map(); // predecessor
|
|
@@ -458,7 +458,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
458
458
|
if (!srcVertex) {
|
|
459
459
|
return undefined;
|
|
460
460
|
}
|
|
461
|
-
for (const vertex of
|
|
461
|
+
for (const vertex of vertexMap) {
|
|
462
462
|
const vertexOrKey = vertex[1];
|
|
463
463
|
if (vertexOrKey instanceof AbstractVertex)
|
|
464
464
|
distMap.set(vertexOrKey, Infinity);
|
|
@@ -479,7 +479,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
479
479
|
return minV;
|
|
480
480
|
};
|
|
481
481
|
const getPaths = (minV) => {
|
|
482
|
-
for (const vertex of
|
|
482
|
+
for (const vertex of vertexMap) {
|
|
483
483
|
const vertexOrKey = vertex[1];
|
|
484
484
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
485
485
|
const path = [vertexOrKey];
|
|
@@ -495,7 +495,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
495
495
|
}
|
|
496
496
|
}
|
|
497
497
|
};
|
|
498
|
-
for (let i = 1; i <
|
|
498
|
+
for (let i = 1; i < vertexMap.size; i++) {
|
|
499
499
|
const cur = getMinOfNoSeen();
|
|
500
500
|
if (cur) {
|
|
501
501
|
seen.add(cur);
|
|
@@ -544,7 +544,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
544
544
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
545
545
|
*
|
|
546
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.
|
|
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
|
|
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.
|
|
548
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.
|
|
549
549
|
*
|
|
550
550
|
* /
|
|
@@ -564,13 +564,13 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
564
564
|
* start. It can be either a vertex object or a vertex ID.
|
|
565
565
|
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
566
566
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
567
|
-
* will calculate the shortest paths to all other
|
|
567
|
+
* will calculate the shortest paths to all other vertexMap from the source vertex.
|
|
568
568
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
569
569
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
570
570
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
571
571
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
572
572
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
573
|
-
* 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
|
|
574
574
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
575
575
|
*/
|
|
576
576
|
dijkstra(src, dest, getMinDist, genPaths) {
|
|
@@ -584,7 +584,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
584
584
|
let minDest = undefined;
|
|
585
585
|
let minPath = [];
|
|
586
586
|
const paths = [];
|
|
587
|
-
const
|
|
587
|
+
const vertexMap = this._vertexMap;
|
|
588
588
|
const distMap = new Map();
|
|
589
589
|
const seen = new Set();
|
|
590
590
|
const preMap = new Map(); // predecessor
|
|
@@ -592,7 +592,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
592
592
|
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
593
593
|
if (!srcVertex)
|
|
594
594
|
return undefined;
|
|
595
|
-
for (const vertex of
|
|
595
|
+
for (const vertex of vertexMap) {
|
|
596
596
|
const vertexOrKey = vertex[1];
|
|
597
597
|
if (vertexOrKey instanceof AbstractVertex)
|
|
598
598
|
distMap.set(vertexOrKey, Infinity);
|
|
@@ -602,12 +602,12 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
602
602
|
distMap.set(srcVertex, 0);
|
|
603
603
|
preMap.set(srcVertex, undefined);
|
|
604
604
|
/**
|
|
605
|
-
* The function `getPaths` retrieves all paths from
|
|
605
|
+
* The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
|
|
606
606
|
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
607
607
|
* undefined.
|
|
608
608
|
*/
|
|
609
609
|
const getPaths = (minV) => {
|
|
610
|
-
for (const vertex of
|
|
610
|
+
for (const vertex of vertexMap) {
|
|
611
611
|
const vertexOrKey = vertex[1];
|
|
612
612
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
613
613
|
const path = [vertexOrKey];
|
|
@@ -685,16 +685,16 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
685
685
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
686
686
|
*
|
|
687
687
|
* one to rest pairs
|
|
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
|
|
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.
|
|
689
689
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
690
|
-
* all other
|
|
690
|
+
* all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
691
691
|
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
692
692
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
693
693
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
694
694
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
695
|
-
* 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
|
|
696
696
|
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
697
|
-
* @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
|
|
698
698
|
* vertex.
|
|
699
699
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
700
700
|
*/
|
|
@@ -715,20 +715,20 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
715
715
|
hasNegativeCycle = false;
|
|
716
716
|
if (!srcVertex)
|
|
717
717
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
718
|
-
const
|
|
719
|
-
const numOfVertices =
|
|
720
|
-
const
|
|
721
|
-
const numOfEdges =
|
|
722
|
-
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 => {
|
|
723
723
|
distMap.set(vertex, Infinity);
|
|
724
724
|
});
|
|
725
725
|
distMap.set(srcVertex, 0);
|
|
726
726
|
for (let i = 1; i < numOfVertices; ++i) {
|
|
727
727
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
728
|
-
const ends = this.getEndsOfEdge(
|
|
728
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
729
729
|
if (ends) {
|
|
730
730
|
const [s, d] = ends;
|
|
731
|
-
const weight =
|
|
731
|
+
const weight = edgeMap[j].weight;
|
|
732
732
|
const sWeight = distMap.get(s);
|
|
733
733
|
const dWeight = distMap.get(d);
|
|
734
734
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
@@ -753,7 +753,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
753
753
|
});
|
|
754
754
|
}
|
|
755
755
|
if (genPath) {
|
|
756
|
-
for (const vertex of
|
|
756
|
+
for (const vertex of vertexMap) {
|
|
757
757
|
const vertexOrKey = vertex[1];
|
|
758
758
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
759
759
|
const path = [vertexOrKey];
|
|
@@ -770,10 +770,10 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
770
770
|
}
|
|
771
771
|
}
|
|
772
772
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
773
|
-
const ends = this.getEndsOfEdge(
|
|
773
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
774
774
|
if (ends) {
|
|
775
775
|
const [s] = ends;
|
|
776
|
-
const weight =
|
|
776
|
+
const weight = edgeMap[j].weight;
|
|
777
777
|
const sWeight = distMap.get(s);
|
|
778
778
|
if (sWeight) {
|
|
779
779
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
@@ -794,7 +794,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
794
794
|
/**
|
|
795
795
|
* BellmanFord time:O(VE) space:O(VO)
|
|
796
796
|
* one to rest pairs
|
|
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
|
|
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.
|
|
798
798
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
799
799
|
*/
|
|
800
800
|
/**
|
|
@@ -802,7 +802,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
802
802
|
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
803
803
|
* Not support graph with negative weight cycle
|
|
804
804
|
* all pairs
|
|
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
|
|
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.
|
|
806
806
|
* /
|
|
807
807
|
|
|
808
808
|
/**
|
|
@@ -811,16 +811,16 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
811
811
|
*
|
|
812
812
|
* Not support graph with negative weight cycle
|
|
813
813
|
* all pairs
|
|
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
|
|
815
|
-
* 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
|
|
816
816
|
* graph.
|
|
817
817
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
818
|
-
* property is a 2D array of numbers representing the shortest path costs between
|
|
819
|
-
* `predecessor` property is a 2D array of
|
|
820
|
-
* 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
|
|
821
821
|
*/
|
|
822
822
|
floydWarshall() {
|
|
823
|
-
const idAndVertices = [...this.
|
|
823
|
+
const idAndVertices = [...this._vertexMap];
|
|
824
824
|
const n = idAndVertices.length;
|
|
825
825
|
const costs = [];
|
|
826
826
|
const predecessor = [];
|
|
@@ -854,7 +854,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
854
854
|
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
855
855
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
856
856
|
* Tarjan can find cycles in directed or undirected graph
|
|
857
|
-
* 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,
|
|
858
858
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
859
859
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
860
860
|
* /
|
|
@@ -865,22 +865,22 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
865
865
|
*
|
|
866
866
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
867
867
|
* Tarjan can find cycles in directed or undirected graph
|
|
868
|
-
* 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,
|
|
869
869
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
870
870
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
871
871
|
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
872
872
|
* strongly connected components (SCCs), and cycles in a graph.
|
|
873
873
|
* @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
|
|
874
|
+
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
875
875
|
* number of connected components in the graph.
|
|
876
876
|
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
877
|
-
* (
|
|
877
|
+
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
878
878
|
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
879
879
|
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
880
880
|
* SCCs will not be calculated or returned.
|
|
881
881
|
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
882
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
|
|
883
|
-
* are arrays of
|
|
883
|
+
* are arrays of vertexMap that form cycles within the SCCs.
|
|
884
884
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
885
885
|
*/
|
|
886
886
|
tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
|
|
@@ -898,12 +898,12 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
898
898
|
needCycles = defaultConfig;
|
|
899
899
|
const dfnMap = new Map();
|
|
900
900
|
const lowMap = new Map();
|
|
901
|
-
const
|
|
902
|
-
|
|
901
|
+
const vertexMap = this._vertexMap;
|
|
902
|
+
vertexMap.forEach(v => {
|
|
903
903
|
dfnMap.set(v, -1);
|
|
904
904
|
lowMap.set(v, Infinity);
|
|
905
905
|
});
|
|
906
|
-
const [root] =
|
|
906
|
+
const [root] = vertexMap.values();
|
|
907
907
|
const cutVertexes = [];
|
|
908
908
|
const bridges = [];
|
|
909
909
|
let dfn = 0;
|
|
@@ -1087,7 +1087,7 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
1087
1087
|
return mapped;
|
|
1088
1088
|
}
|
|
1089
1089
|
*_getIterator() {
|
|
1090
|
-
for (const vertex of this.
|
|
1090
|
+
for (const vertex of this._vertexMap.values()) {
|
|
1091
1091
|
yield [vertex.key, vertex.value];
|
|
1092
1092
|
}
|
|
1093
1093
|
}
|
|
@@ -1096,12 +1096,12 @@ export class AbstractGraph extends IterablePairBase {
|
|
|
1096
1096
|
return false;
|
|
1097
1097
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
1098
1098
|
}
|
|
1099
|
-
this.
|
|
1099
|
+
this._vertexMap.set(newVertex.key, newVertex);
|
|
1100
1100
|
return true;
|
|
1101
1101
|
}
|
|
1102
1102
|
_getVertex(vertexOrKey) {
|
|
1103
1103
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1104
|
-
return this.
|
|
1104
|
+
return this._vertexMap.get(vertexKey) || undefined;
|
|
1105
1105
|
}
|
|
1106
1106
|
_getVertexKey(vertexOrKey) {
|
|
1107
1107
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|