data-structure-typed 1.50.1 → 1.50.3

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 (196) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +30 -28
  3. package/README_zh-CN.md +1 -1
  4. package/SPECIFICATION.md +56 -0
  5. package/SPONSOR-zh-CN.md +90 -0
  6. package/SPONSOR.md +54 -0
  7. package/benchmark/report.html +13 -13
  8. package/benchmark/report.json +149 -161
  9. package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
  10. package/dist/cjs/data-structures/base/iterable-base.js +143 -7
  11. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  16. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  17. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  19. package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
  22. package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
  23. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  25. package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  28. package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
  29. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  30. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  31. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
  32. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  33. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
  34. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
  35. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  36. package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
  37. package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
  38. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
  40. package/dist/cjs/data-structures/graph/map-graph.js +14 -0
  41. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  42. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
  43. package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
  44. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  45. package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
  46. package/dist/cjs/data-structures/hash/hash-map.js +347 -78
  47. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  48. package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
  49. package/dist/cjs/data-structures/heap/heap.js +95 -26
  50. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  52. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
  53. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  55. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
  56. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  57. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  58. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
  59. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  60. package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
  61. package/dist/cjs/data-structures/matrix/matrix.js +50 -11
  62. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  63. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  64. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  65. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  66. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  67. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  68. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  69. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  70. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
  71. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  72. package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
  73. package/dist/cjs/data-structures/queue/deque.js +200 -62
  74. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  75. package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
  76. package/dist/cjs/data-structures/queue/queue.js +111 -49
  77. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  78. package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
  79. package/dist/cjs/data-structures/stack/stack.js +58 -22
  80. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  81. package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
  82. package/dist/cjs/data-structures/tree/tree.js +77 -11
  83. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  84. package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
  85. package/dist/cjs/data-structures/trie/trie.js +153 -33
  86. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  87. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  88. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
  89. package/dist/cjs/types/utils/utils.d.ts +1 -0
  90. package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
  91. package/dist/mjs/data-structures/base/iterable-base.js +143 -7
  92. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  93. package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
  94. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  95. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  96. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  97. package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
  98. package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
  99. package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
  100. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  101. package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
  102. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  103. package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
  104. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  105. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
  106. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
  107. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
  108. package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
  109. package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
  110. package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
  111. package/dist/mjs/data-structures/graph/map-graph.js +14 -0
  112. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
  113. package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
  114. package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
  115. package/dist/mjs/data-structures/hash/hash-map.js +353 -84
  116. package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
  117. package/dist/mjs/data-structures/heap/heap.js +95 -26
  118. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  119. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
  120. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  121. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
  122. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  123. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
  124. package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
  125. package/dist/mjs/data-structures/matrix/matrix.js +50 -11
  126. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  127. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  128. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  129. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  130. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  131. package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
  132. package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
  133. package/dist/mjs/data-structures/queue/deque.js +205 -67
  134. package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
  135. package/dist/mjs/data-structures/queue/queue.js +111 -49
  136. package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
  137. package/dist/mjs/data-structures/stack/stack.js +58 -22
  138. package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
  139. package/dist/mjs/data-structures/tree/tree.js +80 -14
  140. package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
  141. package/dist/mjs/data-structures/trie/trie.js +156 -36
  142. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  143. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
  144. package/dist/mjs/types/utils/utils.d.ts +1 -0
  145. package/dist/umd/data-structure-typed.js +3121 -1583
  146. package/dist/umd/data-structure-typed.min.js +3 -3
  147. package/dist/umd/data-structure-typed.min.js.map +1 -1
  148. package/package.json +61 -55
  149. package/src/data-structures/base/iterable-base.ts +184 -19
  150. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  151. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  152. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  153. package/src/data-structures/binary-tree/bst.ts +127 -136
  154. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  155. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  156. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  157. package/src/data-structures/graph/abstract-graph.ts +4 -211
  158. package/src/data-structures/graph/directed-graph.ts +152 -0
  159. package/src/data-structures/graph/map-graph.ts +15 -0
  160. package/src/data-structures/graph/undirected-graph.ts +171 -19
  161. package/src/data-structures/hash/hash-map.ts +389 -96
  162. package/src/data-structures/heap/heap.ts +97 -26
  163. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  164. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  165. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  166. package/src/data-structures/matrix/matrix.ts +52 -12
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  170. package/src/data-structures/queue/deque.ts +225 -70
  171. package/src/data-structures/queue/queue.ts +118 -49
  172. package/src/data-structures/stack/stack.ts +63 -23
  173. package/src/data-structures/tree/tree.ts +89 -15
  174. package/src/data-structures/trie/trie.ts +173 -38
  175. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  176. package/src/types/data-structures/hash/hash-map.ts +4 -3
  177. package/src/types/utils/utils.ts +2 -0
  178. package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
  179. package/test/performance/data-structures/queue/deque.test.ts +26 -25
  180. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
  181. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
  182. package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
  183. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
  184. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
  185. package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
  186. package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
  187. package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
  188. package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
  189. package/test/unit/data-structures/heap/heap.test.ts +18 -1
  190. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
  191. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  193. package/test/unit/data-structures/queue/deque.test.ts +65 -5
  194. package/test/unit/data-structures/queue/queue.test.ts +24 -5
  195. package/test/unit/data-structures/stack/stack.test.ts +17 -0
  196. package/test/unit/data-structures/trie/trie.test.ts +17 -0
