stack-typed 2.0.4 → 2.1.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 (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +6 -6
  64. package/dist/utils/utils.d.ts +110 -49
  65. package/dist/utils/utils.js +148 -73
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  72. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  74. package/src/data-structures/binary-tree/bst.ts +568 -570
  75. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  76. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +9 -5
  101. package/src/utils/utils.ts +152 -86
@@ -1,32 +1,22 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Pablo Zeng
6
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.UndirectedGraph = exports.UndirectedEdge = exports.UndirectedVertex = void 0;
4
11
  const abstract_graph_1 = require("./abstract-graph");
5
12
  const utils_1 = require("../../utils");
6
13
  class UndirectedVertex extends abstract_graph_1.AbstractVertex {
7
- /**
8
- * The constructor function initializes a vertex with an optional value.
9
- * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
10
- * used to uniquely identify the vertex within a graph or network.
11
- * @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
12
- * vertex. If no value is provided, the vertex will be initialized with a default value.
13
- */
14
14
  constructor(key, value) {
15
15
  super(key, value);
16
16
  }
17
17
  }
18
18
  exports.UndirectedVertex = UndirectedVertex;
19
19
  class UndirectedEdge extends abstract_graph_1.AbstractEdge {
20
- /**
21
- * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
22
- * value.
23
- * @param {VertexKey} v1 - The first vertex ID of the edge.
24
- * @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
25
- * graph edge.
26
- * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
27
- * @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
28
- * with the edge.
29
- */
30
20
  constructor(v1, v2, weight, value) {
31
21
  super(weight, value);
32
22
  this.endpoints = [v1, v2];
@@ -34,14 +24,22 @@ class UndirectedEdge extends abstract_graph_1.AbstractEdge {
34
24
  }
35
25
  exports.UndirectedEdge = UndirectedEdge;
36
26
  /**
37
- *
27
+ * Undirected graph implementation.
28
+ * @template V - Vertex value type.
29
+ * @template E - Edge value type.
30
+ * @template VO - Concrete vertex class (extends AbstractVertex<V>).
31
+ * @template EO - Concrete edge class (extends AbstractEdge<E>).
32
+ * @remarks Time O(1), Space O(1)
33
+ * @example examples will be generated by unit test
38
34
  */
39
35
  class UndirectedGraph extends abstract_graph_1.AbstractGraph {
40
36
  /**
41
- * The constructor initializes a new Map object to store edgeMap.
37
+ * Construct an undirected graph with runtime defaults.
38
+ * @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
39
+ * @remarks Time O(1), Space O(1)
42
40
  */
43
- constructor() {
44
- super();
41
+ constructor(options) {
42
+ super(options);
45
43
  this._edgeMap = new Map();
46
44
  }
47
45
  get edgeMap() {
@@ -51,40 +49,62 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
51
49
  this._edgeMap = v;
52
50
  }
53
51
  /**
54
- * The function creates a new vertex with an optional value and returns it.
55
- * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
56
- * vertex from another in the graph.
57
- * @param [value] - The `value` parameter is an optional value that can be assigned to the vertex. If a value is provided,
58
- * it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
59
- * the vertex.
60
- * @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
52
+ * Construct an undirected graph from keys with value initializer `v => v`.
53
+ * @template K - Vertex key type.
54
+ * @param keys - Iterable of vertex keys.
55
+ * @returns UndirectedGraph with all keys added.
56
+ * @remarks Time O(V), Space O(V)
57
+ */
58
+ static fromKeys(keys) {
59
+ const g = new UndirectedGraph({
60
+ vertexValueInitializer: (k) => k
61
+ });
62
+ for (const k of keys)
63
+ g.addVertex(k);
64
+ return g;
65
+ }
66
+ /**
67
+ * Construct an undirected graph from `[key, value]` entries.
68
+ * @template V - Vertex value type.
69
+ * @param entries - Iterable of `[key, value]` pairs.
70
+ * @returns UndirectedGraph with all vertices added.
71
+ * @remarks Time O(V), Space O(V)
72
+ */
73
+ static fromEntries(entries) {
74
+ const g = new UndirectedGraph();
75
+ for (const [k, v] of entries)
76
+ g.addVertex(k, v);
77
+ return g;
78
+ }
79
+ /**
80
+ * Create an undirected vertex instance. Does not insert into the graph.
81
+ * @param key - Vertex identifier.
82
+ * @param value - Optional payload.
83
+ * @returns Concrete vertex instance.
84
+ * @remarks Time O(1), Space O(1)
61
85
  */
62
86
  createVertex(key, value) {
63
- return new UndirectedVertex(key, value !== null && value !== void 0 ? value : key);
87
+ return new UndirectedVertex(key, value);
64
88
  }
65
89
  /**
66
- * The function creates an undirected edge between two vertexMap with an optional weight and value.
67
- * @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
68
- * @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
69
- * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
70
- * no weight is provided, it defaults to 1.
71
- * @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type and
72
- * is used to store additional information or data associated with the edge.
73
- * @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
90
+ * Create an undirected edge instance. Does not insert into the graph.
91
+ * @param v1 - One endpoint key.
92
+ * @param v2 - The other endpoint key.
93
+ * @param weight - Edge weight; defaults to `defaultEdgeWeight`.
94
+ * @param value - Edge payload.
95
+ * @returns Concrete edge instance.
96
+ * @remarks Time O(1), Space O(1)
74
97
  */
75
98
  createEdge(v1, v2, weight, value) {
76
- return new UndirectedEdge(v1, v2, weight !== null && weight !== void 0 ? weight : 1, value);
99
+ var _a;
100
+ return new UndirectedEdge(v1, v2, (_a = weight !== null && weight !== void 0 ? weight : this.options.defaultEdgeWeight) !== null && _a !== void 0 ? _a : 1, value);
77
101
  }
78
102
  /**
79
- * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
80
- * Space Complexity: O(1)
81
- *
82
- * The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
83
- * @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
84
- * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
85
- * @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
86
- * object), `undefined`, or `VertexKey` (vertex ID).
87
- * @returns an edge (EO) or undefined.
103
+ * Get an undirected edge between two vertices, if present.
104
+ * @param v1 - One vertex or key.
105
+ * @param v2 - The other vertex or key.
106
+ * @returns Edge instance or `undefined`.
107
+ * @remarks Time O(1) avg, Space O(1)
88
108
  */
89
109
  getEdge(v1, v2) {
90
110
  var _a;
@@ -99,14 +119,11 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
99
119
  return edgeMap ? edgeMap[0] || undefined : undefined;
100
120
  }
101
121
  /**
102
- * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
103
- * Space Complexity: O(1)
104
- *
105
- * The function removes an edge between two vertexMap in a graph and returns the removed edge.
106
- * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
107
- * @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
108
- * (VertexKey). It represents the second vertex of the edge that needs to be removed.
109
- * @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
122
+ * Delete a single undirected edge between two vertices.
123
+ * @param v1 - One vertex or key.
124
+ * @param v2 - The other vertex or key.
125
+ * @returns Removed edge or `undefined`.
126
+ * @remarks Time O(1) avg, Space O(1)
110
127
  */
111
128
  deleteEdgeBetween(v1, v2) {
112
129
  const vertex1 = this._getVertex(v1);
@@ -126,17 +143,11 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
126
143
  return removed;
127
144
  }
128
145
  /**
129
- * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
130
- * Space Complexity: O(1)
131
- *
132
- * The function `deleteEdge` deletes an edge between two endpoints in a graph.
133
- * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
134
- * either an edge object or a vertex key.
135
- * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
136
- * parameter that represents the key of the vertex on the other side of the edge. It is used when the
137
- * `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
138
- * other side of the
139
- * @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
146
+ * Delete an edge by instance or by a pair of keys.
147
+ * @param edgeOrOneSideVertexKey - Edge instance or one endpoint vertex/key.
148
+ * @param otherSideVertexKey - Required second endpoint when deleting by pair.
149
+ * @returns Removed edge or `undefined`.
150
+ * @remarks Time O(1) avg, Space O(1)
140
151
  */
141
152
  deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
142
153
  let oneSide, otherSide;
@@ -161,13 +172,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
161
172
  }
162
173
  }
163
174
  /**
164
- * Time Complexity: O(1) - Constant time for Map operations.
165
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
166
- *
167
- * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
168
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
169
- * (`VertexKey`).
170
- * @returns The method is returning a boolean value.
175
+ * Delete a vertex and remove it from all neighbor lists.
176
+ * @param vertexOrKey - Vertex or key.
177
+ * @returns `true` if removed; otherwise `false`.
178
+ * @remarks Time O(deg), Space O(1)
171
179
  */
172
180
  deleteVertex(vertexOrKey) {
173
181
  let vertexKey;
@@ -180,6 +188,12 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
180
188
  vertex = vertexOrKey;
181
189
  vertexKey = this._getVertexKey(vertexOrKey);
182
190
  }
191
+ /**
192
+ * All neighbors connected via undirected edges.
193
+ * @param vertexOrKey - Vertex or key.
194
+ * @returns Array of neighbor vertices.
195
+ * @remarks Time O(deg), Space O(deg)
196
+ */
183
197
  const neighbors = this.getNeighbors(vertexOrKey);
184
198
  if (vertex) {
185
199
  neighbors.forEach(neighbor => {
@@ -196,14 +210,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
196
210
  return this._vertexMap.delete(vertexKey);
197
211
  }
198
212
  /**
199
- * Time Complexity: O(1)
200
- * Space Complexity: O(1)
201
- *
202
- * The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
203
- * vertex.
204
- * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
205
- * @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
206
- * edgeMap connected to that vertex.
213
+ * Degree of a vertex (# of incident undirected edges).
214
+ * @param vertexOrKey - Vertex or key.
215
+ * @returns Non-negative integer.
216
+ * @remarks Time O(1) avg, Space O(1)
207
217
  */
208
218
  degreeOf(vertexOrKey) {
209
219
  var _a;
@@ -216,13 +226,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
216
226
  }
217
227
  }
218
228
  /**
219
- * Time Complexity: O(1)
220
- * Space Complexity: O(1)
221
- *
222
- * The function returns the edgeMap of a given vertex or vertex ID.
223
- * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
224
- * unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
225
- * @returns an array of edgeMap.
229
+ * Incident undirected edges of a vertex.
230
+ * @param vertexOrKey - Vertex or key.
231
+ * @returns Array of incident edges.
232
+ * @remarks Time O(deg), Space O(deg)
226
233
  */
227
234
  edgesOf(vertexOrKey) {
228
235
  const vertex = this._getVertex(vertexOrKey);
@@ -234,11 +241,9 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
234
241
  }
235
242
  }
236
243
  /**
237
- * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
238
- * Space Complexity: O(|E|)
239
- *
240
- * The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
241
- * @returns The method `edgeSet()` returns an array of type `EO[]`.
244
+ * Unique set of undirected edges across endpoints.
245
+ * @returns Array of edges.
246
+ * @remarks Time O(E), Space O(E)
242
247
  */
243
248
  edgeSet() {
244
249
  const edgeSet = new Set();
@@ -249,15 +254,6 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
249
254
  });
250
255
  return [...edgeSet];
251
256
  }
252
- /**
253
- * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
254
- * Space Complexity: O(|E|)
255
- *
256
- * The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
257
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
258
- * (`VertexKey`).
259
- * @returns an array of vertexMap (VO[]).
260
- */
261
257
  getNeighbors(vertexOrKey) {
262
258
  const neighbors = [];
263
259
  const vertex = this._getVertex(vertexOrKey);
@@ -273,14 +269,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
273
269
  return neighbors;
274
270
  }
275
271
  /**
276
- * Time Complexity: O(1)
277
- * Space Complexity: O(1)
278
- *
279
- * The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
280
- * it returns undefined.
281
- * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
282
- * @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
283
- * graph. If the edge does not exist, it returns `undefined`.
272
+ * Resolve an edge's two endpoints to vertex instances.
273
+ * @param edge - Edge instance.
274
+ * @returns `[v1, v2]` or `undefined` if either endpoint is missing.
275
+ * @remarks Time O(1), Space O(1)
284
276
  */
285
277
  getEndsOfEdge(edge) {
286
278
  if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
@@ -296,46 +288,32 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
296
288
  }
297
289
  }
