min-heap-typed 1.50.1 → 1.50.3

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 (85) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +120 -9
  2. package/dist/data-structures/base/iterable-base.js +143 -7
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
  4. package/dist/data-structures/binary-tree/avl-tree.js +101 -72
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
  8. package/dist/data-structures/binary-tree/binary-tree.js +484 -376
  9. package/dist/data-structures/binary-tree/bst.d.ts +92 -79
  10. package/dist/data-structures/binary-tree/bst.js +68 -76
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
  12. package/dist/data-structures/binary-tree/rb-tree.js +152 -99
  13. package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
  14. package/dist/data-structures/binary-tree/segment-tree.js +127 -10
  15. package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  16. package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
  17. package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
  18. package/dist/data-structures/graph/abstract-graph.js +3 -189
  19. package/dist/data-structures/graph/directed-graph.d.ts +73 -0
  20. package/dist/data-structures/graph/directed-graph.js +131 -0
  21. package/dist/data-structures/graph/map-graph.d.ts +8 -0
  22. package/dist/data-structures/graph/map-graph.js +14 -0
  23. package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
  24. package/dist/data-structures/graph/undirected-graph.js +151 -18
  25. package/dist/data-structures/hash/hash-map.d.ts +254 -28
  26. package/dist/data-structures/hash/hash-map.js +347 -78
  27. package/dist/data-structures/heap/heap.d.ts +95 -25
  28. package/dist/data-structures/heap/heap.js +95 -26
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  32. package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
  33. package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  34. package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
  35. package/dist/data-structures/matrix/matrix.d.ts +35 -4
  36. package/dist/data-structures/matrix/matrix.js +50 -11
  37. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  38. package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
  39. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  40. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
  41. package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
  42. package/dist/data-structures/priority-queue/priority-queue.js +8 -0
  43. package/dist/data-structures/queue/deque.d.ts +139 -35
  44. package/dist/data-structures/queue/deque.js +200 -62
  45. package/dist/data-structures/queue/queue.d.ts +103 -49
  46. package/dist/data-structures/queue/queue.js +111 -49
  47. package/dist/data-structures/stack/stack.d.ts +51 -21
  48. package/dist/data-structures/stack/stack.js +58 -22
  49. package/dist/data-structures/tree/tree.d.ts +57 -3
  50. package/dist/data-structures/tree/tree.js +77 -11
  51. package/dist/data-structures/trie/trie.d.ts +135 -34
  52. package/dist/data-structures/trie/trie.js +153 -33
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  54. package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
  55. package/dist/types/utils/utils.d.ts +1 -0
  56. package/package.json +2 -2
  57. package/src/data-structures/base/iterable-base.ts +184 -19
  58. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  59. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  60. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  61. package/src/data-structures/binary-tree/bst.ts +127 -136
  62. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  63. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  64. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  65. package/src/data-structures/graph/abstract-graph.ts +4 -211
  66. package/src/data-structures/graph/directed-graph.ts +152 -0
  67. package/src/data-structures/graph/map-graph.ts +15 -0
  68. package/src/data-structures/graph/undirected-graph.ts +171 -19
  69. package/src/data-structures/hash/hash-map.ts +389 -96
  70. package/src/data-structures/heap/heap.ts +97 -26
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  72. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  73. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  74. package/src/data-structures/matrix/matrix.ts +52 -12
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  78. package/src/data-structures/queue/deque.ts +225 -70
  79. package/src/data-structures/queue/queue.ts +118 -49
  80. package/src/data-structures/stack/stack.ts +63 -23
  81. package/src/data-structures/tree/tree.ts +89 -15
  82. package/src/data-structures/trie/trie.ts +173 -38
  83. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  84. package/src/types/data-structures/hash/hash-map.ts +4 -3
  85. package/src/types/utils/utils.ts +2 -0
@@ -19,7 +19,7 @@ export declare class UndirectedVertex<V = any> extends AbstractVertex<V> {
19
19
  constructor(key: VertexKey, value?: V);
20
20
  }
21
21
  export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
22
- vertexMap: [VertexKey, VertexKey];
22
+ endpoints: [VertexKey, VertexKey];
23
23
  /**
24
24
  * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
25
25
  * value.
@@ -39,6 +39,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
39
39
  constructor();
40
40
  protected _edgeMap: Map<VO, EO[]>;
41
41
  get edgeMap(): Map<VO, EO[]>;
42
+ set edgeMap(v: Map<VO, EO[]>);
42
43
  /**
43
44
  * The function creates a new vertex with an optional value and returns it.
44
45
  * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
@@ -68,7 +69,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
68
69
  * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
69
70
  * Space Complexity: O(1)
70
71
  *
71
- * The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
72
+ * The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
72
73
  * @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
73
74
  * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
74
75
  * @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
@@ -88,7 +89,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
88
89
  * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
89
90
  * @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
90
91
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
91
- * @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
92
+ * @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
92
93
  */
