data-structure-typed 1.34.6 → 1.34.8

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 (98) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +74 -35
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +70 -70
  4. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/avl-tree.js +8 -8
  6. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/data-structures/binary-tree/binary-tree.js +4 -4
  8. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/bst.js +59 -59
  10. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/data-structures/binary-tree/rb-tree.js +4 -4
  12. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  13. package/dist/data-structures/binary-tree/tree-multiset.js +39 -39
  14. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  15. package/dist/data-structures/graph/abstract-graph.js +49 -49
  16. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  17. package/dist/data-structures/graph/directed-graph.js +33 -33
  18. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  19. package/dist/data-structures/graph/map-graph.js +4 -4
  20. package/dist/data-structures/graph/map-graph.js.map +1 -1
  21. package/dist/data-structures/graph/undirected-graph.js +14 -14
  22. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  23. package/dist/data-structures/tree/tree.js +5 -5
  24. package/dist/data-structures/tree/tree.js.map +1 -1
  25. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +71 -71
  26. package/lib/data-structures/binary-tree/abstract-binary-tree.js +110 -110
  27. package/lib/data-structures/binary-tree/avl-tree.d.ts +10 -10
  28. package/lib/data-structures/binary-tree/avl-tree.js +13 -13
  29. package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
  30. package/lib/data-structures/binary-tree/binary-tree.js +7 -7
  31. package/lib/data-structures/binary-tree/bst.d.ts +34 -34
  32. package/lib/data-structures/binary-tree/bst.js +80 -80
  33. package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
  34. package/lib/data-structures/binary-tree/rb-tree.js +4 -4
  35. package/lib/data-structures/binary-tree/tree-multiset.d.ts +27 -27
  36. package/lib/data-structures/binary-tree/tree-multiset.js +55 -55
  37. package/lib/data-structures/graph/abstract-graph.d.ts +60 -60
  38. package/lib/data-structures/graph/abstract-graph.js +81 -81
  39. package/lib/data-structures/graph/directed-graph.d.ts +51 -51
  40. package/lib/data-structures/graph/directed-graph.js +63 -63
  41. package/lib/data-structures/graph/map-graph.d.ts +13 -13
  42. package/lib/data-structures/graph/map-graph.js +12 -12
  43. package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
  44. package/lib/data-structures/graph/undirected-graph.js +32 -32
  45. package/lib/data-structures/heap/heap.d.ts +1 -1
  46. package/lib/data-structures/tree/tree.d.ts +4 -4
  47. package/lib/data-structures/tree/tree.js +6 -6
  48. package/lib/interfaces/abstract-binary-tree.d.ts +24 -24
  49. package/lib/interfaces/abstract-graph.d.ts +13 -13
  50. package/lib/interfaces/avl-tree.d.ts +3 -3
  51. package/lib/interfaces/bst.d.ts +8 -8
  52. package/lib/interfaces/directed-graph.d.ts +5 -5
  53. package/lib/interfaces/rb-tree.d.ts +2 -2
  54. package/lib/interfaces/undirected-graph.d.ts +2 -2
  55. package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
  56. package/lib/types/data-structures/abstract-graph.d.ts +2 -2
  57. package/lib/types/data-structures/bst.d.ts +2 -2
  58. package/lib/types/data-structures/tree-multiset.d.ts +1 -1
  59. package/lib/types/utils/validate-type.d.ts +8 -8
  60. package/package.json +1 -1
  61. package/scripts/rename_clear_files.sh +29 -0
  62. package/src/data-structures/binary-tree/abstract-binary-tree.ts +147 -147
  63. package/src/data-structures/binary-tree/avl-tree.ts +14 -14
  64. package/src/data-structures/binary-tree/binary-tree.ts +8 -8
  65. package/src/data-structures/binary-tree/bst.ts +98 -90
  66. package/src/data-structures/binary-tree/rb-tree.ts +9 -9
  67. package/src/data-structures/binary-tree/tree-multiset.ts +62 -62
  68. package/src/data-structures/graph/abstract-graph.ts +109 -104
  69. package/src/data-structures/graph/directed-graph.ts +77 -77
  70. package/src/data-structures/graph/map-graph.ts +20 -15
  71. package/src/data-structures/graph/undirected-graph.ts +39 -39
  72. package/src/data-structures/heap/heap.ts +1 -1
  73. package/src/data-structures/tree/tree.ts +7 -7
  74. package/src/interfaces/abstract-binary-tree.ts +24 -24
  75. package/src/interfaces/abstract-graph.ts +13 -13
  76. package/src/interfaces/avl-tree.ts +3 -3
  77. package/src/interfaces/bst.ts +8 -8
  78. package/src/interfaces/directed-graph.ts +5 -5
  79. package/src/interfaces/rb-tree.ts +2 -2
  80. package/src/interfaces/undirected-graph.ts +2 -2
  81. package/src/types/data-structures/abstract-binary-tree.ts +3 -3
  82. package/src/types/data-structures/abstract-graph.ts +2 -2
  83. package/src/types/data-structures/bst.ts +2 -2
  84. package/src/types/data-structures/tree-multiset.ts +1 -1
  85. package/src/types/utils/validate-type.ts +10 -10
  86. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -24
  87. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
  88. package/test/unit/data-structures/binary-tree/bst.test.ts +71 -71
  89. package/test/unit/data-structures/binary-tree/overall.test.ts +19 -19
  90. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +72 -72
  91. package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
  92. package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
  93. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  94. package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
  95. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
  96. package/test/unit/data-structures/tree/tree.test.ts +2 -2
  97. package/umd/bundle.min.js +1 -1
  98. package/umd/bundle.min.js.map +1 -1