298
290
  /**
299
- * The isEmpty function checks if the graph is empty.
300
- * @return True if the graph is empty and false otherwise
291
+ * Whether the graph has no vertices and no edges.
292
+ * @remarks Time O(1), Space O(1)
301
293
  */
302
294
  isEmpty() {
303
295
  return this.vertexMap.size === 0 && this.edgeMap.size === 0;
304
296
  }
305
297
  /**
306
- * Time Complexity: O(1)
307
- * Space Complexity: O(1)
308
- *
309
- * The clear function resets the vertex and edge maps to empty maps.
298
+ * Remove all vertices and edges.
299
+ * @remarks Time O(V + E), Space O(1)
310
300
  */
311
301
  clear() {
312
302
  this._vertexMap = new Map();
313
303
  this._edgeMap = new Map();
314
304
  }
315
305
  /**
316
- * The clone function creates a new UndirectedGraph object and copies the
317
- * vertexMap and edgeMap from this graph to the new one. This is done by
318
- * assigning each of these properties to their respective counterparts in the
319
- * cloned graph. The clone function returns a reference to this newly created,
320
- * cloned UndirectedGraph object.
321
- *
322
- * @return A new instance of the undirectedgraph class
306
+ * Deep clone as the same concrete class.
307
+ * @returns A new graph of the same concrete class (`this` type).
308
+ * @remarks Time O(V + E), Space O(V + E)
323
309
  */