@@ -19,7 +19,7 @@ export declare class UndirectedVertex<V = any> extends AbstractVertex<V> {
19
19
  constructor(key: VertexKey, value?: V);
20
20
  }
21
21
  export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
22
- vertexMap: [VertexKey, VertexKey];
22
+ endpoints: [VertexKey, VertexKey];
23
23
  /**
24
24
  * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
25
25
  * value.
@@ -39,6 +39,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
39
39
  constructor();
40
40
  protected _edgeMap: Map<VO, EO[]>;
41
41
  get edgeMap(): Map<VO, EO[]>;
42
+ set edgeMap(v: Map<VO, EO[]>);
42
43
  /**
43
44
  * The function creates a new vertex with an optional value and returns it.
44
45
  * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
@@ -68,7 +69,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
68
69
  * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
69
70
  * Space Complexity: O(1)
70
71
  *
71
- * The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
72
+ * The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
72
73
  * @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
73
74
  * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
74
75
  * @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
@@ -88,7 +89,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
88
89
  * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
89
90
  * @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
90
91
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
91
- * @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
92
+ * @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
92
93
  */
93
94
  deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined;
94
95
  /**
@@ -99,7 +100,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
99
100
  * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
100
101
  * Space Complexity: O(1)
101
102
  *
102
- * The function `deleteEdge` deletes an edge between two vertexMap in a graph.
103
+ * The function `deleteEdge` deletes an edge between two endpoints in a graph.
103
104
  * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
104
105
  * either an edge object or a vertex key.
105
106
  * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
@@ -172,7 +173,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
172
173
  * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
173
174
  * Space Complexity: O(|E|)
174
175
  *
175
- * The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
176
+ * The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
176
177
  * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
177
178
  * (`VertexKey`).
178
179
  * @returns an array of vertexMap (VO[]).
@@ -186,17 +187,85 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
186
187
  * Time Complexity: O(1)
187
188
  * Space Complexity: O(1)
188
189
  *
189
- * The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
190
+ * The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
190
191
  * it returns undefined.
191
192
  * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
192
- * @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
193
+ * @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
193
194
  * graph. If the edge does not exist, it returns `undefined`.
194
195
  */
195
196
  getEndsOfEdge(edge: EO): [VO, VO] | undefined;
197
+ /**
198
+ * The isEmpty function checks if the graph is empty.
199
+ * @return True if the graph is empty and false otherwise
200
+ */
201
+ isEmpty(): boolean;
202
+ /**
203
+ * Time Complexity: O(1)
204
+ * Space Complexity: O(1)
205
+ */
206
+ /**
207
+ * Time Complexity: O(1)
208
+ * Space Complexity: O(1)
209
+ *
210
+ * The clear function resets the vertex and edge maps to empty maps.
211
+ */
212
+ clear(): void;
213
+ /**
214
+ * The clone function creates a new UndirectedGraph object and copies the
215
+ * vertexMap and edgeMap from this graph to the new one. This is done by
216
+ * assigning each of these properties to their respective counterparts in the
217
+ * cloned graph. The clone function returns a reference to this newly created,
218
+ * cloned UndirectedGraph object.
219
+ *
220
+ * @return A new instance of the undirectedgraph class
221
+ */
222
+ clone(): UndirectedGraph<V, E, VO, EO>;
196
223
  /**
197
224
  * Time Complexity: O(1)
198
225
  * Space Complexity: O(1)
199
226
  */
