data-structure-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.
Files changed (27) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +6 -6
  3. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
  4. package/dist/cjs/data-structures/graph/abstract-graph.js +4 -0
  5. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  6. package/dist/cjs/data-structures/graph/directed-graph.d.ts +25 -7
  7. package/dist/cjs/data-structures/graph/directed-graph.js +58 -12
  8. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  9. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +25 -6
  10. package/dist/cjs/data-structures/graph/undirected-graph.js +70 -7
  11. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  12. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
  13. package/dist/mjs/data-structures/graph/abstract-graph.js +4 -0
  14. package/dist/mjs/data-structures/graph/directed-graph.d.ts +25 -7
  15. package/dist/mjs/data-structures/graph/directed-graph.js +58 -12
  16. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +25 -6
  17. package/dist/mjs/data-structures/graph/undirected-graph.js +70 -7
  18. package/dist/umd/data-structure-typed.js +124 -18
  19. package/dist/umd/data-structure-typed.min.js +1 -1
  20. package/dist/umd/data-structure-typed.min.js.map +1 -1
  21. package/package.json +1 -1
  22. package/src/data-structures/graph/abstract-graph.ts +5 -0
  23. package/src/data-structures/graph/directed-graph.ts +61 -12
  24. package/src/data-structures/graph/undirected-graph.ts +75 -7
  25. package/test/integration/bst.test.ts +1 -1
  26. package/test/unit/data-structures/graph/directed-graph.test.ts +37 -0
  27. package/test/unit/data-structures/graph/undirected-graph.test.ts +37 -0
@@ -147,34 +147,80 @@ export class DirectedGraph extends AbstractGraph {
147
147
  return removed;
148
148
  }
149
149
  /**
150
- * Time Complexity: O(|E|) where |E| is the number of edges
150
+ * Time Complexity: O(E) where E is the number of edges
151
151
  * Space Complexity: O(1)
152
152
  */
153
153
  /**
154
- * Time Complexity: O(|E|) where |E| is the number of edges
154
+ * Time Complexity: O(E) where E is the number of edges
155
155
  * Space Complexity: O(1)
156
156
  *
157
- * The function removes an edge from a graph and returns the removed edge, or undefined if the edge was not found.
158
- * @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
159
- * and `dest`, which represent the source and destination vertices of the edge, respectively.
160
- * @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `undefined` if the edge does not exist.
161
- */
162
- deleteEdge(edge) {
157
+ * The `deleteEdge` function removes an edge from a graph and returns the removed edge.
158
+ * @param {EO | VertexKey} edgeOrSrcVertexKey - The `edge` parameter can be either an `EO` object (edge object) or
159
+ * a `VertexKey` (key of a vertex).
160
+ * @param {VertexKey} [destVertexKey] - The `destVertexKey` parameter is an optional parameter that
161
+ * represents the key of the destination vertex of the edge. It is used to specify the destination
162
+ * vertex when the `edge` parameter is a vertex key. If `destVertexKey` is not provided, the function
163
+ * assumes that the `edge`
164
+ * @returns the removed edge (EO) or undefined if no edge was removed.
165
+ */
166
+ deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
163
167
  let removed = undefined;
164
- const src = this._getVertex(edge.src);
165
- const dest = this._getVertex(edge.dest);
168
+ let src, dest;
169
+ if (this.isVertexKey(edgeOrSrcVertexKey)) {
170
+ if (this.isVertexKey(destVertexKey)) {
171
+ src = this._getVertex(edgeOrSrcVertexKey);
172
+ dest = this._getVertex(destVertexKey);
173
+ }
174
+ else {
175
+ return;
176
+ }
177
+ }
178
+ else {
179
+ src = this._getVertex(edgeOrSrcVertexKey.src);
180
+ dest = this._getVertex(edgeOrSrcVertexKey.dest);
181
+ }
166
182
  if (src && dest) {
167
183
  const srcOutEdges = this._outEdgeMap.get(src);
168
184
  if (srcOutEdges && srcOutEdges.length > 0) {
169
- arrayRemove(srcOutEdges, (edge) => edge.src === src.key);
185
+ arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest === dest?.key);
170
186
  }
171
187
  const destInEdges = this._inEdgeMap.get(dest);
172
188
  if (destInEdges && destInEdges.length > 0) {
173
- removed = arrayRemove(destInEdges, (edge) => edge.dest === dest.key)[0];
189
+ removed = arrayRemove(destInEdges, (edge) => edge.src === src.key && edge.dest === dest.key)[0];
174
190
  }
175
191
  }
