directed-graph-typed 1.48.0 → 1.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +28 -19
- package/dist/data-structures/binary-tree/avl-tree.js +22 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -152
- package/dist/data-structures/binary-tree/binary-tree.js +241 -215
- package/dist/data-structures/binary-tree/bst.d.ts +64 -48
- package/dist/data-structures/binary-tree/bst.js +94 -65
- package/dist/data-structures/binary-tree/rb-tree.d.ts +39 -39
- package/dist/data-structures/binary-tree/rb-tree.js +42 -49
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +60 -34
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -27
- package/dist/data-structures/graph/abstract-graph.d.ts +92 -53
- package/dist/data-structures/graph/abstract-graph.js +130 -103
- 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 +160 -44
- package/dist/data-structures/hash/hash-map.js +314 -82
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +42 -55
- package/dist/data-structures/linked-list/doubly-linked-list.js +50 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -55
- package/dist/data-structures/linked-list/singly-linked-list.js +44 -77
- package/dist/data-structures/queue/deque.d.ts +35 -167
- package/dist/data-structures/queue/deque.js +43 -249
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/avl-tree.ts +37 -25
- package/src/data-structures/binary-tree/binary-tree.ts +336 -296
- package/src/data-structures/binary-tree/bst.ts +135 -89
- package/src/data-structures/binary-tree/rb-tree.ts +60 -69
- package/src/data-structures/binary-tree/tree-multimap.ts +86 -49
- package/src/data-structures/graph/abstract-graph.ts +136 -104
- 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 +335 -84
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +54 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -84
- package/src/data-structures/queue/deque.ts +43 -275
- package/src/data-structures/queue/queue.ts +71 -86
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/index.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
|
@@ -24,7 +24,7 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
27
|
-
|
|
27
|
+
vertexMap: [VertexKey, VertexKey];
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
@@ -38,7 +38,7 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
38
38
|
*/
|
|
39
39
|
constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
|
|
40
40
|
super(weight, value);
|
|
41
|
-
this.
|
|
41
|
+
this.vertexMap = [v1, v2];
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -51,17 +51,17 @@ export class UndirectedGraph<
|
|
|
51
51
|
extends AbstractGraph<V, E, VO, EO>
|
|
52
52
|
implements IGraph<V, E, VO, EO> {
|
|
53
53
|
/**
|
|
54
|
-
* The constructor initializes a new Map object to store
|
|
54
|
+
* The constructor initializes a new Map object to store edgeMap.
|
|
55
55
|
*/
|
|
56
56
|
constructor() {
|
|
57
57
|
super();
|
|
58
|
-
this.
|
|
58
|
+
this._edgeMap = new Map<VO, EO[]>();
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
protected
|
|
61
|
+
protected _edgeMap: Map<VO, EO[]>;
|
|
62
62
|
|
|
63
|
-
get
|
|
64
|
-
return this.
|
|
63
|
+
get edgeMap(): Map<VO, EO[]> {
|
|
64
|
+
return this._edgeMap;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/**
|
|
@@ -78,7 +78,7 @@ export class UndirectedGraph<
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/**
|
|
81
|
-
* The function creates an undirected edge between two
|
|
81
|
+
* The function creates an undirected edge between two vertexMap with an optional weight and value.
|
|
82
82
|
* @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
83
83
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
84
84
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
@@ -92,15 +92,15 @@ export class UndirectedGraph<
|
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
95
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
96
96
|
* Space Complexity: O(1)
|
|
97
97
|
*/
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
100
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
101
101
|
* Space Complexity: O(1)
|
|
102
102
|
*
|
|
103
|
-
* The function `getEdge` returns the first edge that connects two
|
|
103
|
+
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
|
|
104
104
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
105
105
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
106
106
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -108,34 +108,34 @@ export class UndirectedGraph<
|
|
|
108
108
|
* @returns an edge (EO) or undefined.
|
|
109
109
|
*/
|
|
110
110
|
getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined {
|
|
111
|
-
let
|
|
111
|
+
let edgeMap: EO[] | undefined = [];
|
|
112
112
|
|
|
113
113
|
if (v1 !== undefined && v2 !== undefined) {
|
|
114
114
|
const vertex1: VO | undefined = this._getVertex(v1);
|
|
115
115
|
const vertex2: VO | undefined = this._getVertex(v2);
|
|
116
116
|
|
|
117
117
|
if (vertex1 && vertex2) {
|
|
118
|
-
|
|
118
|
+
edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.vertexMap.includes(vertex2.key));
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
return
|
|
122
|
+
return edgeMap ? edgeMap[0] || undefined : undefined;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
126
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
127
127
|
* Space Complexity: O(1)
|
|
128
128
|
*/
|
|
129
129
|
|
|
130
130
|
/**
|
|
131
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
131
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
132
132
|
* Space Complexity: O(1)
|
|
133
133
|
*
|
|
134
|
-
* The function removes an edge between two
|
|
134
|
+
* The function removes an edge between two vertexMap in a graph and returns the removed edge.
|
|
135
135
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
136
136
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
137
137
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
138
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the
|
|
138
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
|
|
139
139
|
*/
|
|
140
140
|
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined {
|
|
141
141
|
const vertex1: VO | undefined = this._getVertex(v1);
|
|
@@ -145,33 +145,101 @@ export class UndirectedGraph<
|
|
|
145
145
|
return undefined;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
const v1Edges = this.
|
|
148
|
+
const v1Edges = this._edgeMap.get(vertex1);
|
|
149
149
|
let removed: EO | undefined = undefined;
|
|
150
150
|
if (v1Edges) {
|
|
151
|
-
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.
|
|
151
|
+
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertexMap.includes(vertex2.key))[0] || undefined;
|
|
152
152
|
}
|
|
153
|
-
const v2Edges = this.
|
|
153
|
+
const v2Edges = this._edgeMap.get(vertex2);
|
|
154
154
|
if (v2Edges) {
|
|
155
|
-
arrayRemove<EO>(v2Edges, (e: EO) => e.
|
|
155
|
+
arrayRemove<EO>(v2Edges, (e: EO) => e.vertexMap.includes(vertex1.key));
|
|
156
156
|
}
|
|
157
157
|
return removed;
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
/**
|
|
161
|
-
* Time Complexity: O(
|
|
161
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
162
162
|
* Space Complexity: O(1)
|
|
163
163
|
*/
|
|
164
164
|
|
|
165
|
+
|
|
165
166
|
/**
|
|
166
|
-
* Time Complexity: O(
|
|
167
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
167
168
|
* Space Complexity: O(1)
|
|
168
169
|
*
|
|
169
|
-
* The deleteEdge
|
|
170
|
-
* @param {EO}
|
|
171
|
-
*
|
|
170
|
+
* The function `deleteEdge` deletes an edge between two vertexMap in a graph.
|
|
171
|
+
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
172
|
+
* either an edge object or a vertex key.
|
|
173
|
+
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
174
|
+
* parameter that represents the key of the vertex on the other side of the edge. It is used when the
|
|
175
|
+
* `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
|
|
176
|
+
* other side of the
|
|
177
|
+
* @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
|
|
178
|
+
*/
|
|
179
|
+
deleteEdge(edgeOrOneSideVertexKey: EO | VertexKey, otherSideVertexKey?: VertexKey): EO | undefined {
|
|
180
|
+
let oneSide: VO | undefined, otherSide: VO | undefined;
|
|
181
|
+
if (this.isVertexKey(edgeOrOneSideVertexKey)) {
|
|
182
|
+
if (this.isVertexKey(otherSideVertexKey)) {
|
|
183
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey);
|
|
184
|
+
otherSide = this._getVertex(otherSideVertexKey);
|
|
185
|
+
} else {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]);
|
|
190
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (oneSide && otherSide) {
|
|
194
|
+
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
195
|
+
|
|
196
|
+
} else {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
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
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
208
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
209
|
+
*
|
|
210
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
211
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
212
|
+
* (`VertexKey`).
|
|
213
|
+
* @returns The method is returning a boolean value.
|
|
172
214
|
*/
|
|
173
|
-
|
|
174
|
-
|
|
215
|
+
override deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
216
|
+
let vertexKey: VertexKey;
|
|
217
|
+
let vertex: VO | undefined;
|
|
218
|
+
if (this.isVertexKey(vertexOrKey)) {
|
|
219
|
+
vertex = this.getVertex(vertexOrKey);
|
|
220
|
+
vertexKey = vertexOrKey;
|
|
221
|
+
} else {
|
|
222
|
+
vertex = vertexOrKey;
|
|
223
|
+
vertexKey = this._getVertexKey(vertexOrKey)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const neighbors = this.getNeighbors(vertexOrKey)
|
|
227
|
+
|
|
228
|
+
if (vertex) {
|
|
229
|
+
neighbors.forEach(neighbor => {
|
|
230
|
+
const neighborEdges = this._edgeMap.get(neighbor);
|
|
231
|
+
if (neighborEdges) {
|
|
232
|
+
const restEdges = neighborEdges.filter(edge => {
|
|
233
|
+
return !edge.vertexMap.includes(vertexKey);
|
|
234
|
+
});
|
|
235
|
+
this._edgeMap.set(neighbor, restEdges);
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
this._edgeMap.delete(vertex);
|
|
239
|
+
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return this._vertexMap.delete(vertexKey);
|
|
175
243
|
}
|
|
176
244
|
|
|
177
245
|
/**
|
|
@@ -183,16 +251,16 @@ export class UndirectedGraph<
|
|
|
183
251
|
* Time Complexity: O(1)
|
|
184
252
|
* Space Complexity: O(1)
|
|
185
253
|
*
|
|
186
|
-
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of
|
|
254
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
|
|
187
255
|
* vertex.
|
|
188
256
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
189
257
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
190
|
-
*
|
|
258
|
+
* edgeMap connected to that vertex.
|
|
191
259
|
*/
|
|
192
260
|
degreeOf(vertexOrKey: VertexKey | VO): number {
|
|
193
261
|
const vertex = this._getVertex(vertexOrKey);
|
|
194
262
|
if (vertex) {
|
|
195
|
-
return this.
|
|
263
|
+
return this._edgeMap.get(vertex)?.length || 0;
|
|
196
264
|
} else {
|
|
197
265
|
return 0;
|
|
198
266
|
}
|
|
@@ -207,36 +275,36 @@ export class UndirectedGraph<
|
|
|
207
275
|
* Time Complexity: O(1)
|
|
208
276
|
* Space Complexity: O(1)
|
|
209
277
|
*
|
|
210
|
-
* The function returns the
|
|
278
|
+
* The function returns the edgeMap of a given vertex or vertex ID.
|
|
211
279
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
212
280
|
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
213
|
-
* @returns an array of
|
|
281
|
+
* @returns an array of edgeMap.
|
|
214
282
|
*/
|
|
215
283
|
edgesOf(vertexOrKey: VertexKey | VO): EO[] {
|
|
216
284
|
const vertex = this._getVertex(vertexOrKey);
|
|
217
285
|
if (vertex) {
|
|
218
|
-
return this.
|
|
286
|
+
return this._edgeMap.get(vertex) || [];
|
|
219
287
|
} else {
|
|
220
288
|
return [];
|
|
221
289
|
}
|
|
222
290
|
}
|
|
223
291
|
|
|
224
292
|
/**
|
|
225
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
293
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
226
294
|
* Space Complexity: O(|E|)
|
|
227
295
|
*/
|
|
228
296
|
|
|
229
297
|
/**
|
|
230
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
298
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
231
299
|
* Space Complexity: O(|E|)
|
|
232
300
|
*
|
|
233
|
-
* The function "edgeSet" returns an array of unique
|
|
301
|
+
* The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
|
|
234
302
|
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
235
303
|
*/
|
|
236
304
|
edgeSet(): EO[] {
|
|
237
305
|
const edgeSet: Set<EO> = new Set();
|
|
238
|
-
this.
|
|
239
|
-
|
|
306
|
+
this._edgeMap.forEach(edgeMap => {
|
|
307
|
+
edgeMap.forEach(edge => {
|
|
240
308
|
edgeSet.add(edge);
|
|
241
309
|
});
|
|
242
310
|
});
|
|
@@ -244,18 +312,18 @@ export class UndirectedGraph<
|
|
|
244
312
|
}
|
|
245
313
|
|
|
246
314
|
/**
|
|
247
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
315
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
248
316
|
* Space Complexity: O(|E|)
|
|
249
317
|
*/
|
|
250
318
|
|
|
251
319
|
/**
|
|
252
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
320
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
253
321
|
* Space Complexity: O(|E|)
|
|
254
322
|
*
|
|
255
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
323
|
+
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
|
|
256
324
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
257
325
|
* (`VertexKey`).
|
|
258
|
-
* @returns an array of
|
|
326
|
+
* @returns an array of vertexMap (VO[]).
|
|
259
327
|
*/
|
|
260
328
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
|
|
261
329
|
const neighbors: VO[] = [];
|
|
@@ -263,7 +331,7 @@ export class UndirectedGraph<
|
|
|
263
331
|
if (vertex) {
|
|
264
332
|
const neighborEdges = this.edgesOf(vertex);
|
|
265
333
|
for (const edge of neighborEdges) {
|
|
266
|
-
const neighbor = this._getVertex(edge.
|
|
334
|
+
const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]);
|
|
267
335
|
if (neighbor) {
|
|
268
336
|
neighbors.push(neighbor);
|
|
269
337
|
}
|
|
@@ -281,18 +349,18 @@ export class UndirectedGraph<
|
|
|
281
349
|
* Time Complexity: O(1)
|
|
282
350
|
* Space Complexity: O(1)
|
|
283
351
|
*
|
|
284
|
-
* The function "getEndsOfEdge" returns the
|
|
352
|
+
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
|
|
285
353
|
* it returns undefined.
|
|
286
354
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
287
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
355
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
288
356
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
289
357
|
*/
|
|
290
358
|
getEndsOfEdge(edge: EO): [VO, VO] | undefined {
|
|
291
|
-
if (!this.hasEdge(edge.
|
|
359
|
+
if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) {
|
|
292
360
|
return undefined;
|
|
293
361
|
}
|
|
294
|
-
const v1 = this._getVertex(edge.
|
|
295
|
-
const v2 = this._getVertex(edge.
|
|
362
|
+
const v1 = this._getVertex(edge.vertexMap[0]);
|
|
363
|
+
const v2 = this._getVertex(edge.vertexMap[1]);
|
|
296
364
|
if (v1 && v2) {
|
|
297
365
|
return [v1, v2];
|
|
298
366
|
} else {
|
|
@@ -309,20 +377,20 @@ export class UndirectedGraph<
|
|
|
309
377
|
* Time Complexity: O(1)
|
|
310
378
|
* Space Complexity: O(1)
|
|
311
379
|
*
|
|
312
|
-
* The function adds an edge to the graph by updating the adjacency list with the
|
|
380
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
|
|
313
381
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
314
382
|
* @returns a boolean value.
|
|
315
383
|
*/
|
|
316
384
|
protected _addEdgeOnly(edge: EO): boolean {
|
|
317
|
-
for (const end of edge.
|
|
385
|
+
for (const end of edge.vertexMap) {
|
|
318
386
|
const endVertex = this._getVertex(end);
|
|
319
387
|
if (endVertex === undefined) return false;
|
|
320
388
|
if (endVertex) {
|
|
321
|
-
const
|
|
322
|
-
if (
|
|
323
|
-
|
|
389
|
+
const edgeMap = this._edgeMap.get(endVertex);
|
|
390
|
+
if (edgeMap) {
|
|
391
|
+
edgeMap.push(edge);
|
|
324
392
|
} else {
|
|
325
|
-
this.
|
|
393
|
+
this._edgeMap.set(endVertex, [edge]);
|
|
326
394
|
}
|
|
327
395
|
}
|
|
328
396
|
}
|