227
+ /**
228
+ * Time Complexity: O(V + E)
229
+ * Space Complexity: O(V)
230
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
231
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
232
+ *
233
+ * The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
234
+ * graph.
235
+ * @returns The function `tarjan()` returns an object with the following properties:
236
+ */
237
+ tarjan(): {
238
+ dfnMap: Map<VO, number>;
239
+ lowMap: Map<VO, number>;
240
+ bridges: EO[];
241
+ cutVertices: VO[];
242
+ };
243
+ /**
244
+ * Time Complexity: O(V + E)
245
+ * Space Complexity: O(V)
246
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
247
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
248
+ */
249
+ /**
250
+ * The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
251
+ * @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
252
+ */
253
+ getBridges(): EO[];
254
+ /**
255
+ * The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
256
+ * @returns the cut vertices found using the Tarjan's algorithm.
257
+ */
258
+ getCutVertices(): VO[];
259
+ /**
260
+ * The function returns the dfnMap property of the result of the tarjan() function.
261
+ * @returns the `dfnMap` property of the result of calling the `tarjan()` function.
262
+ */
263
+ getDFNMap(): Map<VO, number>;
264
+ /**
265
+ * The function returns the lowMap property of the result of the tarjan() function.
266
+ * @returns the lowMap property of the result of calling the tarjan() function.
267
+ */
268
+ getLowMap(): Map<VO, number>;
200
269
  /**
201
270
  * Time Complexity: O(1)
202
271
  * Space Complexity: O(1)
@@ -13,7 +13,7 @@ export class UndirectedVertex extends AbstractVertex {
13
13
  }
14
14
  }
15
15
  export class UndirectedEdge extends AbstractEdge {
16
- vertexMap;
16
+ endpoints;
17
17
  /**
18
18
  * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
19
19
  * value.
@@ -26,7 +26,7 @@ export class UndirectedEdge extends AbstractEdge {
26
26
  */
27
27
  constructor(v1, v2, weight, value) {
28
28
  super(weight, value);
29
- this.vertexMap = [v1, v2];
29
+ this.endpoints = [v1, v2];
30
30
  }
31
31
  }
32
32
  export class UndirectedGraph extends AbstractGraph {
@@ -41,6 +41,9 @@ export class UndirectedGraph extends AbstractGraph {
41
41
  get edgeMap() {
42
42
  return this._edgeMap;
43
43
  }
44
+ set edgeMap(v) {
45
+ this._edgeMap = v;
46
+ }
44
47
  /**
45
48
  * The function creates a new vertex with an optional value and returns it.
46
49
  * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
@@ -74,7 +77,7 @@ export class UndirectedGraph extends AbstractGraph {
74
77
  * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
75
78
  * Space Complexity: O(1)
76
79
  *
77
- * The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
80
+ * The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
78
81
  * @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
79
82
  * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
80
83
  * @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
@@ -87,7 +90,7 @@ export class UndirectedGraph extends AbstractGraph {
87
90
  const vertex1 = this._getVertex(v1);
88
91
  const vertex2 = this._getVertex(v2);
89
92
  if (vertex1 && vertex2) {
90
- edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.vertexMap.includes(vertex2.key));
93
+ edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.endpoints.includes(vertex2.key));
91
94
  }
92
95
  }
93
96
  return edgeMap ? edgeMap[0] || undefined : undefined;
@@ -104,7 +107,7 @@ export class UndirectedGraph extends AbstractGraph {
104
107
  * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
105
108
  * @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
106
109
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
107
- * @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
110
+ * @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
108
111
  */
109
112
  deleteEdgeBetween(v1, v2) {
110
113
  const vertex1 = this._getVertex(v1);
@@ -115,11 +118,11 @@ export class UndirectedGraph extends AbstractGraph {
115
118
  const v1Edges = this._edgeMap.get(vertex1);
116
119
  let removed = undefined;
117
120
  if (v1Edges) {
118
- removed = arrayRemove(v1Edges, (e) => e.vertexMap.includes(vertex2.key))[0] || undefined;
121
+ removed = arrayRemove(v1Edges, (e) => e.endpoints.includes(vertex2.key))[0] || undefined;
119
122
  }
120
123
  const v2Edges = this._edgeMap.get(vertex2);
121
124
  if (v2Edges) {
122
- arrayRemove(v2Edges, (e) => e.vertexMap.includes(vertex1.key));
125
+ arrayRemove(v2Edges, (e) => e.endpoints.includes(vertex1.key));
123
126
  }
124
127
  return removed;
125
128
  }
@@ -131,7 +134,7 @@ export class UndirectedGraph extends AbstractGraph {
131
134
  * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
132
135
  * Space Complexity: O(1)
133
136
  *
134
- * The function `deleteEdge` deletes an edge between two vertexMap in a graph.
137
+ * The function `deleteEdge` deletes an edge between two endpoints in a graph.
135
138
  * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
136
139
  * either an edge object or a vertex key.
137
140
  * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
@@ -152,8 +155,8 @@ export class UndirectedGraph extends AbstractGraph {
152
155
  }
153
156
  }
154
157
  else {
155
- oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]);
156
- otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]);
158
+ oneSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[0]);
159
+ otherSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[1]);
157
160
  }
