min-heap-typed 1.39.4 → 1.39.6
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/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/data-structures/binary-tree/binary-tree.js +17 -17
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +13 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +96 -96
- package/dist/data-structures/graph/abstract-graph.js +64 -64
- package/dist/data-structures/graph/directed-graph.d.ts +68 -68
- package/dist/data-structures/graph/directed-graph.js +48 -48
- package/dist/data-structures/graph/map-graph.d.ts +13 -13
- package/dist/data-structures/graph/map-graph.js +15 -15
- package/dist/data-structures/graph/undirected-graph.d.ts +42 -42
- package/dist/data-structures/graph/undirected-graph.js +32 -32
- package/dist/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/data-structures/hash/hash-table.js +8 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/data-structures/linked-list/doubly-linked-list.js +54 -54
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/data-structures/linked-list/singly-linked-list.js +52 -52
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/queue/queue.js +4 -4
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/interfaces/graph.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +13 -13
- package/src/data-structures/binary-tree/binary-tree.ts +18 -18
- package/src/data-structures/binary-tree/bst.ts +16 -16
- package/src/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/data-structures/binary-tree/segment-tree.ts +15 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -18
- package/src/data-structures/graph/abstract-graph.ts +156 -154
- package/src/data-structures/graph/directed-graph.ts +99 -94
- package/src/data-structures/graph/map-graph.ts +22 -25
- package/src/data-structures/graph/undirected-graph.ts +62 -60
- package/src/data-structures/hash/hash-table.ts +9 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +61 -61
- package/src/data-structures/linked-list/singly-linked-list.ts +58 -58
- package/src/data-structures/queue/queue.ts +2 -2
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +3 -3
|
@@ -15,15 +15,15 @@ export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
15
15
|
* The constructor function initializes a vertex with an optional value.
|
|
16
16
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
17
17
|
* used to uniquely identify the vertex within a graph or data structure.
|
|
18
|
-
* @param {V} [
|
|
18
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
19
19
|
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
20
|
*/
|
|
21
|
-
constructor(key: VertexKey,
|
|
22
|
-
super(key,
|
|
21
|
+
constructor(key: VertexKey, value?: V) {
|
|
22
|
+
super(key, value);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export class DirectedEdge<
|
|
26
|
+
export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
27
27
|
/**
|
|
28
28
|
* The constructor function initializes the source and destination vertices of an edge, along with an optional weight
|
|
29
29
|
* and value.
|
|
@@ -32,11 +32,11 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
|
32
32
|
* @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
|
|
33
33
|
* `VertexKey`, which is likely a unique identifier for a vertex in a graph.
|
|
34
34
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
35
|
-
* @param {
|
|
35
|
+
* @param {E} [value] - The `value` parameter is an optional parameter of type `E`. It represents the value associated with
|
|
36
36
|
* the edge.
|
|
37
37
|
*/
|
|
38
|
-
constructor(src: VertexKey, dest: VertexKey, weight?: number,
|
|
39
|
-
super(weight,
|
|
38
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
|
39
|
+
super(weight, value);
|
|
40
40
|
this._src = src;
|
|
41
41
|
this._dest = dest;
|
|
42
42
|
}
|
|
@@ -62,9 +62,14 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export class DirectedGraph<
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
export class DirectedGraph<
|
|
66
|
+
V = any,
|
|
67
|
+
E = any,
|
|
68
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
69
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
70
|
+
>
|
|
71
|
+
extends AbstractGraph<V, E, VO, EO>
|
|
72
|
+
implements IGraph<V, E, VO, EO>
|
|
68
73
|
{
|
|
69
74
|
/**
|
|
70
75
|
* The constructor function initializes an instance of a class.
|
|
@@ -73,15 +78,15 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
73
78
|
super();
|
|
74
79
|
}
|
|
75
80
|
|
|
76
|
-
private _outEdgeMap: Map<
|
|
81
|
+
private _outEdgeMap: Map<VO, EO[]> = new Map<VO, EO[]>();
|
|
77
82
|
|
|
78
|
-
get outEdgeMap(): Map<
|
|
83
|
+
get outEdgeMap(): Map<VO, EO[]> {
|
|
79
84
|
return this._outEdgeMap;
|
|
80
85
|
}
|
|
81
86
|
|
|
82
|
-
private _inEdgeMap: Map<
|
|
87
|
+
private _inEdgeMap: Map<VO, EO[]> = new Map<VO, EO[]>();
|
|
83
88
|
|
|
84
|
-
get inEdgeMap(): Map<
|
|
89
|
+
get inEdgeMap(): Map<VO, EO[]> {
|
|
85
90
|
return this._inEdgeMap;
|
|
86
91
|
}
|
|
87
92
|
|
|
@@ -94,13 +99,13 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
94
99
|
* The function creates a new vertex with an optional value and returns it.
|
|
95
100
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
|
|
96
101
|
* could be a number or a string depending on how you want to identify your vertices.
|
|
97
|
-
* @param [
|
|
98
|
-
* it will be assigned to the '
|
|
102
|
+
* @param [value] - The 'value' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
103
|
+
* it will be assigned to the 'value' property of the vertex. If no value is provided, the 'value' property will be
|
|
99
104
|
* assigned the same value as the 'key' parameter
|
|
100
|
-
* @returns a new instance of a DirectedVertex object, casted as type
|
|
105
|
+
* @returns a new instance of a DirectedVertex object, casted as type VO.
|
|
101
106
|
*/
|
|
102
|
-
createVertex(key: VertexKey,
|
|
103
|
-
return new DirectedVertex(key,
|
|
107
|
+
createVertex(key: VertexKey, value?: V): VO {
|
|
108
|
+
return new DirectedVertex(key, value ?? key) as VO;
|
|
104
109
|
}
|
|
105
110
|
|
|
106
111
|
/**
|
|
@@ -114,28 +119,28 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
114
119
|
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
|
|
115
120
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
|
|
116
121
|
* weight is provided, it defaults to 1.
|
|
117
|
-
* @param [
|
|
122
|
+
* @param [value] - The 'value' parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
118
123
|
* is used to store additional information or data associated with the edge.
|
|
119
|
-
* @returns a new instance of a DirectedEdge object, casted as type
|
|
124
|
+
* @returns a new instance of a DirectedEdge object, casted as type EO.
|
|
120
125
|
*/
|
|
121
|
-
createEdge(src: VertexKey, dest: VertexKey, weight?: number,
|
|
122
|
-
return new DirectedEdge(src, dest, weight ?? 1,
|
|
126
|
+
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
127
|
+
return new DirectedEdge(src, dest, weight ?? 1, value) as EO;
|
|
123
128
|
}
|
|
124
129
|
|
|
125
130
|
/**
|
|
126
131
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
127
|
-
* @param {
|
|
128
|
-
* @param {
|
|
129
|
-
* destination vertex of the edge. It can be either a vertex object (`
|
|
132
|
+
* @param {VO | VertexKey | null} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
133
|
+
* @param {VO | VertexKey | null} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
134
|
+
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `null` if the
|
|
130
135
|
* destination is not specified.
|
|
131
136
|
* @returns the first edge found between the source and destination vertices, or null if no such edge is found.
|
|
132
137
|
*/
|
|
133
|
-
getEdge(srcOrKey:
|
|
134
|
-
let edges:
|
|
138
|
+
getEdge(srcOrKey: VO | VertexKey | null, destOrKey: VO | VertexKey | null): EO | null {
|
|
139
|
+
let edges: EO[] = [];
|
|
135
140
|
|
|
136
141
|
if (srcOrKey !== null && destOrKey !== null) {
|
|
137
|
-
const src:
|
|
138
|
-
const dest:
|
|
142
|
+
const src: VO | null = this._getVertex(srcOrKey);
|
|
143
|
+
const dest: VO | null = this._getVertex(destOrKey);
|
|
139
144
|
|
|
140
145
|
if (src && dest) {
|
|
141
146
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
@@ -150,49 +155,49 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
150
155
|
|
|
151
156
|
/**
|
|
152
157
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
153
|
-
* @param {
|
|
154
|
-
* @param {
|
|
155
|
-
* @returns the removed edge (
|
|
158
|
+
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
|
|
159
|
+
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
160
|
+
* @returns the removed edge (EO) if it exists, or null if either the source or destination vertex does not exist.
|
|
156
161
|
*/
|
|
157
|
-
deleteEdgeSrcToDest(srcOrKey:
|
|
158
|
-
const src:
|
|
159
|
-
const dest:
|
|
160
|
-
let removed:
|
|
162
|
+
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | null {
|
|
163
|
+
const src: VO | null = this._getVertex(srcOrKey);
|
|
164
|
+
const dest: VO | null = this._getVertex(destOrKey);
|
|
165
|
+
let removed: EO | null = null;
|
|
161
166
|
if (!src || !dest) {
|
|
162
167
|
return null;
|
|
163
168
|
}
|
|
164
169
|
|
|
165
170
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
166
171
|
if (srcOutEdges) {
|
|
167
|
-
arrayRemove<
|
|
172
|
+
arrayRemove<EO>(srcOutEdges, (edge: EO) => edge.dest === dest.key);
|
|
168
173
|
}
|
|
169
174
|
|
|
170
175
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
171
176
|
if (destInEdges) {
|
|
172
|
-
removed = arrayRemove<
|
|
177
|
+
removed = arrayRemove<EO>(destInEdges, (edge: EO) => edge.src === src.key)[0] || null;
|
|
173
178
|
}
|
|
174
179
|
return removed;
|
|
175
180
|
}
|
|
176
181
|
|
|
177
182
|
/**
|
|
178
183
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
179
|
-
* @param {
|
|
184
|
+
* @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
180
185
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
181
|
-
* @returns The method `deleteEdge` returns the removed edge (`
|
|
186
|
+
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `null` if the edge does not exist.
|
|
182
187
|
*/
|
|
183
|
-
deleteEdge(edge:
|
|
184
|
-
let removed:
|
|
188
|
+
deleteEdge(edge: EO): EO | null {
|
|
189
|
+
let removed: EO | null = null;
|
|
185
190
|
const src = this._getVertex(edge.src);
|
|
186
191
|
const dest = this._getVertex(edge.dest);
|
|
187
192
|
if (src && dest) {
|
|
188
193
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
189
194
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
190
|
-
arrayRemove(srcOutEdges, (edge:
|
|
195
|
+
arrayRemove(srcOutEdges, (edge: EO) => edge.src === src.key);
|
|
191
196
|
}
|
|
192
197
|
|
|
193
198
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
194
199
|
if (destInEdges && destInEdges.length > 0) {
|
|
195
|
-
removed = arrayRemove(destInEdges, (edge:
|
|
200
|
+
removed = arrayRemove(destInEdges, (edge: EO) => edge.dest === dest.key)[0];
|
|
196
201
|
}
|
|
197
202
|
}
|
|
198
203
|
|
|
@@ -201,14 +206,14 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
201
206
|
|
|
202
207
|
/**
|
|
203
208
|
* The function removes edges between two vertices and returns the removed edges.
|
|
204
|
-
* @param {VertexKey |
|
|
205
|
-
* unique identifier of a vertex in a graph, while `
|
|
206
|
-
* @param {VertexKey |
|
|
209
|
+
* @param {VertexKey | VO} v1 - The parameter `v1` can be either a `VertexKey` or a `VO`. A `VertexKey` represents the
|
|
210
|
+
* unique identifier of a vertex in a graph, while `VO` represents the actual vertex object.
|
|
211
|
+
* @param {VertexKey | VO} v2 - The parameter `v2` represents either a `VertexKey` or a `VO` object. It is used to specify
|
|
207
212
|
* the second vertex in the edge that needs to be removed.
|
|
208
|
-
* @returns an array of removed edges (
|
|
213
|
+
* @returns an array of removed edges (EO[]).
|
|
209
214
|
*/
|
|
210
|
-
deleteEdgesBetween(v1: VertexKey |
|
|
211
|
-
const removed:
|
|
215
|
+
deleteEdgesBetween(v1: VertexKey | VO, v2: VertexKey | VO): EO[] {
|
|
216
|
+
const removed: EO[] = [];
|
|
212
217
|
|
|
213
218
|
if (v1 && v2) {
|
|
214
219
|
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
@@ -223,11 +228,11 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
223
228
|
|
|
224
229
|
/**
|
|
225
230
|
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
|
|
226
|
-
* @param {
|
|
231
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
227
232
|
* (`VertexKey`).
|
|
228
|
-
* @returns The method `incomingEdgesOf` returns an array of edges (`
|
|
233
|
+
* @returns The method `incomingEdgesOf` returns an array of edges (`EO[]`).
|
|
229
234
|
*/
|
|
230
|
-
incomingEdgesOf(vertexOrKey:
|
|
235
|
+
incomingEdgesOf(vertexOrKey: VO | VertexKey): EO[] {
|
|
231
236
|
const target = this._getVertex(vertexOrKey);
|
|
232
237
|
if (target) {
|
|
233
238
|
return this.inEdgeMap.get(target) || [];
|
|
@@ -237,11 +242,11 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
237
242
|
|
|
238
243
|
/**
|
|
239
244
|
* The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
|
|
240
|
-
* @param {
|
|
245
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`VO`) or a vertex ID
|
|
241
246
|
* (`VertexKey`).
|
|
242
|
-
* @returns The method `outgoingEdgesOf` returns an array of edges (`
|
|
247
|
+
* @returns The method `outgoingEdgesOf` returns an array of edges (`EO[]`).
|
|
243
248
|
*/
|
|
244
|
-
outgoingEdgesOf(vertexOrKey:
|
|
249
|
+
outgoingEdgesOf(vertexOrKey: VO | VertexKey): EO[] {
|
|
245
250
|
const target = this._getVertex(vertexOrKey);
|
|
246
251
|
if (target) {
|
|
247
252
|
return this._outEdgeMap.get(target) || [];
|
|
@@ -251,69 +256,69 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
251
256
|
|
|
252
257
|
/**
|
|
253
258
|
* The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
|
|
254
|
-
* @param {VertexKey |
|
|
259
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
255
260
|
* @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
|
|
256
261
|
*/
|
|
257
|
-
degreeOf(vertexOrKey: VertexKey |
|
|
262
|
+
degreeOf(vertexOrKey: VertexKey | VO): number {
|
|
258
263
|
return this.outDegreeOf(vertexOrKey) + this.inDegreeOf(vertexOrKey);
|
|
259
264
|
}
|
|
260
265
|
|
|
261
266
|
/**
|
|
262
267
|
* The function "inDegreeOf" returns the number of incoming edges for a given vertex.
|
|
263
|
-
* @param {VertexKey |
|
|
268
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
264
269
|
* @returns The number of incoming edges of the specified vertex or vertex ID.
|
|
265
270
|
*/
|
|
266
|
-
inDegreeOf(vertexOrKey: VertexKey |
|
|
271
|
+
inDegreeOf(vertexOrKey: VertexKey | VO): number {
|
|
267
272
|
return this.incomingEdgesOf(vertexOrKey).length;
|
|
268
273
|
}
|
|
269
274
|
|
|
270
275
|
/**
|
|
271
276
|
* The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
|
|
272
|
-
* @param {VertexKey |
|
|
277
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
273
278
|
* @returns The number of outgoing edges from the specified vertex or vertex ID.
|
|
274
279
|
*/
|
|
275
|
-
outDegreeOf(vertexOrKey: VertexKey |
|
|
280
|
+
outDegreeOf(vertexOrKey: VertexKey | VO): number {
|
|
276
281
|
return this.outgoingEdgesOf(vertexOrKey).length;
|
|
277
282
|
}
|
|
278
283
|
|
|
279
284
|
/**
|
|
280
285
|
* The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
|
|
281
|
-
* @param {VertexKey |
|
|
286
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
282
287
|
* @returns The function `edgesOf` returns an array of edges.
|
|
283
288
|
*/
|
|
284
|
-
edgesOf(vertexOrKey: VertexKey |
|
|
289
|
+
edgesOf(vertexOrKey: VertexKey | VO): EO[] {
|
|
285
290
|
return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
|
|
286
291
|
}
|
|
287
292
|
|
|
288
293
|
/**
|
|
289
294
|
* The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
|
|
290
|
-
* @param {
|
|
291
|
-
* @returns either a vertex object (
|
|
295
|
+
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
|
|
296
|
+
* @returns either a vertex object (VO) or null.
|
|
292
297
|
*/
|
|
293
|
-
getEdgeSrc(e:
|
|
298
|
+
getEdgeSrc(e: EO): VO | null {
|
|
294
299
|
return this._getVertex(e.src);
|
|
295
300
|
}
|
|
296
301
|
|
|
297
302
|
/**
|
|
298
303
|
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
299
|
-
* @param {
|
|
300
|
-
* @returns either a vertex object of type
|
|
304
|
+
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
|
|
305
|
+
* @returns either a vertex object of type VO or null.
|
|
301
306
|
*/
|
|
302
|
-
getEdgeDest(e:
|
|
307
|
+
getEdgeDest(e: EO): VO | null {
|
|
303
308
|
return this._getVertex(e.dest);
|
|
304
309
|
}
|
|
305
310
|
|
|
306
311
|
/**
|
|
307
312
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
308
|
-
* @param {
|
|
309
|
-
* find the destinations. It can be either a `
|
|
310
|
-
* @returns an array of vertices (
|
|
313
|
+
* @param {VO | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
314
|
+
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `null`.
|
|
315
|
+
* @returns an array of vertices (VO[]).
|
|
311
316
|
*/
|
|
312
|
-
getDestinations(vertex:
|
|
317
|
+
getDestinations(vertex: VO | VertexKey | null): VO[] {
|
|
313
318
|
if (vertex === null) {
|
|
314
319
|
return [];
|
|
315
320
|
}
|
|
316
|
-
const destinations:
|
|
321
|
+
const destinations: VO[] = [];
|
|
317
322
|
const outgoingEdges = this.outgoingEdgesOf(vertex);
|
|
318
323
|
for (const outEdge of outgoingEdges) {
|
|
319
324
|
const child = this.getEdgeDest(outEdge);
|
|
@@ -332,18 +337,18 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
332
337
|
* specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
|
|
333
338
|
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
|
|
334
339
|
*/
|
|
335
|
-
topologicalSort(propertyName?: 'vertex' | 'key'): Array<
|
|
340
|
+
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | null {
|
|
336
341
|
propertyName = propertyName ?? 'key';
|
|
337
342
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
338
343
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
339
|
-
const statusMap: Map<
|
|
344
|
+
const statusMap: Map<VO | VertexKey, TopologicalStatus> = new Map<VO | VertexKey, TopologicalStatus>();
|
|
340
345
|
for (const entry of this.vertices) {
|
|
341
346
|
statusMap.set(entry[1], 0);
|
|
342
347
|
}
|
|
343
348
|
|
|
344
|
-
let sorted: (
|
|
349
|
+
let sorted: (VO | VertexKey)[] = [];
|
|
345
350
|
let hasCycle = false;
|
|
346
|
-
const dfs = (cur:
|
|
351
|
+
const dfs = (cur: VO | VertexKey) => {
|
|
347
352
|
statusMap.set(cur, 1);
|
|
348
353
|
const children = this.getDestinations(cur);
|
|
349
354
|
for (const child of children) {
|
|
@@ -372,10 +377,10 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
372
377
|
|
|
373
378
|
/**
|
|
374
379
|
* The `edgeSet` function returns an array of all the edges in the graph.
|
|
375
|
-
* @returns The `edgeSet()` method returns an array of edges (`
|
|
380
|
+
* @returns The `edgeSet()` method returns an array of edges (`EO[]`).
|
|
376
381
|
*/
|
|
377
|
-
edgeSet():
|
|
378
|
-
let edges:
|
|
382
|
+
edgeSet(): EO[] {
|
|
383
|
+
let edges: EO[] = [];
|
|
379
384
|
this._outEdgeMap.forEach(outEdges => {
|
|
380
385
|
edges = [...edges, ...outEdges];
|
|
381
386
|
});
|
|
@@ -384,12 +389,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
384
389
|
|
|
385
390
|
/**
|
|
386
391
|
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
|
|
387
|
-
* @param {
|
|
392
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
388
393
|
* (`VertexKey`).
|
|
389
|
-
* @returns an array of vertices (
|
|
394
|
+
* @returns an array of vertices (VO[]).
|
|
390
395
|
*/
|
|
391
|
-
getNeighbors(vertexOrKey:
|
|
392
|
-
const neighbors:
|
|
396
|
+
getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
|
|
397
|
+
const neighbors: VO[] = [];
|
|
393
398
|
const vertex = this._getVertex(vertexOrKey);
|
|
394
399
|
if (vertex) {
|
|
395
400
|
const outEdges = this.outgoingEdgesOf(vertex);
|
|
@@ -407,11 +412,11 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
407
412
|
/**
|
|
408
413
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
409
414
|
* otherwise it returns null.
|
|
410
|
-
* @param {
|
|
411
|
-
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[
|
|
415
|
+
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
416
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
412
417
|
* graph. If the edge does not exist, it returns `null`.
|
|
413
418
|
*/
|
|
414
|
-
getEndsOfEdge(edge:
|
|
419
|
+
getEndsOfEdge(edge: EO): [VO, VO] | null {
|
|
415
420
|
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
416
421
|
return null;
|
|
417
422
|
}
|
|
@@ -426,12 +431,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
426
431
|
|
|
427
432
|
/**
|
|
428
433
|
* The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertices exist.
|
|
429
|
-
* @param {
|
|
434
|
+
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
430
435
|
* needs to be added to the graph.
|
|
431
436
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
432
437
|
* source or destination vertex does not exist in the graph.
|
|
433
438
|
*/
|
|
434
|
-
protected _addEdgeOnly(edge:
|
|
439
|
+
protected _addEdgeOnly(edge: EO): boolean {
|
|
435
440
|
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
436
441
|
return false;
|
|
437
442
|
}
|
|
@@ -460,11 +465,11 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
460
465
|
}
|
|
461
466
|
}
|
|
462
467
|
|
|
463
|
-
protected _setOutEdgeMap(value: Map<
|
|
468
|
+
protected _setOutEdgeMap(value: Map<VO, EO[]>) {
|
|
464
469
|
this._outEdgeMap = value;
|
|
465
470
|
}
|
|
466
471
|
|
|
467
|
-
protected _setInEdgeMap(value: Map<
|
|
472
|
+
protected _setInEdgeMap(value: Map<VO, EO[]>) {
|
|
468
473
|
this._inEdgeMap = value;
|
|
469
474
|
}
|
|
470
475
|
}
|
|
@@ -11,11 +11,11 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
|
11
11
|
* @param {number} long - The "long" parameter represents the longitude of a location. Longitude is a geographic
|
|
12
12
|
* coordinate that specifies the east-west position of a point on the Earth's surface. It is measured in degrees, with
|
|
13
13
|
* values ranging from -180 to 180.
|
|
14
|
-
* @param {V} [
|
|
14
|
+
* @param {V} [value] - The "value" parameter is an optional value of type V. It is not required to be provided when
|
|
15
15
|
* creating an instance of the class.
|
|
16
16
|
*/
|
|
17
|
-
constructor(key: VertexKey, lat: number, long: number
|
|
18
|
-
super(key,
|
|
17
|
+
constructor(key: VertexKey, value: V, lat: number, long: number) {
|
|
18
|
+
super(key, value);
|
|
19
19
|
this._lat = lat;
|
|
20
20
|
this._long = long;
|
|
21
21
|
}
|
|
@@ -41,7 +41,7 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export class MapEdge<
|
|
44
|
+
export class MapEdge<E = any> extends DirectedEdge<E> {
|
|
45
45
|
/**
|
|
46
46
|
* The constructor function initializes a new instance of a class with the given source, destination, weight, and
|
|
47
47
|
* value.
|
|
@@ -49,18 +49,20 @@ export class MapEdge<V = any> extends DirectedEdge<V> {
|
|
|
49
49
|
* a graph.
|
|
50
50
|
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
|
|
51
51
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
52
|
-
* @param {
|
|
52
|
+
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store additional
|
|
53
53
|
* information or data associated with the edge.
|
|
54
54
|
*/
|
|
55
|
-
constructor(src: VertexKey, dest: VertexKey, weight?: number,
|
|
56
|
-
super(src, dest, weight,
|
|
55
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
|
56
|
+
super(src, dest, weight, value);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
export class MapGraph<
|
|
61
|
-
V,
|
|
62
|
-
E
|
|
63
|
-
>
|
|
60
|
+
export class MapGraph<
|
|
61
|
+
V = any,
|
|
62
|
+
E = any,
|
|
63
|
+
VO extends MapVertex<V> = MapVertex<V>,
|
|
64
|
+
EO extends MapEdge<E> = MapEdge<E>
|
|
65
|
+
> extends DirectedGraph<V, E, VO, EO> {
|
|
64
66
|
/**
|
|
65
67
|
* The constructor function initializes the origin and bottomRight properties of a MapGraphCoordinate object.
|
|
66
68
|
* @param {MapGraphCoordinate} origin - The `origin` parameter is a `MapGraphCoordinate` object that represents the
|
|
@@ -100,20 +102,15 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
|
|
|
100
102
|
* The function creates a new vertex with the given key, value, latitude, and longitude.
|
|
101
103
|
* @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could
|
|
102
104
|
* be a string or a number depending on how you define it in your code.
|
|
103
|
-
* @param [
|
|
104
|
-
* is of type `V
|
|
105
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the `value` property of the vertex. It
|
|
106
|
+
* is of type `V`, which means it should be of the same type as the `value` property of the vertex class `VO`.
|
|
105
107
|
* @param {number} lat - The `lat` parameter represents the latitude of the vertex. It is a number that specifies the
|
|
106
108
|
* position of the vertex on the Earth's surface in the north-south direction.
|
|
107
109
|
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
108
|
-
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `
|
|
110
|
+
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
|
|
109
111
|
*/
|
|
110
|
-
override createVertex(
|
|
111
|
-
key
|
|
112
|
-
lat: number = this.origin[0],
|
|
113
|
-
long: number = this.origin[1],
|
|
114
|
-
val?: V['val']
|
|
115
|
-
): V {
|
|
116
|
-
return new MapVertex(key, lat, long, val) as V;
|
|
112
|
+
override createVertex(key: VertexKey, value?: V, lat: number = this.origin[0], long: number = this.origin[1]): VO {
|
|
113
|
+
return new MapVertex(key, value, lat, long) as VO;
|
|
117
114
|
}
|
|
118
115
|
|
|
119
116
|
/**
|
|
@@ -124,11 +121,11 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
|
|
|
124
121
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
|
|
125
122
|
* is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
|
|
126
123
|
* If the weight is not provided, it can be set to a default value or left undefined.
|
|
127
|
-
* @param [
|
|
124
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type,
|
|
128
125
|
* depending on the specific implementation of the `MapEdge` class.
|
|
129
|
-
* @returns a new instance of the `MapEdge` class, cast as type `
|
|
126
|
+
* @returns a new instance of the `MapEdge` class, cast as type `EO`.
|
|
130
127
|
*/
|
|
131
|
-
override createEdge(src: VertexKey, dest: VertexKey, weight?: number,
|
|
132
|
-
return new MapEdge(src, dest, weight,
|
|
128
|
+
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
129
|
+
return new MapEdge(src, dest, weight, value) as EO;
|
|
133
130
|
}
|
|
134
131
|
}
|