324
310
  clone() {
325
- const cloned = new UndirectedGraph();
326
- cloned.vertexMap = new Map(this.vertexMap);
327
- cloned.edgeMap = new Map(this.edgeMap);
328
- return cloned;
311
+ return super.clone();
329
312
  }
330
313
  /**
331
- * Time Complexity: O(V + E)
332
- * Space Complexity: O(V)
333
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
334
- * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
335
- *
336
- * The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
337
- * graph.
338
- * @returns The function `tarjan()` returns an object with the following properties:
314
+ * Tarjan-based bridge and articulation point detection.
315
+ * @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
316
+ * @remarks Time O(V + E), Space O(V + E)
339
317
  */
340
318
  tarjan() {
341
319
  const dfnMap = new Map();
@@ -388,40 +366,42 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
388
366
  };
389
367
  }
390
368
  /**
391
- * The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
392
- * @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
369
+ * Get bridges discovered by `tarjan()`.
370
+ * @returns Array of edges that are bridges.
371
+ * @remarks Time O(B), Space O(1)
393
372
  */
394
373
  getBridges() {
395
374
  return this.tarjan().bridges;
396
375
  }
397
376
  /**
398
- * The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
399
- * @returns the cut vertices found using the Tarjan's algorithm.
377
+ * Get articulation points discovered by `tarjan()`.
378
+ * @returns Array of cut vertices.
379
+ * @remarks Time O(C), Space O(1)
400
380
  */
