data-structure-typed 1.34.7 → 1.34.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +70 -70
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +4 -4
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +59 -59
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +39 -39
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +49 -49
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +33 -33
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +4 -4
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +14 -14
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/tree/tree.js +5 -5
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +71 -71
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +110 -110
- package/lib/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/lib/data-structures/binary-tree/avl-tree.js +13 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +80 -80
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +27 -27
- package/lib/data-structures/binary-tree/tree-multiset.js +55 -55
- package/lib/data-structures/graph/abstract-graph.d.ts +60 -60
- package/lib/data-structures/graph/abstract-graph.js +81 -81
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +24 -24
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +3 -3
- package/lib/interfaces/bst.d.ts +8 -8
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +1 -1
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +147 -147
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +98 -90
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +62 -62
- package/src/data-structures/graph/abstract-graph.ts +109 -104
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +24 -24
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +3 -3
- package/src/interfaces/bst.ts +8 -8
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -3
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -24
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/overall.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +72 -72
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -7,19 +7,19 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import {arrayRemove} from '../../utils';
|
|
9
9
|
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
|
-
import type {TopologicalStatus,
|
|
10
|
+
import type {TopologicalStatus, VertexKey} from '../../types';
|
|
11
11
|
import {IDirectedGraph} from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
15
15
|
* The constructor function initializes a vertex with an optional value.
|
|
16
|
-
* @param {
|
|
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
18
|
* @param {V} [val] - The "val" 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(
|
|
22
|
-
super(
|
|
21
|
+
constructor(key: VertexKey, val?: V) {
|
|
22
|
+
super(key, val);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -27,37 +27,37 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
|
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.
|
|
30
|
-
* @param {
|
|
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.
|
|
32
|
-
* @param {
|
|
33
|
-
* `
|
|
32
|
+
* @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
|
|
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
35
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value associated with
|
|
36
36
|
* the edge.
|
|
37
37
|
*/
|
|
38
|
-
constructor(src:
|
|
38
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, val?: V) {
|
|
39
39
|
super(weight, val);
|
|
40
40
|
this._src = src;
|
|
41
41
|
this._dest = dest;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
private _src:
|
|
44
|
+
private _src: VertexKey;
|
|
45
45
|
|
|
46
|
-
get src():
|
|
46
|
+
get src(): VertexKey {
|
|
47
47
|
return this._src;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
set src(v:
|
|
50
|
+
set src(v: VertexKey) {
|
|
51
51
|
this._src = v;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
private _dest:
|
|
54
|
+
private _dest: VertexKey;
|
|
55
55
|
|
|
56
|
-
get dest():
|
|
56
|
+
get dest(): VertexKey {
|
|
57
57
|
return this._dest;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
set dest(v:
|
|
60
|
+
set dest(v: VertexKey) {
|
|
61
61
|
this._dest = v;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
@@ -92,15 +92,15 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
94
|
* The function creates a new vertex with an optional value and returns it.
|
|
95
|
-
* @param {
|
|
95
|
+
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
|
|
96
96
|
* could be a number or a string depending on how you want to identify your vertices.
|
|
97
97
|
* @param [val] - The 'val' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
98
98
|
* it will be assigned to the 'val' property of the vertex. If no value is provided, the 'val' property will be
|
|
99
|
-
* assigned the same value as the '
|
|
99
|
+
* assigned the same value as the 'key' parameter
|
|
100
100
|
* @returns a new instance of a DirectedVertex object, casted as type V.
|
|
101
101
|
*/
|
|
102
|
-
createVertex(
|
|
103
|
-
return new DirectedVertex(
|
|
102
|
+
createVertex(key: VertexKey, val?: V['val']): V {
|
|
103
|
+
return new DirectedVertex(key, val ?? key) as V;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
/**
|
|
@@ -110,37 +110,37 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
112
|
* The function creates a directed edge between two vertices with an optional weight and value.
|
|
113
|
-
* @param {
|
|
114
|
-
* @param {
|
|
113
|
+
* @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
|
|
114
|
+
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
|
|
115
115
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
|
|
116
116
|
* weight is provided, it defaults to 1.
|
|
117
117
|
* @param [val] - The 'val' parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
118
118
|
* is used to store additional information or data associated with the edge.
|
|
119
119
|
* @returns a new instance of a DirectedEdge object, casted as type E.
|
|
120
120
|
*/
|
|
121
|
-
createEdge(src:
|
|
121
|
+
createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E {
|
|
122
122
|
return new DirectedEdge(src, dest, weight ?? 1, val) as E;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
126
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
127
|
-
* @param {V | null |
|
|
128
|
-
* @param {V | null |
|
|
129
|
-
* destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`
|
|
127
|
+
* @param {V | null | VertexKey} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
128
|
+
* @param {V | null | VertexKey} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
129
|
+
* destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`VertexKey`), or `null` if the
|
|
130
130
|
* destination is not specified.
|
|
131
131
|
* @returns the first edge found between the source and destination vertices, or null if no such edge is found.
|
|
132
132
|
*/
|
|
133
|
-
getEdge(
|
|
133
|
+
getEdge(srcOrKey: V | null | VertexKey, destOrKey: V | null | VertexKey): E | null {
|
|
134
134
|
let edges: E[] = [];
|
|
135
135
|
|
|
136
|
-
if (
|
|
137
|
-
const src: V | null = this._getVertex(
|
|
138
|
-
const dest: V | null = this._getVertex(
|
|
136
|
+
if (srcOrKey !== null && destOrKey !== null) {
|
|
137
|
+
const src: V | null = this._getVertex(srcOrKey);
|
|
138
|
+
const dest: V | null = this._getVertex(destOrKey);
|
|
139
139
|
|
|
140
140
|
if (src && dest) {
|
|
141
141
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
142
142
|
if (srcOutEdges) {
|
|
143
|
-
edges = srcOutEdges.filter(edge => edge.dest === dest.
|
|
143
|
+
edges = srcOutEdges.filter(edge => edge.dest === dest.key);
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
}
|
|
@@ -150,13 +150,13 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
152
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
153
|
-
* @param {V |
|
|
154
|
-
* @param {V |
|
|
153
|
+
* @param {V | VertexKey} srcOrKey - The source vertex or its ID.
|
|
154
|
+
* @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
155
155
|
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
156
156
|
*/
|
|
157
|
-
removeEdgeSrcToDest(
|
|
158
|
-
const src: V | null = this._getVertex(
|
|
159
|
-
const dest: V | null = this._getVertex(
|
|
157
|
+
removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
|
|
158
|
+
const src: V | null = this._getVertex(srcOrKey);
|
|
159
|
+
const dest: V | null = this._getVertex(destOrKey);
|
|
160
160
|
let removed: E | null = null;
|
|
161
161
|
if (!src || !dest) {
|
|
162
162
|
return null;
|
|
@@ -164,12 +164,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
164
164
|
|
|
165
165
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
166
166
|
if (srcOutEdges) {
|
|
167
|
-
arrayRemove<E>(srcOutEdges, (edge: E) => edge.dest === dest.
|
|
167
|
+
arrayRemove<E>(srcOutEdges, (edge: E) => edge.dest === dest.key);
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
171
171
|
if (destInEdges) {
|
|
172
|
-
removed = arrayRemove<E>(destInEdges, (edge: E) => edge.src === src.
|
|
172
|
+
removed = arrayRemove<E>(destInEdges, (edge: E) => edge.src === src.key)[0] || null;
|
|
173
173
|
}
|
|
174
174
|
return removed;
|
|
175
175
|
}
|
|
@@ -187,12 +187,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
187
187
|
if (src && dest) {
|
|
188
188
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
189
189
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
190
|
-
arrayRemove(srcOutEdges, (edge: E) => edge.src === src.
|
|
190
|
+
arrayRemove(srcOutEdges, (edge: E) => edge.src === src.key);
|
|
191
191
|
}
|
|
192
192
|
|
|
193
193
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
194
194
|
if (destInEdges && destInEdges.length > 0) {
|
|
195
|
-
removed = arrayRemove(destInEdges, (edge: E) => edge.dest === dest.
|
|
195
|
+
removed = arrayRemove(destInEdges, (edge: E) => edge.dest === dest.key)[0];
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
|
|
@@ -201,13 +201,13 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
201
201
|
|
|
202
202
|
/**
|
|
203
203
|
* The function removes edges between two vertices and returns the removed edges.
|
|
204
|
-
* @param {
|
|
204
|
+
* @param {VertexKey | V} v1 - The parameter `v1` can be either a `VertexKey` or a `V`. A `VertexKey` represents the
|
|
205
205
|
* unique identifier of a vertex in a graph, while `V` represents the actual vertex object.
|
|
206
|
-
* @param {
|
|
206
|
+
* @param {VertexKey | V} v2 - The parameter `v2` represents either a `VertexKey` or a `V` object. It is used to specify
|
|
207
207
|
* the second vertex in the edge that needs to be removed.
|
|
208
208
|
* @returns an array of removed edges (E[]).
|
|
209
209
|
*/
|
|
210
|
-
removeEdgesBetween(v1:
|
|
210
|
+
removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
|
|
211
211
|
const removed: E[] = [];
|
|
212
212
|
|
|
213
213
|
if (v1 && v2) {
|
|
@@ -223,12 +223,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
223
223
|
|
|
224
224
|
/**
|
|
225
225
|
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
|
|
226
|
-
* @param {V |
|
|
227
|
-
* (`
|
|
226
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
227
|
+
* (`VertexKey`).
|
|
228
228
|
* @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
|
|
229
229
|
*/
|
|
230
|
-
incomingEdgesOf(
|
|
231
|
-
const target = this._getVertex(
|
|
230
|
+
incomingEdgesOf(vertexOrKey: V | VertexKey): E[] {
|
|
231
|
+
const target = this._getVertex(vertexOrKey);
|
|
232
232
|
if (target) {
|
|
233
233
|
return this.inEdgeMap.get(target) || [];
|
|
234
234
|
}
|
|
@@ -237,12 +237,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
237
237
|
|
|
238
238
|
/**
|
|
239
239
|
* The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
|
|
240
|
-
* @param {V |
|
|
241
|
-
* (`
|
|
240
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`V`) or a vertex ID
|
|
241
|
+
* (`VertexKey`).
|
|
242
242
|
* @returns The method `outgoingEdgesOf` returns an array of edges (`E[]`).
|
|
243
243
|
*/
|
|
244
|
-
outgoingEdgesOf(
|
|
245
|
-
const target = this._getVertex(
|
|
244
|
+
outgoingEdgesOf(vertexOrKey: V | VertexKey): E[] {
|
|
245
|
+
const target = this._getVertex(vertexOrKey);
|
|
246
246
|
if (target) {
|
|
247
247
|
return this._outEdgeMap.get(target) || [];
|
|
248
248
|
}
|
|
@@ -251,38 +251,38 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
251
251
|
|
|
252
252
|
/**
|
|
253
253
|
* The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
|
|
254
|
-
* @param {
|
|
254
|
+
* @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
|
|
255
255
|
* @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
|
|
256
256
|
*/
|
|
257
|
-
degreeOf(
|
|
258
|
-
return this.outDegreeOf(
|
|
257
|
+
degreeOf(vertexOrKey: VertexKey | V): number {
|
|
258
|
+
return this.outDegreeOf(vertexOrKey) + this.inDegreeOf(vertexOrKey);
|
|
259
259
|
}
|
|
260
260
|
|
|
261
261
|
/**
|
|
262
262
|
* The function "inDegreeOf" returns the number of incoming edges for a given vertex.
|
|
263
|
-
* @param {
|
|
263
|
+
* @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
|
|
264
264
|
* @returns The number of incoming edges of the specified vertex or vertex ID.
|
|
265
265
|
*/
|
|
266
|
-
inDegreeOf(
|
|
267
|
-
return this.incomingEdgesOf(
|
|
266
|
+
inDegreeOf(vertexOrKey: VertexKey | V): number {
|
|
267
|
+
return this.incomingEdgesOf(vertexOrKey).length;
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
/**
|
|
271
271
|
* The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
|
|
272
|
-
* @param {
|
|
272
|
+
* @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
|
|
273
273
|
* @returns The number of outgoing edges from the specified vertex or vertex ID.
|
|
274
274
|
*/
|
|
275
|
-
outDegreeOf(
|
|
276
|
-
return this.outgoingEdgesOf(
|
|
275
|
+
outDegreeOf(vertexOrKey: VertexKey | V): number {
|
|
276
|
+
return this.outgoingEdgesOf(vertexOrKey).length;
|
|
277
277
|
}
|
|
278
278
|
|
|
279
279
|
/**
|
|
280
280
|
* The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
|
|
281
|
-
* @param {
|
|
281
|
+
* @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
|
|
282
282
|
* @returns The function `edgesOf` returns an array of edges.
|
|
283
283
|
*/
|
|
284
|
-
edgesOf(
|
|
285
|
-
return [...this.outgoingEdgesOf(
|
|
284
|
+
edgesOf(vertexOrKey: VertexKey | V): E[] {
|
|
285
|
+
return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
|
|
286
286
|
}
|
|
287
287
|
|
|
288
288
|
/**
|
|
@@ -305,11 +305,11 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
305
305
|
|
|
306
306
|
/**
|
|
307
307
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
308
|
-
* @param {V |
|
|
309
|
-
* find the destinations. It can be either a `V` object, a `
|
|
308
|
+
* @param {V | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
309
|
+
* find the destinations. It can be either a `V` object, a `VertexKey` value, or `null`.
|
|
310
310
|
* @returns an array of vertices (V[]).
|
|
311
311
|
*/
|
|
312
|
-
getDestinations(vertex: V |
|
|
312
|
+
getDestinations(vertex: V | VertexKey | null): V[] {
|
|
313
313
|
if (vertex === null) {
|
|
314
314
|
return [];
|
|
315
315
|
}
|
|
@@ -327,23 +327,23 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
327
327
|
/**
|
|
328
328
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
329
329
|
* in the sorted order, or null if the graph contains a cycle.
|
|
330
|
-
* @param {'vertex' | '
|
|
331
|
-
* property to use for sorting the vertices. It can have two possible values: 'vertex' or '
|
|
332
|
-
* specified, the vertices themselves will be used for sorting. If '
|
|
330
|
+
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
331
|
+
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
332
|
+
* specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
|
|
333
333
|
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
|
|
334
334
|
*/
|
|
335
|
-
topologicalSort(propertyName?: 'vertex' | '
|
|
336
|
-
propertyName = propertyName ?? '
|
|
335
|
+
topologicalSort(propertyName?: 'vertex' | 'key'): Array<V | VertexKey> | null {
|
|
336
|
+
propertyName = propertyName ?? 'key';
|
|
337
337
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
338
338
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
339
|
-
const statusMap: Map<V |
|
|
339
|
+
const statusMap: Map<V | VertexKey, TopologicalStatus> = new Map<V | VertexKey, TopologicalStatus>();
|
|
340
340
|
for (const entry of this.vertices) {
|
|
341
341
|
statusMap.set(entry[1], 0);
|
|
342
342
|
}
|
|
343
343
|
|
|
344
|
-
let sorted: (V |
|
|
344
|
+
let sorted: (V | VertexKey)[] = [];
|
|
345
345
|
let hasCycle = false;
|
|
346
|
-
const dfs = (cur: V |
|
|
346
|
+
const dfs = (cur: V | VertexKey) => {
|
|
347
347
|
statusMap.set(cur, 1);
|
|
348
348
|
const children = this.getDestinations(cur);
|
|
349
349
|
for (const child of children) {
|
|
@@ -366,7 +366,7 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
366
366
|
|
|
367
367
|
if (hasCycle) return null;
|
|
368
368
|
|
|
369
|
-
if (propertyName === '
|
|
369
|
+
if (propertyName === 'key') sorted = sorted.map(vertex => (vertex instanceof DirectedVertex ? vertex.key : vertex));
|
|
370
370
|
return sorted.reverse();
|
|
371
371
|
}
|
|
372
372
|
|
|
@@ -384,13 +384,13 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
384
384
|
|
|
385
385
|
/**
|
|
386
386
|
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
|
|
387
|
-
* @param {V |
|
|
388
|
-
* (`
|
|
387
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
388
|
+
* (`VertexKey`).
|
|
389
389
|
* @returns an array of vertices (V[]).
|
|
390
390
|
*/
|
|
391
|
-
getNeighbors(
|
|
391
|
+
getNeighbors(vertexOrKey: V | VertexKey): V[] {
|
|
392
392
|
const neighbors: V[] = [];
|
|
393
|
-
const vertex = this._getVertex(
|
|
393
|
+
const vertex = this._getVertex(vertexOrKey);
|
|
394
394
|
if (vertex) {
|
|
395
395
|
const outEdges = this.outgoingEdgesOf(vertex);
|
|
396
396
|
for (const outEdge of outEdges) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {MapGraphCoordinate,
|
|
1
|
+
import {MapGraphCoordinate, VertexKey} from '../../types';
|
|
2
2
|
import {DirectedEdge, DirectedGraph, DirectedVertex} from './directed-graph';
|
|
3
3
|
|
|
4
4
|
export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
5
5
|
/**
|
|
6
|
-
* The constructor function initializes an object with an
|
|
7
|
-
* @param {
|
|
6
|
+
* The constructor function initializes an object with an key, latitude, longitude, and an optional value.
|
|
7
|
+
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex.
|
|
8
8
|
* @param {number} lat - The "lat" parameter represents the latitude of a vertex. Latitude is a geographic coordinate
|
|
9
9
|
* that specifies the north-south position of a point on the Earth's surface. It is measured in degrees, with positive
|
|
10
10
|
* values representing points north of the equator and negative values representing points south of the equator.
|
|
@@ -14,8 +14,8 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
|
14
14
|
* @param {V} [val] - The "val" 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(
|
|
18
|
-
super(
|
|
17
|
+
constructor(key: VertexKey, lat: number, long: number, val?: V) {
|
|
18
|
+
super(key, val);
|
|
19
19
|
this._lat = lat;
|
|
20
20
|
this._long = long;
|
|
21
21
|
}
|
|
@@ -45,14 +45,14 @@ export class MapEdge<V = any> extends DirectedEdge<V> {
|
|
|
45
45
|
/**
|
|
46
46
|
* The constructor function initializes a new instance of a class with the given source, destination, weight, and
|
|
47
47
|
* value.
|
|
48
|
-
* @param {
|
|
48
|
+
* @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
49
49
|
* a graph.
|
|
50
|
-
* @param {
|
|
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
52
|
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to store additional
|
|
53
53
|
* information or data associated with the edge.
|
|
54
54
|
*/
|
|
55
|
-
constructor(src:
|
|
55
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, val?: V) {
|
|
56
56
|
super(src, dest, weight, val);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -97,8 +97,8 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
|
-
* The function creates a new vertex with the given
|
|
101
|
-
* @param {
|
|
100
|
+
* The function creates a new vertex with the given key, value, latitude, and longitude.
|
|
101
|
+
* @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could
|
|
102
102
|
* be a string or a number depending on how you define it in your code.
|
|
103
103
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the vertex. It
|
|
104
104
|
* is of type `V['val']`, which means it should be of the same type as the `val` property of the vertex class `V`.
|
|
@@ -107,14 +107,19 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
|
|
|
107
107
|
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
108
108
|
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `V`.
|
|
109
109
|
*/
|
|
110
|
-
override createVertex(
|
|
111
|
-
|
|
110
|
+
override createVertex(
|
|
111
|
+
key: VertexKey,
|
|
112
|
+
val?: V['val'],
|
|
113
|
+
lat: number = this.origin[0],
|
|
114
|
+
long: number = this.origin[1]
|
|
115
|
+
): V {
|
|
116
|
+
return new MapVertex(key, lat, long, val) as V;
|
|
112
117
|
}
|
|
113
118
|
|
|
114
119
|
/**
|
|
115
120
|
* The function creates a new instance of a MapEdge with the given source, destination, weight, and value.
|
|
116
|
-
* @param {
|
|
117
|
-
* @param {
|
|
121
|
+
* @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
|
|
122
|
+
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge being
|
|
118
123
|
* created.
|
|
119
124
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
|
|
120
125
|
* is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
|
|
@@ -123,7 +128,7 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
|
|
|
123
128
|
* depending on the specific implementation of the `MapEdge` class.
|
|
124
129
|
* @returns a new instance of the `MapEdge` class, casted as type `E`.
|
|
125
130
|
*/
|
|
126
|
-
override createEdge(src:
|
|
131
|
+
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E {
|
|
127
132
|
return new MapEdge(src, dest, weight, val) as E;
|
|
128
133
|
}
|
|
129
134
|
}
|