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.
- package/CHANGELOG.md +2 -1
- package/README.md +30 -28
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -161
- package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/cjs/data-structures/hash/hash-map.js +347 -78
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/cjs/data-structures/heap/heap.js +95 -26
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/cjs/data-structures/queue/deque.js +200 -62
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/cjs/data-structures/queue/queue.js +111 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/cjs/data-structures/stack/stack.js +58 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/cjs/data-structures/trie/trie.js +153 -33
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/mjs/data-structures/hash/hash-map.js +353 -84
- package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/mjs/data-structures/heap/heap.js +95 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/mjs/data-structures/queue/deque.js +205 -67
- package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/mjs/data-structures/queue/queue.js +111 -49
- package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/mjs/data-structures/stack/stack.js +58 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/mjs/data-structures/trie/trie.js +156 -36
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +3121 -1583
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +61 -55
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +24 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- 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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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.
|
|
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
|
|
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.
|
|
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
|
|
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.
|
|
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.
|
|
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
|
|
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.
|
|
156
|
-
otherSide = this._getVertex(edgeOrOneSideVertexKey.
|
|
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.
|
|
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
|
|
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.
|
|
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
|
|
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
|
|
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.
|
|
314
|
+
if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
|
|
312
315
|
return undefined;
|
|
313
316
|
}
|
|
314
|
-
const v1 = this._getVertex(edge.
|
|
315
|
-
const v2 = this._getVertex(edge.
|
|
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.
|
|
469
|
+
for (const end of edge.endpoints) {
|
|
337
470
|
const endVertex = this._getVertex(end);
|
|
338
471
|
if (endVertex === undefined)
|
|
339
472
|
return false;
|