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