directed-graph-typed 1.48.0 → 1.49.0

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.
Files changed (89) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +28 -19
  6. package/dist/data-structures/binary-tree/avl-tree.js +22 -11
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -152
  8. package/dist/data-structures/binary-tree/binary-tree.js +241 -215
  9. package/dist/data-structures/binary-tree/bst.d.ts +64 -48
  10. package/dist/data-structures/binary-tree/bst.js +94 -65
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +39 -39
  12. package/dist/data-structures/binary-tree/rb-tree.js +42 -49
  13. package/dist/data-structures/binary-tree/tree-multimap.d.ts +60 -34
  14. package/dist/data-structures/binary-tree/tree-multimap.js +59 -27
  15. package/dist/data-structures/graph/abstract-graph.d.ts +92 -53
  16. package/dist/data-structures/graph/abstract-graph.js +130 -103
  17. package/dist/data-structures/graph/directed-graph.d.ts +70 -52
  18. package/dist/data-structures/graph/directed-graph.js +111 -65
  19. package/dist/data-structures/graph/map-graph.d.ts +5 -5
  20. package/dist/data-structures/graph/map-graph.js +8 -8
  21. package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
  22. package/dist/data-structures/graph/undirected-graph.js +117 -54
  23. package/dist/data-structures/hash/hash-map.d.ts +160 -44
  24. package/dist/data-structures/hash/hash-map.js +314 -82
  25. package/dist/data-structures/heap/heap.d.ts +50 -7
  26. package/dist/data-structures/heap/heap.js +60 -30
  27. package/dist/data-structures/index.d.ts +1 -0
  28. package/dist/data-structures/index.js +1 -0
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +42 -55
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +50 -77
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -55
  32. package/dist/data-structures/linked-list/singly-linked-list.js +44 -77
  33. package/dist/data-structures/queue/deque.d.ts +35 -167
  34. package/dist/data-structures/queue/deque.js +43 -249
  35. package/dist/data-structures/queue/queue.d.ts +49 -48
  36. package/dist/data-structures/queue/queue.js +69 -82
  37. package/dist/data-structures/stack/stack.d.ts +43 -10
  38. package/dist/data-structures/stack/stack.js +50 -31
  39. package/dist/data-structures/trie/trie.d.ts +41 -6
  40. package/dist/data-structures/trie/trie.js +53 -32
  41. package/dist/interfaces/binary-tree.d.ts +6 -6
  42. package/dist/types/common.d.ts +11 -8
  43. package/dist/types/common.js +6 -1
  44. package/dist/types/data-structures/base/base.d.ts +5 -0
  45. package/dist/types/data-structures/base/base.js +2 -0
  46. package/dist/types/data-structures/base/index.d.ts +1 -0
  47. package/dist/types/data-structures/base/index.js +17 -0
  48. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  49. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  50. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
  51. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  52. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  53. package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
  54. package/dist/types/data-structures/index.d.ts +1 -0
  55. package/dist/types/data-structures/index.js +1 -0
  56. package/package.json +2 -2
  57. package/src/data-structures/base/index.ts +1 -0
  58. package/src/data-structures/base/iterable-base.ts +329 -0
  59. package/src/data-structures/binary-tree/avl-tree.ts +37 -25
  60. package/src/data-structures/binary-tree/binary-tree.ts +336 -296
  61. package/src/data-structures/binary-tree/bst.ts +135 -89
  62. package/src/data-structures/binary-tree/rb-tree.ts +60 -69
  63. package/src/data-structures/binary-tree/tree-multimap.ts +86 -49
  64. package/src/data-structures/graph/abstract-graph.ts +136 -104
  65. package/src/data-structures/graph/directed-graph.ts +114 -65
  66. package/src/data-structures/graph/map-graph.ts +8 -8
  67. package/src/data-structures/graph/undirected-graph.ts +124 -56
  68. package/src/data-structures/hash/hash-map.ts +335 -84
  69. package/src/data-structures/heap/heap.ts +63 -36
  70. package/src/data-structures/index.ts +1 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +54 -83
  72. package/src/data-structures/linked-list/singly-linked-list.ts +49 -84
  73. package/src/data-structures/queue/deque.ts +43 -275
  74. package/src/data-structures/queue/queue.ts +71 -86
  75. package/src/data-structures/stack/stack.ts +53 -34
  76. package/src/data-structures/trie/trie.ts +58 -35
  77. package/src/interfaces/binary-tree.ts +5 -6
  78. package/src/types/common.ts +11 -8
  79. package/src/types/data-structures/base/base.ts +6 -0
  80. package/src/types/data-structures/base/index.ts +1 -0
  81. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  82. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  83. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  84. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  85. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  86. package/src/types/data-structures/hash/hash-map.ts +2 -0
  87. package/src/types/data-structures/heap/heap.ts +1 -1
  88. package/src/types/data-structures/index.ts +1 -0
  89. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