93
94
  deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined;
94
95
  /**
@@ -99,7 +100,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
99
100
  * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
100
101
  * Space Complexity: O(1)
101
102
  *
102
- * The function `deleteEdge` deletes an edge between two vertexMap in a graph.
103
+ * The function `deleteEdge` deletes an edge between two endpoints in a graph.
103
104
  * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
104
105
  * either an edge object or a vertex key.
105
106
  * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
@@ -172,7 +173,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
172
173
  * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
173
174
  * Space Complexity: O(|E|)
174
175
  *
175
- * The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
176
+ * The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
176
177
  * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
177
178
  * (`VertexKey`).
178
179
  * @returns an array of vertexMap (VO[]).
@@ -186,17 +187,85 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
186
187
  * Time Complexity: O(1)
187
188
  * Space Complexity: O(1)
188
189
  *
189
- * The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
190
+ * The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
190
191
  * it returns undefined.
191
192
  * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
192
- * @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
193
+ * @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
193
194
  * graph. If the edge does not exist, it returns `undefined`.
194
195
  */
195
196
  getEndsOfEdge(edge: EO): [VO, VO] | undefined;
197
+ /**
198
+ * The isEmpty function checks if the graph is empty.
199
+ * @return True if the graph is empty and false otherwise
200
+ */
201
+ isEmpty(): boolean;
202
+ /**
203
+ * Time Complexity: O(1)
204
+ * Space Complexity: O(1)
205
+ */
206
+ /**
207
+ * Time Complexity: O(1)
208
+ * Space Complexity: O(1)
209
+ *
210
+ * The clear function resets the vertex and edge maps to empty maps.
211
+ */
212
+ clear(): void;
213
+ /**
214
+ * The clone function creates a new UndirectedGraph object and copies the
215
+ * vertexMap and edgeMap from this graph to the new one. This is done by
216
+ * assigning each of these properties to their respective counterparts in the
217
+ * cloned graph. The clone function returns a reference to this newly created,
218
+ * cloned UndirectedGraph object.
219
+ *
220
+ * @return A new instance of the undirectedgraph class
221
+ */
222
+ clone(): UndirectedGraph<V, E, VO, EO>;
196
223
  /**
197
224
  * Time Complexity: O(1)
198
225
  * Space Complexity: O(1)
199
226
  */
227
+ /**
228
+ * Time Complexity: O(V + E)
229
+ * Space Complexity: O(V)
230
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
231
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
232
+ *
233
+ * The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
234
+ * graph.
235
+ * @returns The function `tarjan()` returns an object with the following properties:
236
+ */
237
+ tarjan(): {
238
+ dfnMap: Map<VO, number>;
239
+ lowMap: Map<VO, number>;
240
+ bridges: EO[];
241
+ cutVertices: VO[];
242
+ };
243
+ /**
244
+ * Time Complexity: O(V + E)
245
+ * Space Complexity: O(V)
246
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
247
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
248
+ */
249
+ /**
250
+ * The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
251
+ * @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
252
+ */
253
+ getBridges(): EO[];
254
+ /**
255
+ * The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
256
+ * @returns the cut vertices found using the Tarjan's algorithm.
257
+ */
258
+ getCutVertices(): VO[];
259
+ /**
260
+ * The function returns the dfnMap property of the result of the tarjan() function.
261
+ * @returns the `dfnMap` property of the result of calling the `tarjan()` function.
262
+ */
263
+ getDFNMap(): Map<VO, number>;
264
+ /**
265
+ * The function returns the lowMap property of the result of the tarjan() function.
266
+ * @returns the lowMap property of the result of calling the tarjan() function.
267
+ */
268
+ getLowMap(): Map<VO, number>;
200
269
  /**
201
270
  * Time Complexity: O(1)
202
271
  * Space Complexity: O(1)
@@ -29,7 +29,7 @@ class UndirectedEdge extends abstract_graph_1.AbstractEdge {
29
29
  */
30
30
  constructor(v1, v2, weight, value) {
31
31
  super(weight, value);
32
- this.vertexMap = [v1, v2];
32
+ this.endpoints = [v1, v2];
33
33
  }
34
34
  }
35
35
  exports.UndirectedEdge = UndirectedEdge;
@@ -44,6 +44,9 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
44
44
  get edgeMap() {
45
45
  return this._edgeMap;
46
46
  }