176
192
  return removed;
177
193
  }
194
+ /**
195
+ * Time Complexity: O(1) - Constant time for Map operations.
196
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
197
+ */
198
+ /**
199
+ * Time Complexity: O(1) - Constant time for Map operations.
200
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
201
+ *
202
+ * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
203
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
204
+ * (`VertexKey`).
205
+ * @returns The method is returning a boolean value.
206
+ */
207
+ deleteVertex(vertexOrKey) {
208
+ let vertexKey;
209
+ let vertex;
210
+ if (this.isVertexKey(vertexOrKey)) {
211
+ vertex = this.getVertex(vertexOrKey);
212
+ vertexKey = vertexOrKey;
213
+ }
214
+ else {
215
+ vertex = vertexOrKey;
216
+ vertexKey = this._getVertexKey(vertexOrKey);
217
+ }
218
+ if (vertex) {
219
+ this._outEdgeMap.delete(vertex);
220
+ this._inEdgeMap.delete(vertex);
221
+ }
222
+ return this._vertices.delete(vertexKey);
223
+ }
178
224
  /**
179
225
  * Time Complexity: O(|E|) where |E| is the number of edges
180
226
  * 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(|E|), where |E| is the number of edges incident to the given vertex.
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(|E|), where |E| is the number of edges incident to the given vertex.
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 function removes an edge between two vertices in a graph.
96
- * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
97
- * @returns The method is returning either the removed edge (of type EO) or undefined if the edge was not found.
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
- deleteEdge(edge: EO): EO | undefined;
118
+ deleteVertex(vertexOrKey: VO | VertexKey): boolean;
100
119
  /**
101
120
  * Time Complexity: O(1)
102
121
  * Space Complexity: O(1)
@@ -131,19 +131,82 @@ export class UndirectedGraph extends AbstractGraph {
131
131
  return removed;
132
132
  }
133
133
  /**
134
- * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
134
+ * Time Complexity: O(E), where E is the number of edges incident to the given vertex.
135
135
  * Space Complexity: O(1)
136
136
  */
137
137
  /**
138
- * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
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
- * The deleteEdge function removes an edge between two vertices in a graph.
142
- * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
143
- * @returns The method is returning either the removed edge (of type EO) or undefined if the edge was not found.
141
+ * The function `deleteEdge` deletes an edge between two vertices in a graph.
142
+ * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
143
+ * either an edge object or a vertex key.
144
+ * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
145
+ * parameter that represents the key of the vertex on the other side of the edge. It is used when the
146
+ * `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
147
+ * other side of the
148
+ * @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
149
+ */
150
+ deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
151
+ let oneSide, otherSide;
152
+ if (this.isVertexKey(edgeOrOneSideVertexKey)) {
153
+ if (this.isVertexKey(otherSideVertexKey)) {
154
+ oneSide = this._getVertex(edgeOrOneSideVertexKey);
155
+ otherSide = this._getVertex(otherSideVertexKey);
156
+ }
157
+ else {
158
+ return;
159
+ }
160
+ }
161
+ else {
162
+ oneSide = this._getVertex(edgeOrOneSideVertexKey.vertices[0]);
163
+ otherSide = this._getVertex(edgeOrOneSideVertexKey.vertices[1]);
164
+ }
165
+ if (oneSide && otherSide) {
166
+ return this.deleteEdgeBetween(oneSide, otherSide);
167
+ }
168
+ else {
169
+ return;
170
+ }
171
+ }
172
+ /**
173
+ * Time Complexity: O(1) - Constant time for Map operations.
174
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
144
175
  */
