data-structure-typed 0.8.18 → 1.12.9
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/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/.idea/modules.xml +1 -1
- package/README.md +298 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
- package/dist/data-structures/binary-tree/avl-tree.js +150 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
- package/dist/data-structures/binary-tree/binary-tree.js +747 -369
- package/dist/data-structures/binary-tree/bst.d.ts +20 -8
- package/dist/data-structures/binary-tree/bst.js +164 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
- package/dist/data-structures/binary-tree/segment-tree.js +95 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
- package/dist/data-structures/graph/abstract-graph.js +712 -323
- package/dist/data-structures/graph/directed-graph.d.ts +114 -12
- package/dist/data-structures/graph/directed-graph.js +372 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
- package/dist/data-structures/graph/undirected-graph.js +233 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
- package/dist/data-structures/hash/coordinate-map.js +70 -20
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +58 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +26 -37
- package/dist/data-structures/heap/heap.js +56 -60
- package/dist/data-structures/heap/max-heap.d.ts +8 -2
- package/dist/data-structures/heap/max-heap.js +32 -9
- package/dist/data-structures/heap/min-heap.d.ts +9 -2
- package/dist/data-structures/heap/min-heap.js +33 -9
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
- package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +19 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
- package/dist/data-structures/matrix/matrix2d.js +158 -61
- package/dist/data-structures/matrix/navigator.d.ts +34 -16
- package/dist/data-structures/matrix/navigator.js +65 -18
- package/dist/data-structures/matrix/vector2d.d.ts +153 -29
- package/dist/data-structures/matrix/vector2d.js +249 -102
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
- package/dist/data-structures/priority-queue/priority-queue.js +285 -116
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +151 -56
- package/dist/data-structures/queue/queue.d.ts +34 -37
- package/dist/data-structures/queue/queue.js +59 -61
- package/dist/data-structures/stack/stack.d.ts +29 -35
- package/dist/data-structures/stack/stack.js +51 -56
- package/dist/data-structures/trie/trie.d.ts +36 -6
- package/dist/data-structures/trie/trie.js +256 -83
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/utils/trampoline.d.ts +14 -0
- package/dist/utils/trampoline.js +130 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{types → utils}/types/utils.d.ts +15 -1
- package/dist/{types → utils/types}/utils.js +21 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
- package/dist/utils/utils.js +651 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +33 -47
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +58 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
- package/src/data-structures/binary-tree/binary-tree.ts +460 -145
- package/src/data-structures/binary-tree/bst.ts +31 -25
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/segment-tree.ts +25 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +224 -108
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +132 -26
- package/src/data-structures/graph/undirected-graph.ts +78 -11
- package/src/data-structures/hash/coordinate-map.ts +33 -1
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +27 -41
- package/src/data-structures/heap/max-heap.ts +8 -2
- package/src/data-structures/heap/min-heap.ts +9 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
- package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +90 -10
- package/src/data-structures/matrix/navigator.ts +34 -14
- package/src/data-structures/matrix/vector2d.ts +187 -63
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/priority-queue.ts +200 -78
- package/src/data-structures/queue/deque.ts +71 -2
- package/src/data-structures/queue/queue.ts +37 -40
- package/src/data-structures/stack/stack.ts +32 -38
- package/src/data-structures/trie/trie.ts +83 -14
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/trampoline.ts +51 -0
- package/src/utils/types/index.ts +1 -0
- package/src/{types → utils/types}/utils.ts +27 -5
- package/src/{utils.ts → utils/utils.ts} +41 -131
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- package/tsconfig.json +9 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/data-structures/trampoline.js +0 -52
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/data-structures/trampoline.ts +0 -91
- package/src/types/index.ts +0 -1
- /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
- /package/dist/{types → utils}/types/index.d.ts +0 -0
- /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
|
@@ -1,88 +1,216 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
var __values = (this && this.__values) || function(o) {
|
|
3
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
+
if (m) return m.call(o);
|
|
5
|
+
if (o && typeof o.length === "number") return {
|
|
6
|
+
next: function () {
|
|
7
|
+
if (o && i >= o.length) o = void 0;
|
|
8
|
+
return { value: o && o[i++], done: !o };
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
+
};
|
|
13
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
+
if (!m) return o;
|
|
16
|
+
var i = m.call(o), r, ar = [], e;
|
|
17
|
+
try {
|
|
18
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
+
}
|
|
20
|
+
catch (error) { e = { error: error }; }
|
|
21
|
+
finally {
|
|
22
|
+
try {
|
|
23
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
+
}
|
|
25
|
+
finally { if (e) throw e.error; }
|
|
9
26
|
}
|
|
10
|
-
|
|
11
|
-
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
+
if (ar || !(i in from)) {
|
|
32
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
+
ar[i] = from[i];
|
|
34
|
+
}
|
|
12
35
|
}
|
|
13
|
-
|
|
36
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
40
|
+
/**
|
|
41
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
42
|
+
* @license MIT
|
|
43
|
+
*/
|
|
44
|
+
var utils_1 = require("../../utils");
|
|
45
|
+
var priority_queue_1 = require("../priority-queue");
|
|
46
|
+
var AbstractVertex = /** @class */ (function () {
|
|
47
|
+
function AbstractVertex(id) {
|
|
14
48
|
this._id = id;
|
|
15
49
|
}
|
|
16
|
-
|
|
50
|
+
Object.defineProperty(AbstractVertex.prototype, "id", {
|
|
51
|
+
get: function () {
|
|
52
|
+
return this._id;
|
|
53
|
+
},
|
|
54
|
+
set: function (v) {
|
|
55
|
+
this._id = v;
|
|
56
|
+
},
|
|
57
|
+
enumerable: false,
|
|
58
|
+
configurable: true
|
|
59
|
+
});
|
|
60
|
+
return AbstractVertex;
|
|
61
|
+
}());
|
|
17
62
|
exports.AbstractVertex = AbstractVertex;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return this._weight;
|
|
21
|
-
}
|
|
22
|
-
set weight(v) {
|
|
23
|
-
this._weight = v;
|
|
24
|
-
}
|
|
25
|
-
get hashCode() {
|
|
26
|
-
return this._hashCode;
|
|
27
|
-
}
|
|
28
|
-
set hashCode(v) {
|
|
29
|
-
this._hashCode = v;
|
|
30
|
-
}
|
|
31
|
-
constructor(weight) {
|
|
63
|
+
var AbstractEdge = /** @class */ (function () {
|
|
64
|
+
function AbstractEdge(weight) {
|
|
32
65
|
if (weight === undefined)
|
|
33
66
|
weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
|
|
34
67
|
this._weight = weight;
|
|
35
68
|
this._hashCode = (0, utils_1.uuidV4)();
|
|
36
69
|
}
|
|
37
|
-
|
|
70
|
+
Object.defineProperty(AbstractEdge.prototype, "weight", {
|
|
71
|
+
get: function () {
|
|
72
|
+
return this._weight;
|
|
73
|
+
},
|
|
74
|
+
set: function (v) {
|
|
75
|
+
this._weight = v;
|
|
76
|
+
},
|
|
77
|
+
enumerable: false,
|
|
78
|
+
configurable: true
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(AbstractEdge.prototype, "hashCode", {
|
|
81
|
+
get: function () {
|
|
82
|
+
return this._hashCode;
|
|
83
|
+
},
|
|
84
|
+
set: function (v) {
|
|
85
|
+
this._hashCode = v;
|
|
86
|
+
},
|
|
87
|
+
enumerable: false,
|
|
88
|
+
configurable: true
|
|
89
|
+
});
|
|
90
|
+
AbstractEdge.DEFAULT_EDGE_WEIGHT = 1;
|
|
91
|
+
return AbstractEdge;
|
|
92
|
+
}());
|
|
38
93
|
exports.AbstractEdge = AbstractEdge;
|
|
39
|
-
AbstractEdge.DEFAULT_EDGE_WEIGHT = 1;
|
|
40
94
|
// Connected Component === Largest Connected Sub-Graph
|
|
41
|
-
|
|
42
|
-
|
|
95
|
+
var AbstractGraph = /** @class */ (function () {
|
|
96
|
+
function AbstractGraph() {
|
|
43
97
|
this._vertices = new Map();
|
|
44
98
|
// unionFind() {
|
|
45
99
|
// }
|
|
46
100
|
/**--- end find cycles --- */
|
|
47
101
|
// Minimum Spanning Tree
|
|
48
102
|
}
|
|
49
|
-
|
|
50
|
-
|
|
103
|
+
/**
|
|
104
|
+
* The function `getVertex` returns the vertex object associated with a given vertex ID or vertex object, or null if it
|
|
105
|
+
* does not exist.
|
|
106
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
107
|
+
* @returns The function `getVertex` returns the vertex object (`V`) corresponding to the given `vertexOrId` parameter.
|
|
108
|
+
* If the vertex is found in the `_vertices` map, it is returned. Otherwise, `null` is returned.
|
|
109
|
+
*/
|
|
110
|
+
AbstractGraph.prototype.getVertex = function (vertexOrId) {
|
|
111
|
+
var vertexId = this.getVertexId(vertexOrId);
|
|
51
112
|
return this._vertices.get(vertexId) || null;
|
|
52
|
-
}
|
|
53
|
-
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The function `getVertexId` returns the id of a vertex, whether it is passed as an instance of `AbstractVertex` or as
|
|
116
|
+
* a `VertexId`.
|
|
117
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
118
|
+
* (`VertexId`).
|
|
119
|
+
* @returns the id of the vertex.
|
|
120
|
+
*/
|
|
121
|
+
AbstractGraph.prototype.getVertexId = function (vertexOrId) {
|
|
54
122
|
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
55
|
-
}
|
|
56
|
-
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* The function checks if a vertex exists in a graph.
|
|
126
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
127
|
+
* (`VertexId`).
|
|
128
|
+
* @returns The method `containsVertex` returns a boolean value.
|
|
129
|
+
*/
|
|
130
|
+
AbstractGraph.prototype.containsVertex = function (vertexOrId) {
|
|
57
131
|
return this._vertices.has(this.getVertexId(vertexOrId));
|
|
58
|
-
}
|
|
59
|
-
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* The function `vertexSet()` returns a map of vertices.
|
|
135
|
+
* @returns The method `vertexSet()` returns a map of vertex IDs to vertex objects.
|
|
136
|
+
*/
|
|
137
|
+
AbstractGraph.prototype.vertexSet = function () {
|
|
60
138
|
return this._vertices;
|
|
61
|
-
}
|
|
62
|
-
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* The addVertex function adds a new vertex to a graph if it does not already exist.
|
|
142
|
+
* @param {V} newVertex - The parameter "newVertex" is of type V, which represents a vertex in a graph.
|
|
143
|
+
* @returns The method is returning a boolean value. If the newVertex is already contained in the graph, it will return
|
|
144
|
+
* false. Otherwise, it will add the newVertex to the graph and return true.
|
|
145
|
+
*/
|
|
146
|
+
AbstractGraph.prototype.addVertex = function (newVertex) {
|
|
63
147
|
if (this.containsVertex(newVertex)) {
|
|
64
148
|
return false;
|
|
65
149
|
}
|
|
66
150
|
this._vertices.set(newVertex.id, newVertex);
|
|
67
151
|
return true;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
155
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
156
|
+
* (`VertexId`).
|
|
157
|
+
* @returns The method `removeVertex` returns a boolean value.
|
|
158
|
+
*/
|
|
159
|
+
AbstractGraph.prototype.removeVertex = function (vertexOrId) {
|
|
160
|
+
var vertexId = this.getVertexId(vertexOrId);
|
|
71
161
|
return this._vertices.delete(vertexId);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
165
|
+
* @param {V[] | VertexId[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
|
|
166
|
+
* of vertex IDs (`VertexId[]`).
|
|
167
|
+
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
168
|
+
* were removed.
|
|
169
|
+
*/
|
|
170
|
+
AbstractGraph.prototype.removeAllVertices = function (vertices) {
|
|
171
|
+
var e_1, _a;
|
|
172
|
+
var removed = [];
|
|
173
|
+
try {
|
|
174
|
+
for (var vertices_1 = __values(vertices), vertices_1_1 = vertices_1.next(); !vertices_1_1.done; vertices_1_1 = vertices_1.next()) {
|
|
175
|
+
var v = vertices_1_1.value;
|
|
176
|
+
removed.push(this.removeVertex(v));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
180
|
+
finally {
|
|
181
|
+
try {
|
|
182
|
+
if (vertices_1_1 && !vertices_1_1.done && (_a = vertices_1.return)) _a.call(vertices_1);
|
|
183
|
+
}
|
|
184
|
+
finally { if (e_1) throw e_1.error; }
|
|
77
185
|
}
|
|
78
186
|
return removed.length > 0;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* The function checks if there is an edge between two vertices in a graph.
|
|
190
|
+
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the identifier of
|
|
191
|
+
* a vertex in a graph, while V represents the type of the vertex itself.
|
|
192
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId`
|
|
193
|
+
* or a `V` type.
|
|
194
|
+
* @returns The function `containsEdge` returns a boolean value. It returns `true` if there is an edge between the
|
|
195
|
+
* vertices `v1` and `v2`, and `false` otherwise.
|
|
196
|
+
*/
|
|
197
|
+
AbstractGraph.prototype.containsEdge = function (v1, v2) {
|
|
198
|
+
var edge = this.getEdge(v1, v2);
|
|
82
199
|
return !!edge;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* The function sets the weight of an edge between two vertices in a graph.
|
|
203
|
+
* @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
|
|
204
|
+
* the source vertex of the edge.
|
|
205
|
+
* @param {VertexId | V} destOrId - The `destOrId` parameter represents the destination vertex of the edge. It can be
|
|
206
|
+
* either a `VertexId` or a vertex object `V`.
|
|
207
|
+
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrId)
|
|
208
|
+
* and the destination vertex (destOrId).
|
|
209
|
+
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
210
|
+
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
211
|
+
*/
|
|
212
|
+
AbstractGraph.prototype.setEdgeWeight = function (srcOrId, destOrId, weight) {
|
|
213
|
+
var edge = this.getEdge(srcOrId, destOrId);
|
|
86
214
|
if (edge) {
|
|
87
215
|
edge.weight = weight;
|
|
88
216
|
return true;
|
|
@@ -90,76 +218,140 @@ class AbstractGraph {
|
|
|
90
218
|
else {
|
|
91
219
|
return false;
|
|
92
220
|
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
224
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
225
|
+
* It is the starting vertex for finding paths.
|
|
226
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex that we
|
|
227
|
+
* want to find paths to from the starting vertex `v1`.
|
|
228
|
+
* @returns an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1
|
|
229
|
+
* and v2).
|
|
230
|
+
*/
|
|
231
|
+
AbstractGraph.prototype.getAllPathsBetween = function (v1, v2) {
|
|
232
|
+
var _this = this;
|
|
233
|
+
var paths = [];
|
|
234
|
+
var vertex1 = this.getVertex(v1);
|
|
235
|
+
var vertex2 = this.getVertex(v2);
|
|
98
236
|
if (!(vertex1 && vertex2)) {
|
|
99
237
|
return [];
|
|
100
238
|
}
|
|
101
|
-
|
|
239
|
+
var dfs = function (cur, dest, visiting, path) {
|
|
240
|
+
var e_2, _a;
|
|
102
241
|
visiting.set(cur, true);
|
|
103
242
|
if (cur === dest) {
|
|
104
|
-
paths.push([vertex1,
|
|
243
|
+
paths.push(__spreadArray([vertex1], __read(path), false));
|
|
105
244
|
}
|
|
106
|
-
|
|
107
|
-
|
|
245
|
+
var neighbors = _this.getNeighbors(cur);
|
|
246
|
+
var _loop_1 = function (neighbor) {
|
|
108
247
|
if (!visiting.get(neighbor)) {
|
|
109
248
|
path.push(neighbor);
|
|
110
249
|
dfs(neighbor, dest, visiting, path);
|
|
111
|
-
(0, utils_1.arrayRemove)(path, vertex
|
|
250
|
+
(0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
try {
|
|
254
|
+
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
|
|
255
|
+
var neighbor = neighbors_1_1.value;
|
|
256
|
+
_loop_1(neighbor);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
260
|
+
finally {
|
|
261
|
+
try {
|
|
262
|
+
if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
|
|
112
263
|
}
|
|
264
|
+
finally { if (e_2) throw e_2.error; }
|
|
113
265
|
}
|
|
114
266
|
visiting.set(cur, false);
|
|
115
267
|
};
|
|
116
268
|
dfs(vertex1, vertex2, new Map(), []);
|
|
117
269
|
return paths;
|
|
118
|
-
}
|
|
119
|
-
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* The function calculates the sum of weights along a given path.
|
|
273
|
+
* @param {V[]} path - An array of vertices (V) representing a path in a graph.
|
|
274
|
+
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
275
|
+
*/
|
|
276
|
+
AbstractGraph.prototype.getPathSumWeight = function (path) {
|
|
120
277
|
var _a;
|
|
121
|
-
|
|
122
|
-
for (
|
|
278
|
+
var sum = 0;
|
|
279
|
+
for (var i = 0; i < path.length; i++) {
|
|
123
280
|
sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
|
|
124
281
|
}
|
|
125
282
|
return sum;
|
|
126
|
-
}
|
|
127
|
-
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
286
|
+
* weights or using a breadth-first search algorithm.
|
|
287
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or vertex ID of the graph.
|
|
288
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the second vertex in the graph. It can be either a vertex
|
|
289
|
+
* object or a vertex ID.
|
|
290
|
+
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
291
|
+
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
292
|
+
* the edges. If isWeight is set to false or not provided, the function will calculate the
|
|
293
|
+
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
|
|
294
|
+
* and `v2`) in a graph. If the `isWeight` parameter is `true`, it calculates the minimum weight between the vertices.
|
|
295
|
+
* If `isWeight` is `false` or not provided, it calculates the minimum number of edges between the vertices. If the
|
|
296
|
+
* vertices are not
|
|
297
|
+
*/
|
|
298
|
+
AbstractGraph.prototype.getMinCostBetween = function (v1, v2, isWeight) {
|
|
299
|
+
var e_3, _a, e_4, _b;
|
|
128
300
|
if (isWeight === undefined)
|
|
129
301
|
isWeight = false;
|
|
130
302
|
if (isWeight) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
303
|
+
var allPaths = this.getAllPathsBetween(v1, v2);
|
|
304
|
+
var min = Infinity;
|
|
305
|
+
try {
|
|
306
|
+
for (var allPaths_1 = __values(allPaths), allPaths_1_1 = allPaths_1.next(); !allPaths_1_1.done; allPaths_1_1 = allPaths_1.next()) {
|
|
307
|
+
var path = allPaths_1_1.value;
|
|
308
|
+
min = Math.min(this.getPathSumWeight(path), min);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
312
|
+
finally {
|
|
313
|
+
try {
|
|
314
|
+
if (allPaths_1_1 && !allPaths_1_1.done && (_a = allPaths_1.return)) _a.call(allPaths_1);
|
|
315
|
+
}
|
|
316
|
+
finally { if (e_3) throw e_3.error; }
|
|
135
317
|
}
|
|
136
318
|
return min;
|
|
137
319
|
}
|
|
138
320
|
else {
|
|
139
321
|
// BFS
|
|
140
|
-
|
|
141
|
-
|
|
322
|
+
var vertex2 = this.getVertex(v2);
|
|
323
|
+
var vertex1 = this.getVertex(v1);
|
|
142
324
|
if (!(vertex1 && vertex2)) {
|
|
143
325
|
return null;
|
|
144
326
|
}
|
|
145
|
-
|
|
146
|
-
|
|
327
|
+
var visited = new Map();
|
|
328
|
+
var queue = [vertex1];
|
|
147
329
|
visited.set(vertex1, true);
|
|
148
|
-
|
|
330
|
+
var cost = 0;
|
|
149
331
|
while (queue.length > 0) {
|
|
150
|
-
for (
|
|
151
|
-
|
|
332
|
+
for (var i = 0; i < queue.length; i++) {
|
|
333
|
+
var cur = queue.shift();
|
|
152
334
|
if (cur === vertex2) {
|
|
153
335
|
return cost;
|
|
154
336
|
}
|
|
155
337
|
// TODO consider optimizing to AbstractGraph
|
|
156
338
|
if (cur !== undefined) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
339
|
+
var neighbors = this.getNeighbors(cur);
|
|
340
|
+
try {
|
|
341
|
+
for (var neighbors_2 = (e_4 = void 0, __values(neighbors)), neighbors_2_1 = neighbors_2.next(); !neighbors_2_1.done; neighbors_2_1 = neighbors_2.next()) {
|
|
342
|
+
var neighbor = neighbors_2_1.value;
|
|
343
|
+
if (!visited.has(neighbor)) {
|
|
344
|
+
visited.set(neighbor, true);
|
|
345
|
+
queue.push(neighbor);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
350
|
+
finally {
|
|
351
|
+
try {
|
|
352
|
+
if (neighbors_2_1 && !neighbors_2_1.done && (_b = neighbors_2.return)) _b.call(neighbors_2);
|
|
162
353
|
}
|
|
354
|
+
finally { if (e_4) throw e_4.error; }
|
|
163
355
|
}
|
|
164
356
|
}
|
|
165
357
|
}
|
|
@@ -167,114 +359,200 @@ class AbstractGraph {
|
|
|
167
359
|
}
|
|
168
360
|
return null;
|
|
169
361
|
}
|
|
170
|
-
}
|
|
171
|
-
|
|
362
|
+
};
|
|
363
|
+
/**
|
|
364
|
+
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
365
|
+
* using a breadth-first search algorithm.
|
|
366
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
367
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex that we
|
|
368
|
+
* want to find the minimum path to from the source vertex `v1`.
|
|
369
|
+
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
370
|
+
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
371
|
+
* to false, the function will use breadth-first search (BFS) to find the minimum path. If
|
|
372
|
+
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
373
|
+
* two vertices (`v1` and `v2`). If no path is found, it returns `null`.
|
|
374
|
+
*/
|
|
375
|
+
AbstractGraph.prototype.getMinPathBetween = function (v1, v2, isWeight) {
|
|
376
|
+
var e_5, _a;
|
|
377
|
+
var _this = this;
|
|
172
378
|
if (isWeight === undefined)
|
|
173
379
|
isWeight = false;
|
|
174
380
|
if (isWeight) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
381
|
+
var allPaths = this.getAllPathsBetween(v1, v2);
|
|
382
|
+
var min = Infinity;
|
|
383
|
+
var minIndex = -1;
|
|
384
|
+
var index = 0;
|
|
385
|
+
try {
|
|
386
|
+
for (var allPaths_2 = __values(allPaths), allPaths_2_1 = allPaths_2.next(); !allPaths_2_1.done; allPaths_2_1 = allPaths_2.next()) {
|
|
387
|
+
var path = allPaths_2_1.value;
|
|
388
|
+
var pathSumWeight = this.getPathSumWeight(path);
|
|
389
|
+
if (pathSumWeight < min) {
|
|
390
|
+
min = pathSumWeight;
|
|
391
|
+
minIndex = index;
|
|
392
|
+
}
|
|
393
|
+
index++;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
397
|
+
finally {
|
|
398
|
+
try {
|
|
399
|
+
if (allPaths_2_1 && !allPaths_2_1.done && (_a = allPaths_2.return)) _a.call(allPaths_2);
|
|
400
|
+
}
|
|
401
|
+
finally { if (e_5) throw e_5.error; }
|
|
186
402
|
}
|
|
187
403
|
return allPaths[minIndex] || null;
|
|
188
404
|
}
|
|
189
405
|
else {
|
|
190
406
|
// BFS
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if (!(
|
|
407
|
+
var minPath_1 = [];
|
|
408
|
+
var vertex1_1 = this.getVertex(v1);
|
|
409
|
+
var vertex2 = this.getVertex(v2);
|
|
410
|
+
if (!(vertex1_1 && vertex2)) {
|
|
195
411
|
return [];
|
|
196
412
|
}
|
|
197
|
-
|
|
413
|
+
var dfs_1 = function (cur, dest, visiting, path) {
|
|
414
|
+
var e_6, _a;
|
|
198
415
|
visiting.set(cur, true);
|
|
199
416
|
if (cur === dest) {
|
|
200
|
-
|
|
417
|
+
minPath_1 = __spreadArray([vertex1_1], __read(path), false);
|
|
201
418
|
return;
|
|
202
419
|
}
|
|
203
|
-
|
|
204
|
-
|
|
420
|
+
var neighbors = _this.getNeighbors(cur);
|
|
421
|
+
var _loop_2 = function (neighbor) {
|
|
205
422
|
if (!visiting.get(neighbor)) {
|
|
206
423
|
path.push(neighbor);
|
|
207
|
-
|
|
208
|
-
(0, utils_1.arrayRemove)(path, vertex
|
|
424
|
+
dfs_1(neighbor, dest, visiting, path);
|
|
425
|
+
(0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
|
|
209
426
|
}
|
|
427
|
+
};
|
|
428
|
+
try {
|
|
429
|
+
for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
|
|
430
|
+
var neighbor = neighbors_3_1.value;
|
|
431
|
+
_loop_2(neighbor);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
435
|
+
finally {
|
|
436
|
+
try {
|
|
437
|
+
if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3);
|
|
438
|
+
}
|
|
439
|
+
finally { if (e_6) throw e_6.error; }
|
|
210
440
|
}
|
|
211
441
|
visiting.set(cur, false);
|
|
212
442
|
};
|
|
213
|
-
|
|
214
|
-
return
|
|
443
|
+
dfs_1(vertex1_1, vertex2, new Map(), []);
|
|
444
|
+
return minPath_1;
|
|
215
445
|
}
|
|
216
|
-
}
|
|
446
|
+
};
|
|
217
447
|
/**
|
|
218
448
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
* @param
|
|
222
|
-
*
|
|
449
|
+
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
450
|
+
* a graph without using a heap data structure.
|
|
451
|
+
* @param {V | VertexId} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
452
|
+
* vertex object or a vertex ID.
|
|
453
|
+
* @param {V | VertexId | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
454
|
+
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
455
|
+
* identifier. If no destination is provided, the value is set to `null`.
|
|
456
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
457
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
458
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
459
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
460
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
461
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
462
|
+
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
|
|
223
463
|
*/
|
|
224
|
-
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
464
|
+
AbstractGraph.prototype.dijkstraWithoutHeap = function (src, dest, getMinDist, genPaths) {
|
|
465
|
+
var e_7, _a, e_8, _b;
|
|
225
466
|
if (getMinDist === undefined)
|
|
226
467
|
getMinDist = false;
|
|
227
468
|
if (genPaths === undefined)
|
|
228
469
|
genPaths = false;
|
|
229
470
|
if (dest === undefined)
|
|
230
471
|
dest = null;
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
472
|
+
var minDist = Infinity;
|
|
473
|
+
var minDest = null;
|
|
474
|
+
var minPath = [];
|
|
475
|
+
var paths = [];
|
|
476
|
+
var vertices = this._vertices;
|
|
477
|
+
var distMap = new Map();
|
|
478
|
+
var seen = new Set();
|
|
479
|
+
var preMap = new Map(); // predecessor
|
|
480
|
+
var srcVertex = this.getVertex(src);
|
|
481
|
+
var destVertex = dest ? this.getVertex(dest) : null;
|
|
241
482
|
if (!srcVertex) {
|
|
242
483
|
return null;
|
|
243
484
|
}
|
|
244
|
-
|
|
245
|
-
|
|
485
|
+
try {
|
|
486
|
+
for (var vertices_2 = __values(vertices), vertices_2_1 = vertices_2.next(); !vertices_2_1.done; vertices_2_1 = vertices_2.next()) {
|
|
487
|
+
var vertex = vertices_2_1.value;
|
|
488
|
+
var vertexOrId = vertex[1];
|
|
489
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
490
|
+
distMap.set(vertexOrId, Infinity);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
494
|
+
finally {
|
|
495
|
+
try {
|
|
496
|
+
if (vertices_2_1 && !vertices_2_1.done && (_a = vertices_2.return)) _a.call(vertices_2);
|
|
497
|
+
}
|
|
498
|
+
finally { if (e_7) throw e_7.error; }
|
|
246
499
|
}
|
|
247
500
|
distMap.set(srcVertex, 0);
|
|
248
501
|
preMap.set(srcVertex, null);
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
502
|
+
var getMinOfNoSeen = function () {
|
|
503
|
+
var e_9, _a;
|
|
504
|
+
var min = Infinity;
|
|
505
|
+
var minV = null;
|
|
506
|
+
try {
|
|
507
|
+
for (var distMap_1 = __values(distMap), distMap_1_1 = distMap_1.next(); !distMap_1_1.done; distMap_1_1 = distMap_1.next()) {
|
|
508
|
+
var _b = __read(distMap_1_1.value, 2), key = _b[0], val = _b[1];
|
|
509
|
+
if (!seen.has(key)) {
|
|
510
|
+
if (val < min) {
|
|
511
|
+
min = val;
|
|
512
|
+
minV = key;
|
|
513
|
+
}
|
|
257
514
|
}
|
|
258
515
|
}
|
|
259
516
|
}
|
|
517
|
+
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
518
|
+
finally {
|
|
519
|
+
try {
|
|
520
|
+
if (distMap_1_1 && !distMap_1_1.done && (_a = distMap_1.return)) _a.call(distMap_1);
|
|
521
|
+
}
|
|
522
|
+
finally { if (e_9) throw e_9.error; }
|
|
523
|
+
}
|
|
260
524
|
return minV;
|
|
261
525
|
};
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
526
|
+
var getPaths = function (minV) {
|
|
527
|
+
var e_10, _a;
|
|
528
|
+
try {
|
|
529
|
+
for (var vertices_3 = __values(vertices), vertices_3_1 = vertices_3.next(); !vertices_3_1.done; vertices_3_1 = vertices_3.next()) {
|
|
530
|
+
var vertex = vertices_3_1.value;
|
|
531
|
+
var vertexOrId = vertex[1];
|
|
532
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
533
|
+
var path = [vertexOrId];
|
|
534
|
+
var parent = preMap.get(vertexOrId);
|
|
535
|
+
while (parent) {
|
|
536
|
+
path.push(parent);
|
|
537
|
+
parent = preMap.get(parent);
|
|
538
|
+
}
|
|
539
|
+
var reversed = path.reverse();
|
|
540
|
+
if (vertex[1] === minV)
|
|
541
|
+
minPath = reversed;
|
|
542
|
+
paths.push(reversed);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
547
|
+
finally {
|
|
548
|
+
try {
|
|
549
|
+
if (vertices_3_1 && !vertices_3_1.done && (_a = vertices_3.return)) _a.call(vertices_3);
|
|
550
|
+
}
|
|
551
|
+
finally { if (e_10) throw e_10.error; }
|
|
274
552
|
}
|
|
275
553
|
};
|
|
276
|
-
for (
|
|
277
|
-
|
|
554
|
+
for (var i = 1; i < vertices.size; i++) {
|
|
555
|
+
var cur = getMinOfNoSeen();
|
|
278
556
|
if (cur) {
|
|
279
557
|
seen.add(cur);
|
|
280
558
|
if (destVertex && destVertex === cur) {
|
|
@@ -284,28 +562,38 @@ class AbstractGraph {
|
|
|
284
562
|
if (genPaths) {
|
|
285
563
|
getPaths(destVertex);
|
|
286
564
|
}
|
|
287
|
-
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
if (
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
565
|
+
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
566
|
+
}
|
|
567
|
+
var neighbors = this.getNeighbors(cur);
|
|
568
|
+
try {
|
|
569
|
+
for (var neighbors_4 = (e_8 = void 0, __values(neighbors)), neighbors_4_1 = neighbors_4.next(); !neighbors_4_1.done; neighbors_4_1 = neighbors_4.next()) {
|
|
570
|
+
var neighbor = neighbors_4_1.value;
|
|
571
|
+
if (!seen.has(neighbor)) {
|
|
572
|
+
var edge = this.getEdge(cur, neighbor);
|
|
573
|
+
if (edge) {
|
|
574
|
+
var curFromMap = distMap.get(cur);
|
|
575
|
+
var neighborFromMap = distMap.get(neighbor);
|
|
576
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
577
|
+
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
578
|
+
if (edge.weight + curFromMap < neighborFromMap) {
|
|
579
|
+
distMap.set(neighbor, edge.weight + curFromMap);
|
|
580
|
+
preMap.set(neighbor, cur);
|
|
581
|
+
}
|
|
301
582
|
}
|
|
302
583
|
}
|
|
303
584
|
}
|
|
304
585
|
}
|
|
305
586
|
}
|
|
587
|
+
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
588
|
+
finally {
|
|
589
|
+
try {
|
|
590
|
+
if (neighbors_4_1 && !neighbors_4_1.done && (_b = neighbors_4.return)) _b.call(neighbors_4);
|
|
591
|
+
}
|
|
592
|
+
finally { if (e_8) throw e_8.error; }
|
|
593
|
+
}
|
|
306
594
|
}
|
|
307
595
|
}
|
|
308
|
-
getMinDist && distMap.forEach((d, v)
|
|
596
|
+
getMinDist && distMap.forEach(function (d, v) {
|
|
309
597
|
if (v !== srcVertex) {
|
|
310
598
|
if (d < minDist) {
|
|
311
599
|
minDist = d;
|
|
@@ -315,61 +603,98 @@ class AbstractGraph {
|
|
|
315
603
|
}
|
|
316
604
|
});
|
|
317
605
|
genPaths && getPaths(minDest);
|
|
318
|
-
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
319
|
-
}
|
|
606
|
+
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
607
|
+
};
|
|
320
608
|
/**
|
|
321
609
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
* @param
|
|
325
|
-
*
|
|
610
|
+
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
611
|
+
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
612
|
+
* @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
613
|
+
* start. It can be either a vertex object or a vertex ID.
|
|
614
|
+
* @param {V | VertexId | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
615
|
+
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
616
|
+
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
617
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
618
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
619
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
620
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
621
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
622
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
623
|
+
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
326
624
|
*/
|
|
327
|
-
dijkstra(src, dest, getMinDist, genPaths) {
|
|
328
|
-
var _a;
|
|
625
|
+
AbstractGraph.prototype.dijkstra = function (src, dest, getMinDist, genPaths) {
|
|
626
|
+
var e_11, _a, e_12, _b;
|
|
627
|
+
var _c;
|
|
329
628
|
if (getMinDist === undefined)
|
|
330
629
|
getMinDist = false;
|
|
331
630
|
if (genPaths === undefined)
|
|
332
631
|
genPaths = false;
|
|
333
632
|
if (dest === undefined)
|
|
334
633
|
dest = null;
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
634
|
+
var minDist = Infinity;
|
|
635
|
+
var minDest = null;
|
|
636
|
+
var minPath = [];
|
|
637
|
+
var paths = [];
|
|
638
|
+
var vertices = this._vertices;
|
|
639
|
+
var distMap = new Map();
|
|
640
|
+
var seen = new Set();
|
|
641
|
+
var preMap = new Map(); // predecessor
|
|
642
|
+
var srcVertex = this.getVertex(src);
|
|
643
|
+
var destVertex = dest ? this.getVertex(dest) : null;
|
|
345
644
|
if (!srcVertex) {
|
|
346
645
|
return null;
|
|
347
646
|
}
|
|
348
|
-
|
|
349
|
-
|
|
647
|
+
try {
|
|
648
|
+
for (var vertices_4 = __values(vertices), vertices_4_1 = vertices_4.next(); !vertices_4_1.done; vertices_4_1 = vertices_4.next()) {
|
|
649
|
+
var vertex = vertices_4_1.value;
|
|
650
|
+
var vertexOrId = vertex[1];
|
|
651
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
652
|
+
distMap.set(vertexOrId, Infinity);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
656
|
+
finally {
|
|
657
|
+
try {
|
|
658
|
+
if (vertices_4_1 && !vertices_4_1.done && (_a = vertices_4.return)) _a.call(vertices_4);
|
|
659
|
+
}
|
|
660
|
+
finally { if (e_11) throw e_11.error; }
|
|
350
661
|
}
|
|
351
|
-
|
|
662
|
+
var heap = new priority_queue_1.PriorityQueue({ comparator: function (a, b) { return a.id - b.id; } });
|
|
352
663
|
heap.offer({ id: 0, val: srcVertex });
|
|
353
664
|
distMap.set(srcVertex, 0);
|
|
354
665
|
preMap.set(srcVertex, null);
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
666
|
+
var getPaths = function (minV) {
|
|
667
|
+
var e_13, _a;
|
|
668
|
+
try {
|
|
669
|
+
for (var vertices_5 = __values(vertices), vertices_5_1 = vertices_5.next(); !vertices_5_1.done; vertices_5_1 = vertices_5.next()) {
|
|
670
|
+
var vertex = vertices_5_1.value;
|
|
671
|
+
var vertexOrId = vertex[1];
|
|
672
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
673
|
+
var path = [vertexOrId];
|
|
674
|
+
var parent = preMap.get(vertexOrId);
|
|
675
|
+
while (parent) {
|
|
676
|
+
path.push(parent);
|
|
677
|
+
parent = preMap.get(parent);
|
|
678
|
+
}
|
|
679
|
+
var reversed = path.reverse();
|
|
680
|
+
if (vertex[1] === minV)
|
|
681
|
+
minPath = reversed;
|
|
682
|
+
paths.push(reversed);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
687
|
+
finally {
|
|
688
|
+
try {
|
|
689
|
+
if (vertices_5_1 && !vertices_5_1.done && (_a = vertices_5.return)) _a.call(vertices_5);
|
|
690
|
+
}
|
|
691
|
+
finally { if (e_13) throw e_13.error; }
|
|
367
692
|
}
|
|
368
693
|
};
|
|
369
694
|
while (heap.size > 0) {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
695
|
+
var curHeapNode = heap.poll();
|
|
696
|
+
var dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
|
|
697
|
+
var cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
|
|
373
698
|
if (dist !== undefined) {
|
|
374
699
|
if (cur) {
|
|
375
700
|
seen.add(cur);
|
|
@@ -380,29 +705,39 @@ class AbstractGraph {
|
|
|
380
705
|
if (genPaths) {
|
|
381
706
|
getPaths(destVertex);
|
|
382
707
|
}
|
|
383
|
-
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
708
|
+
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
384
709
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (
|
|
390
|
-
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
710
|
+
var neighbors = this.getNeighbors(cur);
|
|
711
|
+
try {
|
|
712
|
+
for (var neighbors_5 = (e_12 = void 0, __values(neighbors)), neighbors_5_1 = neighbors_5.next(); !neighbors_5_1.done; neighbors_5_1 = neighbors_5.next()) {
|
|
713
|
+
var neighbor = neighbors_5_1.value;
|
|
714
|
+
if (!seen.has(neighbor)) {
|
|
715
|
+
var weight = (_c = this.getEdge(cur, neighbor)) === null || _c === void 0 ? void 0 : _c.weight;
|
|
716
|
+
if (typeof weight === 'number') {
|
|
717
|
+
var distSrcToNeighbor = distMap.get(neighbor);
|
|
718
|
+
if (distSrcToNeighbor) {
|
|
719
|
+
if (dist + weight < distSrcToNeighbor) {
|
|
720
|
+
heap.offer({ id: dist + weight, val: neighbor });
|
|
721
|
+
preMap.set(neighbor, cur);
|
|
722
|
+
distMap.set(neighbor, dist + weight);
|
|
723
|
+
}
|
|
396
724
|
}
|
|
397
725
|
}
|
|
398
726
|
}
|
|
399
727
|
}
|
|
400
728
|
}
|
|
729
|
+
catch (e_12_1) { e_12 = { error: e_12_1 }; }
|
|
730
|
+
finally {
|
|
731
|
+
try {
|
|
732
|
+
if (neighbors_5_1 && !neighbors_5_1.done && (_b = neighbors_5.return)) _b.call(neighbors_5);
|
|
733
|
+
}
|
|
734
|
+
finally { if (e_12) throw e_12.error; }
|
|
735
|
+
}
|
|
401
736
|
}
|
|
402
737
|
}
|
|
403
738
|
}
|
|
404
739
|
if (getMinDist) {
|
|
405
|
-
distMap.forEach((d, v)
|
|
740
|
+
distMap.forEach(function (d, v) {
|
|
406
741
|
if (v !== srcVertex) {
|
|
407
742
|
if (d < minDist) {
|
|
408
743
|
minDist = d;
|
|
@@ -415,49 +750,57 @@ class AbstractGraph {
|
|
|
415
750
|
if (genPaths) {
|
|
416
751
|
getPaths(minDest);
|
|
417
752
|
}
|
|
418
|
-
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
419
|
-
}
|
|
753
|
+
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
754
|
+
};
|
|
420
755
|
/**
|
|
421
756
|
* BellmanFord time:O(VE) space:O(V)
|
|
422
757
|
* one to rest pairs
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
* @param
|
|
426
|
-
*
|
|
758
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
759
|
+
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
760
|
+
* @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
761
|
+
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
762
|
+
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
763
|
+
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
764
|
+
* calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
|
|
765
|
+
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
766
|
+
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
|
|
767
|
+
* vertex.
|
|
768
|
+
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
427
769
|
*/
|
|
428
|
-
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
770
|
+
AbstractGraph.prototype.bellmanFord = function (src, scanNegativeCycle, getMin, genPath) {
|
|
771
|
+
var e_14, _a;
|
|
429
772
|
if (getMin === undefined)
|
|
430
773
|
getMin = false;
|
|
431
774
|
if (genPath === undefined)
|
|
432
775
|
genPath = false;
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
776
|
+
var srcVertex = this.getVertex(src);
|
|
777
|
+
var paths = [];
|
|
778
|
+
var distMap = new Map();
|
|
779
|
+
var preMap = new Map(); // predecessor
|
|
780
|
+
var min = Infinity;
|
|
781
|
+
var minPath = [];
|
|
439
782
|
// TODO
|
|
440
|
-
|
|
783
|
+
var hasNegativeCycle;
|
|
441
784
|
if (scanNegativeCycle)
|
|
442
785
|
hasNegativeCycle = false;
|
|
443
786
|
if (!srcVertex)
|
|
444
|
-
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
this._vertices.forEach(vertex
|
|
787
|
+
return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
|
|
788
|
+
var vertices = this._vertices;
|
|
789
|
+
var numOfVertices = vertices.size;
|
|
790
|
+
var edges = this.edgeSet();
|
|
791
|
+
var numOfEdges = edges.length;
|
|
792
|
+
this._vertices.forEach(function (vertex) {
|
|
450
793
|
distMap.set(vertex, Infinity);
|
|
451
794
|
});
|
|
452
795
|
distMap.set(srcVertex, 0);
|
|
453
|
-
for (
|
|
454
|
-
for (
|
|
455
|
-
|
|
796
|
+
for (var i = 1; i < numOfVertices; ++i) {
|
|
797
|
+
for (var j = 0; j < numOfEdges; ++j) {
|
|
798
|
+
var ends = this.getEndsOfEdge(edges[j]);
|
|
456
799
|
if (ends) {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
800
|
+
var _b = __read(ends, 2), s = _b[0], d = _b[1];
|
|
801
|
+
var weight = edges[j].weight;
|
|
802
|
+
var sWeight = distMap.get(s);
|
|
803
|
+
var dWeight = distMap.get(d);
|
|
461
804
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
462
805
|
if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
|
|
463
806
|
distMap.set(d, sWeight + weight);
|
|
@@ -467,9 +810,9 @@ class AbstractGraph {
|
|
|
467
810
|
}
|
|
468
811
|
}
|
|
469
812
|
}
|
|
470
|
-
|
|
813
|
+
var minDest = null;
|
|
471
814
|
if (getMin) {
|
|
472
|
-
distMap.forEach((d, v)
|
|
815
|
+
distMap.forEach(function (d, v) {
|
|
473
816
|
if (v !== srcVertex) {
|
|
474
817
|
if (d < min) {
|
|
475
818
|
min = d;
|
|
@@ -480,59 +823,78 @@ class AbstractGraph {
|
|
|
480
823
|
});
|
|
481
824
|
}
|
|
482
825
|
if (genPath) {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
826
|
+
try {
|
|
827
|
+
for (var vertices_6 = __values(vertices), vertices_6_1 = vertices_6.next(); !vertices_6_1.done; vertices_6_1 = vertices_6.next()) {
|
|
828
|
+
var vertex = vertices_6_1.value;
|
|
829
|
+
var vertexOrId = vertex[1];
|
|
830
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
831
|
+
var path = [vertexOrId];
|
|
832
|
+
var parent = preMap.get(vertexOrId);
|
|
833
|
+
while (parent !== undefined) {
|
|
834
|
+
path.push(parent);
|
|
835
|
+
parent = preMap.get(parent);
|
|
836
|
+
}
|
|
837
|
+
var reversed = path.reverse();
|
|
838
|
+
if (vertex[1] === minDest)
|
|
839
|
+
minPath = reversed;
|
|
840
|
+
paths.push(reversed);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
845
|
+
finally {
|
|
846
|
+
try {
|
|
847
|
+
if (vertices_6_1 && !vertices_6_1.done && (_a = vertices_6.return)) _a.call(vertices_6);
|
|
489
848
|
}
|
|
490
|
-
|
|
491
|
-
if (vertex[1] === minDest)
|
|
492
|
-
minPath = reversed;
|
|
493
|
-
paths.push(reversed);
|
|
849
|
+
finally { if (e_14) throw e_14.error; }
|
|
494
850
|
}
|
|
495
851
|
}
|
|
496
|
-
for (
|
|
497
|
-
|
|
852
|
+
for (var j = 0; j < numOfEdges; ++j) {
|
|
853
|
+
var ends = this.getEndsOfEdge(edges[j]);
|
|
498
854
|
if (ends) {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
855
|
+
var _c = __read(ends, 1), s = _c[0];
|
|
856
|
+
var weight = edges[j].weight;
|
|
857
|
+
var sWeight = distMap.get(s);
|
|
502
858
|
if (sWeight) {
|
|
503
859
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
504
860
|
hasNegativeCycle = true;
|
|
505
861
|
}
|
|
506
862
|
}
|
|
507
863
|
}
|
|
508
|
-
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
509
|
-
}
|
|
864
|
+
return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
|
|
865
|
+
};
|
|
510
866
|
/**
|
|
511
867
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
512
868
|
* all pairs
|
|
869
|
+
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
870
|
+
* graph.
|
|
871
|
+
* @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
872
|
+
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
873
|
+
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
874
|
+
* path between vertices in the
|
|
513
875
|
*/
|
|
514
|
-
floyd() {
|
|
876
|
+
AbstractGraph.prototype.floyd = function () {
|
|
515
877
|
var _a;
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
878
|
+
var idAndVertices = __spreadArray([], __read(this._vertices), false);
|
|
879
|
+
var n = idAndVertices.length;
|
|
880
|
+
var costs = [];
|
|
881
|
+
var predecessor = [];
|
|
520
882
|
// successors
|
|
521
|
-
for (
|
|
883
|
+
for (var i = 0; i < n; i++) {
|
|
522
884
|
costs[i] = [];
|
|
523
885
|
predecessor[i] = [];
|
|
524
|
-
for (
|
|
886
|
+
for (var j = 0; j < n; j++) {
|
|
525
887
|
predecessor[i][j] = null;
|
|
526
888
|
}
|
|
527
889
|
}
|
|
528
|
-
for (
|
|
529
|
-
for (
|
|
890
|
+
for (var i = 0; i < n; i++) {
|
|
891
|
+
for (var j = 0; j < n; j++) {
|
|
530
892
|
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
|
|
531
893
|
}
|
|
532
894
|
}
|
|
533
|
-
for (
|
|
534
|
-
for (
|
|
535
|
-
for (
|
|
895
|
+
for (var k = 0; k < n; k++) {
|
|
896
|
+
for (var i = 0; i < n; i++) {
|
|
897
|
+
for (var j = 0; j < n; j++) {
|
|
536
898
|
if (costs[i][j] > costs[i][k] + costs[k][j]) {
|
|
537
899
|
costs[i][j] = costs[i][k] + costs[k][j];
|
|
538
900
|
predecessor[i][j] = idAndVertices[k][1];
|
|
@@ -540,8 +902,8 @@ class AbstractGraph {
|
|
|
540
902
|
}
|
|
541
903
|
}
|
|
542
904
|
}
|
|
543
|
-
return { costs, predecessor };
|
|
544
|
-
}
|
|
905
|
+
return { costs: costs, predecessor: predecessor };
|
|
906
|
+
};
|
|
545
907
|
/**--- start find cycles --- */
|
|
546
908
|
/**
|
|
547
909
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
@@ -549,12 +911,27 @@ class AbstractGraph {
|
|
|
549
911
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
550
912
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
551
913
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
914
|
+
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
915
|
+
* strongly connected components (SCCs), and cycles in a graph.
|
|
916
|
+
* @param {boolean} [needArticulationPoints] - A boolean value indicating whether or not to calculate and return the
|
|
917
|
+
* articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
|
|
918
|
+
* number of connected components in the graph.
|
|
919
|
+
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
920
|
+
* (edges whose removal would increase the number of connected components in the graph).
|
|
921
|
+
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
922
|
+
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
923
|
+
* SCCs will not be calculated or returned.
|
|
924
|
+
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
925
|
+
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
926
|
+
* are arrays of vertices that form cycles within the SCCs.
|
|
927
|
+
* @returns The function `tarjan` returns an object with the following properties:
|
|
552
928
|
*/
|
|
553
|
-
tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
929
|
+
AbstractGraph.prototype.tarjan = function (needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
554
930
|
// !! in undirected graph we will not let child visit parent when DFS
|
|
555
931
|
// !! articulation point(in DFS search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
556
932
|
// !! bridge: low(child) > dfn(cur)
|
|
557
|
-
|
|
933
|
+
var _this = this;
|
|
934
|
+
var defaultConfig = false;
|
|
558
935
|
if (needArticulationPoints === undefined)
|
|
559
936
|
needArticulationPoints = defaultConfig;
|
|
560
937
|
if (needBridges === undefined)
|
|
@@ -563,60 +940,71 @@ class AbstractGraph {
|
|
|
563
940
|
needSCCs = defaultConfig;
|
|
564
941
|
if (needCycles === undefined)
|
|
565
942
|
needCycles = defaultConfig;
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
vertices.forEach(v
|
|
943
|
+
var dfnMap = new Map();
|
|
944
|
+
var lowMap = new Map();
|
|
945
|
+
var vertices = this._vertices;
|
|
946
|
+
vertices.forEach(function (v) {
|
|
570
947
|
dfnMap.set(v, -1);
|
|
571
948
|
lowMap.set(v, Infinity);
|
|
572
949
|
});
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
950
|
+
var _a = __read(vertices.values(), 1), root = _a[0];
|
|
951
|
+
var articulationPoints = [];
|
|
952
|
+
var bridges = [];
|
|
953
|
+
var dfn = 0;
|
|
954
|
+
var dfs = function (cur, parent) {
|
|
955
|
+
var e_15, _a;
|
|
578
956
|
dfn++;
|
|
579
957
|
dfnMap.set(cur, dfn);
|
|
580
958
|
lowMap.set(cur, dfn);
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
const curLow = lowMap.get(cur);
|
|
591
|
-
// TODO after no-non-null-assertion not ensure the logic
|
|
592
|
-
if (curLow !== undefined && childLow !== undefined) {
|
|
593
|
-
lowMap.set(cur, Math.min(curLow, childLow));
|
|
594
|
-
}
|
|
595
|
-
const curFromMap = dfnMap.get(cur);
|
|
596
|
-
if (childLow !== undefined && curFromMap !== undefined) {
|
|
597
|
-
if (needArticulationPoints) {
|
|
598
|
-
if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
|
|
599
|
-
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
600
|
-
articulationPoints.push(cur);
|
|
601
|
-
}
|
|
959
|
+
var neighbors = _this.getNeighbors(cur);
|
|
960
|
+
var childCount = 0; // child in DFS tree not child in graph
|
|
961
|
+
try {
|
|
962
|
+
for (var neighbors_6 = __values(neighbors), neighbors_6_1 = neighbors_6.next(); !neighbors_6_1.done; neighbors_6_1 = neighbors_6.next()) {
|
|
963
|
+
var neighbor = neighbors_6_1.value;
|
|
964
|
+
if (neighbor !== parent) {
|
|
965
|
+
if (dfnMap.get(neighbor) === -1) {
|
|
966
|
+
childCount++;
|
|
967
|
+
dfs(neighbor, cur);
|
|
602
968
|
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
969
|
+
var childLow = lowMap.get(neighbor);
|
|
970
|
+
var curLow = lowMap.get(cur);
|
|
971
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
972
|
+
if (curLow !== undefined && childLow !== undefined) {
|
|
973
|
+
lowMap.set(cur, Math.min(curLow, childLow));
|
|
974
|
+
}
|
|
975
|
+
var curFromMap = dfnMap.get(cur);
|
|
976
|
+
if (childLow !== undefined && curFromMap !== undefined) {
|
|
977
|
+
if (needArticulationPoints) {
|
|
978
|
+
if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
|
|
979
|
+
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
980
|
+
articulationPoints.push(cur);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
if (needBridges) {
|
|
984
|
+
if (childLow > curFromMap) {
|
|
985
|
+
var edgeCurToNeighbor = _this.getEdge(cur, neighbor);
|
|
986
|
+
if (edgeCurToNeighbor) {
|
|
987
|
+
bridges.push(edgeCurToNeighbor);
|
|
988
|
+
}
|
|
608
989
|
}
|
|
609
990
|
}
|
|
610
991
|
}
|
|
611
992
|
}
|
|
612
993
|
}
|
|
613
994
|
}
|
|
995
|
+
catch (e_15_1) { e_15 = { error: e_15_1 }; }
|
|
996
|
+
finally {
|
|
997
|
+
try {
|
|
998
|
+
if (neighbors_6_1 && !neighbors_6_1.done && (_a = neighbors_6.return)) _a.call(neighbors_6);
|
|
999
|
+
}
|
|
1000
|
+
finally { if (e_15) throw e_15.error; }
|
|
1001
|
+
}
|
|
614
1002
|
};
|
|
615
1003
|
dfs(root, null);
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
lowMap.forEach((low, vertex)
|
|
1004
|
+
var SCCs = new Map();
|
|
1005
|
+
var getSCCs = function () {
|
|
1006
|
+
var SCCs = new Map();
|
|
1007
|
+
lowMap.forEach(function (low, vertex) {
|
|
620
1008
|
var _a;
|
|
621
1009
|
if (!SCCs.has(low)) {
|
|
622
1010
|
SCCs.set(low, [vertex]);
|
|
@@ -630,19 +1018,20 @@ class AbstractGraph {
|
|
|
630
1018
|
if (needSCCs) {
|
|
631
1019
|
SCCs = getSCCs();
|
|
632
1020
|
}
|
|
633
|
-
|
|
1021
|
+
var cycles = new Map();
|
|
634
1022
|
if (needCycles) {
|
|
635
|
-
|
|
636
|
-
if (
|
|
637
|
-
|
|
1023
|
+
var SCCs_1 = new Map();
|
|
1024
|
+
if (SCCs_1.size < 1) {
|
|
1025
|
+
SCCs_1 = getSCCs();
|
|
638
1026
|
}
|
|
639
|
-
|
|
1027
|
+
SCCs_1.forEach(function (SCC, low) {
|
|
640
1028
|
if (SCC.length > 1) {
|
|
641
1029
|
cycles.set(low, SCC);
|
|
642
1030
|
}
|
|
643
1031
|
});
|
|
644
1032
|
}
|
|
645
|
-
return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
|
|
646
|
-
}
|
|
647
|
-
|
|
1033
|
+
return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
|
|
1034
|
+
};
|
|
1035
|
+
return AbstractGraph;
|
|
1036
|
+
}());
|
|
648
1037
|
exports.AbstractGraph = AbstractGraph;
|