47
+ set edgeMap(v) {
48
+ this._edgeMap = v;
49
+ }
47
50
  /**
48
51
  * The function creates a new vertex with an optional value and returns it.
49
52
  * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
@@ -77,7 +80,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
77
80
  * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
78
81
  * Space Complexity: O(1)
79
82
  *
80
- * The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
83
+ * The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
81
84
  * @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
82
85
  * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
83
86
  * @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
@@ -91,7 +94,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
91
94
  const vertex1 = this._getVertex(v1);
92
95
  const vertex2 = this._getVertex(v2);
93
96
  if (vertex1 && vertex2) {
94
- edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.vertexMap.includes(vertex2.key));
97
+ edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.endpoints.includes(vertex2.key));
95
98
  }
96
99
  }
97
100
  return edgeMap ? edgeMap[0] || undefined : undefined;
@@ -108,7 +111,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
108
111
  * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
109
112
  * @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
110
113
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
111
- * @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
114
+ * @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
112
115
  */
113
116
  deleteEdgeBetween(v1, v2) {
114
117
  const vertex1 = this._getVertex(v1);
@@ -119,11 +122,11 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
119
122
  const v1Edges = this._edgeMap.get(vertex1);
120
123
  let removed = undefined;
121
124
  if (v1Edges) {
122
- removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.vertexMap.includes(vertex2.key))[0] || undefined;
125
+ removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.endpoints.includes(vertex2.key))[0] || undefined;
123
126
  }
124
127
  const v2Edges = this._edgeMap.get(vertex2);
125
128
  if (v2Edges) {
126
- (0, utils_1.arrayRemove)(v2Edges, (e) => e.vertexMap.includes(vertex1.key));
129
+ (0, utils_1.arrayRemove)(v2Edges, (e) => e.endpoints.includes(vertex1.key));
127
130
  }
128
131
  return removed;
129
132
  }
@@ -135,7 +138,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
135
138
  * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
136
139
  * Space Complexity: O(1)
137
140
  *
138
- * The function `deleteEdge` deletes an edge between two vertexMap in a graph.
141
+ * The function `deleteEdge` deletes an edge between two endpoints in a graph.
139
142
  * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
140
143
  * either an edge object or a vertex key.
141
144
  * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
@@ -156,8 +159,8 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
156
159
  }
157
160
  }
158
161
  else {
159
- oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]);
160
- otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]);
162
+ oneSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[0]);
163
+ otherSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[1]);
161
164
  }
162
165
  if (oneSide && otherSide) {
163
166
  return this.deleteEdgeBetween(oneSide, otherSide);
@@ -196,7 +199,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
196
199
  const neighborEdges = this._edgeMap.get(neighbor);
197
200
  if (neighborEdges) {
198
201
  const restEdges = neighborEdges.filter(edge => {
199
- return !edge.vertexMap.includes(vertexKey);
202
+ return !edge.endpoints.includes(vertexKey);
200
203
  });
201
204
  this._edgeMap.set(neighbor, restEdges);
202
205
  }
@@ -279,7 +282,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
279
282
  * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
280
283
  * Space Complexity: O(|E|)
281
284
  *
282
- * The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
285
+ * The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
283
286
  * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
284
287
  * (`VertexKey`).
285
288
  * @returns an array of vertexMap (VO[]).
@@ -290,7 +293,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
290
293
  if (vertex) {
291
294
  const neighborEdges = this.edgesOf(vertex);
292
295
  for (const edge of neighborEdges) {
293
- const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]);
296
+ const neighbor = this._getVertex(edge.endpoints.filter(e => e !== vertex.key)[0]);
294
297
  if (neighbor) {
295
298
  neighbors.push(neighbor);
296
299
  }
@@ -306,18 +309,18 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
306
309
  * Time Complexity: O(1)
307
310
  * Space Complexity: O(1)
308
311
  *
309
- * The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
312
+ * The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
310
313
  * it returns undefined.
311
314
  * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
312
- * @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
315
+ * @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
313
316
  * graph. If the edge does not exist, it returns `undefined`.
314
317
  */
315
318
  getEndsOfEdge(edge) {
316
- if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) {
319
+ if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
317
320
  return undefined;
318
321
  }
319
- const v1 = this._getVertex(edge.vertexMap[0]);
320
- const v2 = this._getVertex(edge.vertexMap[1]);
322
+ const v1 = this._getVertex(edge.endpoints[0]);
323
+ const v2 = this._getVertex(edge.endpoints[1]);
321
324
  if (v1 && v2) {
322
325
  return [v1, v2];
323
326
  }
@@ -325,10 +328,140 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
325
328
  return undefined;
326
329
  }
327
330
  }