145
- deleteEdge(edge) {
146
- return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
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.
179
+ *
180
+ * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
181
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
182
+ * (`VertexKey`).
183
+ * @returns The method is returning a boolean value.
184
+ */
185
+ deleteVertex(vertexOrKey) {
186
+ let vertexKey;
187
+ let vertex;
188
+ if (this.isVertexKey(vertexOrKey)) {
189
+ vertex = this.getVertex(vertexOrKey);
190
+ vertexKey = vertexOrKey;
191
+ }
192
+ else {
193
+ vertex = vertexOrKey;
194
+ vertexKey = this._getVertexKey(vertexOrKey);
195
+ }
196
+ const neighbors = this.getNeighbors(vertexOrKey);
197
+ if (vertex) {
198
+ neighbors.forEach(neighbor => {
199
+ const neighborEdges = this._edges.get(neighbor);
200
+ if (neighborEdges) {
201
+ const restEdges = neighborEdges.filter(edge => {
202
+ return !edge.vertices.includes(vertexKey);
203
+ });
204
+ this._edges.set(neighbor, restEdges);
205
+ }
206
+ });
207
+ this._edges.delete(vertex);
208
+ }
209
+ return this._vertices.delete(vertexKey);
147
210
  }
148
211
  /**
149
212
  * Time Complexity: O(1)
@@ -5473,6 +5473,10 @@ var dataStructureTyped = (() => {
5473
5473
  return this._addVertexOnly(newVertex);
5474
5474
  }
5475
5475
  }
5476
+ isVertexKey(potentialKey) {
5477
+ const potentialKeyType = typeof potentialKey;
5478
+ return potentialKeyType === "string" || potentialKeyType === "number";
5479
+ }
5476
5480
  /**
5477
5481
  * Time Complexity: O(1) - Constant time for Map operations.
5478
5482
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -6598,34 +6602,77 @@ var dataStructureTyped = (() => {
6598
6602
  return removed;
6599
6603
  }
6600
6604
  /**
6601
- * Time Complexity: O(|E|) where |E| is the number of edges
6605
+ * Time Complexity: O(E) where E is the number of edges
6602
6606
  * Space Complexity: O(1)
6603
6607
  */
6604
6608
  /**
6605
- * Time Complexity: O(|E|) where |E| is the number of edges
6609
+ * Time Complexity: O(E) where E is the number of edges
6606
6610
  * Space Complexity: O(1)
6607
6611
  *
6608
- * The function removes an edge from a graph and returns the removed edge, or undefined if the edge was not found.
6609
- * @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
6610
- * and `dest`, which represent the source and destination vertices of the edge, respectively.
6611
- * @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `undefined` if the edge does not exist.
6612
+ * The `deleteEdge` function removes an edge from a graph and returns the removed edge.
6613
+ * @param {EO | VertexKey} edgeOrSrcVertexKey - The `edge` parameter can be either an `EO` object (edge object) or
6614
+ * a `VertexKey` (key of a vertex).
6615
+ * @param {VertexKey} [destVertexKey] - The `destVertexKey` parameter is an optional parameter that
6616
+ * represents the key of the destination vertex of the edge. It is used to specify the destination
6617
+ * vertex when the `edge` parameter is a vertex key. If `destVertexKey` is not provided, the function
6618
+ * assumes that the `edge`
6619
+ * @returns the removed edge (EO) or undefined if no edge was removed.
6612
6620
  */
6613
- deleteEdge(edge) {
6621
+ deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
6614
6622
  let removed = void 0;
6615
- const src = this._getVertex(edge.src);
6616
- const dest = this._getVertex(edge.dest);
6623
+ let src, dest;
6624
+ if (this.isVertexKey(edgeOrSrcVertexKey)) {
6625
+ if (this.isVertexKey(destVertexKey)) {
6626
+ src = this._getVertex(edgeOrSrcVertexKey);
6627
+ dest = this._getVertex(destVertexKey);
6628
+ } else {
6629
+ return;
6630
+ }
6631
+ } else {
6632
+ src = this._getVertex(edgeOrSrcVertexKey.src);
6633
+ dest = this._getVertex(edgeOrSrcVertexKey.dest);
6634
+ }
6617
6635
  if (src && dest) {
6618
6636
  const srcOutEdges = this._outEdgeMap.get(src);
6619
6637
  if (srcOutEdges && srcOutEdges.length > 0) {
6620
- arrayRemove(srcOutEdges, (edge2) => edge2.src === src.key);
6638
+ arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest === (dest == null ? void 0 : dest.key));
6621
6639
  }
6622
6640
  const destInEdges = this._inEdgeMap.get(dest);
6623
6641
  if (destInEdges && destInEdges.length > 0) {
6624
- removed = arrayRemove(destInEdges, (edge2) => edge2.dest === dest.key)[0];
6642
+ removed = arrayRemove(destInEdges, (edge) => edge.src === src.key && edge.dest === dest.key)[0];
6625
6643
  }
6626
6644
  }
6627
6645
  return removed;
6628
6646
  }