@@ -9,21 +9,21 @@ import { arrayRemove, uuidV4 } from '../../utils';
9
9
  import { PriorityQueue } from '../priority-queue';
10
10
  export class AbstractVertex {
11
11
  /**
12
- * The function is a protected constructor that takes an id and an optional value as parameters.
13
- * @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
12
+ * The function is a protected constructor that takes an key and an optional value as parameters.
13
+ * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
14
14
  * used to uniquely identify the vertex object.
15
15
  * @param {V} [val] - The parameter "val" is an optional parameter of type V. It is used to assign a value to the
16
16
  * vertex. If no value is provided, it will be set to undefined.
17
17
  */
18
- constructor(id, val) {
19
- this._id = id;
18
+ constructor(key, val) {
19
+ this._key = key;
20
20
  this._val = val;
21
21
  }
22
- get id() {
23
- return this._id;
22
+ get key() {
23
+ return this._key;
24
24
  }
25
- set id(v) {
26
- this._id = v;
25
+ set key(v) {
26
+ this._key = v;
27
27
  }
28
28
  get val() {
29
29
  return this._val;
@@ -84,46 +84,46 @@ export class AbstractGraph {
84
84
  }
85
85
  /**
86
86
  * The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
87
- * @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
87
+ * @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
88
88
  * the `_vertices` map.
89
- * @returns The method `getVertex` returns the vertex with the specified `vertexId` if it exists in the `_vertices`
89
+ * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
90
90
  * map. If the vertex does not exist, it returns `null`.
91
91
  */
92
- getVertex(vertexId) {
93
- return this._vertices.get(vertexId) || null;
92
+ getVertex(vertexKey) {
93
+ return this._vertices.get(vertexKey) || null;
94
94
  }
95
95
  /**
96
96
  * The function checks if a vertex exists in a graph.
97
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
98
- * (`VertexId`).
97
+ * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
98
+ * (`VertexKey`).
99
99
  * @returns a boolean value.
100
100
  */
101
- hasVertex(vertexOrId) {
102
- return this._vertices.has(this._getVertexId(vertexOrId));
101
+ hasVertex(vertexOrKey) {
102
+ return this._vertices.has(this._getVertexKey(vertexOrKey));
103
103
  }
104
- addVertex(idOrVertex, val) {
105
- if (idOrVertex instanceof AbstractVertex) {
106
- return this._addVertexOnly(idOrVertex);
104
+ addVertex(keyOrVertex, val) {
105
+ if (keyOrVertex instanceof AbstractVertex) {
106
+ return this._addVertexOnly(keyOrVertex);
107
107
  }
108
108
  else {
109
- const newVertex = this.createVertex(idOrVertex, val);
109
+ const newVertex = this.createVertex(keyOrVertex, val);
110
110
  return this._addVertexOnly(newVertex);
111
111
  }
112
112
  }
113
113
  /**
114
114
  * The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
115
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
116
- * (`VertexId`).
115
+ * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
116
+ * (`VertexKey`).
117
117
  * @returns The method is returning a boolean value.
118
118
  */
119
- removeVertex(vertexOrId) {
120
- const vertexId = this._getVertexId(vertexOrId);
121
- return this._vertices.delete(vertexId);
119
+ removeVertex(vertexOrKey) {
120
+ const vertexKey = this._getVertexKey(vertexOrKey);
121
+ return this._vertices.delete(vertexKey);
122
122
  }
123
123
  /**
124
124
  * The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
125
- * @param {V[] | VertexId[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
126
- * of vertex IDs (`VertexId[]`).
125
+ * @param {V[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
126
+ * of vertex IDs (`VertexKey[]`).
127
127
  * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
128
128
  * were removed.
129
129
  */
@@ -136,10 +136,10 @@ export class AbstractGraph {
136
136
  }
137
137
  /**
138
138
  * The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
139
- * @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the unique
139
+ * @param {VertexKey | V} v1 - The parameter v1 can be either a VertexKey or a V. A VertexKey represents the unique
140
140
  * identifier of a vertex in a graph, while V represents the type of the vertex object itself.
141
- * @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
142
- * `VertexId` or a `V` type, which represents the type of the vertex.
141
+ * @param {VertexKey | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
142
+ * `VertexKey` or a `V` type, which represents the type of the vertex.
143
143
  * @returns A boolean value is being returned.
144
144
  */
145
145
  hasEdge(v1, v2) {
@@ -155,30 +155,30 @@ export class AbstractGraph {
155
155
  if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
156
156
  return false;
157
157
  if (srcOrEdge instanceof AbstractVertex)
158
- srcOrEdge = srcOrEdge.id;
158
+ srcOrEdge = srcOrEdge.key;
159
159
  if (dest instanceof AbstractVertex)
160
- dest = dest.id;
160
+ dest = dest.key;
161
161
  const newEdge = this.createEdge(srcOrEdge, dest, weight, val);
162
162
  return this._addEdgeOnly(newEdge);
163
163
  }
164
164
  else {
165
- throw new Error('dest must be a Vertex or vertex id while srcOrEdge is an Edge');
165
+ throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
166
166
  }
167
167
  }
168
168
  }
169
169
  /**
170
170
  * The function sets the weight of an edge between two vertices in a graph.
171
- * @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
171
+ * @param {VertexKey | V} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `V` object. It represents
172
172
  * the source vertex of the edge.
173
- * @param {VertexId | V} destOrId - The `destOrId` parameter represents the destination vertex of the edge. It can be
174
- * either a `VertexId` or a vertex object `V`.
175
- * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrId)
176
- * and the destination vertex (destOrId).
173
+ * @param {VertexKey | V} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
174
+ * either a `VertexKey` or a vertex object `V`.
175
+ * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
176
+ * and the destination vertex (destOrKey).
177
177
  * @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
178
178
  * the weight of the edge and return true. If the edge does not exist, the function will return false.
179
179
  */
180
- setEdgeWeight(srcOrId, destOrId, weight) {
181
- const edge = this.getEdge(srcOrId, destOrId);
180
+ setEdgeWeight(srcOrKey, destOrKey, weight) {
181
+ const edge = this.getEdge(srcOrKey, destOrKey);
182
182
  if (edge) {
183
183
  edge.weight = weight;
184
184
  return true;
@@ -189,9 +189,9 @@ export class AbstractGraph {
189
189
  }
190
190
  /**
191
191
  * The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
192
- * @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
192
+ * @param {V | VertexKey} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
193
193
  * It is the starting vertex for finding paths.
194
- * @param {V | VertexId} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
194
+ * @param {V | VertexKey} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
195
195
  * @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
196
196
  */
197
197
  getAllPathsBetween(v1, v2) {
@@ -235,8 +235,8 @@ export class AbstractGraph {
235
235
  /**
236
236
  * The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
237
237
  * weights or using a breadth-first search algorithm.
238
- * @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
239
- * @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
238
+ * @param {V | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
239
+ * @param {V | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
240
240
  * you want to find the minimum cost or weight from the source vertex `v1`.
241
241
  * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
242
242
  * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
@@ -293,9 +293,9 @@ export class AbstractGraph {
293
293
  /**
294
294
  * The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
295
295
  * using a breadth-first search algorithm.
296
- * @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
297
- * object (`V`) or a vertex ID (`VertexId`).
298
- * @param {V | VertexId} v2 - V | VertexId - The second vertex or vertex ID between which we want to find the minimum
296
+ * @param {V | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
297
+ * object (`V`) or a vertex ID (`VertexKey`).
298
+ * @param {V | VertexKey} v2 - V | VertexKey - The second vertex or vertex ID between which we want to find the minimum
299
299
  * path.
300
300
  * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
301
301
  * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
@@ -357,9 +357,9 @@ export class AbstractGraph {
357
357
  * Dijkstra algorithm time: O(VE) space: O(V + E)
358
358
  * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
359
359
  * a graph without using a heap data structure.
360
- * @param {V | VertexId} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
360
+ * @param {V | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
361
361
  * vertex object or a vertex ID.
362
- * @param {V | VertexId | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
362
+ * @param {V | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
363
363
  * parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
364
364
  * identifier. If no destination is provided, the value is set to `null`.
365
365
  * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
@@ -391,9 +391,9 @@ export class AbstractGraph {
391
391
  return null;
392
392
  }
393
393
  for (const vertex of vertices) {
394
- const vertexOrId = vertex[1];
395
- if (vertexOrId instanceof AbstractVertex)
396
- distMap.set(vertexOrId, Infinity);
394
+ const vertexOrKey = vertex[1];
395
+ if (vertexOrKey instanceof AbstractVertex)
396
+ distMap.set(vertexOrKey, Infinity);
397
397
  }
398
398
  distMap.set(srcVertex, 0);
399
399
  preMap.set(srcVertex, null);
@@ -412,10 +412,10 @@ export class AbstractGraph {
412
412
  };
413
413
  const getPaths = (minV) => {
414
414
  for (const vertex of vertices) {
415
- const vertexOrId = vertex[1];
416
- if (vertexOrId instanceof AbstractVertex) {
417
- const path = [vertexOrId];
418
- let parent = preMap.get(vertexOrId);
415
+ const vertexOrKey = vertex[1];
416
+ if (vertexOrKey instanceof AbstractVertex) {
417
+ const path = [vertexOrKey];
418
+ let parent = preMap.get(vertexOrKey);
419
419
  while (parent) {
420
420
  path.push(parent);
421
421
  parent = preMap.get(parent);
@@ -485,9 +485,9 @@ export class AbstractGraph {
485
485
  * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
486
486
  * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
487
487
  * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
488
- * @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
488
+ * @param {V | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
489
489
  * start. It can be either a vertex object or a vertex ID.
490
- * @param {V | VertexId | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
490
+ * @param {V | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
491
491
  * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
492
492
  * will calculate the shortest paths to all other vertices from the source vertex.
493
493
  * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
@@ -519,14 +519,14 @@ export class AbstractGraph {
519
519
  if (!srcVertex)
520
520
  return null;
521
521
  for (const vertex of vertices) {
522
- const vertexOrId = vertex[1];
523
- if (vertexOrId instanceof AbstractVertex)
524
- distMap.set(vertexOrId, Infinity);
522
+ const vertexOrKey = vertex[1];
523
+ if (vertexOrKey instanceof AbstractVertex)
524
+ distMap.set(vertexOrKey, Infinity);
525
525
  }
526
526
  const heap = new PriorityQueue({
527
- comparator: (a, b) => a.id - b.id
527
+ comparator: (a, b) => a.key - b.key
528
528
  });
529
- heap.add({ id: 0, val: srcVertex });
529
+ heap.add({ key: 0, val: srcVertex });
530
530
  distMap.set(srcVertex, 0);
531
531
  preMap.set(srcVertex, null);
532
532
  /**
@@ -536,10 +536,10 @@ export class AbstractGraph {
536
536
  */
537
537
  const getPaths = (minV) => {
538
538
  for (const vertex of vertices) {
539
- const vertexOrId = vertex[1];
540
- if (vertexOrId instanceof AbstractVertex) {
541
- const path = [vertexOrId];
542
- let parent = preMap.get(vertexOrId);
539
+ const vertexOrKey = vertex[1];
540
+ if (vertexOrKey instanceof AbstractVertex) {
541
+ const path = [vertexOrKey];
542
+ let parent = preMap.get(vertexOrKey);
543
543
  while (parent) {
544
544
  path.push(parent);
545
545
  parent = preMap.get(parent);
@@ -553,7 +553,7 @@ export class AbstractGraph {
553
553
  };
554
554
  while (heap.size > 0) {
555
555
  const curHeapNode = heap.poll();
556
- const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
556
+ const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.key;
557
557
  const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
558
558
  if (dist !== undefined) {
559
559
  if (cur) {
@@ -575,7 +575,7 @@ export class AbstractGraph {
575
575
  const distSrcToNeighbor = distMap.get(neighbor);
576
576
  if (distSrcToNeighbor) {
577
577
  if (dist + weight < distSrcToNeighbor) {
578
- heap.add({ id: dist + weight, val: neighbor });
578
+ heap.add({ key: dist + weight, val: neighbor });
579
579
  preMap.set(neighbor, cur);
580
580
  distMap.set(neighbor, dist + weight);
581
581
  }
@@ -613,7 +613,7 @@ export class AbstractGraph {
613
613
  * 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.
614
614
  * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
615
615
  * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
616
- * @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
616
+ * @param {V | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
617
617
  * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
618
618
  * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
619
619
  * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
@@ -679,10 +679,10 @@ export class AbstractGraph {
679
679
  }
680
680
  if (genPath) {
681
681
  for (const vertex of vertices) {
682
- const vertexOrId = vertex[1];
683
- if (vertexOrId instanceof AbstractVertex) {
684
- const path = [vertexOrId];
685
- let parent = preMap.get(vertexOrId);
682
+ const vertexOrKey = vertex[1];
683
+ if (vertexOrKey instanceof AbstractVertex) {
684
+ const path = [vertexOrKey];
685
+ let parent = preMap.get(vertexOrKey);
686
686
  while (parent !== undefined) {
687
687
  path.push(parent);
688
688
  parent = preMap.get(parent);
@@ -900,17 +900,17 @@ export class AbstractGraph {
900
900
  _addVertexOnly(newVertex) {
901
901
  if (this.hasVertex(newVertex)) {
902
902
  return false;
903
- // throw (new Error('Duplicated vertex id is not allowed'));
903
+ // throw (new Error('Duplicated vertex key is not allowed'));
904
904
  }
905
- this._vertices.set(newVertex.id, newVertex);
905
+ this._vertices.set(newVertex.key, newVertex);
906
906
  return true;
907
907
  }
908
- _getVertex(vertexOrId) {
909
- const vertexId = this._getVertexId(vertexOrId);
910
- return this._vertices.get(vertexId) || null;
908
+ _getVertex(vertexOrKey) {
909
+ const vertexKey = this._getVertexKey(vertexOrKey);
910
+ return this._vertices.get(vertexKey) || null;
911
911
  }
912
- _getVertexId(vertexOrId) {
913
- return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
912
+ _getVertexKey(vertexOrKey) {
913
+ return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
914
914
  }
915
915
  _setVertices(value) {
916
916
  this._vertices = value;
@@ -1,35 +1,35 @@
1
1
  import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
2
- import type { VertexId } from '../../types';
2
+ import type { VertexKey } from '../../types';
3
3
  import { IDirectedGraph } from '../../interfaces';
4
4
  export declare class DirectedVertex<V = any> extends AbstractVertex<V> {
5
5
  /**
6
6
  * The constructor function initializes a vertex with an optional value.
7
- * @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
7
+ * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
8
8
  * used to uniquely identify the vertex within a graph or data structure.
9
9
  * @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to initialize the value of the
10
10
  * vertex. If no value is provided, the vertex will be initialized with a default value.
11
11
  */
12
- constructor(id: VertexId, val?: V);
12
+ constructor(key: VertexKey, val?: V);
13
13
  }
14
14
  export declare class DirectedEdge<V = any> extends AbstractEdge<V> {
15
15
  /**
16
16
  * The constructor function initializes the source and destination vertices of an edge, along with an optional weight
17
17
  * and value.
18
- * @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
18
+ * @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
19
19
  * a graph.
20
- * @param {VertexId} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
21
- * `VertexId`, which is likely a unique identifier for a vertex in a graph.
20
+ * @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
21
+ * `VertexKey`, which is likely a unique identifier for a vertex in a graph.
22
22
  * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
23
23
  * @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value associated with
24
24
  * the edge.
25
25
  */
26
- constructor(src: VertexId, dest: VertexId, weight?: number, val?: V);
26
+ constructor(src: VertexKey, dest: VertexKey, weight?: number, val?: V);
27
27
  private _src;
28
- get src(): VertexId;
29
- set src(v: VertexId);
28
+ get src(): VertexKey;
29
+ set src(v: VertexKey);
30
30
  private _dest;
31
- get dest(): VertexId;
32
- set dest(v: VertexId);
31
+ get dest(): VertexKey;
32
+ set dest(v: VertexKey);
33
33
  }
34
34
  export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
35
35
  /**
@@ -46,45 +46,45 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
46
46
  */
47
47
  /**
48
48
  * The function creates a new vertex with an optional value and returns it.
49
- * @param {VertexId} id - The `id` parameter is the unique identifier for the vertex. It is of type `VertexId`, which
49
+ * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
50
50
  * could be a number or a string depending on how you want to identify your vertices.
51
51
  * @param [val] - The 'val' parameter is an optional value that can be assigned to the vertex. If a value is provided,
52
52
  * it will be assigned to the 'val' property of the vertex. If no value is provided, the 'val' property will be
53
- * assigned the same value as the 'id' parameter
53
+ * assigned the same value as the 'key' parameter
54
54
  * @returns a new instance of a DirectedVertex object, casted as type V.
55
55
  */
56
- createVertex(id: VertexId, val?: V['val']): V;
56
+ createVertex(key: VertexKey, val?: V['val']): V;
57
57
  /**
58
58
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
59
59
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
60
60
  */
61
61
  /**
62
62
  * The function creates a directed edge between two vertices with an optional weight and value.
63
- * @param {VertexId} src - The source vertex ID of the edge. It represents the starting point of the edge.
64
- * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
63
+ * @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
64
+ * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
65
65
  * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
66
66
  * weight is provided, it defaults to 1.
67
67
  * @param [val] - The 'val' parameter is an optional value that can be assigned to the edge. It can be of any type and
68
68
  * is used to store additional information or data associated with the edge.
69
69
  * @returns a new instance of a DirectedEdge object, casted as type E.
70
70
  */
71
- createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E;
71
+ createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E;
72
72
  /**
73
73
  * The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
74
- * @param {V | null | VertexId} srcOrId - The source vertex or its ID. It can be either a vertex object or a vertex ID.
75
- * @param {V | null | VertexId} destOrId - The `destOrId` parameter in the `getEdge` function represents the
76
- * destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`VertexId`), or `null` if the
74
+ * @param {V | null | VertexKey} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
75
+ * @param {V | null | VertexKey} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
76
+ * destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`VertexKey`), or `null` if the
77
77
  * destination is not specified.
78
78
  * @returns the first edge found between the source and destination vertices, or null if no such edge is found.
79
79
  */
80
- getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null;
80
+ getEdge(srcOrKey: V | null | VertexKey, destOrKey: V | null | VertexKey): E | null;
81
81
  /**
82
82
  * The function removes an edge between two vertices in a graph and returns the removed edge.
83
- * @param {V | VertexId} srcOrId - The source vertex or its ID.
84
- * @param {V | VertexId} destOrId - The `destOrId` parameter represents the destination vertex or its ID.
83
+ * @param {V | VertexKey} srcOrKey - The source vertex or its ID.
84
+ * @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
85
85
  * @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
86
86
  */
87
- removeEdgeSrcToDest(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
87
+ removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
88
88
  /**
89
89
  * The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
90
90
  * @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
@@ -94,51 +94,51 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
94
94
  removeEdge(edge: E): E | null;
95
95
  /**
96
96
  * The function removes edges between two vertices and returns the removed edges.
97
- * @param {VertexId | V} v1 - The parameter `v1` can be either a `VertexId` or a `V`. A `VertexId` represents the
97
+ * @param {VertexKey | V} v1 - The parameter `v1` can be either a `VertexKey` or a `V`. A `VertexKey` represents the
98
98
  * unique identifier of a vertex in a graph, while `V` represents the actual vertex object.
99
- * @param {VertexId | V} v2 - The parameter `v2` represents either a `VertexId` or a `V` object. It is used to specify
99
+ * @param {VertexKey | V} v2 - The parameter `v2` represents either a `VertexKey` or a `V` object. It is used to specify
100
100
  * the second vertex in the edge that needs to be removed.
101
101
  * @returns an array of removed edges (E[]).
102
102
  */
103
- removeEdgesBetween(v1: VertexId | V, v2: VertexId | V): E[];
103
+ removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[];
104
104
  /**
105
105
  * The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
106
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
107
- * (`VertexId`).
106
+ * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
107
+ * (`VertexKey`).
108
108
  * @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
109
109
  */
110
- incomingEdgesOf(vertexOrId: V | VertexId): E[];
110
+ incomingEdgesOf(vertexOrKey: V | VertexKey): E[];
111
111
  /**
112
112
  * The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
113
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
114
- * (`VertexId`).
113
+ * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`V`) or a vertex ID
114
+ * (`VertexKey`).
115
115
  * @returns The method `outgoingEdgesOf` returns an array of edges (`E[]`).
116
116
  */
117
- outgoingEdgesOf(vertexOrId: V | VertexId): E[];
117
+ outgoingEdgesOf(vertexOrKey: V | VertexKey): E[];
118
118
  /**
119
119
  * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
120
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
120
+ * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
121
121
  * @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
122
122
  */
123
- degreeOf(vertexOrId: VertexId | V): number;
123
+ degreeOf(vertexOrKey: VertexKey | V): number;
124
124
  /**
125
125
  * The function "inDegreeOf" returns the number of incoming edges for a given vertex.
126
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
126
+ * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
127
127
  * @returns The number of incoming edges of the specified vertex or vertex ID.
128
128
  */
129
- inDegreeOf(vertexOrId: VertexId | V): number;
129
+ inDegreeOf(vertexOrKey: VertexKey | V): number;
130
130
  /**
131
131
  * The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
132
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
132
+ * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
133
133
  * @returns The number of outgoing edges from the specified vertex or vertex ID.
134
134
  */
135
- outDegreeOf(vertexOrId: VertexId | V): number;
135
+ outDegreeOf(vertexOrKey: VertexKey | V): number;
136
136
  /**
137
137
  * The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
138
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
138
+ * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
139
139
  * @returns The function `edgesOf` returns an array of edges.
140
140
  */
141
- edgesOf(vertexOrId: VertexId | V): E[];
141
+ edgesOf(vertexOrKey: VertexKey | V): E[];
142
142
  /**
143
143
  * The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
144
144
  * @param {E} e - The parameter "e" is of type E, which represents an edge in a graph.
@@ -153,20 +153,20 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
153
153
  getEdgeDest(e: E): V | null;
154
154
  /**
155
155
  * The function `getDestinations` returns an array of destination vertices connected to a given vertex.
156
- * @param {V | VertexId | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
157
- * find the destinations. It can be either a `V` object, a `VertexId` value, or `null`.
156
+ * @param {V | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
157
+ * find the destinations. It can be either a `V` object, a `VertexKey` value, or `null`.
158
158
  * @returns an array of vertices (V[]).
159
159
  */
160
- getDestinations(vertex: V | VertexId | null): V[];
160
+ getDestinations(vertex: V | VertexKey | null): V[];
161
161
  /**
162
162
  * The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
163
163
  * in the sorted order, or null if the graph contains a cycle.
164
- * @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
165
- * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
166
- * specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
164
+ * @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
165
+ * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
166
+ * specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
167
167
  * @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
168
168
  */
169
- topologicalSort(propertyName?: 'vertex' | 'id'): Array<V | VertexId> | null;
169
+ topologicalSort(propertyName?: 'vertex' | 'key'): Array<V | VertexKey> | null;
170
170
  /**
171
171
  * The `edgeSet` function returns an array of all the edges in the graph.
172
172
  * @returns The `edgeSet()` method returns an array of edges (`E[]`).
@@ -174,11 +174,11 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
174
174
  edgeSet(): E[];
175
175
  /**
176
176
  * The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
177
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
178
- * (`VertexId`).
177
+ * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
178
+ * (`VertexKey`).
179
179
  * @returns an array of vertices (V[]).
180
180
  */
181
- getNeighbors(vertexOrId: V | VertexId): V[];
181
+ getNeighbors(vertexOrKey: V | VertexKey): V[];
182
182
  /**
183
183
  * The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
184
184
  * otherwise it returns null.