331
+ /**
332
+ * The isEmpty function checks if the graph is empty.
333
+ * @return True if the graph is empty and false otherwise
334
+ */
335
+ isEmpty() {
336
+ return this.vertexMap.size === 0 && this.edgeMap.size === 0;
337
+ }
338
+ /**
339
+ * Time Complexity: O(1)
340
+ * Space Complexity: O(1)
341
+ */
342
+ /**
343
+ * Time Complexity: O(1)
344
+ * Space Complexity: O(1)
345
+ *
346
+ * The clear function resets the vertex and edge maps to empty maps.
347
+ */
348
+ clear() {
349
+ this._vertexMap = new Map();
350
+ this._edgeMap = new Map();
351
+ }
352
+ /**
353
+ * The clone function creates a new UndirectedGraph object and copies the
354
+ * vertexMap and edgeMap from this graph to the new one. This is done by
355
+ * assigning each of these properties to their respective counterparts in the
356
+ * cloned graph. The clone function returns a reference to this newly created,
357
+ * cloned UndirectedGraph object.
358
+ *
359
+ * @return A new instance of the undirectedgraph class
360
+ */
361
+ clone() {
362
+ const cloned = new UndirectedGraph();
363
+ cloned.vertexMap = new Map(this.vertexMap);
364
+ cloned.edgeMap = new Map(this.edgeMap);
365
+ return cloned;
366
+ }
328
367
  /**
329
368
  * Time Complexity: O(1)
330
369
  * Space Complexity: O(1)
331
370
  */
371
+ /**
372
+ * Time Complexity: O(V + E)
373
+ * Space Complexity: O(V)
374
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
375
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
376
+ *
377
+ * The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
378
+ * graph.
379
+ * @returns The function `tarjan()` returns an object with the following properties:
380
+ */
381
+ tarjan() {
382
+ const dfnMap = new Map();
383
+ const lowMap = new Map();
384
+ const bridges = [];
385
+ const cutVertices = [];
386
+ let time = 0;
387
+ const dfs = (vertex, parent) => {
388
+ dfnMap.set(vertex, time);
389
+ lowMap.set(vertex, time);
390
+ time++;
391
+ const neighbors = this.getNeighbors(vertex);
392
+ let childCount = 0;
393
+ for (const neighbor of neighbors) {
394
+ if (!dfnMap.has(neighbor)) {
395
+ childCount++;
396
+ dfs(neighbor, vertex);
397
+ lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
398
+ if (lowMap.get(neighbor) > dfnMap.get(vertex)) {
399
+ // Found a bridge
400
+ const edge = this.getEdge(vertex, neighbor);
401
+ if (edge) {
402
+ bridges.push(edge);
403
+ }
404
+ }
405
+ if (parent !== undefined && lowMap.get(neighbor) >= dfnMap.get(vertex)) {
406
+ // Found an articulation point
407
+ cutVertices.push(vertex);
408
+ }
409
+ }
410
+ else if (neighbor !== parent) {
411
+ lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
412
+ }
413
+ }
414
+ if (parent === undefined && childCount > 1) {
415
+ // Special case for root in DFS tree
416
+ cutVertices.push(vertex);
417
+ }
418
+ };
419
+ for (const vertex of this.vertexMap.values()) {
420
+ if (!dfnMap.has(vertex)) {
421
+ dfs(vertex, undefined);
422
+ }
423
+ }
424
+ return {
425
+ dfnMap,
426
+ lowMap,
427
+ bridges,
428
+ cutVertices
429
+ };
430
+ }
431
+ /**
432
+ * Time Complexity: O(V + E)
433
+ * Space Complexity: O(V)
434
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
435
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
436
+ */
437
+ /**
438
+ * The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
439
+ * @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
440
+ */
441
+ getBridges() {
442
+ return this.tarjan().bridges;
443
+ }
444
+ /**
445
+ * The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
446
+ * @returns the cut vertices found using the Tarjan's algorithm.
447
+ */
448
+ getCutVertices() {
449
+ return this.tarjan().cutVertices;
450
+ }
451
+ /**
452
+ * The function returns the dfnMap property of the result of the tarjan() function.
453
+ * @returns the `dfnMap` property of the result of calling the `tarjan()` function.
454
+ */
455
+ getDFNMap() {
456
+ return this.tarjan().dfnMap;
457
+ }
458
+ /**
459
+ * The function returns the lowMap property of the result of the tarjan() function.
460
+ * @returns the lowMap property of the result of calling the tarjan() function.
461
+ */
462
+ getLowMap() {
463
+ return this.tarjan().lowMap;
464
+ }
332
465
  /**
333
466
  * Time Complexity: O(1)
334
467
  * Space Complexity: O(1)
@@ -338,7 +471,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
338
471
  * @returns a boolean value.
339
472
  */
340
473
  _addEdge(edge) {
341
- for (const end of edge.vertexMap) {
474
+ for (const end of edge.endpoints) {
342
475
  const endVertex = this._getVertex(end);
343
476
  if (endVertex === undefined)
344
477
  return false;