@@ -8,8 +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 { EntryCallback } from "../../types";
11
12
  import { IGraph } from '../../interfaces';
12
13
  import { Queue } from '../queue';
14
+ import { IterableEntryBase } from "../base";
13
15
 
14
16
  export abstract class AbstractVertex<V = any> {
15
17
  key: VertexKey;
@@ -64,11 +66,15 @@ export abstract class AbstractGraph<
64
66
  E = any,
65
67
  VO extends AbstractVertex<V> = AbstractVertex<V>,
66
68
  EO extends AbstractEdge<E> = AbstractEdge<E>
67
- > implements IGraph<V, E, VO, EO> {
68
- protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
69
+ > extends IterableEntryBase<VertexKey, V | undefined> implements IGraph<V, E, VO, EO> {
70
+ constructor() {
71
+ super();
72
+ }
73
+
74
+ protected _vertexMap: Map<VertexKey, VO> = new Map<VertexKey, VO>();
69
75
 
70
- get vertices(): Map<VertexKey, VO> {
71
- return this._vertices;
76
+ get vertexMap(): Map<VertexKey, VO> {
77
+ return this._vertexMap;
72
78
  }
73
79
 
74
80
  /**
@@ -114,12 +120,12 @@ export abstract class AbstractGraph<
114
120
  *
115
121
  * The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
116
122
  * @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
117
- * the `_vertices` map.
118
- * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
123
+ * the `_vertexMap` map.
124
+ * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertexMap`
119
125
  * map. If the vertex does not exist, it returns `undefined`.
120
126
  */
121
127
  getVertex(vertexKey: VertexKey): VO | undefined {
122
- return this._vertices.get(vertexKey) || undefined;
128
+ return this._vertexMap.get(vertexKey) || undefined;
123
129
  }
124
130
 
125
131
  /**
@@ -137,7 +143,7 @@ export abstract class AbstractGraph<
137
143
  * @returns a boolean value.
138
144
  */
139
145
  hasVertex(vertexOrKey: VO | VertexKey): boolean {
140
- return this._vertices.has(this._getVertexKey(vertexOrKey));
146
+ return this._vertexMap.has(this._getVertexKey(vertexOrKey));
141
147
  }
142
148
 
143
149
  addVertex(vertex: VO): boolean;
@@ -158,6 +164,11 @@ export abstract class AbstractGraph<
158
164
  }
159
165
  }
160
166
 
167
+ isVertexKey(potentialKey: any): potentialKey is VertexKey {
168
+ const potentialKeyType = typeof potentialKey;
169
+ return potentialKeyType === "string" || potentialKeyType === "number"
170
+ }
171
+
161
172
  /**
162
173
  * Time Complexity: O(1) - Constant time for Map operations.
163
174
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -174,27 +185,27 @@ export abstract class AbstractGraph<
174
185
  */
175
186
  deleteVertex(vertexOrKey: VO | VertexKey): boolean {
176
187
  const vertexKey = this._getVertexKey(vertexOrKey);
177
- return this._vertices.delete(vertexKey);
188
+ return this._vertexMap.delete(vertexKey);
178
189
  }
179
190
 
180
191
  /**
181
- * Time Complexity: O(K), where K is the number of vertices to be removed.
192
+ * Time Complexity: O(K), where K is the number of vertexMap to be removed.
182
193
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
183
194
  */
184
195
 
185
196
  /**
186
- * Time Complexity: O(K), where K is the number of vertices to be removed.
197
+ * Time Complexity: O(K), where K is the number of vertexMap to be removed.
187
198
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
188
199
  *
189
- * The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
190
- * @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
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
191
202
  * of vertex IDs (`VertexKey[]`).
192
- * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
203
+ * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertexMap
193
204
  * were removed.
194
205
  */
195
- removeManyVertices(vertices: VO[] | VertexKey[]): boolean {
206
+ removeManyVertices(vertexMap: VO[] | VertexKey[]): boolean {
196
207
  const removed: boolean[] = [];
197
- for (const v of vertices) {
208
+ for (const v of vertexMap) {
198
209
  removed.push(this.deleteVertex(v));
199
210
  }
200
211
  return removed.length > 0;
@@ -209,7 +220,7 @@ export abstract class AbstractGraph<
209
220
  * Time Complexity: O(1) - Depends on the implementation in the concrete class.
210
221
  * Space Complexity: O(1) - Depends on the implementation in the concrete class.
211
222
  *
212
- * The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
223
+ * The function checks if there is an edge between two vertexMap and returns a boolean value indicating the result.
213
224
  * @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
214
225
  * identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
215
226
  * @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
@@ -255,14 +266,14 @@ export abstract class AbstractGraph<
255
266
  * Time Complexity: O(1) - Constant time for Map and Edge operations.
256
267
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
257
268
  *
258
- * The function sets the weight of an edge between two vertices in a graph.
269
+ * The function sets the weight of an edge between two vertexMap in a graph.
259
270
  * @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
260
271
  * the source vertex of the edge.
261
272
  * @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
262
273
  * either a `VertexKey` or a vertex object `VO`.
263
274
  * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
264
275
  * and the destination vertex (destOrKey).
265
- * @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
276
+ * @returns a boolean value. If the edge exists between the source and destination vertexMap, the function will update
266
277
  * the weight of the edge and return true. If the edge does not exist, the function will return false.
267
278
  */
268
279
  setEdgeWeight(srcOrKey: VertexKey | VO, destOrKey: VertexKey | VO, weight: number): boolean {
@@ -284,12 +295,12 @@ export abstract class AbstractGraph<
284
295
  * Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
285
296
  * Space Complexity: O(P) - Linear space, where P is the number of paths found.
286
297
  *
287
- * The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
298
+ * The function `getAllPathsBetween` finds all paths between two vertexMap in a graph using depth-first search.
288
299
  * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
289
300
  * It is the starting vertex for finding paths.
290
301
  * @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
291
302
  * @param limit - The count of limitation of result array.
292
- * @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`VO[][]`).
303
+ * @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
293
304
  */
294
305
  getAllPathsBetween(v1: VO | VertexKey, v2: VO | VertexKey, limit = 1000): VO[][] {
295
306
  const paths: VO[][] = [];
@@ -332,8 +343,8 @@ export abstract class AbstractGraph<
332
343
  * Space Complexity: O(1) - Constant space.
333
344
  *
334
345
  * The function calculates the sum of weights along a given path.
335
- * @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
336
- * @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
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.
337
348
  */
338
349
  getPathSumWeight(path: VO[]): number {
339
350
  let sum = 0;
@@ -352,17 +363,17 @@ export abstract class AbstractGraph<
352
363
  * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
353
364
  * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
354
365
  *
355
- * The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
366
+ * The function `getMinCostBetween` calculates the minimum cost between two vertexMap in a graph, either based on edge
356
367
  * weights or using a breadth-first search algorithm.
357
368
  * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
358
369
  * @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
359
370
  * you want to find the minimum cost or weight from the source vertex `v1`.
360
- * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
371
+ * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
361
372
  * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
362
- * the edges. If isWeight is set to false or not provided, the function will calculate the
363
- * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
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`
364
375
  * and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
365
- * vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
376
+ * vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
366
377
  * minimum number of
367
378
  */
368
379
  getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | undefined {
@@ -419,20 +430,20 @@ export abstract class AbstractGraph<
419
430
  * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
420
431
  * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
421
432
  *
422
- * The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
433
+ * The function `getMinPathBetween` returns the minimum path between two vertexMap in a graph, either based on weight or
423
434
  * using a breadth-first search algorithm.
424
435
  * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
425
436
  * object (`VO`) or a vertex ID (`VertexKey`).
426
437
  * @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
427
438
  * path.
428
- * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
439
+ * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
429
440
  * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
430
441
  * to false, the function will use breadth-first search (BFS) to find the minimum path.
431
442
  * @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
432
443
  * followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
433
444
  * so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
434
- * @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
435
- * two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
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`.
436
447
  */
437
448
  getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS = false): VO[] | undefined {
438
449
  if (isWeight === undefined) isWeight = false;
@@ -499,7 +510,7 @@ export abstract class AbstractGraph<
499
510
  * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
500
511
  * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
501
512
  *
502
- * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
513
+ * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertexMap in
503
514
  * a graph without using a heap data structure.
504
515
  * @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
505
516
  * vertex object or a vertex ID.
@@ -511,7 +522,7 @@ export abstract class AbstractGraph<
511
522
  * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
512
523
  * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
513
524
  * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
514
- * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
525
+ * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
515
526
  * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
516
527
  */
517
528
  dijkstraWithoutHeap(
@@ -529,7 +540,7 @@ export abstract class AbstractGraph<
529
540
  let minPath: VO[] = [];
530
541
  const paths: VO[][] = [];
531
542
 
532
- const vertices = this._vertices;
543
+ const vertexMap = this._vertexMap;
533
544
  const distMap: Map<VO, number> = new Map();
534
545
  const seen: Set<VO> = new Set();
535
546
  const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
@@ -541,7 +552,7 @@ export abstract class AbstractGraph<
541
552
  return undefined;
542
553
  }
543
554
 
544
- for (const vertex of vertices) {
555
+ for (const vertex of vertexMap) {
545
556
  const vertexOrKey = vertex[1];
546
557
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
547
558
  }
@@ -563,7 +574,7 @@ export abstract class AbstractGraph<
563
574
  };
564
575
 
565
576
  const getPaths = (minV: VO | undefined) => {
566
- for (const vertex of vertices) {
577
+ for (const vertex of vertexMap) {
567
578
  const vertexOrKey = vertex[1];
568
579
 
569
580
  if (vertexOrKey instanceof AbstractVertex) {
@@ -580,7 +591,7 @@ export abstract class AbstractGraph<
580
591
  }
581
592
  };
582
593
 
583
- for (let i = 1; i < vertices.size; i++) {
594
+ for (let i = 1; i < vertexMap.size; i++) {
584
595
  const cur = getMinOfNoSeen();
585
596
  if (cur) {
586
597
  seen.add(cur);
@@ -632,7 +643,7 @@ export abstract class AbstractGraph<
632
643
  * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
633
644
  *
634
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.
635
- * 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 edges.
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.
636
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.
637
648
  *
638
649
  * /
@@ -653,13 +664,13 @@ export abstract class AbstractGraph<
653
664
  * start. It can be either a vertex object or a vertex ID.
654
665
  * @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
655
666
  * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
656
- * will calculate the shortest paths to all other vertices from the source vertex.
667
+ * will calculate the shortest paths to all other vertexMap from the source vertex.
657
668
  * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
658
669
  * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
659
670
  * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
660
671
  * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
661
672
  * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
662
- * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
673
+ * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
663
674
  * @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
664
675
  */
665
676
  dijkstra(
@@ -676,7 +687,7 @@ export abstract class AbstractGraph<
676
687
  let minDest: VO | undefined = undefined;
677
688
  let minPath: VO[] = [];
678
689
  const paths: VO[][] = [];
679
- const vertices = this._vertices;
690
+ const vertexMap = this._vertexMap;
680
691
  const distMap: Map<VO, number> = new Map();
681
692
  const seen: Set<VO> = new Set();
682
693
  const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
@@ -686,7 +697,7 @@ export abstract class AbstractGraph<
686
697
 
687
698
  if (!srcVertex) return undefined;
688
699
 
689
- for (const vertex of vertices) {
700
+ for (const vertex of vertexMap) {
690
701
  const vertexOrKey = vertex[1];
691
702
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
692
703
  }
@@ -698,12 +709,12 @@ export abstract class AbstractGraph<
698
709
  preMap.set(srcVertex, undefined);
699
710
 
700
711
  /**
701
- * The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
712
+ * The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
702
713
  * @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
703
714
  * undefined.
704
715
  */
705
716
  const getPaths = (minV: VO | undefined) => {
706
- for (const vertex of vertices) {
717
+ for (const vertex of vertexMap) {
707
718
  const vertexOrKey = vertex[1];
708
719
  if (vertexOrKey instanceof AbstractVertex) {
709
720
  const path: VO[] = [vertexOrKey];
@@ -784,16 +795,16 @@ export abstract class AbstractGraph<
784
795
  * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
785
796
  *
786
797
  * one to rest pairs
787
- * 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 edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
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.
788
799
  * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
789
- * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
800
+ * all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
790
801
  * @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
791
802
  * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
792
803
  * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
793
804
  * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
794
- * calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
805
+ * calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
795
806
  * `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
796
- * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
807
+ * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
797
808
  * vertex.
798
809
  * @returns The function `bellmanFord` returns an object with the following properties:
799
810
  */
@@ -812,12 +823,12 @@ export abstract class AbstractGraph<
812
823
  if (scanNegativeCycle) hasNegativeCycle = false;
813
824
  if (!srcVertex) return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
814
825
 
815
- const vertices = this._vertices;
816
- const numOfVertices = vertices.size;
817
- const edges = this.edgeSet();
818
- const numOfEdges = edges.length;
826
+ const vertexMap = this._vertexMap;
827
+ const numOfVertices = vertexMap.size;
828
+ const edgeMap = this.edgeSet();
829
+ const numOfEdges = edgeMap.length;
819
830
 
820
- this._vertices.forEach(vertex => {
831
+ this._vertexMap.forEach(vertex => {
821
832
  distMap.set(vertex, Infinity);
822
833
  });
823
834
 
@@ -825,10 +836,10 @@ export abstract class AbstractGraph<
825
836
 
826
837
  for (let i = 1; i < numOfVertices; ++i) {
827
838
  for (let j = 0; j < numOfEdges; ++j) {
828
- const ends = this.getEndsOfEdge(edges[j]);
839
+ const ends = this.getEndsOfEdge(edgeMap[j]);
829
840
  if (ends) {
830
841
  const [s, d] = ends;
831
- const weight = edges[j].weight;
842
+ const weight = edgeMap[j].weight;
832
843
  const sWeight = distMap.get(s);
833
844
  const dWeight = distMap.get(d);
834
845
  if (sWeight !== undefined && dWeight !== undefined) {
@@ -854,7 +865,7 @@ export abstract class AbstractGraph<
854
865
  }
855
866
 
856
867
  if (genPath) {
857
- for (const vertex of vertices) {
868
+ for (const vertex of vertexMap) {
858
869
  const vertexOrKey = vertex[1];
859
870
  if (vertexOrKey instanceof AbstractVertex) {
860
871
  const path: VO[] = [vertexOrKey];
@@ -871,10 +882,10 @@ export abstract class AbstractGraph<
871
882
  }
872
883
 
873
884
  for (let j = 0; j < numOfEdges; ++j) {
874
- const ends = this.getEndsOfEdge(edges[j]);
885
+ const ends = this.getEndsOfEdge(edgeMap[j]);
875
886
  if (ends) {
876
887
  const [s] = ends;
877
- const weight = edges[j].weight;
888
+ const weight = edgeMap[j].weight;
878
889
  const sWeight = distMap.get(s);
879
890
  if (sWeight) {
880
891
  if (sWeight !== Infinity && sWeight + weight < sWeight) hasNegativeCycle = true;
@@ -897,7 +908,7 @@ export abstract class AbstractGraph<
897
908
  /**
898
909
  * BellmanFord time:O(VE) space:O(VO)
899
910
  * one to rest pairs
900
- * 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 edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
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.
901
912
  * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
902
913
  */
903
914
 
@@ -906,7 +917,7 @@ export abstract class AbstractGraph<
906
917
  * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
907
918
  * Not support graph with negative weight cycle
908
919
  * all pairs
909
- * 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 edges, and it can simultaneously compute shortest paths between any two nodes.
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.
910
921
  * /
911
922
 
912
923
  /**
@@ -915,16 +926,16 @@ export abstract class AbstractGraph<
915
926
  *
916
927
  * Not support graph with negative weight cycle
917
928
  * all pairs
918
- * 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 edges, and it can simultaneously compute shortest paths between any two nodes.
919
- * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
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
920
931
  * graph.
921
932
  * @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
922
- * property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
923
- * `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
924
- * path between vertices in the
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
925
936
  */
926
937
  floydWarshall(): { costs: number[][]; predecessor: (VO | undefined)[][] } {
927
- const idAndVertices = [...this._vertices];
938
+ const idAndVertices = [...this._vertexMap];
928
939
  const n = idAndVertices.length;
929
940
 
930
941
  const costs: number[][] = [];
@@ -963,7 +974,7 @@ export abstract class AbstractGraph<
963
974
  * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
964
975
  * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
965
976
  * Tarjan can find cycles in directed or undirected graph
966
- * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
977
+ * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
967
978
  * Tarjan solve the bi-connected components of undirected graphs;
968
979
  * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
969
980
  * /
@@ -974,22 +985,22 @@ export abstract class AbstractGraph<
974
985
  *
975
986
  * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
976
987
  * Tarjan can find cycles in directed or undirected graph
977
- * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
988
+ * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
978
989
  * Tarjan solve the bi-connected components of undirected graphs;
979
990
  * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
980
991
  * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
981
992
  * strongly connected components (SCCs), and cycles in a graph.
982
993
  * @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
983
- * articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
994
+ * articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
984
995
  * number of connected components in the graph.
985
996
  * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
986
- * (edges whose removal would increase the number of connected components in the graph).
997
+ * (edgeMap whose removal would increase the number of connected components in the graph).
987
998
  * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
988
999
  * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
989
1000
  * SCCs will not be calculated or returned.
990
1001
  * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
991
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
992
- * are arrays of vertices that form cycles within the SCCs.
1003
+ * are arrays of vertexMap that form cycles within the SCCs.
993
1004
  * @returns The function `tarjan` returns an object with the following properties:
994
1005
  */
995
1006
  tarjan(
@@ -1010,13 +1021,13 @@ export abstract class AbstractGraph<
1010
1021
 
1011
1022
  const dfnMap: Map<VO, number> = new Map();
1012
1023
  const lowMap: Map<VO, number> = new Map();
1013
- const vertices = this._vertices;
1014
- vertices.forEach(v => {
1024
+ const vertexMap = this._vertexMap;
1025
+ vertexMap.forEach(v => {
1015
1026
  dfnMap.set(v, -1);
1016
1027
  lowMap.set(v, Infinity);
1017
1028
  });
1018
1029
 
1019
- const [root] = vertices.values();
1030
+ const [root] = vertexMap.values();
1020
1031
 
1021
1032
  const cutVertexes: VO[] = [];
1022
1033
  const bridges: EO[] = [];
@@ -1159,50 +1170,71 @@ export abstract class AbstractGraph<
1159
1170
  return this.tarjan(false, true, false, false).bridges;
1160
1171
  }
1161
1172
 
1162
- * [Symbol.iterator](): Iterator<[VertexKey, V | undefined]> {
1163
- for (const vertex of this._vertices.values()) {
1164
- yield [vertex.key, vertex.value];
1165
- }
1166
- }
1167
-
1168
- forEach(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => void): void {
1169
- let index = 0;
1170
- for (const vertex of this) {
1171
- callback(vertex, index, this._vertices);
1172
- index++;
1173
- }
1174
- }
1173
+ /**
1174
+ * Time Complexity: O(n)
1175
+ * Space Complexity: O(n)
1176
+ */
1175
1177
 
1176
- filter(predicate: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => boolean): [VertexKey, V | undefined][] {
1178
+ /**
1179
+ * Time Complexity: O(n)
1180
+ * Space Complexity: O(n)
1181
+ *
1182
+ * The `filter` function iterates over key-value pairs in a data structure and returns an array of
1183
+ * pairs that satisfy a given predicate.
1184
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
1185
+ * `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
1186
+ * in the filtered array. The callback function should return `true` if the element should be
1187
+ * included, and `
1188
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1189
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
1190
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
1191
+ * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
1192
+ * that satisfy the given predicate function.
1193
+ */
1194
+ filter(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?: any): [VertexKey, V | undefined][] {
1177
1195
  const filtered: [VertexKey, V | undefined][] = [];
1178
1196
  let index = 0;
1179
- for (const entry of this) {
1180
- if (predicate(entry, index, this._vertices)) {
1181
- filtered.push(entry);
1197
+ for (const [key, value] of this) {
1198
+ if (predicate.call(thisArg, value, key, index, this)) {
1199
+ filtered.push([key, value]);
1182
1200
  }
1183
1201
  index++;
1184
1202
  }
1185
1203
  return filtered;
1186
1204
  }
1187
1205
 
1188
- map<T>(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T): T[] {
1206
+ /**
1207
+ * Time Complexity: O(n)
1208
+ * Space Complexity: O(n)
1209
+ */
1210
+
1211
+ /**
1212
+ * Time Complexity: O(n)
1213
+ * Space Complexity: O(n)
1214
+ *
1215
+ * The `map` function iterates over the elements of a collection and applies a callback function to
1216
+ * each element, returning an array of the results.
1217
+ * @param callback - The callback parameter is a function that will be called for each element in the
1218
+ * map. It takes four arguments:
1219
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1220
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
1221
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
1222
+ * @returns The `map` function is returning an array of type `T[]`.
1223
+ */
1224
+ map<T>(callback: EntryCallback<VertexKey, V | undefined, T>, thisArg?: any): T[] {
1189
1225
  const mapped: T[] = [];
1190
1226
  let index = 0;
1191
- for (const entry of this) {
1192
- mapped.push(callback(entry, index, this._vertices));
1227
+ for (const [key, value] of this) {
1228
+ mapped.push(callback.call(thisArg, value, key, index, this));
1193
1229
  index++;
1194
1230
  }
1195
1231
  return mapped;
1196
1232
  }
1197
1233
 
1198
- reduce<T>(callback: (accumulator: T, entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T, initialValue: T): T {
1199
- let accumulator: T = initialValue;
1200
- let index = 0;
1201
- for (const entry of this) {
1202
- accumulator = callback(accumulator, entry, index, this._vertices);
1203
- index++;
1234
+ protected* _getIterator(): IterableIterator<[VertexKey, V | undefined]> {
1235
+ for (const vertex of this._vertexMap.values()) {
1236
+ yield [vertex.key, vertex.value];
1204
1237
  }
1205
- return accumulator;
1206
1238
  }
1207
1239
 
1208
1240
  protected abstract _addEdgeOnly(edge: EO): boolean;
@@ -1212,13 +1244,13 @@ export abstract class AbstractGraph<
1212
1244
  return false;
1213
1245
  // throw (new Error('Duplicated vertex key is not allowed'));
1214
1246
  }
1215
- this._vertices.set(newVertex.key, newVertex);
1247
+ this._vertexMap.set(newVertex.key, newVertex);
1216
1248
  return true;
1217
1249
  }
1218
1250
 
1219
1251
  protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined {
1220
1252
  const vertexKey = this._getVertexKey(vertexOrKey);
1221
- return this._vertices.get(vertexKey) || undefined;
1253
+ return this._vertexMap.get(vertexKey) || undefined;
1222
1254
  }
1223
1255
 
1224
1256
  protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey {