data-structure-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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +188 -32
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/cjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- 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 +21 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +13 -13
- package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/cjs/data-structures/stack/stack.js +7 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/mjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +13 -13
- package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/mjs/data-structures/stack/stack.js +7 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +583 -627
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +158 -2
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +10 -10
- package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
2
2
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
3
|
export class TreeMultimapNode extends AVLTreeNode {
|
|
4
4
|
count;
|
|
@@ -21,20 +21,17 @@ export class TreeMultimapNode extends AVLTreeNode {
|
|
|
21
21
|
* 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.
|
|
22
22
|
*/
|
|
23
23
|
export class TreeMultimap extends AVLTree {
|
|
24
|
-
/**
|
|
25
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
26
|
-
* merge duplicated values.
|
|
27
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
28
|
-
* TreeMultimap.
|
|
29
|
-
*/
|
|
30
24
|
constructor(elements, options) {
|
|
31
25
|
super([], options);
|
|
32
26
|
if (elements)
|
|
33
|
-
this.
|
|
27
|
+
this.addMany(elements);
|
|
34
28
|
}
|
|
35
29
|
_count = 0;
|
|
30
|
+
// TODO the _count is not accurate after nodes count modified
|
|
36
31
|
get count() {
|
|
37
|
-
|
|
32
|
+
let sum = 0;
|
|
33
|
+
this.subTreeTraverse(node => sum += node.count);
|
|
34
|
+
return sum;
|
|
38
35
|
}
|
|
39
36
|
/**
|
|
40
37
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
@@ -54,131 +51,68 @@ export class TreeMultimap extends AVLTree {
|
|
|
54
51
|
comparator: this.comparator, ...options
|
|
55
52
|
});
|
|
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) === 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) === 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?.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?.[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 @@ export class TreeMultimap extends 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 @@ export class TreeMultimap extends 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
|
}
|
|
@@ -288,7 +222,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
288
222
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
|
|
289
223
|
if (leftSubTreeRightMost) {
|
|
290
224
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
291
|
-
orgCurrent = this.
|
|
225
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
292
226
|
if (parentOfLeftSubTreeMax) {
|
|
293
227
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
294
228
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -322,23 +256,6 @@ export class TreeMultimap extends AVLTree {
|
|
|
322
256
|
super.clear();
|
|
323
257
|
this._count = 0;
|
|
324
258
|
}
|
|
325
|
-
/**
|
|
326
|
-
* 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.
|
|
327
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
328
|
-
*/
|
|
329
|
-
init(elements) {
|
|
330
|
-
if (elements) {
|
|
331
|
-
for (const entryOrKey of elements) {
|
|
332
|
-
if (Array.isArray(entryOrKey)) {
|
|
333
|
-
const [key, value] = entryOrKey;
|
|
334
|
-
this.add(key, value);
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
this.add(entryOrKey);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
259
|
/**
|
|
343
260
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
344
261
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -355,7 +272,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
355
272
|
* added, or `undefined` if no node was added.
|
|
356
273
|
*/
|
|
357
274
|
_addTo(newNode, parent) {
|
|
358
|
-
parent = this.
|
|
275
|
+
parent = this.ensureNode(parent);
|
|
359
276
|
if (parent) {
|
|
360
277
|
if (parent.left === undefined) {
|
|
361
278
|
parent.left = newNode;
|
|
@@ -382,7 +299,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
382
299
|
}
|
|
383
300
|
}
|
|
384
301
|
/**
|
|
385
|
-
* The `
|
|
302
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
386
303
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
387
304
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
388
305
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -390,9 +307,9 @@ export class TreeMultimap extends AVLTree {
|
|
|
390
307
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
391
308
|
* if either `srcNode` or `destNode` is undefined.
|
|
392
309
|
*/
|
|
393
|
-
|
|
394
|
-
srcNode = this.
|
|
395
|
-
destNode = this.
|
|
310
|
+
_swapProperties(srcNode, destNode) {
|
|
311
|
+
srcNode = this.ensureNode(srcNode);
|
|
312
|
+
destNode = this.ensureNode(destNode);
|
|
396
313
|
if (srcNode && destNode) {
|
|
397
314
|
const { key, value, count, height } = destNode;
|
|
398
315
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -411,4 +328,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
411
328
|
}
|
|
412
329
|
return undefined;
|
|
413
330
|
}
|
|
331
|
+
_replaceNode(oldNode, newNode) {
|
|
332
|
+
newNode.count = oldNode.count + newNode.count;
|
|
333
|
+
return super._replaceNode(oldNode, newNode);
|
|
334
|
+
}
|
|
414
335
|
}
|
|
@@ -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
|
}
|
|
@@ -58,14 +58,14 @@ export class AbstractGraph {
|
|
|
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.
|
|
60
60
|
*
|
|
61
|
-
* The function "getVertex" returns the vertex with the specified ID or
|
|
61
|
+
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
62
62
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
63
63
|
* the `_vertices` map.
|
|
64
64
|
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
65
|
-
* map. If the vertex does not exist, it returns `
|
|
65
|
+
* map. If the vertex does not exist, it returns `undefined`.
|
|
66
66
|
*/
|
|
67
67
|
getVertex(vertexKey) {
|
|
68
|
-
return this._vertices.get(vertexKey) ||
|
|
68
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
69
69
|
}
|
|
70
70
|
/**
|
|
71
71
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
@@ -302,7 +302,7 @@ export class AbstractGraph {
|
|
|
302
302
|
const vertex2 = this._getVertex(v2);
|
|
303
303
|
const vertex1 = this._getVertex(v1);
|
|
304
304
|
if (!(vertex1 && vertex2)) {
|
|
305
|
-
return
|
|
305
|
+
return undefined;
|
|
306
306
|
}
|
|
307
307
|
const visited = new Map();
|
|
308
308
|
const queue = new Queue([vertex1]);
|
|
@@ -327,7 +327,7 @@ export class AbstractGraph {
|
|
|
327
327
|
}
|
|
328
328
|
cost++;
|
|
329
329
|
}
|
|
330
|
-
return
|
|
330
|
+
return undefined;
|
|
331
331
|
}
|
|
332
332
|
}
|
|
333
333
|
/**
|
|
@@ -351,7 +351,7 @@ export class AbstractGraph {
|
|
|
351
351
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
352
352
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
353
353
|
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
354
|
-
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `
|
|
354
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
|
|
355
355
|
*/
|
|
356
356
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
357
357
|
if (isWeight === undefined)
|
|
@@ -370,7 +370,7 @@ export class AbstractGraph {
|
|
|
370
370
|
}
|
|
371
371
|
index++;
|
|
372
372
|
}
|
|
373
|
-
return allPaths[minIndex] ||
|
|
373
|
+
return allPaths[minIndex] || undefined;
|
|
374
374
|
}
|
|
375
375
|
else {
|
|
376
376
|
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
@@ -419,9 +419,9 @@ export class AbstractGraph {
|
|
|
419
419
|
* a graph without using a heap data structure.
|
|
420
420
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
421
421
|
* vertex object or a vertex ID.
|
|
422
|
-
* @param {VO | VertexKey |
|
|
422
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
423
423
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
424
|
-
* identifier. If no destination is provided, the value is set to `
|
|
424
|
+
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
425
425
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
426
426
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
427
427
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
@@ -436,9 +436,9 @@ export class AbstractGraph {
|
|
|
436
436
|
if (genPaths === undefined)
|
|
437
437
|
genPaths = false;
|
|
438
438
|
if (dest === undefined)
|
|
439
|
-
dest =
|
|
439
|
+
dest = undefined;
|
|
440
440
|
let minDist = Infinity;
|
|
441
|
-
let minDest =
|
|
441
|
+
let minDest = undefined;
|
|
442
442
|
let minPath = [];
|
|
443
443
|
const paths = [];
|
|
444
444
|
const vertices = this._vertices;
|
|
@@ -446,9 +446,9 @@ export class AbstractGraph {
|
|
|
446
446
|
const seen = new Set();
|
|
447
447
|
const preMap = new Map(); // predecessor
|
|
448
448
|
const srcVertex = this._getVertex(src);
|
|
449
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
449
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
450
450
|
if (!srcVertex) {
|
|
451
|
-
return
|
|
451
|
+
return undefined;
|
|
452
452
|
}
|
|
453
453
|
for (const vertex of vertices) {
|
|
454
454
|
const vertexOrKey = vertex[1];
|
|
@@ -456,10 +456,10 @@ export class AbstractGraph {
|
|
|
456
456
|
distMap.set(vertexOrKey, Infinity);
|
|
457
457
|
}
|
|
458
458
|
distMap.set(srcVertex, 0);
|
|
459
|
-
preMap.set(srcVertex,
|
|
459
|
+
preMap.set(srcVertex, undefined);
|
|
460
460
|
const getMinOfNoSeen = () => {
|
|
461
461
|
let min = Infinity;
|
|
462
|
-
let minV =
|
|
462
|
+
let minV = undefined;
|
|
463
463
|
for (const [key, value] of distMap) {
|
|
464
464
|
if (!seen.has(key)) {
|
|
465
465
|
if (value < min) {
|
|
@@ -507,7 +507,7 @@ export class AbstractGraph {
|
|
|
507
507
|
if (edge) {
|
|
508
508
|
const curFromMap = distMap.get(cur);
|
|
509
509
|
const neighborFromMap = distMap.get(neighbor);
|
|
510
|
-
// TODO after no-non-
|
|
510
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
511
511
|
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
512
512
|
if (edge.weight + curFromMap < neighborFromMap) {
|
|
513
513
|
distMap.set(neighbor, edge.weight + curFromMap);
|
|
@@ -554,7 +554,7 @@ export class AbstractGraph {
|
|
|
554
554
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
555
555
|
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
556
556
|
* start. It can be either a vertex object or a vertex ID.
|
|
557
|
-
* @param {VO | VertexKey |
|
|
557
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
558
558
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
559
559
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
560
560
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -571,9 +571,9 @@ export class AbstractGraph {
|
|
|
571
571
|
if (genPaths === undefined)
|
|
572
572
|
genPaths = false;
|
|
573
573
|
if (dest === undefined)
|
|
574
|
-
dest =
|
|
574
|
+
dest = undefined;
|
|
575
575
|
let minDist = Infinity;
|
|
576
|
-
let minDest =
|
|
576
|
+
let minDest = undefined;
|
|
577
577
|
let minPath = [];
|
|
578
578
|
const paths = [];
|
|
579
579
|
const vertices = this._vertices;
|
|
@@ -581,9 +581,9 @@ export class AbstractGraph {
|
|
|
581
581
|
const seen = new Set();
|
|
582
582
|
const preMap = new Map(); // predecessor
|
|
583
583
|
const srcVertex = this._getVertex(src);
|
|
584
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
584
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
585
585
|
if (!srcVertex)
|
|
586
|
-
return
|
|
586
|
+
return undefined;
|
|
587
587
|
for (const vertex of vertices) {
|
|
588
588
|
const vertexOrKey = vertex[1];
|
|
589
589
|
if (vertexOrKey instanceof AbstractVertex)
|
|
@@ -592,11 +592,11 @@ export class AbstractGraph {
|
|
|
592
592
|
const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
593
593
|
heap.add({ key: 0, value: srcVertex });
|
|
594
594
|
distMap.set(srcVertex, 0);
|
|
595
|
-
preMap.set(srcVertex,
|
|
595
|
+
preMap.set(srcVertex, undefined);
|
|
596
596
|
/**
|
|
597
597
|
* The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
|
|
598
|
-
* @param {VO |
|
|
599
|
-
*
|
|
598
|
+
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
599
|
+
* undefined.
|
|
600
600
|
*/
|
|
601
601
|
const getPaths = (minV) => {
|
|
602
602
|
for (const vertex of vertices) {
|
|
@@ -732,7 +732,7 @@ export class AbstractGraph {
|
|
|
732
732
|
}
|
|
733
733
|
}
|
|
734
734
|
}
|
|
735
|
-
let minDest =
|
|
735
|
+
let minDest = undefined;
|
|
736
736
|
if (getMin) {
|
|
737
737
|
distMap.forEach((d, v) => {
|
|
738
738
|
if (v !== srcVertex) {
|
|
@@ -808,7 +808,7 @@ export class AbstractGraph {
|
|
|
808
808
|
* graph.
|
|
809
809
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
810
810
|
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
811
|
-
* `predecessor` property is a 2D array of vertices (or `
|
|
811
|
+
* `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
|
|
812
812
|
* path between vertices in the
|
|
813
813
|
*/
|
|
814
814
|
floydWarshall() {
|
|
@@ -821,7 +821,7 @@ export class AbstractGraph {
|
|
|
821
821
|
costs[i] = [];
|
|
822
822
|
predecessor[i] = [];
|
|
823
823
|
for (let j = 0; j < n; j++) {
|
|
824
|
-
predecessor[i][j] =
|
|
824
|
+
predecessor[i][j] = undefined;
|
|
825
825
|
}
|
|
826
826
|
}
|
|
827
827
|
for (let i = 0; i < n; i++) {
|
|
@@ -913,7 +913,7 @@ export class AbstractGraph {
|
|
|
913
913
|
}
|
|
914
914
|
const childLow = lowMap.get(neighbor);
|
|
915
915
|
const curLow = lowMap.get(cur);
|
|
916
|
-
// TODO after no-non-
|
|
916
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
917
917
|
if (curLow !== undefined && childLow !== undefined) {
|
|
918
918
|
lowMap.set(cur, Math.min(curLow, childLow));
|
|
919
919
|
}
|
|
@@ -937,7 +937,7 @@ export class AbstractGraph {
|
|
|
937
937
|
}
|
|
938
938
|
}
|
|
939
939
|
};
|
|
940
|
-
dfs(root,
|
|
940
|
+
dfs(root, undefined);
|
|
941
941
|
let SCCs = new Map();
|
|
942
942
|
const getSCCs = () => {
|
|
943
943
|
const SCCs = new Map();
|
|
@@ -1031,7 +1031,7 @@ export class AbstractGraph {
|
|
|
1031
1031
|
}
|
|
1032
1032
|
_getVertex(vertexOrKey) {
|
|
1033
1033
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1034
|
-
return this._vertices.get(vertexKey) ||
|
|
1034
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
1035
1035
|
}
|
|
1036
1036
|
_getVertexKey(vertexOrKey) {
|
|
1037
1037
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|