queue-typed 1.48.3 → 1.48.4
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/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +4 -0
- package/dist/data-structures/graph/directed-graph.d.ts +25 -7
- package/dist/data-structures/graph/directed-graph.js +58 -12
- package/dist/data-structures/graph/undirected-graph.d.ts +25 -6
- package/dist/data-structures/graph/undirected-graph.js +70 -7
- package/package.json +2 -2
- package/src/data-structures/graph/abstract-graph.ts +5 -0
- package/src/data-structures/graph/directed-graph.ts +61 -12
- package/src/data-structures/graph/undirected-graph.ts +75 -7
|
@@ -88,6 +88,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
88
88
|
hasVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
89
89
|
addVertex(vertex: VO): boolean;
|
|
90
90
|
addVertex(key: VertexKey, value?: V): boolean;
|
|
91
|
+
isVertexKey(potentialKey: any): potentialKey is VertexKey;
|
|
91
92
|
/**
|
|
92
93
|
* Time Complexity: O(1) - Constant time for Map operations.
|
|
93
94
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -100,6 +100,10 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
100
100
|
return this._addVertexOnly(newVertex);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
+
isVertexKey(potentialKey) {
|
|
104
|
+
const potentialKeyType = typeof potentialKey;
|
|
105
|
+
return potentialKeyType === "string" || potentialKeyType === "number";
|
|
106
|
+
}
|
|
103
107
|
/**
|
|
104
108
|
* Time Complexity: O(1) - Constant time for Map operations.
|
|
105
109
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -96,19 +96,37 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
96
96
|
*/
|
|
97
97
|
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
|
|
98
98
|
/**
|
|
99
|
-
* Time Complexity: O(
|
|
99
|
+
* Time Complexity: O(E) where E is the number of edges
|
|
100
100
|
* Space Complexity: O(1)
|
|
101
101
|
*/
|
|
102
102
|
/**
|
|
103
|
-
* Time Complexity: O(
|
|
103
|
+
* Time Complexity: O(E) where E is the number of edges
|
|
104
104
|
* Space Complexity: O(1)
|
|
105
105
|
*
|
|
106
|
-
* The function removes an edge from a graph and returns the removed edge
|
|
107
|
-
* @param {EO}
|
|
108
|
-
*
|
|
109
|
-
* @
|
|
106
|
+
* The `deleteEdge` function removes an edge from a graph and returns the removed edge.
|
|
107
|
+
* @param {EO | VertexKey} edgeOrSrcVertexKey - The `edge` parameter can be either an `EO` object (edge object) or
|
|
108
|
+
* a `VertexKey` (key of a vertex).
|
|
109
|
+
* @param {VertexKey} [destVertexKey] - The `destVertexKey` parameter is an optional parameter that
|
|
110
|
+
* represents the key of the destination vertex of the edge. It is used to specify the destination
|
|
111
|
+
* vertex when the `edge` parameter is a vertex key. If `destVertexKey` is not provided, the function
|
|
112
|
+
* assumes that the `edge`
|
|
113
|
+
* @returns the removed edge (EO) or undefined if no edge was removed.
|
|
114
|
+
*/
|
|
115
|
+
deleteEdge(edgeOrSrcVertexKey: EO | VertexKey, destVertexKey?: VertexKey): EO | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
118
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
119
|
+
*/
|
|
120
|
+
/**
|
|
121
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
122
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
123
|
+
*
|
|
124
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
125
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
126
|
+
* (`VertexKey`).
|
|
127
|
+
* @returns The method is returning a boolean value.
|
|
110
128
|
*/
|
|
111
|
-
|
|
129
|
+
deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
112
130
|
/**
|
|
113
131
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
114
132
|
* Space Complexity: O(1)
|
|
@@ -150,34 +150,80 @@ 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 edges
|
|
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 edges
|
|
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
|
}
|
|
197
|
+
/**
|
|
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._vertices.delete(vertexKey);
|
|
226
|
+
}
|
|
181
227
|
/**
|
|
182
228
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
183
229
|
* Space Complexity: O(1)
|
|
@@ -85,18 +85,37 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
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 edges 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 edges 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 vertices 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)
|
|
@@ -135,19 +135,82 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
135
135
|
return removed;
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
|
-
* Time Complexity: O(
|
|
138
|
+
* Time Complexity: O(E), where E is the number of edges incident to the given vertex.
|
|
139
139
|
* Space Complexity: O(1)
|
|
140
140
|
*/
|
|
141
141
|
/**
|
|
142
|
-
* Time Complexity: O(
|
|
142
|
+
* Time Complexity: O(E), where E is the number of edges incident to the given vertex.
|
|
143
143
|
* Space Complexity: O(1)
|
|
144
144
|
*
|
|
145
|
-
* The deleteEdge
|
|
146
|
-
* @param {EO}
|
|
147
|
-
*
|
|
145
|
+
* The function `deleteEdge` deletes an edge between two vertices in a graph.
|
|
146
|
+
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
147
|
+
* either an edge object or a vertex key.
|
|
148
|
+
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
149
|
+
* parameter that represents the key of the vertex on the other side of the edge. It is used when the
|
|
150
|
+
* `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
|
|
151
|
+
* other side of the
|
|
152
|
+
* @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
|
|
153
|
+
*/
|
|
154
|
+
deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
|
|
155
|
+
let oneSide, otherSide;
|
|
156
|
+
if (this.isVertexKey(edgeOrOneSideVertexKey)) {
|
|
157
|
+
if (this.isVertexKey(otherSideVertexKey)) {
|
|
158
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey);
|
|
159
|
+
otherSide = this._getVertex(otherSideVertexKey);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey.vertices[0]);
|
|
167
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertices[1]);
|
|
168
|
+
}
|
|
169
|
+
if (oneSide && otherSide) {
|
|
170
|
+
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
178
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
148
179
|
*/
|
|
149
|
-
|
|
150
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
182
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
183
|
+
*
|
|
184
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
185
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
186
|
+
* (`VertexKey`).
|
|
187
|
+
* @returns The method is returning a boolean value.
|
|
188
|
+
*/
|
|
189
|
+
deleteVertex(vertexOrKey) {
|
|
190
|
+
let vertexKey;
|
|
191
|
+
let vertex;
|
|
192
|
+
if (this.isVertexKey(vertexOrKey)) {
|
|
193
|
+
vertex = this.getVertex(vertexOrKey);
|
|
194
|
+
vertexKey = vertexOrKey;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
vertex = vertexOrKey;
|
|
198
|
+
vertexKey = this._getVertexKey(vertexOrKey);
|
|
199
|
+
}
|
|
200
|
+
const neighbors = this.getNeighbors(vertexOrKey);
|
|
201
|
+
if (vertex) {
|
|
202
|
+
neighbors.forEach(neighbor => {
|
|
203
|
+
const neighborEdges = this._edges.get(neighbor);
|
|
204
|
+
if (neighborEdges) {
|
|
205
|
+
const restEdges = neighborEdges.filter(edge => {
|
|
206
|
+
return !edge.vertices.includes(vertexKey);
|
|
207
|
+
});
|
|
208
|
+
this._edges.set(neighbor, restEdges);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
this._edges.delete(vertex);
|
|
212
|
+
}
|
|
213
|
+
return this._vertices.delete(vertexKey);
|
|
151
214
|
}
|
|
152
215
|
/**
|
|
153
216
|
* Time Complexity: O(1)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "queue-typed",
|
|
3
|
-
"version": "1.48.
|
|
3
|
+
"version": "1.48.4",
|
|
4
4
|
"description": "Queue, ArrayQueue. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -115,6 +115,6 @@
|
|
|
115
115
|
"typescript": "^4.9.5"
|
|
116
116
|
},
|
|
117
117
|
"dependencies": {
|
|
118
|
-
"data-structure-typed": "^1.48.
|
|
118
|
+
"data-structure-typed": "^1.48.4"
|
|
119
119
|
}
|
|
120
120
|
}
|
|
@@ -164,6 +164,11 @@ export abstract class AbstractGraph<
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
isVertexKey(potentialKey: any): potentialKey is VertexKey {
|
|
168
|
+
const potentialKeyType = typeof potentialKey;
|
|
169
|
+
return potentialKeyType === "string" || potentialKeyType === "number"
|
|
170
|
+
}
|
|
171
|
+
|
|
167
172
|
/**
|
|
168
173
|
* Time Complexity: O(1) - Constant time for Map operations.
|
|
169
174
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -178,38 +178,87 @@ export class DirectedGraph<
|
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
/**
|
|
181
|
-
* Time Complexity: O(
|
|
181
|
+
* Time Complexity: O(E) where E is the number of edges
|
|
182
182
|
* Space Complexity: O(1)
|
|
183
183
|
*/
|
|
184
184
|
|
|
185
|
+
|
|
185
186
|
/**
|
|
186
|
-
* Time Complexity: O(
|
|
187
|
+
* Time Complexity: O(E) where E is the number of edges
|
|
187
188
|
* Space Complexity: O(1)
|
|
188
189
|
*
|
|
189
|
-
* The function removes an edge from a graph and returns the removed edge
|
|
190
|
-
* @param {EO}
|
|
191
|
-
*
|
|
192
|
-
* @
|
|
193
|
-
|
|
194
|
-
|
|
190
|
+
* The `deleteEdge` function removes an edge from a graph and returns the removed edge.
|
|
191
|
+
* @param {EO | VertexKey} edgeOrSrcVertexKey - The `edge` parameter can be either an `EO` object (edge object) or
|
|
192
|
+
* a `VertexKey` (key of a vertex).
|
|
193
|
+
* @param {VertexKey} [destVertexKey] - The `destVertexKey` parameter is an optional parameter that
|
|
194
|
+
* represents the key of the destination vertex of the edge. It is used to specify the destination
|
|
195
|
+
* vertex when the `edge` parameter is a vertex key. If `destVertexKey` is not provided, the function
|
|
196
|
+
* assumes that the `edge`
|
|
197
|
+
* @returns the removed edge (EO) or undefined if no edge was removed.
|
|
198
|
+
*/
|
|
199
|
+
deleteEdge(edgeOrSrcVertexKey: EO | VertexKey, destVertexKey?: VertexKey): EO | undefined {
|
|
195
200
|
let removed: EO | undefined = undefined;
|
|
196
|
-
|
|
197
|
-
|
|
201
|
+
let src: VO | undefined, dest: VO | undefined;
|
|
202
|
+
if (this.isVertexKey(edgeOrSrcVertexKey)) {
|
|
203
|
+
if (this.isVertexKey(destVertexKey)) {
|
|
204
|
+
src = this._getVertex(edgeOrSrcVertexKey);
|
|
205
|
+
dest = this._getVertex(destVertexKey);
|
|
206
|
+
} else {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
src = this._getVertex(edgeOrSrcVertexKey.src);
|
|
211
|
+
dest = this._getVertex(edgeOrSrcVertexKey.dest);
|
|
212
|
+
}
|
|
213
|
+
|
|
198
214
|
if (src && dest) {
|
|
199
215
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
200
216
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
201
|
-
arrayRemove(srcOutEdges, (edge: EO) => edge.src === src.key);
|
|
217
|
+
arrayRemove(srcOutEdges, (edge: EO) => edge.src === src!.key && edge.dest === dest?.key);
|
|
202
218
|
}
|
|
203
219
|
|
|
204
220
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
205
221
|
if (destInEdges && destInEdges.length > 0) {
|
|
206
|
-
removed = arrayRemove(destInEdges, (edge: EO) => edge.dest === dest
|
|
222
|
+
removed = arrayRemove(destInEdges, (edge: EO) => edge.src === src!.key && edge.dest === dest!.key)[0];
|
|
207
223
|
}
|
|
208
224
|
}
|
|
209
225
|
|
|
210
226
|
return removed;
|
|
211
227
|
}
|
|
212
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
231
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
232
|
+
*/
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
236
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
237
|
+
*
|
|
238
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
239
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
240
|
+
* (`VertexKey`).
|
|
241
|
+
* @returns The method is returning a boolean value.
|
|
242
|
+
*/
|
|
243
|
+
override deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
244
|
+
let vertexKey: VertexKey;
|
|
245
|
+
let vertex: VO | undefined;
|
|
246
|
+
if (this.isVertexKey(vertexOrKey)) {
|
|
247
|
+
vertex = this.getVertex(vertexOrKey);
|
|
248
|
+
vertexKey = vertexOrKey;
|
|
249
|
+
} else {
|
|
250
|
+
vertex = vertexOrKey;
|
|
251
|
+
vertexKey = this._getVertexKey(vertexOrKey)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (vertex) {
|
|
255
|
+
this._outEdgeMap.delete(vertex)
|
|
256
|
+
this._inEdgeMap.delete(vertex)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return this._vertices.delete(vertexKey);
|
|
260
|
+
}
|
|
261
|
+
|
|
213
262
|
/**
|
|
214
263
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
215
264
|
* Space Complexity: O(1)
|
|
@@ -158,20 +158,88 @@ export class UndirectedGraph<
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
/**
|
|
161
|
-
* Time Complexity: O(
|
|
161
|
+
* Time Complexity: O(E), where E is the number of edges 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 edges 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 vertices 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.vertices[0]);
|
|
190
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertices[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.
|
|
172
204
|
*/
|
|
173
|
-
|
|
174
|
-
|
|
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.
|
|
214
|
+
*/
|
|
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._edges.get(neighbor);
|
|
231
|
+
if (neighborEdges) {
|
|
232
|
+
const restEdges = neighborEdges.filter(edge => {
|
|
233
|
+
return !edge.vertices.includes(vertexKey);
|
|
234
|
+
});
|
|
235
|
+
this._edges.set(neighbor, restEdges);
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
this._edges.delete(vertex);
|
|
239
|
+
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return this._vertices.delete(vertexKey);
|
|
175
243
|
}
|
|
176
244
|
|
|
177
245
|
/**
|