158
161
  if (oneSide && otherSide) {
159
162
  return this.deleteEdgeBetween(oneSide, otherSide);
@@ -192,7 +195,7 @@ export class UndirectedGraph extends AbstractGraph {
192
195
  const neighborEdges = this._edgeMap.get(neighbor);
193
196
  if (neighborEdges) {
194
197
  const restEdges = neighborEdges.filter(edge => {
195
- return !edge.vertexMap.includes(vertexKey);
198
+ return !edge.endpoints.includes(vertexKey);
196
199
  });
197
200
  this._edgeMap.set(neighbor, restEdges);
198
201
  }
@@ -274,7 +277,7 @@ export class UndirectedGraph extends AbstractGraph {
274
277
  * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
275
278
  * Space Complexity: O(|E|)
276
279
  *
277
- * The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
280
+ * The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
278
281
  * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
279
282
  * (`VertexKey`).
280
283
  * @returns an array of vertexMap (VO[]).
@@ -285,7 +288,7 @@ export class UndirectedGraph extends AbstractGraph {
285
288
  if (vertex) {
286
289
  const neighborEdges = this.edgesOf(vertex);
287
290
  for (const edge of neighborEdges) {
288
- const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]);
291
+ const neighbor = this._getVertex(edge.endpoints.filter(e => e !== vertex.key)[0]);
289
292
  if (neighbor) {
290
293
  neighbors.push(neighbor);
291
294
  }
@@ -301,18 +304,18 @@ export class UndirectedGraph extends AbstractGraph {
301
304
  * Time Complexity: O(1)
302
305
  * Space Complexity: O(1)
303
306
  *
304
- * The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
307
+ * The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
305
308
  * it returns undefined.
306
309
  * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
307
- * @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
310
+ * @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
308
311
  * graph. If the edge does not exist, it returns `undefined`.
309
312
  */
310
313
  getEndsOfEdge(edge) {
311
- if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) {
314
+ if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
312
315
  return undefined;
313
316
  }
314
- const v1 = this._getVertex(edge.vertexMap[0]);
315
- const v2 = this._getVertex(edge.vertexMap[1]);
317
+ const v1 = this._getVertex(edge.endpoints[0]);
318
+ const v2 = this._getVertex(edge.endpoints[1]);
316
319
  if (v1 && v2) {
317
320
  return [v1, v2];
318
321
  }
@@ -320,10 +323,140 @@ export class UndirectedGraph extends AbstractGraph {
320
323
  return undefined;
321
324
  }
322
325
  }
326
+ /**
327
+ * The isEmpty function checks if the graph is empty.
328
+ * @return True if the graph is empty and false otherwise
329
+ */
330
+ isEmpty() {
331
+ return this.vertexMap.size === 0 && this.edgeMap.size === 0;
332
+ }
333
+ /**
334
+ * Time Complexity: O(1)
335
+ * Space Complexity: O(1)
336
+ */
337
+ /**
338
+ * Time Complexity: O(1)
339
+ * Space Complexity: O(1)
340
+ *
341
+ * The clear function resets the vertex and edge maps to empty maps.
342
+ */
343
+ clear() {
344
+ this._vertexMap = new Map();
345
+ this._edgeMap = new Map();
346
+ }
347
+ /**
348
+ * The clone function creates a new UndirectedGraph object and copies the
349
+ * vertexMap and edgeMap from this graph to the new one. This is done by
350
+ * assigning each of these properties to their respective counterparts in the
351
+ * cloned graph. The clone function returns a reference to this newly created,
352
+ * cloned UndirectedGraph object.
353
+ *
354
+ * @return A new instance of the undirectedgraph class
355
+ */
356
+ clone() {
357
+ const cloned = new UndirectedGraph();
358
+ cloned.vertexMap = new Map(this.vertexMap);
359
+ cloned.edgeMap = new Map(this.edgeMap);
360
+ return cloned;
361
+ }
323
362
  /**
324
363
  * Time Complexity: O(1)
325
364
  * Space Complexity: O(1)
326
365
  */
366
+ /**
367
+ * Time Complexity: O(V + E)
368
+ * Space Complexity: O(V)
369
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
370
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
371
+ *
372
+ * The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
373
+ * graph.
374
+ * @returns The function `tarjan()` returns an object with the following properties:
375
+ */
376
+ tarjan() {
377
+ const dfnMap = new Map();
378
+ const lowMap = new Map();
379
+ const bridges = [];
380
+ const cutVertices = [];
381
+ let time = 0;
382
+ const dfs = (vertex, parent) => {
383
+ dfnMap.set(vertex, time);
384
+ lowMap.set(vertex, time);
385
+ time++;
386
+ const neighbors = this.getNeighbors(vertex);
387
+ let childCount = 0;
388
+ for (const neighbor of neighbors) {
389
+ if (!dfnMap.has(neighbor)) {
390
+ childCount++;
391
+ dfs(neighbor, vertex);
392
+ lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
393
+ if (lowMap.get(neighbor) > dfnMap.get(vertex)) {
394
+ // Found a bridge
395
+ const edge = this.getEdge(vertex, neighbor);
396
+ if (edge) {
397
+ bridges.push(edge);
398
+ }
399
+ }
400
+ if (parent !== undefined && lowMap.get(neighbor) >= dfnMap.get(vertex)) {
401
+ // Found an articulation point
402
+ cutVertices.push(vertex);
403
+ }
404
+ }
405
+ else if (neighbor !== parent) {
406
+ lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
407
+ }
408
+ }
409
+ if (parent === undefined && childCount > 1) {
410
+ // Special case for root in DFS tree
411
+ cutVertices.push(vertex);
412
+ }
413
+ };
414
+ for (const vertex of this.vertexMap.values()) {
415
+ if (!dfnMap.has(vertex)) {
416
+ dfs(vertex, undefined);
417
+ }
418
+ }
419
+ return {
420
+ dfnMap,
421
+ lowMap,
422
+ bridges,
423
+ cutVertices
424
+ };
425
+ }
426
+ /**
427
+ * Time Complexity: O(V + E)
428
+ * Space Complexity: O(V)
429
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
430
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
431
+ */
432
+ /**
433
+ * The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
434
+ * @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
435
+ */
436
+ getBridges() {
437
+ return this.tarjan().bridges;
438
+ }
439
+ /**
440
+ * The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
441
+ * @returns the cut vertices found using the Tarjan's algorithm.
442
+ */
443
+ getCutVertices() {
444
+ return this.tarjan().cutVertices;
445
+ }
446
+ /**
447
+ * The function returns the dfnMap property of the result of the tarjan() function.
448
+ * @returns the `dfnMap` property of the result of calling the `tarjan()` function.
449
+ */
450
+ getDFNMap() {
451
+ return this.tarjan().dfnMap;
452
+ }
453
+ /**
454
+ * The function returns the lowMap property of the result of the tarjan() function.
455
+ * @returns the lowMap property of the result of calling the tarjan() function.
456
+ */
457
+ getLowMap() {
458
+ return this.tarjan().lowMap;
459
+ }
327
460
  /**
328
461
  * Time Complexity: O(1)
329
462
  * Space Complexity: O(1)
@@ -333,7 +466,7 @@ export class UndirectedGraph extends AbstractGraph {
333
466
  * @returns a boolean value.
334
467
  */
335
468
  _addEdge(edge) {
336
- for (const end of edge.vertexMap) {
469
+ for (const end of edge.endpoints) {
337
470
  const endVertex = this._getVertex(end);
338
471
  if (endVertex === undefined)
339
472
  return false;