401
381
  getCutVertices() {
402
382
  return this.tarjan().cutVertices;
403
383
  }
404
384
  /**
405
- * The function returns the dfnMap property of the result of the tarjan() function.
406
- * @returns the `dfnMap` property of the result of calling the `tarjan()` function.
385
+ * DFN index map computed by `tarjan()`.
386
+ * @returns Map from vertex to DFN index.
387
+ * @remarks Time O(V), Space O(V)
407
388
  */
408
389
  getDFNMap() {
409
390
  return this.tarjan().dfnMap;
410
391
  }
411
392
  /**
412
- * The function returns the lowMap property of the result of the tarjan() function.
413
- * @returns the lowMap property of the result of calling the tarjan() function.
393
+ * LOW link map computed by `tarjan()`.
394
+ * @returns Map from vertex to LOW value.
395
+ * @remarks Time O(V), Space O(V)
414
396
  */
415
397
  getLowMap() {
416
398
  return this.tarjan().lowMap;
417
399
  }
418
400
  /**
419
- * Time Complexity: O(1)
420
- * Space Complexity: O(1)
421
- *
422
- * The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
423
- * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
424
- * @returns a boolean value.
401
+ * Internal hook to attach an undirected edge into adjacency maps.
402
+ * @param edge - Edge instance.
403
+ * @returns `true` if both endpoints exist; otherwise `false`.
404
+ * @remarks Time O(1) avg, Space O(1)
425
405
  */
426
406
  _addEdge(edge) {
427
407
  for (const end of edge.endpoints) {