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
@@ -11,6 +11,7 @@ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
11
11
  const utils_1 = require("../../utils");
12
12
  const priority_queue_1 = require("../priority-queue");
13
13
  const queue_1 = require("../queue");
14
+ const base_1 = require("../base");
14
15
  class AbstractVertex {
15
16
  /**
16
17
  * The function is a protected constructor that takes an key and an optional value as parameters.
@@ -45,12 +46,13 @@ class AbstractEdge {
45
46
  }
46
47
  }
47
48
  exports.AbstractEdge = AbstractEdge;
48
- class AbstractGraph {
49
+ class AbstractGraph extends base_1.IterableEntryBase {
49
50
  constructor() {
50
- this._vertices = new Map();
51
+ super();
52
+ this._vertexMap = new Map();
51
53
  }
52
- get vertices() {
53
- return this._vertices;
54
+ get vertexMap() {
55
+ return this._vertexMap;
54
56
  }
55
57
  /**
56
58
  * Time Complexity: O(1) - Constant time for Map lookup.
@@ -62,12 +64,12 @@ class AbstractGraph {
62
64
  *
63
65
  * The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
64
66
  * @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
65
- * the `_vertices` map.
66
- * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
67
+ * the `_vertexMap` map.
68
+ * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertexMap`
67
69
  * map. If the vertex does not exist, it returns `undefined`.
68
70
  */
69
71
  getVertex(vertexKey) {
70
- return this._vertices.get(vertexKey) || undefined;
72
+ return this._vertexMap.get(vertexKey) || undefined;
71
73
  }
72
74
  /**
73
75
  * Time Complexity: O(1) - Constant time for Map lookup.
@@ -83,7 +85,7 @@ class AbstractGraph {
83
85
  * @returns a boolean value.
84
86
  */
85
87
  hasVertex(vertexOrKey) {
86
- return this._vertices.has(this._getVertexKey(vertexOrKey));
88
+ return this._vertexMap.has(this._getVertexKey(vertexOrKey));
87
89
  }
88
90
  /**
89
91
  * Time Complexity: O(1) - Constant time for Map operations.
@@ -98,6 +100,10 @@ class AbstractGraph {
98
100
  return this._addVertexOnly(newVertex);
99
101
  }
100
102
  }
103
+ isVertexKey(potentialKey) {
104
+ const potentialKeyType = typeof potentialKey;
105
+ return potentialKeyType === "string" || potentialKeyType === "number";
106
+ }
101
107
  /**
102
108
  * Time Complexity: O(1) - Constant time for Map operations.
103
109
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -113,25 +119,25 @@ class AbstractGraph {
113
119
  */
114
120
  deleteVertex(vertexOrKey) {
115
121
  const vertexKey = this._getVertexKey(vertexOrKey);
116
- return this._vertices.delete(vertexKey);
122
+ return this._vertexMap.delete(vertexKey);
117
123
  }
118
124
  /**
119
- * Time Complexity: O(K), where K is the number of vertices to be removed.
125
+ * Time Complexity: O(K), where K is the number of vertexMap to be removed.
120
126
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
121
127
  */
122
128
  /**
123
- * Time Complexity: O(K), where K is the number of vertices to be removed.
129
+ * Time Complexity: O(K), where K is the number of vertexMap to be removed.
124
130
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
125
131
  *
126
- * The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
127
- * @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
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
128
134
  * of vertex IDs (`VertexKey[]`).
129
- * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
135
+ * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertexMap
130
136
  * were removed.
131
137
  */
132
- removeManyVertices(vertices) {
138
+ removeManyVertices(vertexMap) {
133
139
  const removed = [];
134
- for (const v of vertices) {
140
+ for (const v of vertexMap) {
135
141
  removed.push(this.deleteVertex(v));
136
142
  }
137
143
  return removed.length > 0;
@@ -144,7 +150,7 @@ class AbstractGraph {
144
150
  * Time Complexity: O(1) - Depends on the implementation in the concrete class.
145
151
  * Space Complexity: O(1) - Depends on the implementation in the concrete class.
146
152
  *
147
- * The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
153
+ * The function checks if there is an edge between two vertexMap and returns a boolean value indicating the result.
148
154
  * @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
149
155
  * identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
150
156
  * @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
@@ -187,14 +193,14 @@ class AbstractGraph {
187
193
  * Time Complexity: O(1) - Constant time for Map and Edge operations.
188
194
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
189
195
  *
190
- * The function sets the weight of an edge between two vertices in a graph.
196
+ * The function sets the weight of an edge between two vertexMap in a graph.
191
197
  * @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
192
198
  * the source vertex of the edge.
193
199
  * @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
194
200
  * either a `VertexKey` or a vertex object `VO`.
195
201
  * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
196
202
  * and the destination vertex (destOrKey).
197
- * @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
203
+ * @returns a boolean value. If the edge exists between the source and destination vertexMap, the function will update
198
204
  * the weight of the edge and return true. If the edge does not exist, the function will return false.
199
205
  */
200
206
  setEdgeWeight(srcOrKey, destOrKey, weight) {
@@ -215,12 +221,12 @@ class AbstractGraph {
215
221
  * Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
216
222
  * Space Complexity: O(P) - Linear space, where P is the number of paths found.
217
223
  *
218
- * The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
224
+ * The function `getAllPathsBetween` finds all paths between two vertexMap in a graph using depth-first search.
219
225
  * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
220
226
  * It is the starting vertex for finding paths.
221
227
  * @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
222
228
  * @param limit - The count of limitation of result array.
223
- * @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`VO[][]`).
229
+ * @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
224
230
  */
225
231
  getAllPathsBetween(v1, v2, limit = 1000) {
226
232
  const paths = [];
@@ -257,8 +263,8 @@ class AbstractGraph {
257
263
  * Space Complexity: O(1) - Constant space.
258
264
  *
259
265
  * The function calculates the sum of weights along a given path.
260
- * @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
261
- * @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
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.
262
268
  */
263
269
  getPathSumWeight(path) {
264
270
  var _a;
@@ -276,17 +282,17 @@ class AbstractGraph {
276
282
  * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
277
283
  * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
278
284
  *
279
- * The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
285
+ * The function `getMinCostBetween` calculates the minimum cost between two vertexMap in a graph, either based on edge
280
286
  * weights or using a breadth-first search algorithm.
281
287
  * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
282
288
  * @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
283
289
  * you want to find the minimum cost or weight from the source vertex `v1`.
284
- * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
290
+ * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
285
291
  * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
286
- * the edges. If isWeight is set to false or not provided, the function will calculate the
287
- * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
292
+ * the edgeMap. If isWeight is set to false or not provided, the function will calculate the
293
+ * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertexMap (`v1`
288
294
  * and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
289
- * vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
295
+ * vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
290
296
  * minimum number of
291
297
  */
292
298
  getMinCostBetween(v1, v2, isWeight) {
@@ -341,20 +347,20 @@ class AbstractGraph {
341
347
  * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
342
348
  * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
343
349
  *
344
- * The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
350
+ * The function `getMinPathBetween` returns the minimum path between two vertexMap in a graph, either based on weight or
345
351
  * using a breadth-first search algorithm.
346
352
  * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
347
353
  * object (`VO`) or a vertex ID (`VertexKey`).
348
354
  * @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
349
355
  * path.
350
- * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
356
+ * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
351
357
  * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
352
358
  * to false, the function will use breadth-first search (BFS) to find the minimum path.
353
359
  * @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
354
360
  * followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
355
361
  * so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
356
- * @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
357
- * two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
362
+ * @returns The function `getMinPathBetween` returns an array of vertexMap (`VO[]`) representing the minimum path between
363
+ * two vertexMap (`v1` and `v2`). If there is no path between the vertexMap, it returns `undefined`.
358
364
  */
359
365
  getMinPathBetween(v1, v2, isWeight, isDFS = false) {
360
366
  var _a, _b;
@@ -419,7 +425,7 @@ class AbstractGraph {
419
425
  * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
420
426
  * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
421
427
  *
422
- * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
428
+ * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertexMap in
423
429
  * a graph without using a heap data structure.
424
430
  * @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
425
431
  * vertex object or a vertex ID.
@@ -431,7 +437,7 @@ class AbstractGraph {
431
437
  * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
432
438
  * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
433
439
  * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
434
- * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
440
+ * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
435
441
  * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
436
442
  */
437
443
  dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
@@ -445,7 +451,7 @@ class AbstractGraph {
445
451
  let minDest = undefined;
446
452
  let minPath = [];
447
453
  const paths = [];
448
- const vertices = this._vertices;
454
+ const vertexMap = this._vertexMap;
449
455
  const distMap = new Map();
450
456
  const seen = new Set();
451
457
  const preMap = new Map(); // predecessor
@@ -454,7 +460,7 @@ class AbstractGraph {
454
460
  if (!srcVertex) {
455
461
  return undefined;
456
462
  }
457
- for (const vertex of vertices) {
463
+ for (const vertex of vertexMap) {
458
464
  const vertexOrKey = vertex[1];
459
465
  if (vertexOrKey instanceof AbstractVertex)
460
466
  distMap.set(vertexOrKey, Infinity);
@@ -475,7 +481,7 @@ class AbstractGraph {
475
481
  return minV;
476
482
  };
477
483
  const getPaths = (minV) => {
478
- for (const vertex of vertices) {
484
+ for (const vertex of vertexMap) {
479
485
  const vertexOrKey = vertex[1];
480
486
  if (vertexOrKey instanceof AbstractVertex) {
481
487
  const path = [vertexOrKey];
@@ -491,7 +497,7 @@ class AbstractGraph {
491
497
  }
492
498
  }
493
499
  };
494
- for (let i = 1; i < vertices.size; i++) {
500
+ for (let i = 1; i < vertexMap.size; i++) {
495
501
  const cur = getMinOfNoSeen();
496
502
  if (cur) {
497
503
  seen.add(cur);
@@ -540,7 +546,7 @@ class AbstractGraph {
540
546
  * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
541
547
  *
542
548
  * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
543
- * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
549
+ * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edgeMap.
544
550
  * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
545
551
  *
546
552
  * /
@@ -560,13 +566,13 @@ class AbstractGraph {
560
566
  * start. It can be either a vertex object or a vertex ID.
561
567
  * @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
562
568
  * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
563
- * will calculate the shortest paths to all other vertices from the source vertex.
569
+ * will calculate the shortest paths to all other vertexMap from the source vertex.
564
570
  * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
565
571
  * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
566
572
  * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
567
573
  * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
568
574
  * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
569
- * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
575
+ * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
570
576
  * @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
571
577
  */
572
578
  dijkstra(src, dest, getMinDist, genPaths) {
@@ -581,7 +587,7 @@ class AbstractGraph {
581
587
  let minDest = undefined;
582
588
  let minPath = [];
583
589
  const paths = [];
584
- const vertices = this._vertices;
590
+ const vertexMap = this._vertexMap;
585
591
  const distMap = new Map();
586
592
  const seen = new Set();
587
593
  const preMap = new Map(); // predecessor
@@ -589,7 +595,7 @@ class AbstractGraph {
589
595
  const destVertex = dest ? this._getVertex(dest) : undefined;
590
596
  if (!srcVertex)
591
597
  return undefined;
592
- for (const vertex of vertices) {
598
+ for (const vertex of vertexMap) {
593
599
  const vertexOrKey = vertex[1];
594
600
  if (vertexOrKey instanceof AbstractVertex)
595
601
  distMap.set(vertexOrKey, Infinity);
@@ -599,12 +605,12 @@ class AbstractGraph {
599
605
  distMap.set(srcVertex, 0);
600
606
  preMap.set(srcVertex, undefined);
601
607
  /**
602
- * The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
608
+ * The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
603
609
  * @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
604
610
  * undefined.
605
611
  */
606
612
  const getPaths = (minV) => {
607
- for (const vertex of vertices) {
613
+ for (const vertex of vertexMap) {
608
614
  const vertexOrKey = vertex[1];
609
615
  if (vertexOrKey instanceof AbstractVertex) {
610
616
  const path = [vertexOrKey];
@@ -682,16 +688,16 @@ class AbstractGraph {
682
688
  * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
683
689
  *
684
690
  * one to rest pairs
685
- * 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.
691
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
686
692
  * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
687
- * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
693
+ * all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
688
694
  * @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
689
695
  * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
690
696
  * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
691
697
  * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
692
- * calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
698
+ * calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
693
699
  * `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
694
- * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
700
+ * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
695
701
  * vertex.
696
702
  * @returns The function `bellmanFord` returns an object with the following properties:
697
703
  */
@@ -712,20 +718,20 @@ class AbstractGraph {
712
718
  hasNegativeCycle = false;
713
719
  if (!srcVertex)
714
720
  return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
715
- const vertices = this._vertices;
716
- const numOfVertices = vertices.size;
717
- const edges = this.edgeSet();
718
- const numOfEdges = edges.length;
719
- this._vertices.forEach(vertex => {
721
+ const vertexMap = this._vertexMap;
722
+ const numOfVertices = vertexMap.size;
723
+ const edgeMap = this.edgeSet();
724
+ const numOfEdges = edgeMap.length;
725
+ this._vertexMap.forEach(vertex => {
720
726
  distMap.set(vertex, Infinity);
721
727
  });
722
728
  distMap.set(srcVertex, 0);
723
729
  for (let i = 1; i < numOfVertices; ++i) {
724
730
  for (let j = 0; j < numOfEdges; ++j) {
725
- const ends = this.getEndsOfEdge(edges[j]);
731
+ const ends = this.getEndsOfEdge(edgeMap[j]);
726
732
  if (ends) {
727
733
  const [s, d] = ends;
728
- const weight = edges[j].weight;
734
+ const weight = edgeMap[j].weight;
729
735
  const sWeight = distMap.get(s);
730
736
  const dWeight = distMap.get(d);
731
737
  if (sWeight !== undefined && dWeight !== undefined) {
@@ -750,7 +756,7 @@ class AbstractGraph {
750
756
  });
751
757
  }
752
758
  if (genPath) {
753
- for (const vertex of vertices) {
759
+ for (const vertex of vertexMap) {
754
760
  const vertexOrKey = vertex[1];
755
761
  if (vertexOrKey instanceof AbstractVertex) {
756
762
  const path = [vertexOrKey];
@@ -767,10 +773,10 @@ class AbstractGraph {
767
773
  }
768
774
  }
769
775
  for (let j = 0; j < numOfEdges; ++j) {
770
- const ends = this.getEndsOfEdge(edges[j]);
776
+ const ends = this.getEndsOfEdge(edgeMap[j]);
771
777
  if (ends) {
772
778
  const [s] = ends;
773
- const weight = edges[j].weight;
779
+ const weight = edgeMap[j].weight;
774
780
  const sWeight = distMap.get(s);
775
781
  if (sWeight) {
776
782
  if (sWeight !== Infinity && sWeight + weight < sWeight)
@@ -791,7 +797,7 @@ class AbstractGraph {
791
797
  /**
792
798
  * BellmanFord time:O(VE) space:O(VO)
793
799
  * one to rest pairs
794
- * 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.
800
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
795
801
  * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
796
802
  */
797
803
  /**
@@ -799,7 +805,7 @@ class AbstractGraph {
799
805
  * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
800
806
  * Not support graph with negative weight cycle
801
807
  * all pairs
802
- * 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.
808
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
803
809
  * /
804
810
 
805
811
  /**
@@ -808,17 +814,17 @@ class AbstractGraph {
808
814
  *
809
815
  * Not support graph with negative weight cycle
810
816
  * all pairs
811
- * 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.
812
- * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
817
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
818
+ * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
813
819
  * graph.
814
820
  * @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
815
- * property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
816
- * `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
817
- * path between vertices in the
821
+ * property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
822
+ * `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
823
+ * path between vertexMap in the
818
824
  */
819
825
  floydWarshall() {
820
826
  var _a;
821
- const idAndVertices = [...this._vertices];
827
+ const idAndVertices = [...this._vertexMap];
822
828
  const n = idAndVertices.length;
823
829
  const costs = [];
824
830
  const predecessor = [];
@@ -852,7 +858,7 @@ class AbstractGraph {
852
858
  * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
853
859
  * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
854
860
  * Tarjan can find cycles in directed or undirected graph
855
- * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
861
+ * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
856
862
  * Tarjan solve the bi-connected components of undirected graphs;
857
863
  * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
858
864
  * /
@@ -863,22 +869,22 @@ class AbstractGraph {
863
869
  *
864
870
  * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
865
871
  * Tarjan can find cycles in directed or undirected graph
866
- * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
872
+ * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
867
873
  * Tarjan solve the bi-connected components of undirected graphs;
868
874
  * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
869
875
  * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
870
876
  * strongly connected components (SCCs), and cycles in a graph.
871
877
  * @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
872
- * articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
878
+ * articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
873
879
  * number of connected components in the graph.
874
880
  * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
875
- * (edges whose removal would increase the number of connected components in the graph).
881
+ * (edgeMap whose removal would increase the number of connected components in the graph).
876
882
  * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
877
883
  * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
878
884
  * SCCs will not be calculated or returned.
879
885
  * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
880
886
  * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
881
- * are arrays of vertices that form cycles within the SCCs.
887
+ * are arrays of vertexMap that form cycles within the SCCs.
882
888
  * @returns The function `tarjan` returns an object with the following properties:
883
889
  */
884
890
  tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
@@ -896,12 +902,12 @@ class AbstractGraph {
896
902
  needCycles = defaultConfig;
897
903
  const dfnMap = new Map();
898
904
  const lowMap = new Map();
899
- const vertices = this._vertices;
900
- vertices.forEach(v => {
905
+ const vertexMap = this._vertexMap;
906
+ vertexMap.forEach(v => {
901
907
  dfnMap.set(v, -1);
902
908
  lowMap.set(v, Infinity);
903
909
  });
904
- const [root] = vertices.values();
910
+ const [root] = vertexMap.values();
905
911
  const cutVertexes = [];
906
912
  const bridges = [];
907
913
  let dfn = 0;
@@ -1028,58 +1034,79 @@ class AbstractGraph {
1028
1034
  getBridges() {
1029
1035
  return this.tarjan(false, true, false, false).bridges;
1030
1036
  }
1031
- *[Symbol.iterator]() {
1032
- for (const vertex of this._vertices.values()) {
1033
- yield [vertex.key, vertex.value];
1034
- }
1035
- }
1036
- forEach(callback) {
1037
- let index = 0;
1038
- for (const vertex of this) {
1039
- callback(vertex, index, this._vertices);
1040
- index++;
1041
- }
1042
- }
1043
- filter(predicate) {
1037
+ /**
1038
+ * Time Complexity: O(n)
1039
+ * Space Complexity: O(n)
1040
+ */
1041
+ /**
1042
+ * Time Complexity: O(n)
1043
+ * Space Complexity: O(n)
1044
+ *
1045
+ * The `filter` function iterates over key-value pairs in a data structure and returns an array of
1046
+ * pairs that satisfy a given predicate.
1047
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
1048
+ * `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
1049
+ * in the filtered array. The callback function should return `true` if the element should be
1050
+ * included, and `
1051
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1052
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
1053
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
1054
+ * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
1055
+ * that satisfy the given predicate function.
1056
+ */
1057
+ filter(predicate, thisArg) {
1044
1058
  const filtered = [];
1045
1059
  let index = 0;
1046
- for (const entry of this) {
1047
- if (predicate(entry, index, this._vertices)) {
1048
- filtered.push(entry);
1060
+ for (const [key, value] of this) {
1061
+ if (predicate.call(thisArg, value, key, index, this)) {
1062
+ filtered.push([key, value]);
1049
1063
  }
1050
1064
  index++;
1051
1065
  }
1052
1066
  return filtered;
1053
1067
  }
1054
- map(callback) {
1068
+ /**
1069
+ * Time Complexity: O(n)
1070
+ * Space Complexity: O(n)
1071
+ */
1072
+ /**
1073
+ * Time Complexity: O(n)
1074
+ * Space Complexity: O(n)
1075
+ *
1076
+ * The `map` function iterates over the elements of a collection and applies a callback function to
1077
+ * each element, returning an array of the results.
1078
+ * @param callback - The callback parameter is a function that will be called for each element in the
1079
+ * map. It takes four arguments:
1080
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1081
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
1082
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
1083
+ * @returns The `map` function is returning an array of type `T[]`.
1084
+ */
1085
+ map(callback, thisArg) {
1055
1086
  const mapped = [];
1056
1087
  let index = 0;
1057
- for (const entry of this) {
1058
- mapped.push(callback(entry, index, this._vertices));
1088
+ for (const [key, value] of this) {
1089
+ mapped.push(callback.call(thisArg, value, key, index, this));
1059
1090
  index++;
1060
1091
  }
1061
1092
  return mapped;
1062
1093
  }
1063
- reduce(callback, initialValue) {
1064
- let accumulator = initialValue;
1065
- let index = 0;
1066
- for (const entry of this) {
1067
- accumulator = callback(accumulator, entry, index, this._vertices);
1068
- index++;
1094
+ *_getIterator() {
1095
+ for (const vertex of this._vertexMap.values()) {
1096
+ yield [vertex.key, vertex.value];
1069
1097
  }
1070
- return accumulator;
1071
1098
  }
1072
1099
  _addVertexOnly(newVertex) {
1073
1100
  if (this.hasVertex(newVertex)) {
1074
1101
  return false;
1075
1102
  // throw (new Error('Duplicated vertex key is not allowed'));
1076
1103
  }
1077
- this._vertices.set(newVertex.key, newVertex);
1104
+ this._vertexMap.set(newVertex.key, newVertex);
1078
1105
  return true;
1079
1106
  }
1080
1107
  _getVertex(vertexOrKey) {
1081
1108
  const vertexKey = this._getVertexKey(vertexOrKey);
1082
- return this._vertices.get(vertexKey) || undefined;
1109
+ return this._vertexMap.get(vertexKey) || undefined;
1083
1110
  }
1084
1111
  _getVertexKey(vertexOrKey) {
1085
1112
  return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;