stack-typed 1.47.6 → 1.47.8
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/binary-tree/avl-tree.d.ts +40 -22
- package/dist/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/data-structures/binary-tree/bst.js +137 -154
- package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +30 -30
- package/dist/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/data-structures/graph/directed-graph.js +28 -28
- package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/data-structures/graph/undirected-graph.js +18 -18
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +13 -13
- package/dist/data-structures/stack/stack.d.ts +6 -6
- package/dist/data-structures/stack/stack.js +7 -7
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +6 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
|
@@ -24,20 +24,17 @@ exports.TreeMultimapNode = TreeMultimapNode;
|
|
|
24
24
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
25
25
|
*/
|
|
26
26
|
class TreeMultimap extends avl_tree_1.AVLTree {
|
|
27
|
-
/**
|
|
28
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
29
|
-
* merge duplicated values.
|
|
30
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
31
|
-
* TreeMultimap.
|
|
32
|
-
*/
|
|
33
27
|
constructor(elements, options) {
|
|
34
28
|
super([], options);
|
|
35
29
|
this._count = 0;
|
|
36
30
|
if (elements)
|
|
37
|
-
this.
|
|
31
|
+
this.addMany(elements);
|
|
38
32
|
}
|
|
33
|
+
// TODO the _count is not accurate after nodes count modified
|
|
39
34
|
get count() {
|
|
40
|
-
|
|
35
|
+
let sum = 0;
|
|
36
|
+
this.subTreeTraverse(node => sum += node.count);
|
|
37
|
+
return sum;
|
|
41
38
|
}
|
|
42
39
|
/**
|
|
43
40
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
@@ -54,131 +51,68 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
54
51
|
createTree(options) {
|
|
55
52
|
return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
56
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
56
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
57
|
+
*/
|
|
57
58
|
/**
|
|
58
59
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
59
60
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
60
61
|
*
|
|
61
|
-
* The `add` function
|
|
62
|
-
*
|
|
63
|
-
* @param
|
|
64
|
-
* following types:
|
|
65
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
66
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
62
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
63
|
+
* and update the count.
|
|
64
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
67
65
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
68
|
-
* times the key
|
|
69
|
-
*
|
|
66
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
67
|
+
* is 1.
|
|
68
|
+
* @returns either a node (`N`) or `undefined`.
|
|
70
69
|
*/
|
|
71
|
-
add(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
76
|
-
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
|
70
|
+
add(keyOrNodeOrEntry, count = 1) {
|
|
71
|
+
let newNode;
|
|
72
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
73
|
+
return;
|
|
77
74
|
}
|
|
78
|
-
else if (
|
|
79
|
-
newNode =
|
|
75
|
+
else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
76
|
+
newNode = keyOrNodeOrEntry;
|
|
80
77
|
}
|
|
81
|
-
else {
|
|
82
|
-
newNode = this.createNode(
|
|
78
|
+
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
79
|
+
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
83
80
|
}
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
82
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
83
|
+
if (key === undefined || key === null) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
newNode = this.createNode(key, value, count);
|
|
88
|
+
}
|
|
90
89
|
}
|
|
91
90
|
else {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
cur.value = newNode.value;
|
|
99
|
-
cur.count += newNode.count;
|
|
100
|
-
this._count += newNode.count;
|
|
101
|
-
traversing = false;
|
|
102
|
-
inserted = cur;
|
|
103
|
-
}
|
|
104
|
-
else if (this._compare(cur.key, newNode.key) === types_1.CP.gt) {
|
|
105
|
-
// Traverse left of the node
|
|
106
|
-
if (cur.left === undefined) {
|
|
107
|
-
//Add to the left of the current node
|
|
108
|
-
cur.left = newNode;
|
|
109
|
-
this._size = this.size + 1;
|
|
110
|
-
this._count += newNode.count;
|
|
111
|
-
traversing = false;
|
|
112
|
-
inserted = cur.left;
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
//Traverse the left of the current node
|
|
116
|
-
if (cur.left)
|
|
117
|
-
cur = cur.left;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
else if (this._compare(cur.key, newNode.key) === types_1.CP.lt) {
|
|
121
|
-
// Traverse right of the node
|
|
122
|
-
if (cur.right === undefined) {
|
|
123
|
-
//Add to the right of the current node
|
|
124
|
-
cur.right = newNode;
|
|
125
|
-
this._size = this.size + 1;
|
|
126
|
-
this._count += newNode.count;
|
|
127
|
-
traversing = false;
|
|
128
|
-
inserted = cur.right;
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
//Traverse the left of the current node
|
|
132
|
-
if (cur.right)
|
|
133
|
-
cur = cur.right;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
// TODO may need to support undefined inserted
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
traversing = false;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
94
|
+
const inserted = super.add(newNode);
|
|
95
|
+
if (inserted) {
|
|
96
|
+
this._count += orgNodeCount;
|
|
145
97
|
}
|
|
146
|
-
if (inserted)
|
|
147
|
-
this._balancePath(inserted);
|
|
148
98
|
return inserted;
|
|
149
99
|
}
|
|
150
100
|
/**
|
|
151
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
101
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
152
102
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
153
103
|
*/
|
|
154
104
|
/**
|
|
155
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
105
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
156
106
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
157
107
|
*
|
|
158
|
-
* The function
|
|
159
|
-
*
|
|
160
|
-
* @param
|
|
161
|
-
*
|
|
162
|
-
* @
|
|
163
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
164
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
165
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
108
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
109
|
+
* structure.
|
|
110
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
111
|
+
* either keys, nodes, or entries.
|
|
112
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
166
113
|
*/
|
|
167
|
-
addMany(
|
|
168
|
-
|
|
169
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
170
|
-
const keyOrNode = keysOrNodes[i];
|
|
171
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
172
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
if (keyOrNode === undefined) {
|
|
176
|
-
inserted.push(this.add(NaN, undefined, 0));
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
inserted.push(this.add(keyOrNode, data === null || data === void 0 ? void 0 : data[i], 1));
|
|
180
|
-
}
|
|
181
|
-
return inserted;
|
|
114
|
+
addMany(keysOrNodesOrEntries) {
|
|
115
|
+
return super.addMany(keysOrNodesOrEntries);
|
|
182
116
|
}
|
|
183
117
|
/**
|
|
184
118
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
@@ -206,7 +140,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
206
140
|
return;
|
|
207
141
|
const m = l + Math.floor((r - l) / 2);
|
|
208
142
|
const midNode = sorted[m];
|
|
209
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
143
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
210
144
|
buildBalanceBST(l, m - 1);
|
|
211
145
|
buildBalanceBST(m + 1, r);
|
|
212
146
|
};
|
|
@@ -222,7 +156,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
222
156
|
if (l <= r) {
|
|
223
157
|
const m = l + Math.floor((r - l) / 2);
|
|
224
158
|
const midNode = sorted[m];
|
|
225
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
159
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
226
160
|
stack.push([m + 1, r]);
|
|
227
161
|
stack.push([l, m - 1]);
|
|
228
162
|
}
|
|
@@ -289,7 +223,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
289
223
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
|
|
290
224
|
if (leftSubTreeRightMost) {
|
|
291
225
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
292
|
-
orgCurrent = this.
|
|
226
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
293
227
|
if (parentOfLeftSubTreeMax) {
|
|
294
228
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
295
229
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -323,23 +257,6 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
323
257
|
super.clear();
|
|
324
258
|
this._count = 0;
|
|
325
259
|
}
|
|
326
|
-
/**
|
|
327
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
328
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
329
|
-
*/
|
|
330
|
-
init(elements) {
|
|
331
|
-
if (elements) {
|
|
332
|
-
for (const entryOrKey of elements) {
|
|
333
|
-
if (Array.isArray(entryOrKey)) {
|
|
334
|
-
const [key, value] = entryOrKey;
|
|
335
|
-
this.add(key, value);
|
|
336
|
-
}
|
|
337
|
-
else {
|
|
338
|
-
this.add(entryOrKey);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
260
|
/**
|
|
344
261
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
345
262
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -356,7 +273,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
356
273
|
* added, or `undefined` if no node was added.
|
|
357
274
|
*/
|
|
358
275
|
_addTo(newNode, parent) {
|
|
359
|
-
parent = this.
|
|
276
|
+
parent = this.ensureNode(parent);
|
|
360
277
|
if (parent) {
|
|
361
278
|
if (parent.left === undefined) {
|
|
362
279
|
parent.left = newNode;
|
|
@@ -383,7 +300,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
383
300
|
}
|
|
384
301
|
}
|
|
385
302
|
/**
|
|
386
|
-
* The `
|
|
303
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
387
304
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
388
305
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
389
306
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -391,9 +308,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
391
308
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
392
309
|
* if either `srcNode` or `destNode` is undefined.
|
|
393
310
|
*/
|
|
394
|
-
|
|
395
|
-
srcNode = this.
|
|
396
|
-
destNode = this.
|
|
311
|
+
_swapProperties(srcNode, destNode) {
|
|
312
|
+
srcNode = this.ensureNode(srcNode);
|
|
313
|
+
destNode = this.ensureNode(destNode);
|
|
397
314
|
if (srcNode && destNode) {
|
|
398
315
|
const { key, value, count, height } = destNode;
|
|
399
316
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -412,5 +329,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
412
329
|
}
|
|
413
330
|
return undefined;
|
|
414
331
|
}
|
|
332
|
+
_replaceNode(oldNode, newNode) {
|
|
333
|
+
newNode.count = oldNode.count + newNode.count;
|
|
334
|
+
return super._replaceNode(oldNode, newNode);
|
|
335
|
+
}
|
|
415
336
|
}
|
|
416
337
|
exports.TreeMultimap = TreeMultimap;
|
|
@@ -47,13 +47,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
47
47
|
* @param value
|
|
48
48
|
*/
|
|
49
49
|
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
50
|
-
abstract deleteEdge(edge: EO): EO |
|
|
51
|
-
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO |
|
|
50
|
+
abstract deleteEdge(edge: EO): EO | undefined;
|
|
51
|
+
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
|
|
52
52
|
abstract degreeOf(vertexOrKey: VO | VertexKey): number;
|
|
53
53
|
abstract edgeSet(): EO[];
|
|
54
54
|
abstract edgesOf(vertexOrKey: VO | VertexKey): EO[];
|
|
55
55
|
abstract getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
56
|
-
abstract getEndsOfEdge(edge: EO): [VO, VO] |
|
|
56
|
+
abstract getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
57
57
|
/**
|
|
58
58
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
59
59
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -62,13 +62,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
62
62
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
63
63
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
64
64
|
*
|
|
65
|
-
* The function "getVertex" returns the vertex with the specified ID or
|
|
65
|
+
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
66
66
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
67
67
|
* the `_vertices` map.
|
|
68
68
|
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
69
|
-
* map. If the vertex does not exist, it returns `
|
|
69
|
+
* map. If the vertex does not exist, it returns `undefined`.
|
|
70
70
|
*/
|
|
71
|
-
getVertex(vertexKey: VertexKey): VO |
|
|
71
|
+
getVertex(vertexKey: VertexKey): VO | undefined;
|
|
72
72
|
/**
|
|
73
73
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
74
74
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -201,7 +201,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
201
201
|
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
202
202
|
* minimum number of
|
|
203
203
|
*/
|
|
204
|
-
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number |
|
|
204
|
+
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | undefined;
|
|
205
205
|
/**
|
|
206
206
|
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
207
207
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
@@ -223,9 +223,9 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
223
223
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
224
224
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
225
225
|
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
226
|
-
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `
|
|
226
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
|
|
227
227
|
*/
|
|
228
|
-
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS?: boolean): VO[] |
|
|
228
|
+
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS?: boolean): VO[] | undefined;
|
|
229
229
|
/**
|
|
230
230
|
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
231
231
|
* /
|
|
@@ -242,9 +242,9 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
242
242
|
* a graph without using a heap data structure.
|
|
243
243
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
244
244
|
* vertex object or a vertex ID.
|
|
245
|
-
* @param {VO | VertexKey |
|
|
245
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
246
246
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
247
|
-
* identifier. If no destination is provided, the value is set to `
|
|
247
|
+
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
248
248
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
249
249
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
250
250
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
@@ -253,7 +253,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
253
253
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
254
254
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
255
255
|
*/
|
|
256
|
-
dijkstraWithoutHeap(src: VO | VertexKey, dest?: VO | VertexKey |
|
|
256
|
+
dijkstraWithoutHeap(src: VO | VertexKey, dest?: VO | VertexKey | undefined, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO>;
|
|
257
257
|
/**
|
|
258
258
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
259
259
|
*
|
|
@@ -276,7 +276,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
276
276
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
277
277
|
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
278
278
|
* start. It can be either a vertex object or a vertex ID.
|
|
279
|
-
* @param {VO | VertexKey |
|
|
279
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
280
280
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
281
281
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
282
282
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -287,7 +287,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
287
287
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
288
288
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
289
289
|
*/
|
|
290
|
-
dijkstra(src: VO | VertexKey, dest?: VO | VertexKey |
|
|
290
|
+
dijkstra(src: VO | VertexKey, dest?: VO | VertexKey | undefined, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO>;
|
|
291
291
|
/**
|
|
292
292
|
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
293
293
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
@@ -353,12 +353,12 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
353
353
|
* graph.
|
|
354
354
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
355
355
|
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
356
|
-
* `predecessor` property is a 2D array of vertices (or `
|
|
356
|
+
* `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
|
|
357
357
|
* path between vertices in the
|
|
358
358
|
*/
|
|
359
359
|
floydWarshall(): {
|
|
360
360
|
costs: number[][];
|
|
361
|
-
predecessor: (VO |
|
|
361
|
+
predecessor: (VO | undefined)[][];
|
|
362
362
|
};
|
|
363
363
|
/**
|
|
364
364
|
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
@@ -445,6 +445,6 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
445
445
|
getBridges(): EO[];
|
|
446
446
|
protected abstract _addEdgeOnly(edge: EO): boolean;
|
|
447
447
|
protected _addVertexOnly(newVertex: VO): boolean;
|
|
448
|
-
protected _getVertex(vertexOrKey: VertexKey | VO): VO |
|
|
448
|
+
protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined;
|
|
449
449
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
|
|
450
450
|
}
|
|
@@ -60,14 +60,14 @@ class AbstractGraph {
|
|
|
60
60
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
61
61
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
62
62
|
*
|
|
63
|
-
* The function "getVertex" returns the vertex with the specified ID or
|
|
63
|
+
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
64
64
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
65
65
|
* the `_vertices` map.
|
|
66
66
|
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
67
|
-
* map. If the vertex does not exist, it returns `
|
|
67
|
+
* map. If the vertex does not exist, it returns `undefined`.
|
|
68
68
|
*/
|
|
69
69
|
getVertex(vertexKey) {
|
|
70
|
-
return this._vertices.get(vertexKey) ||
|
|
70
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
73
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
@@ -305,7 +305,7 @@ class AbstractGraph {
|
|
|
305
305
|
const vertex2 = this._getVertex(v2);
|
|
306
306
|
const vertex1 = this._getVertex(v1);
|
|
307
307
|
if (!(vertex1 && vertex2)) {
|
|
308
|
-
return
|
|
308
|
+
return undefined;
|
|
309
309
|
}
|
|
310
310
|
const visited = new Map();
|
|
311
311
|
const queue = new queue_1.Queue([vertex1]);
|
|
@@ -330,7 +330,7 @@ class AbstractGraph {
|
|
|
330
330
|
}
|
|
331
331
|
cost++;
|
|
332
332
|
}
|
|
333
|
-
return
|
|
333
|
+
return undefined;
|
|
334
334
|
}
|
|
335
335
|
}
|
|
336
336
|
/**
|
|
@@ -354,7 +354,7 @@ class AbstractGraph {
|
|
|
354
354
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
355
355
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
356
356
|
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
357
|
-
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `
|
|
357
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
|
|
358
358
|
*/
|
|
359
359
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
360
360
|
var _a, _b;
|
|
@@ -374,7 +374,7 @@ class AbstractGraph {
|
|
|
374
374
|
}
|
|
375
375
|
index++;
|
|
376
376
|
}
|
|
377
|
-
return allPaths[minIndex] ||
|
|
377
|
+
return allPaths[minIndex] || undefined;
|
|
378
378
|
}
|
|
379
379
|
else {
|
|
380
380
|
return (_b = (_a = this.dijkstra(v1, v2, true, true)) === null || _a === void 0 ? void 0 : _a.minPath) !== null && _b !== void 0 ? _b : [];
|
|
@@ -423,9 +423,9 @@ class AbstractGraph {
|
|
|
423
423
|
* a graph without using a heap data structure.
|
|
424
424
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
425
425
|
* vertex object or a vertex ID.
|
|
426
|
-
* @param {VO | VertexKey |
|
|
426
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
427
427
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
428
|
-
* identifier. If no destination is provided, the value is set to `
|
|
428
|
+
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
429
429
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
430
430
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
431
431
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
@@ -440,9 +440,9 @@ class AbstractGraph {
|
|
|
440
440
|
if (genPaths === undefined)
|
|
441
441
|
genPaths = false;
|
|
442
442
|
if (dest === undefined)
|
|
443
|
-
dest =
|
|
443
|
+
dest = undefined;
|
|
444
444
|
let minDist = Infinity;
|
|
445
|
-
let minDest =
|
|
445
|
+
let minDest = undefined;
|
|
446
446
|
let minPath = [];
|
|
447
447
|
const paths = [];
|
|
448
448
|
const vertices = this._vertices;
|
|
@@ -450,9 +450,9 @@ class AbstractGraph {
|
|
|
450
450
|
const seen = new Set();
|
|
451
451
|
const preMap = new Map(); // predecessor
|
|
452
452
|
const srcVertex = this._getVertex(src);
|
|
453
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
453
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
454
454
|
if (!srcVertex) {
|
|
455
|
-
return
|
|
455
|
+
return undefined;
|
|
456
456
|
}
|
|
457
457
|
for (const vertex of vertices) {
|
|
458
458
|
const vertexOrKey = vertex[1];
|
|
@@ -460,10 +460,10 @@ class AbstractGraph {
|
|
|
460
460
|
distMap.set(vertexOrKey, Infinity);
|
|
461
461
|
}
|
|
462
462
|
distMap.set(srcVertex, 0);
|
|
463
|
-
preMap.set(srcVertex,
|
|
463
|
+
preMap.set(srcVertex, undefined);
|
|
464
464
|
const getMinOfNoSeen = () => {
|
|
465
465
|
let min = Infinity;
|
|
466
|
-
let minV =
|
|
466
|
+
let minV = undefined;
|
|
467
467
|
for (const [key, value] of distMap) {
|
|
468
468
|
if (!seen.has(key)) {
|
|
469
469
|
if (value < min) {
|
|
@@ -511,7 +511,7 @@ class AbstractGraph {
|
|
|
511
511
|
if (edge) {
|
|
512
512
|
const curFromMap = distMap.get(cur);
|
|
513
513
|
const neighborFromMap = distMap.get(neighbor);
|
|
514
|
-
// TODO after no-non-
|
|
514
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
515
515
|
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
516
516
|
if (edge.weight + curFromMap < neighborFromMap) {
|
|
517
517
|
distMap.set(neighbor, edge.weight + curFromMap);
|
|
@@ -558,7 +558,7 @@ class AbstractGraph {
|
|
|
558
558
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
559
559
|
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
560
560
|
* start. It can be either a vertex object or a vertex ID.
|
|
561
|
-
* @param {VO | VertexKey |
|
|
561
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
562
562
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
563
563
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
564
564
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -576,9 +576,9 @@ class AbstractGraph {
|
|
|
576
576
|
if (genPaths === undefined)
|
|
577
577
|
genPaths = false;
|
|
578
578
|
if (dest === undefined)
|
|
579
|
-
dest =
|
|
579
|
+
dest = undefined;
|
|
580
580
|
let minDist = Infinity;
|
|
581
|
-
let minDest =
|
|
581
|
+
let minDest = undefined;
|
|
582
582
|
let minPath = [];
|
|
583
583
|
const paths = [];
|
|
584
584
|
const vertices = this._vertices;
|
|
@@ -586,9 +586,9 @@ class AbstractGraph {
|
|
|
586
586
|
const seen = new Set();
|
|
587
587
|
const preMap = new Map(); // predecessor
|
|
588
588
|
const srcVertex = this._getVertex(src);
|
|
589
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
589
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
590
590
|
if (!srcVertex)
|
|
591
|
-
return
|
|
591
|
+
return undefined;
|
|
592
592
|
for (const vertex of vertices) {
|
|
593
593
|
const vertexOrKey = vertex[1];
|
|
594
594
|
if (vertexOrKey instanceof AbstractVertex)
|
|
@@ -597,11 +597,11 @@ class AbstractGraph {
|
|
|
597
597
|
const heap = new priority_queue_1.PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
598
598
|
heap.add({ key: 0, value: srcVertex });
|
|
599
599
|
distMap.set(srcVertex, 0);
|
|
600
|
-
preMap.set(srcVertex,
|
|
600
|
+
preMap.set(srcVertex, undefined);
|
|
601
601
|
/**
|
|
602
602
|
* The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
|
|
603
|
-
* @param {VO |
|
|
604
|
-
*
|
|
603
|
+
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
604
|
+
* undefined.
|
|
605
605
|
*/
|
|
606
606
|
const getPaths = (minV) => {
|
|
607
607
|
for (const vertex of vertices) {
|
|
@@ -737,7 +737,7 @@ class AbstractGraph {
|
|
|
737
737
|
}
|
|
738
738
|
}
|
|
739
739
|
}
|
|
740
|
-
let minDest =
|
|
740
|
+
let minDest = undefined;
|
|
741
741
|
if (getMin) {
|
|
742
742
|
distMap.forEach((d, v) => {
|
|
743
743
|
if (v !== srcVertex) {
|
|
@@ -813,7 +813,7 @@ class AbstractGraph {
|
|
|
813
813
|
* graph.
|
|
814
814
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
815
815
|
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
816
|
-
* `predecessor` property is a 2D array of vertices (or `
|
|
816
|
+
* `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
|
|
817
817
|
* path between vertices in the
|
|
818
818
|
*/
|
|
819
819
|
floydWarshall() {
|
|
@@ -827,7 +827,7 @@ class AbstractGraph {
|
|
|
827
827
|
costs[i] = [];
|
|
828
828
|
predecessor[i] = [];
|
|
829
829
|
for (let j = 0; j < n; j++) {
|
|
830
|
-
predecessor[i][j] =
|
|
830
|
+
predecessor[i][j] = undefined;
|
|
831
831
|
}
|
|
832
832
|
}
|
|
833
833
|
for (let i = 0; i < n; i++) {
|
|
@@ -919,7 +919,7 @@ class AbstractGraph {
|
|
|
919
919
|
}
|
|
920
920
|
const childLow = lowMap.get(neighbor);
|
|
921
921
|
const curLow = lowMap.get(cur);
|
|
922
|
-
// TODO after no-non-
|
|
922
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
923
923
|
if (curLow !== undefined && childLow !== undefined) {
|
|
924
924
|
lowMap.set(cur, Math.min(curLow, childLow));
|
|
925
925
|
}
|
|
@@ -943,7 +943,7 @@ class AbstractGraph {
|
|
|
943
943
|
}
|
|
944
944
|
}
|
|
945
945
|
};
|
|
946
|
-
dfs(root,
|
|
946
|
+
dfs(root, undefined);
|
|
947
947
|
let SCCs = new Map();
|
|
948
948
|
const getSCCs = () => {
|
|
949
949
|
const SCCs = new Map();
|
|
@@ -1038,7 +1038,7 @@ class AbstractGraph {
|
|
|
1038
1038
|
}
|
|
1039
1039
|
_getVertex(vertexOrKey) {
|
|
1040
1040
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1041
|
-
return this._vertices.get(vertexKey) ||
|
|
1041
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
1042
1042
|
}
|
|
1043
1043
|
_getVertexKey(vertexOrKey) {
|
|
1044
1044
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|