6647
+ /**
6648
+ * Time Complexity: O(1) - Constant time for Map operations.
6649
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
6650
+ */
6651
+ /**
6652
+ * Time Complexity: O(1) - Constant time for Map operations.
6653
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
6654
+ *
6655
+ * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
6656
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
6657
+ * (`VertexKey`).
6658
+ * @returns The method is returning a boolean value.
6659
+ */
6660
+ deleteVertex(vertexOrKey) {
6661
+ let vertexKey;
6662
+ let vertex;
6663
+ if (this.isVertexKey(vertexOrKey)) {
6664
+ vertex = this.getVertex(vertexOrKey);
6665
+ vertexKey = vertexOrKey;
6666
+ } else {
6667
+ vertex = vertexOrKey;
6668
+ vertexKey = this._getVertexKey(vertexOrKey);
6669
+ }
6670
+ if (vertex) {
6671
+ this._outEdgeMap.delete(vertex);
6672
+ this._inEdgeMap.delete(vertex);
6673
+ }
6674
+ return this._vertices.delete(vertexKey);
6675
+ }
6629
6676
  /**
6630
6677
  * Time Complexity: O(|E|) where |E| is the number of edges
6631
6678
  * Space Complexity: O(1)
@@ -7093,19 +7140,78 @@ var dataStructureTyped = (() => {
7093
7140
  return removed;
7094
7141
  }
7095
7142
  /**
7096
- * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
7143
+ * Time Complexity: O(E), where E is the number of edges incident to the given vertex.
7097
7144
  * Space Complexity: O(1)
7098
7145
  */
7099
7146
  /**
7100
- * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
7147
+ * Time Complexity: O(E), where E is the number of edges incident to the given vertex.
7101
7148
  * Space Complexity: O(1)
7102
7149
  *
7103
- * The deleteEdge function removes an edge between two vertices in a graph.
7104
- * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
7105
- * @returns The method is returning either the removed edge (of type EO) or undefined if the edge was not found.
7150
+ * The function `deleteEdge` deletes an edge between two vertices in a graph.
7151
+ * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
7152
+ * either an edge object or a vertex key.
7153
+ * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
7154
+ * parameter that represents the key of the vertex on the other side of the edge. It is used when the
7155
+ * `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
7156
+ * other side of the
7157
+ * @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
7106
7158
  */
7107
- deleteEdge(edge) {
7108
- return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
7159
+ deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
7160
+ let oneSide, otherSide;
7161
+ if (this.isVertexKey(edgeOrOneSideVertexKey)) {
7162
+ if (this.isVertexKey(otherSideVertexKey)) {
7163
+ oneSide = this._getVertex(edgeOrOneSideVertexKey);
7164
+ otherSide = this._getVertex(otherSideVertexKey);
7165
+ } else {
7166
+ return;
7167
+ }
7168
+ } else {
7169
+ oneSide = this._getVertex(edgeOrOneSideVertexKey.vertices[0]);
7170
+ otherSide = this._getVertex(edgeOrOneSideVertexKey.vertices[1]);
7171
+ }
7172
+ if (oneSide && otherSide) {
7173
+ return this.deleteEdgeBetween(oneSide, otherSide);
7174
+ } else {
7175
+ return;
7176
+ }
7177
+ }
7178
+ /**
7179
+ * Time Complexity: O(1) - Constant time for Map operations.
7180
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
7181
+ */
7182
+ /**
7183
+ * Time Complexity: O(1) - Constant time for Map operations.
7184
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
7185
+ *
7186
+ * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
7187
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
7188
+ * (`VertexKey`).
7189
+ * @returns The method is returning a boolean value.
7190
+ */
7191
+ deleteVertex(vertexOrKey) {
7192
+ let vertexKey;
7193
+ let vertex;
7194
+ if (this.isVertexKey(vertexOrKey)) {
7195
+ vertex = this.getVertex(vertexOrKey);
7196
+ vertexKey = vertexOrKey;
7197
+ } else {
7198
+ vertex = vertexOrKey;
7199
+ vertexKey = this._getVertexKey(vertexOrKey);
7200
+ }
7201
+ const neighbors = this.getNeighbors(vertexOrKey);
7202
+ if (vertex) {
7203
+ neighbors.forEach((neighbor) => {
7204
+ const neighborEdges = this._edges.get(neighbor);
7205
+ if (neighborEdges) {
7206
+ const restEdges = neighborEdges.filter((edge) => {
7207
+ return !edge.vertices.includes(vertexKey);
7208
+ });
7209
+ this._edges.set(neighbor, restEdges);
7210
+ }
7211
+ });
7212
+ this._edges.delete(vertex);
7213
+ }
7214
+ return this._vertices.delete(vertexKey);
7109
7215
  }
7110
7216
  /**
7111
7217
  * Time Complexity: O(1)