heap-typed 1.48.3 → 1.48.5
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.
- package/dist/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/data-structures/base/iterable-base.js +3 -3
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/data-structures/binary-tree/binary-tree.js +16 -13
- package/dist/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/data-structures/binary-tree/bst.js +17 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +53 -52
- package/dist/data-structures/graph/abstract-graph.js +82 -78
- package/dist/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/data-structures/graph/directed-graph.js +111 -65
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/data-structures/graph/undirected-graph.js +117 -54
- package/dist/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/base/base.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +8 -5
- package/src/data-structures/binary-tree/binary-tree.ts +23 -19
- package/src/data-structures/binary-tree/bst.ts +19 -14
- package/src/data-structures/binary-tree/rb-tree.ts +20 -14
- package/src/data-structures/binary-tree/tree-multimap.ts +27 -19
- package/src/data-structures/graph/abstract-graph.ts +87 -82
- package/src/data-structures/graph/directed-graph.ts +114 -65
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +124 -56
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/base/base.ts +3 -3
|
@@ -25,7 +25,7 @@ class DirectedVertex extends abstract_graph_1.AbstractVertex {
|
|
|
25
25
|
exports.DirectedVertex = DirectedVertex;
|
|
26
26
|
class DirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
27
27
|
/**
|
|
28
|
-
* The constructor function initializes the source and destination
|
|
28
|
+
* The constructor function initializes the source and destination vertexMap of an edge, along with an optional weight
|
|
29
29
|
* and value.
|
|
30
30
|
* @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
31
31
|
* a graph.
|
|
@@ -64,7 +64,7 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
64
64
|
/**
|
|
65
65
|
* The function creates a new vertex with an optional value and returns it.
|
|
66
66
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
|
|
67
|
-
* could be a number or a string depending on how you want to identify your
|
|
67
|
+
* could be a number or a string depending on how you want to identify your vertexMap.
|
|
68
68
|
* @param [value] - The 'value' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
69
69
|
* it will be assigned to the 'value' property of the vertex. If no value is provided, the 'value' property will be
|
|
70
70
|
* assigned the same value as the 'key' parameter
|
|
@@ -78,7 +78,7 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
78
78
|
* 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.
|
|
79
79
|
*/
|
|
80
80
|
/**
|
|
81
|
-
* The function creates a directed edge between two
|
|
81
|
+
* The function creates a directed edge between two vertexMap with an optional weight and value.
|
|
82
82
|
* @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
|
|
83
83
|
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
|
|
84
84
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
|
|
@@ -91,43 +91,43 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
91
91
|
return new DirectedEdge(src, dest, weight !== null && weight !== void 0 ? weight : 1, value);
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
|
-
* Time Complexity: O(|V|) where |V| is the number of
|
|
94
|
+
* Time Complexity: O(|V|) where |V| is the number of vertexMap
|
|
95
95
|
* Space Complexity: O(1)
|
|
96
96
|
*/
|
|
97
97
|
/**
|
|
98
|
-
* Time Complexity: O(|V|) where |V| is the number of
|
|
98
|
+
* Time Complexity: O(|V|) where |V| is the number of vertexMap
|
|
99
99
|
* Space Complexity: O(1)
|
|
100
100
|
*
|
|
101
|
-
* The `getEdge` function retrieves an edge between two
|
|
101
|
+
* The `getEdge` function retrieves an edge between two vertexMap based on their source and destination IDs.
|
|
102
102
|
* @param {VO | VertexKey | undefined} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
103
103
|
* @param {VO | VertexKey | undefined} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
104
104
|
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `undefined` if the
|
|
105
105
|
* destination is not specified.
|
|
106
|
-
* @returns the first edge found between the source and destination
|
|
106
|
+
* @returns the first edge found between the source and destination vertexMap, or undefined if no such edge is found.
|
|
107
107
|
*/
|
|
108
108
|
getEdge(srcOrKey, destOrKey) {
|
|
109
|
-
let
|
|
109
|
+
let edgeMap = [];
|
|
110
110
|
if (srcOrKey !== undefined && destOrKey !== undefined) {
|
|
111
111
|
const src = this._getVertex(srcOrKey);
|
|
112
112
|
const dest = this._getVertex(destOrKey);
|
|
113
113
|
if (src && dest) {
|
|
114
114
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
115
115
|
if (srcOutEdges) {
|
|
116
|
-
|
|
116
|
+
edgeMap = srcOutEdges.filter(edge => edge.dest === dest.key);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
|
-
return
|
|
120
|
+
return edgeMap[0] || undefined;
|
|
121
121
|
}
|
|
122
122
|
/**
|
|
123
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
123
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
124
124
|
* Space Complexity: O(1)
|
|
125
125
|
*/
|
|
126
126
|
/**
|
|
127
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
127
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
128
128
|
* Space Complexity: O(1)
|
|
129
129
|
*
|
|
130
|
-
* The function removes an edge between two
|
|
130
|
+
* The function removes an edge between two vertexMap in a graph and returns the removed edge.
|
|
131
131
|
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
|
|
132
132
|
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
133
133
|
* @returns the removed edge (EO) if it exists, or undefined if either the source or destination vertex does not exist.
|
|
@@ -150,48 +150,94 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
150
150
|
return removed;
|
|
151
151
|
}
|
|
152
152
|
/**
|
|
153
|
-
* Time Complexity: O(
|
|
153
|
+
* Time Complexity: O(E) where E is the number of edgeMap
|
|
154
154
|
* Space Complexity: O(1)
|
|
155
155
|
*/
|
|
156
156
|
/**
|
|
157
|
-
* Time Complexity: O(
|
|
157
|
+
* Time Complexity: O(E) where E is the number of edgeMap
|
|
158
158
|
* Space Complexity: O(1)
|
|
159
159
|
*
|
|
160
|
-
* The function removes an edge from a graph and returns the removed edge
|
|
161
|
-
* @param {EO}
|
|
162
|
-
*
|
|
163
|
-
* @
|
|
164
|
-
|
|
165
|
-
|
|
160
|
+
* The `deleteEdge` function removes an edge from a graph and returns the removed edge.
|
|
161
|
+
* @param {EO | VertexKey} edgeOrSrcVertexKey - The `edge` parameter can be either an `EO` object (edge object) or
|
|
162
|
+
* a `VertexKey` (key of a vertex).
|
|
163
|
+
* @param {VertexKey} [destVertexKey] - The `destVertexKey` parameter is an optional parameter that
|
|
164
|
+
* represents the key of the destination vertex of the edge. It is used to specify the destination
|
|
165
|
+
* vertex when the `edge` parameter is a vertex key. If `destVertexKey` is not provided, the function
|
|
166
|
+
* assumes that the `edge`
|
|
167
|
+
* @returns the removed edge (EO) or undefined if no edge was removed.
|
|
168
|
+
*/
|
|
169
|
+
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
166
170
|
let removed = undefined;
|
|
167
|
-
|
|
168
|
-
|
|
171
|
+
let src, dest;
|
|
172
|
+
if (this.isVertexKey(edgeOrSrcVertexKey)) {
|
|
173
|
+
if (this.isVertexKey(destVertexKey)) {
|
|
174
|
+
src = this._getVertex(edgeOrSrcVertexKey);
|
|
175
|
+
dest = this._getVertex(destVertexKey);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
src = this._getVertex(edgeOrSrcVertexKey.src);
|
|
183
|
+
dest = this._getVertex(edgeOrSrcVertexKey.dest);
|
|
184
|
+
}
|
|
169
185
|
if (src && dest) {
|
|
170
186
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
171
187
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
172
|
-
(0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.src === src.key);
|
|
188
|
+
(0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.src === src.key && edge.dest === (dest === null || dest === void 0 ? void 0 : dest.key));
|
|
173
189
|
}
|
|
174
190
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
175
191
|
if (destInEdges && destInEdges.length > 0) {
|
|
176
|
-
removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.dest === dest.key)[0];
|
|
192
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.src === src.key && edge.dest === dest.key)[0];
|
|
177
193
|
}
|
|
178
194
|
}
|
|
179
195
|
return removed;
|
|
180
196
|
}
|
|
181
197
|
/**
|
|
182
|
-
* Time Complexity: O(
|
|
198
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
199
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
200
|
+
*/
|
|
201
|
+
/**
|
|
202
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
203
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
204
|
+
*
|
|
205
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
206
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
207
|
+
* (`VertexKey`).
|
|
208
|
+
* @returns The method is returning a boolean value.
|
|
209
|
+
*/
|
|
210
|
+
deleteVertex(vertexOrKey) {
|
|
211
|
+
let vertexKey;
|
|
212
|
+
let vertex;
|
|
213
|
+
if (this.isVertexKey(vertexOrKey)) {
|
|
214
|
+
vertex = this.getVertex(vertexOrKey);
|
|
215
|
+
vertexKey = vertexOrKey;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
vertex = vertexOrKey;
|
|
219
|
+
vertexKey = this._getVertexKey(vertexOrKey);
|
|
220
|
+
}
|
|
221
|
+
if (vertex) {
|
|
222
|
+
this._outEdgeMap.delete(vertex);
|
|
223
|
+
this._inEdgeMap.delete(vertex);
|
|
224
|
+
}
|
|
225
|
+
return this._vertexMap.delete(vertexKey);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
183
229
|
* Space Complexity: O(1)
|
|
184
230
|
*/
|
|
185
231
|
/**
|
|
186
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
232
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
187
233
|
* Space Complexity: O(1)
|
|
188
234
|
*
|
|
189
|
-
* The function removes
|
|
235
|
+
* The function removes edgeMap between two vertexMap and returns the removed edgeMap.
|
|
190
236
|
* @param {VertexKey | VO} v1 - The parameter `v1` can be either a `VertexKey` or a `VO`. A `VertexKey` represents the
|
|
191
237
|
* unique identifier of a vertex in a graph, while `VO` represents the actual vertex object.
|
|
192
238
|
* @param {VertexKey | VO} v2 - The parameter `v2` represents either a `VertexKey` or a `VO` object. It is used to specify
|
|
193
239
|
* the second vertex in the edge that needs to be removed.
|
|
194
|
-
* @returns an array of removed
|
|
240
|
+
* @returns an array of removed edgeMap (EO[]).
|
|
195
241
|
*/
|
|
196
242
|
deleteEdgesBetween(v1, v2) {
|
|
197
243
|
const removed = [];
|
|
@@ -211,10 +257,10 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
211
257
|
* Time Complexity: O(1)
|
|
212
258
|
* Space Complexity: O(1)
|
|
213
259
|
*
|
|
214
|
-
* The function `incomingEdgesOf` returns an array of incoming
|
|
260
|
+
* The function `incomingEdgesOf` returns an array of incoming edgeMap for a given vertex or vertex ID.
|
|
215
261
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
216
262
|
* (`VertexKey`).
|
|
217
|
-
* @returns The method `incomingEdgesOf` returns an array of
|
|
263
|
+
* @returns The method `incomingEdgesOf` returns an array of edgeMap (`EO[]`).
|
|
218
264
|
*/
|
|
219
265
|
incomingEdgesOf(vertexOrKey) {
|
|
220
266
|
const target = this._getVertex(vertexOrKey);
|
|
@@ -231,10 +277,10 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
231
277
|
* Time Complexity: O(1)
|
|
232
278
|
* Space Complexity: O(1)
|
|
233
279
|
*
|
|
234
|
-
* The function `outgoingEdgesOf` returns an array of outgoing
|
|
280
|
+
* The function `outgoingEdgesOf` returns an array of outgoing edgeMap from a given vertex or vertex ID.
|
|
235
281
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`VO`) or a vertex ID
|
|
236
282
|
* (`VertexKey`).
|
|
237
|
-
* @returns The method `outgoingEdgesOf` returns an array of
|
|
283
|
+
* @returns The method `outgoingEdgesOf` returns an array of edgeMap (`EO[]`).
|
|
238
284
|
*/
|
|
239
285
|
outgoingEdgesOf(vertexOrKey) {
|
|
240
286
|
const target = this._getVertex(vertexOrKey);
|
|
@@ -266,9 +312,9 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
266
312
|
* Time Complexity: O(1)
|
|
267
313
|
* Space Complexity: O(1)
|
|
268
314
|
*
|
|
269
|
-
* The function "inDegreeOf" returns the number of incoming
|
|
315
|
+
* The function "inDegreeOf" returns the number of incoming edgeMap for a given vertex.
|
|
270
316
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
271
|
-
* @returns The number of incoming
|
|
317
|
+
* @returns The number of incoming edgeMap of the specified vertex or vertex ID.
|
|
272
318
|
*/
|
|
273
319
|
inDegreeOf(vertexOrKey) {
|
|
274
320
|
return this.incomingEdgesOf(vertexOrKey).length;
|
|
@@ -281,9 +327,9 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
281
327
|
* Time Complexity: O(1)
|
|
282
328
|
* Space Complexity: O(1)
|
|
283
329
|
*
|
|
284
|
-
* The function `outDegreeOf` returns the number of outgoing
|
|
330
|
+
* The function `outDegreeOf` returns the number of outgoing edgeMap from a given vertex.
|
|
285
331
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
286
|
-
* @returns The number of outgoing
|
|
332
|
+
* @returns The number of outgoing edgeMap from the specified vertex or vertex ID.
|
|
287
333
|
*/
|
|
288
334
|
outDegreeOf(vertexOrKey) {
|
|
289
335
|
return this.outgoingEdgesOf(vertexOrKey).length;
|
|
@@ -296,9 +342,9 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
296
342
|
* Time Complexity: O(1)
|
|
297
343
|
* Space Complexity: O(1)
|
|
298
344
|
*
|
|
299
|
-
* The function "edgesOf" returns an array of both outgoing and incoming
|
|
345
|
+
* The function "edgesOf" returns an array of both outgoing and incoming edgeMap of a given vertex or vertex ID.
|
|
300
346
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
301
|
-
* @returns The function `edgesOf` returns an array of
|
|
347
|
+
* @returns The function `edgesOf` returns an array of edgeMap.
|
|
302
348
|
*/
|
|
303
349
|
edgesOf(vertexOrKey) {
|
|
304
350
|
return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
|
|
@@ -334,17 +380,17 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
334
380
|
return this._getVertex(e.dest);
|
|
335
381
|
}
|
|
336
382
|
/**
|
|
337
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
383
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
338
384
|
* Space Complexity: O(1)
|
|
339
385
|
*/
|
|
340
386
|
/**
|
|
341
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
387
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
342
388
|
* Space Complexity: O(1)
|
|
343
389
|
*
|
|
344
|
-
* The function `getDestinations` returns an array of destination
|
|
390
|
+
* The function `getDestinations` returns an array of destination vertexMap connected to a given vertex.
|
|
345
391
|
* @param {VO | VertexKey | undefined} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
346
392
|
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `undefined`.
|
|
347
|
-
* @returns an array of
|
|
393
|
+
* @returns an array of vertexMap (VO[]).
|
|
348
394
|
*/
|
|
349
395
|
getDestinations(vertex) {
|
|
350
396
|
if (vertex === undefined) {
|
|
@@ -361,26 +407,26 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
361
407
|
return destinations;
|
|
362
408
|
}
|
|
363
409
|
/**
|
|
364
|
-
* Time Complexity: O(|V| + |E|) where |V| is the number of
|
|
410
|
+
* Time Complexity: O(|V| + |E|) where |V| is the number of vertexMap and |E| is the number of edgeMap
|
|
365
411
|
* Space Complexity: O(|V|)
|
|
366
412
|
*/
|
|
367
413
|
/**
|
|
368
|
-
* Time Complexity: O(|V| + |E|) where |V| is the number of
|
|
414
|
+
* Time Complexity: O(|V| + |E|) where |V| is the number of vertexMap and |E| is the number of edgeMap
|
|
369
415
|
* Space Complexity: O(|V|)
|
|
370
416
|
*
|
|
371
|
-
* The `topologicalSort` function performs a topological sort on a graph and returns an array of
|
|
417
|
+
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertexMap or vertex IDs
|
|
372
418
|
* in the sorted order, or undefined if the graph contains a cycle.
|
|
373
419
|
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
374
|
-
* property to use for sorting the
|
|
375
|
-
* specified, the
|
|
376
|
-
* @returns an array of
|
|
420
|
+
* property to use for sorting the vertexMap. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
421
|
+
* specified, the vertexMap themselves will be used for sorting. If 'key' is specified, the ids of
|
|
422
|
+
* @returns an array of vertexMap or vertex IDs in topological order. If there is a cycle in the graph, it returns undefined.
|
|
377
423
|
*/
|
|
378
424
|
topologicalSort(propertyName) {
|
|
379
425
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
380
426
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
381
427
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
382
428
|
const statusMap = new Map();
|
|
383
|
-
for (const entry of this.
|
|
429
|
+
for (const entry of this.vertexMap) {
|
|
384
430
|
statusMap.set(entry[1], 0);
|
|
385
431
|
}
|
|
386
432
|
let sorted = [];
|
|
@@ -400,7 +446,7 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
400
446
|
statusMap.set(cur, 2);
|
|
401
447
|
sorted.push(cur);
|
|
402
448
|
};
|
|
403
|
-
for (const entry of this.
|
|
449
|
+
for (const entry of this.vertexMap) {
|
|
404
450
|
if (statusMap.get(entry[1]) === 0) {
|
|
405
451
|
dfs(entry[1]);
|
|
406
452
|
}
|
|
@@ -412,35 +458,35 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
412
458
|
return sorted.reverse();
|
|
413
459
|
}
|
|
414
460
|
/**
|
|
415
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
461
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
416
462
|
* Space Complexity: O(|E|)
|
|
417
463
|
*/
|
|
418
464
|
/**
|
|
419
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
465
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
420
466
|
* Space Complexity: O(|E|)
|
|
421
467
|
*
|
|
422
|
-
* The `edgeSet` function returns an array of all the
|
|
423
|
-
* @returns The `edgeSet()` method returns an array of
|
|
468
|
+
* The `edgeSet` function returns an array of all the edgeMap in the graph.
|
|
469
|
+
* @returns The `edgeSet()` method returns an array of edgeMap (`EO[]`).
|
|
424
470
|
*/
|
|
425
471
|
edgeSet() {
|
|
426
|
-
let
|
|
472
|
+
let edgeMap = [];
|
|
427
473
|
this._outEdgeMap.forEach(outEdges => {
|
|
428
|
-
|
|
474
|
+
edgeMap = [...edgeMap, ...outEdges];
|
|
429
475
|
});
|
|
430
|
-
return
|
|
476
|
+
return edgeMap;
|
|
431
477
|
}
|
|
432
478
|
/**
|
|
433
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
479
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
434
480
|
* Space Complexity: O(1)
|
|
435
481
|
*/
|
|
436
482
|
/**
|
|
437
|
-
* Time Complexity: O(|E|) where |E| is the number of
|
|
483
|
+
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
438
484
|
* Space Complexity: O(1)
|
|
439
485
|
*
|
|
440
|
-
* The function `getNeighbors` returns an array of neighboring
|
|
486
|
+
* The function `getNeighbors` returns an array of neighboring vertexMap of a given vertex or vertex ID in a graph.
|
|
441
487
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
442
488
|
* (`VertexKey`).
|
|
443
|
-
* @returns an array of
|
|
489
|
+
* @returns an array of vertexMap (VO[]).
|
|
444
490
|
*/
|
|
445
491
|
getNeighbors(vertexOrKey) {
|
|
446
492
|
const neighbors = [];
|
|
@@ -465,10 +511,10 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
465
511
|
* Time Complexity: O(1)
|
|
466
512
|
* Space Complexity: O(1)
|
|
467
513
|
*
|
|
468
|
-
* The function "getEndsOfEdge" returns the source and destination
|
|
514
|
+
* The function "getEndsOfEdge" returns the source and destination vertexMap of an edge if it exists in the graph,
|
|
469
515
|
* otherwise it returns undefined.
|
|
470
516
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
471
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
517
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
472
518
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
473
519
|
*/
|
|
474
520
|
getEndsOfEdge(edge) {
|
|
@@ -492,7 +538,7 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
492
538
|
* Time Complexity: O(1)
|
|
493
539
|
* Space Complexity: O(1)
|
|
494
540
|
*
|
|
495
|
-
* The function `_addEdgeOnly` adds an edge to a graph if the source and destination
|
|
541
|
+
* The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertexMap exist.
|
|
496
542
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
497
543
|
* needs to be added to the graph.
|
|
498
544
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
@@ -32,17 +32,17 @@ export declare class MapEdge<E = any> extends DirectedEdge<E> {
|
|
|
32
32
|
}
|
|
33
33
|
export declare class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVertex<V>, EO extends MapEdge<E> = MapEdge<E>> extends DirectedGraph<V, E, VO, EO> {
|
|
34
34
|
/**
|
|
35
|
-
* The constructor function initializes the
|
|
36
|
-
* @param {MapGraphCoordinate}
|
|
35
|
+
* The constructor function initializes the originCoord and bottomRight properties of a MapGraphCoordinate object.
|
|
36
|
+
* @param {MapGraphCoordinate} originCoord - The `originCoord` parameter is a `MapGraphCoordinate` object that represents the
|
|
37
37
|
* starting point or reference point of the map graph. It defines the coordinates of the top-left corner of the map
|
|
38
38
|
* graph.
|
|
39
39
|
* @param {MapGraphCoordinate} [bottomRight] - The `bottomRight` parameter is an optional parameter of type
|
|
40
40
|
* `MapGraphCoordinate`. It represents the bottom right coordinate of a map graph. If this parameter is not provided,
|
|
41
41
|
* it will default to `undefined`.
|
|
42
42
|
*/
|
|
43
|
-
constructor(
|
|
44
|
-
protected
|
|
45
|
-
get
|
|
43
|
+
constructor(originCoord: MapGraphCoordinate, bottomRight?: MapGraphCoordinate);
|
|
44
|
+
protected _originCoord: MapGraphCoordinate;
|
|
45
|
+
get originCoord(): MapGraphCoordinate;
|
|
46
46
|
protected _bottomRight: MapGraphCoordinate | undefined;
|
|
47
47
|
get bottomRight(): MapGraphCoordinate | undefined;
|
|
48
48
|
/**
|
|
@@ -40,22 +40,22 @@ class MapEdge extends directed_graph_1.DirectedEdge {
|
|
|
40
40
|
exports.MapEdge = MapEdge;
|
|
41
41
|
class MapGraph extends directed_graph_1.DirectedGraph {
|
|
42
42
|
/**
|
|
43
|
-
* The constructor function initializes the
|
|
44
|
-
* @param {MapGraphCoordinate}
|
|
43
|
+
* The constructor function initializes the originCoord and bottomRight properties of a MapGraphCoordinate object.
|
|
44
|
+
* @param {MapGraphCoordinate} originCoord - The `originCoord` parameter is a `MapGraphCoordinate` object that represents the
|
|
45
45
|
* starting point or reference point of the map graph. It defines the coordinates of the top-left corner of the map
|
|
46
46
|
* graph.
|
|
47
47
|
* @param {MapGraphCoordinate} [bottomRight] - The `bottomRight` parameter is an optional parameter of type
|
|
48
48
|
* `MapGraphCoordinate`. It represents the bottom right coordinate of a map graph. If this parameter is not provided,
|
|
49
49
|
* it will default to `undefined`.
|
|
50
50
|
*/
|
|
51
|
-
constructor(
|
|
51
|
+
constructor(originCoord, bottomRight) {
|
|
52
52
|
super();
|
|
53
|
-
this.
|
|
54
|
-
this.
|
|
53
|
+
this._originCoord = [0, 0];
|
|
54
|
+
this._originCoord = originCoord;
|
|
55
55
|
this._bottomRight = bottomRight;
|
|
56
56
|
}
|
|
57
|
-
get
|
|
58
|
-
return this.
|
|
57
|
+
get originCoord() {
|
|
58
|
+
return this._originCoord;
|
|
59
59
|
}
|
|
60
60
|
get bottomRight() {
|
|
61
61
|
return this._bottomRight;
|
|
@@ -71,7 +71,7 @@ class MapGraph extends directed_graph_1.DirectedGraph {
|
|
|
71
71
|
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
72
72
|
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
|
|
73
73
|
*/
|
|
74
|
-
createVertex(key, value, lat = this.
|
|
74
|
+
createVertex(key, value, lat = this.originCoord[0], long = this.originCoord[1]) {
|
|
75
75
|
return new MapVertex(key, value, lat, long);
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
@@ -12,7 +12,7 @@ export declare class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
12
12
|
constructor(key: VertexKey, value?: V);
|
|
13
13
|
}
|
|
14
14
|
export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
15
|
-
|
|
15
|
+
vertexMap: [VertexKey, VertexKey];
|
|
16
16
|
/**
|
|
17
17
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
18
18
|
* value.
|
|
@@ -27,11 +27,11 @@ export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
27
27
|
}
|
|
28
28
|
export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVertex<V> = UndirectedVertex<V>, EO extends UndirectedEdge<E> = UndirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
|
|
29
29
|
/**
|
|
30
|
-
* The constructor initializes a new Map object to store
|
|
30
|
+
* The constructor initializes a new Map object to store edgeMap.
|
|
31
31
|
*/
|
|
32
32
|
constructor();
|
|
33
|
-
protected
|
|
34
|
-
get
|
|
33
|
+
protected _edgeMap: Map<VO, EO[]>;
|
|
34
|
+
get edgeMap(): Map<VO, EO[]>;
|
|
35
35
|
/**
|
|
36
36
|
* The function creates a new vertex with an optional value and returns it.
|
|
37
37
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
@@ -43,7 +43,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
43
43
|
*/
|
|
44
44
|
createVertex(key: VertexKey, value?: VO['value']): VO;
|
|
45
45
|
/**
|
|
46
|
-
* The function creates an undirected edge between two
|
|
46
|
+
* The function creates an undirected edge between two vertexMap with an optional weight and value.
|
|
47
47
|
* @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
48
48
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
49
49
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
@@ -54,14 +54,14 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
54
54
|
*/
|
|
55
55
|
createEdge(v1: VertexKey, v2: VertexKey, weight?: number, value?: EO['value']): EO;
|
|
56
56
|
/**
|
|
57
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
57
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
58
58
|
* Space Complexity: O(1)
|
|
59
59
|
*/
|
|
60
60
|
/**
|
|
61
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
61
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
62
62
|
* Space Complexity: O(1)
|
|
63
63
|
*
|
|
64
|
-
* The function `getEdge` returns the first edge that connects two
|
|
64
|
+
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
|
|
65
65
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
66
66
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
67
67
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -70,33 +70,52 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
70
70
|
*/
|
|
71
71
|
getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined;
|
|
72
72
|
/**
|
|
73
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
73
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
74
74
|
* Space Complexity: O(1)
|
|
75
75
|
*/
|
|
76
76
|
/**
|
|
77
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
77
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
78
78
|
* Space Complexity: O(1)
|
|
79
79
|
*
|
|
80
|
-
* The function removes an edge between two
|
|
80
|
+
* The function removes an edge between two vertexMap in a graph and returns the removed edge.
|
|
81
81
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
82
82
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
83
83
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
84
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the
|
|
84
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
|
|
85
85
|
*/
|
|
86
86
|
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined;
|
|
87
87
|
/**
|
|
88
|
-
* Time Complexity: O(
|
|
88
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
89
89
|
* Space Complexity: O(1)
|
|
90
90
|
*/
|
|
91
91
|
/**
|
|
92
|
-
* Time Complexity: O(
|
|
92
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
93
93
|
* Space Complexity: O(1)
|
|
94
94
|
*
|
|
95
|
-
* The deleteEdge
|
|
96
|
-
* @param {EO}
|
|
97
|
-
*
|
|
95
|
+
* The function `deleteEdge` deletes an edge between two vertexMap in a graph.
|
|
96
|
+
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
97
|
+
* either an edge object or a vertex key.
|
|
98
|
+
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
99
|
+
* parameter that represents the key of the vertex on the other side of the edge. It is used when the
|
|
100
|
+
* `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
|
|
101
|
+
* other side of the
|
|
102
|
+
* @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
|
|
103
|
+
*/
|
|
104
|
+
deleteEdge(edgeOrOneSideVertexKey: EO | VertexKey, otherSideVertexKey?: VertexKey): EO | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
107
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
108
|
+
*/
|
|
109
|
+
/**
|
|
110
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
111
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
112
|
+
*
|
|
113
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
114
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
115
|
+
* (`VertexKey`).
|
|
116
|
+
* @returns The method is returning a boolean value.
|
|
98
117
|
*/
|
|
99
|
-
|
|
118
|
+
deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
100
119
|
/**
|
|
101
120
|
* Time Complexity: O(1)
|
|
102
121
|
* Space Complexity: O(1)
|
|
@@ -105,11 +124,11 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
105
124
|
* Time Complexity: O(1)
|
|
106
125
|
* Space Complexity: O(1)
|
|
107
126
|
*
|
|
108
|
-
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of
|
|
127
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
|
|
109
128
|
* vertex.
|
|
110
129
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
111
130
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
112
|
-
*
|
|
131
|
+
* edgeMap connected to that vertex.
|
|
113
132
|
*/
|
|
114
133
|
degreeOf(vertexOrKey: VertexKey | VO): number;
|
|
115
134
|
/**
|
|
@@ -120,36 +139,36 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
120
139
|
* Time Complexity: O(1)
|
|
121
140
|
* Space Complexity: O(1)
|
|
122
141
|
*
|
|
123
|
-
* The function returns the
|
|
142
|
+
* The function returns the edgeMap of a given vertex or vertex ID.
|
|
124
143
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
125
144
|
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
126
|
-
* @returns an array of
|
|
145
|
+
* @returns an array of edgeMap.
|
|
127
146
|
*/
|
|
128
147
|
edgesOf(vertexOrKey: VertexKey | VO): EO[];
|
|
129
148
|
/**
|
|
130
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
149
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
131
150
|
* Space Complexity: O(|E|)
|
|
132
151
|
*/
|
|
133
152
|
/**
|
|
134
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
153
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
135
154
|
* Space Complexity: O(|E|)
|
|
136
155
|
*
|
|
137
|
-
* The function "edgeSet" returns an array of unique
|
|
156
|
+
* The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
|
|
138
157
|
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
139
158
|
*/
|
|
140
159
|
edgeSet(): EO[];
|
|
141
160
|
/**
|
|
142
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
161
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
143
162
|
* Space Complexity: O(|E|)
|
|
144
163
|
*/
|
|
145
164
|
/**
|
|
146
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
165
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
147
166
|
* Space Complexity: O(|E|)
|
|
148
167
|
*
|
|
149
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
168
|
+
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
|
|
150
169
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
151
170
|
* (`VertexKey`).
|
|
152
|
-
* @returns an array of
|
|
171
|
+
* @returns an array of vertexMap (VO[]).
|
|
153
172
|
*/
|
|
154
173
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
155
174
|
/**
|
|
@@ -160,10 +179,10 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
160
179
|
* Time Complexity: O(1)
|
|
161
180
|
* Space Complexity: O(1)
|
|
162
181
|
*
|
|
163
|
-
* The function "getEndsOfEdge" returns the
|
|
182
|
+
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
|
|
164
183
|
* it returns undefined.
|
|
165
184
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
166
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
185
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
167
186
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
168
187
|
*/
|
|
169
188
|
getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
@@ -175,7 +194,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
175
194
|
* Time Complexity: O(1)
|
|
176
195
|
* Space Complexity: O(1)
|
|
177
196
|
*
|
|
178
|
-
* The function adds an edge to the graph by updating the adjacency list with the
|
|
197
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
|
|
179
198
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
180
199
|
* @returns a boolean value.
|
|
181
200
|
*/
|