data-structure-typed 0.9.16 → 1.3.1
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/LICENSE +21 -0
- package/README.md +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +134 -23
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- 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/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +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/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -87
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/navigator.ts +0 -12
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -1,149 +1,189 @@
|
|
|
1
1
|
"use strict";
|
|
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; }
|
|
26
|
-
}
|
|
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
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
const utils_1 = require("../../utils");
|
|
12
|
+
const priority_queue_1 = require("../priority-queue");
|
|
13
|
+
class AbstractVertex {
|
|
14
|
+
/**
|
|
15
|
+
* The function is a protected constructor that takes an id and an optional value as parameters.
|
|
16
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
17
|
+
* used to uniquely identify the vertex object.
|
|
18
|
+
* @param {T} [val] - The parameter "val" is an optional parameter of type T. It is used to assign a value to the
|
|
19
|
+
* vertex. If no value is provided, it will be set to undefined.
|
|
20
|
+
*/
|
|
21
|
+
constructor(id, val) {
|
|
44
22
|
this._id = id;
|
|
23
|
+
this._val = val;
|
|
45
24
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
25
|
+
get id() {
|
|
26
|
+
return this._id;
|
|
27
|
+
}
|
|
28
|
+
set id(v) {
|
|
29
|
+
this._id = v;
|
|
30
|
+
}
|
|
31
|
+
get val() {
|
|
32
|
+
return this._val;
|
|
33
|
+
}
|
|
34
|
+
set val(value) {
|
|
35
|
+
this._val = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
58
38
|
exports.AbstractVertex = AbstractVertex;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
39
|
+
class AbstractEdge {
|
|
40
|
+
/**
|
|
41
|
+
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
42
|
+
* object.
|
|
43
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
44
|
+
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
45
|
+
* will be assigned.
|
|
46
|
+
* @param {T} [val] - The `val` parameter is of type `T`, which means it can be any type. It is an optional parameter,
|
|
47
|
+
* meaning it can be omitted when creating an instance of the class.
|
|
48
|
+
*/
|
|
49
|
+
constructor(weight, val) {
|
|
50
|
+
this._weight = weight !== undefined ? weight : 1;
|
|
51
|
+
this._val = val;
|
|
64
52
|
this._hashCode = (0, utils_1.uuidV4)();
|
|
65
53
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
54
|
+
get val() {
|
|
55
|
+
return this._val;
|
|
56
|
+
}
|
|
57
|
+
set val(value) {
|
|
58
|
+
this._val = value;
|
|
59
|
+
}
|
|
60
|
+
get weight() {
|
|
61
|
+
return this._weight;
|
|
62
|
+
}
|
|
63
|
+
set weight(v) {
|
|
64
|
+
this._weight = v;
|
|
65
|
+
}
|
|
66
|
+
get hashCode() {
|
|
67
|
+
return this._hashCode;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
71
|
+
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
72
|
+
*/
|
|
73
|
+
/**
|
|
74
|
+
* The function sets the value of the _hashCode property to the provided string.
|
|
75
|
+
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
76
|
+
* "_hashCode" property.
|
|
77
|
+
*/
|
|
78
|
+
_setHashCode(v) {
|
|
79
|
+
this._hashCode = v;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
89
82
|
exports.AbstractEdge = AbstractEdge;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
function AbstractGraph() {
|
|
83
|
+
class AbstractGraph {
|
|
84
|
+
constructor() {
|
|
93
85
|
this._vertices = new Map();
|
|
94
|
-
// unionFind() {
|
|
95
|
-
// }
|
|
96
|
-
/**--- end find cycles --- */
|
|
97
|
-
// Minimum Spanning Tree
|
|
98
86
|
}
|
|
99
|
-
|
|
100
|
-
var vertexId = this.getVertexId(vertexOrId);
|
|
101
|
-
return this._vertices.get(vertexId) || null;
|
|
102
|
-
};
|
|
103
|
-
AbstractGraph.prototype.getVertexId = function (vertexOrId) {
|
|
104
|
-
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
105
|
-
};
|
|
106
|
-
AbstractGraph.prototype.containsVertex = function (vertexOrId) {
|
|
107
|
-
return this._vertices.has(this.getVertexId(vertexOrId));
|
|
108
|
-
};
|
|
109
|
-
AbstractGraph.prototype.vertexSet = function () {
|
|
87
|
+
get vertices() {
|
|
110
88
|
return this._vertices;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
92
|
+
* @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
|
|
93
|
+
* the `_vertices` map.
|
|
94
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexId` if it exists in the `_vertices`
|
|
95
|
+
* map. If the vertex does not exist, it returns `null`.
|
|
96
|
+
*/
|
|
97
|
+
getVertex(vertexId) {
|
|
98
|
+
return this._vertices.get(vertexId) || null;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The function checks if a vertex exists in a graph.
|
|
102
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
103
|
+
* (`VertexId`).
|
|
104
|
+
* @returns a boolean value.
|
|
105
|
+
*/
|
|
106
|
+
hasVertex(vertexOrId) {
|
|
107
|
+
return this._vertices.has(this._getVertexId(vertexOrId));
|
|
108
|
+
}
|
|
109
|
+
addVertex(idOrVertex, val) {
|
|
110
|
+
if (idOrVertex instanceof AbstractVertex) {
|
|
111
|
+
return this._addVertexOnly(idOrVertex);
|
|
115
112
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
AbstractGraph.prototype.removeVertex = function (vertexOrId) {
|
|
120
|
-
var vertexId = this.getVertexId(vertexOrId);
|
|
121
|
-
return this._vertices.delete(vertexId);
|
|
122
|
-
};
|
|
123
|
-
AbstractGraph.prototype.removeAllVertices = function (vertices) {
|
|
124
|
-
var e_1, _a;
|
|
125
|
-
var removed = [];
|
|
126
|
-
try {
|
|
127
|
-
for (var vertices_1 = __values(vertices), vertices_1_1 = vertices_1.next(); !vertices_1_1.done; vertices_1_1 = vertices_1.next()) {
|
|
128
|
-
var v = vertices_1_1.value;
|
|
129
|
-
removed.push(this.removeVertex(v));
|
|
130
|
-
}
|
|
113
|
+
else {
|
|
114
|
+
const newVertex = this.createVertex(idOrVertex, val);
|
|
115
|
+
return this._addVertexOnly(newVertex);
|
|
131
116
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
120
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
121
|
+
* (`VertexId`).
|
|
122
|
+
* @returns The method is returning a boolean value.
|
|
123
|
+
*/
|
|
124
|
+
removeVertex(vertexOrId) {
|
|
125
|
+
const vertexId = this._getVertexId(vertexOrId);
|
|
126
|
+
return this._vertices.delete(vertexId);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
130
|
+
* @param {V[] | VertexId[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
|
|
131
|
+
* of vertex IDs (`VertexId[]`).
|
|
132
|
+
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
133
|
+
* were removed.
|
|
134
|
+
*/
|
|
135
|
+
removeAllVertices(vertices) {
|
|
136
|
+
const removed = [];
|
|
137
|
+
for (const v of vertices) {
|
|
138
|
+
removed.push(this.removeVertex(v));
|
|
138
139
|
}
|
|
139
140
|
return removed.length > 0;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
144
|
+
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the unique
|
|
145
|
+
* identifier of a vertex in a graph, while V represents the type of the vertex object itself.
|
|
146
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
147
|
+
* `VertexId` or a `V` type, which represents the type of the vertex.
|
|
148
|
+
* @returns A boolean value is being returned.
|
|
149
|
+
*/
|
|
150
|
+
hasEdge(v1, v2) {
|
|
151
|
+
const edge = this.getEdge(v1, v2);
|
|
143
152
|
return !!edge;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
153
|
+
}
|
|
154
|
+
addEdge(srcOrEdge, dest, weight, val) {
|
|
155
|
+
if (srcOrEdge instanceof AbstractEdge) {
|
|
156
|
+
return this._addEdgeOnly(srcOrEdge);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
160
|
+
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
|
|
161
|
+
return false;
|
|
162
|
+
if (srcOrEdge instanceof AbstractVertex)
|
|
163
|
+
srcOrEdge = srcOrEdge.id;
|
|
164
|
+
if (dest instanceof AbstractVertex)
|
|
165
|
+
dest = dest.id;
|
|
166
|
+
const newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
167
|
+
return this._addEdgeOnly(newEdge);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
throw new Error('dest must be a Vertex or vertex id while srcOrEdge is an Edge');
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The function sets the weight of an edge between two vertices in a graph.
|
|
176
|
+
* @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
|
|
177
|
+
* the source vertex of the edge.
|
|
178
|
+
* @param {VertexId | V} destOrId - The `destOrId` parameter represents the destination vertex of the edge. It can be
|
|
179
|
+
* either a `VertexId` or a vertex object `V`.
|
|
180
|
+
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrId)
|
|
181
|
+
* and the destination vertex (destOrId).
|
|
182
|
+
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
183
|
+
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
184
|
+
*/
|
|
185
|
+
setEdgeWeight(srcOrId, destOrId, weight) {
|
|
186
|
+
const edge = this.getEdge(srcOrId, destOrId);
|
|
147
187
|
if (edge) {
|
|
148
188
|
edge.weight = weight;
|
|
149
189
|
return true;
|
|
@@ -151,112 +191,102 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
151
191
|
else {
|
|
152
192
|
return false;
|
|
153
193
|
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
197
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
198
|
+
* It is the starting vertex for finding paths.
|
|
199
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
200
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
201
|
+
*/
|
|
202
|
+
getAllPathsBetween(v1, v2) {
|
|
203
|
+
const paths = [];
|
|
204
|
+
const vertex1 = this._getVertex(v1);
|
|
205
|
+
const vertex2 = this._getVertex(v2);
|
|
160
206
|
if (!(vertex1 && vertex2)) {
|
|
161
207
|
return [];
|
|
162
208
|
}
|
|
163
|
-
|
|
164
|
-
var e_2, _a;
|
|
209
|
+
const dfs = (cur, dest, visiting, path) => {
|
|
165
210
|
visiting.set(cur, true);
|
|
166
211
|
if (cur === dest) {
|
|
167
|
-
paths.push(
|
|
212
|
+
paths.push([vertex1, ...path]);
|
|
168
213
|
}
|
|
169
|
-
|
|
170
|
-
|
|
214
|
+
const neighbors = this.getNeighbors(cur);
|
|
215
|
+
for (const neighbor of neighbors) {
|
|
171
216
|
if (!visiting.get(neighbor)) {
|
|
172
217
|
path.push(neighbor);
|
|
173
218
|
dfs(neighbor, dest, visiting, path);
|
|
174
|
-
(0, utils_1.arrayRemove)(path,
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
try {
|
|
178
|
-
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
|
|
179
|
-
var neighbor = neighbors_1_1.value;
|
|
180
|
-
_loop_1(neighbor);
|
|
219
|
+
(0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
|
|
181
220
|
}
|
|
182
221
|
}
|
|
183
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
184
|
-
finally {
|
|
185
|
-
try {
|
|
186
|
-
if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
|
|
187
|
-
}
|
|
188
|
-
finally { if (e_2) throw e_2.error; }
|
|
189
|
-
}
|
|
190
222
|
visiting.set(cur, false);
|
|
191
223
|
};
|
|
192
224
|
dfs(vertex1, vertex2, new Map(), []);
|
|
193
225
|
return paths;
|
|
194
|
-
}
|
|
195
|
-
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The function calculates the sum of weights along a given path.
|
|
229
|
+
* @param {V[]} path - An array of vertices (V) representing a path in a graph.
|
|
230
|
+
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
231
|
+
*/
|
|
232
|
+
getPathSumWeight(path) {
|
|
196
233
|
var _a;
|
|
197
|
-
|
|
198
|
-
for (
|
|
234
|
+
let sum = 0;
|
|
235
|
+
for (let i = 0; i < path.length; i++) {
|
|
199
236
|
sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
|
|
200
237
|
}
|
|
201
238
|
return sum;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
242
|
+
* weights or using a breadth-first search algorithm.
|
|
243
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
244
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
245
|
+
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
246
|
+
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
247
|
+
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
248
|
+
* the edges. If isWeight is set to false or not provided, the function will calculate the
|
|
249
|
+
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
|
|
250
|
+
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
251
|
+
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
252
|
+
* minimum number of
|
|
253
|
+
*/
|
|
254
|
+
getMinCostBetween(v1, v2, isWeight) {
|
|
205
255
|
if (isWeight === undefined)
|
|
206
256
|
isWeight = false;
|
|
207
257
|
if (isWeight) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
var path = allPaths_1_1.value;
|
|
213
|
-
min = Math.min(this.getPathSumWeight(path), min);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
217
|
-
finally {
|
|
218
|
-
try {
|
|
219
|
-
if (allPaths_1_1 && !allPaths_1_1.done && (_a = allPaths_1.return)) _a.call(allPaths_1);
|
|
220
|
-
}
|
|
221
|
-
finally { if (e_3) throw e_3.error; }
|
|
258
|
+
const allPaths = this.getAllPathsBetween(v1, v2);
|
|
259
|
+
let min = Infinity;
|
|
260
|
+
for (const path of allPaths) {
|
|
261
|
+
min = Math.min(this.getPathSumWeight(path), min);
|
|
222
262
|
}
|
|
223
263
|
return min;
|
|
224
264
|
}
|
|
225
265
|
else {
|
|
226
266
|
// BFS
|
|
227
|
-
|
|
228
|
-
|
|
267
|
+
const vertex2 = this._getVertex(v2);
|
|
268
|
+
const vertex1 = this._getVertex(v1);
|
|
229
269
|
if (!(vertex1 && vertex2)) {
|
|
230
270
|
return null;
|
|
231
271
|
}
|
|
232
|
-
|
|
233
|
-
|
|
272
|
+
const visited = new Map();
|
|
273
|
+
const queue = [vertex1];
|
|
234
274
|
visited.set(vertex1, true);
|
|
235
|
-
|
|
275
|
+
let cost = 0;
|
|
236
276
|
while (queue.length > 0) {
|
|
237
|
-
for (
|
|
238
|
-
|
|
277
|
+
for (let i = 0; i < queue.length; i++) {
|
|
278
|
+
const cur = queue.shift();
|
|
239
279
|
if (cur === vertex2) {
|
|
240
280
|
return cost;
|
|
241
281
|
}
|
|
242
282
|
// TODO consider optimizing to AbstractGraph
|
|
243
283
|
if (cur !== undefined) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
visited.set(neighbor, true);
|
|
250
|
-
queue.push(neighbor);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
255
|
-
finally {
|
|
256
|
-
try {
|
|
257
|
-
if (neighbors_2_1 && !neighbors_2_1.done && (_b = neighbors_2.return)) _b.call(neighbors_2);
|
|
284
|
+
const neighbors = this.getNeighbors(cur);
|
|
285
|
+
for (const neighbor of neighbors) {
|
|
286
|
+
if (!visited.has(neighbor)) {
|
|
287
|
+
visited.set(neighbor, true);
|
|
288
|
+
queue.push(neighbor);
|
|
258
289
|
}
|
|
259
|
-
finally { if (e_4) throw e_4.error; }
|
|
260
290
|
}
|
|
261
291
|
}
|
|
262
292
|
}
|
|
@@ -264,178 +294,146 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
264
294
|
}
|
|
265
295
|
return null;
|
|
266
296
|
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
300
|
+
* using a breadth-first search algorithm.
|
|
301
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
302
|
+
* object (`V`) or a vertex ID (`VertexId`).
|
|
303
|
+
* @param {V | VertexId} v2 - V | VertexId - The second vertex or vertex ID between which we want to find the minimum
|
|
304
|
+
* path.
|
|
305
|
+
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
306
|
+
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
307
|
+
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
308
|
+
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
309
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
310
|
+
*/
|
|
311
|
+
getMinPathBetween(v1, v2, isWeight) {
|
|
271
312
|
if (isWeight === undefined)
|
|
272
313
|
isWeight = false;
|
|
273
314
|
if (isWeight) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
index++;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
290
|
-
finally {
|
|
291
|
-
try {
|
|
292
|
-
if (allPaths_2_1 && !allPaths_2_1.done && (_a = allPaths_2.return)) _a.call(allPaths_2);
|
|
293
|
-
}
|
|
294
|
-
finally { if (e_5) throw e_5.error; }
|
|
315
|
+
const allPaths = this.getAllPathsBetween(v1, v2);
|
|
316
|
+
let min = Infinity;
|
|
317
|
+
let minIndex = -1;
|
|
318
|
+
let index = 0;
|
|
319
|
+
for (const path of allPaths) {
|
|
320
|
+
const pathSumWeight = this.getPathSumWeight(path);
|
|
321
|
+
if (pathSumWeight < min) {
|
|
322
|
+
min = pathSumWeight;
|
|
323
|
+
minIndex = index;
|
|
324
|
+
}
|
|
325
|
+
index++;
|
|
295
326
|
}
|
|
296
327
|
return allPaths[minIndex] || null;
|
|
297
328
|
}
|
|
298
329
|
else {
|
|
299
330
|
// BFS
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (!(
|
|
331
|
+
let minPath = [];
|
|
332
|
+
const vertex1 = this._getVertex(v1);
|
|
333
|
+
const vertex2 = this._getVertex(v2);
|
|
334
|
+
if (!(vertex1 && vertex2)) {
|
|
304
335
|
return [];
|
|
305
336
|
}
|
|
306
|
-
|
|
307
|
-
var e_6, _a;
|
|
337
|
+
const dfs = (cur, dest, visiting, path) => {
|
|
308
338
|
visiting.set(cur, true);
|
|
309
339
|
if (cur === dest) {
|
|
310
|
-
|
|
340
|
+
minPath = [vertex1, ...path];
|
|
311
341
|
return;
|
|
312
342
|
}
|
|
313
|
-
|
|
314
|
-
|
|
343
|
+
const neighbors = this.getNeighbors(cur);
|
|
344
|
+
for (const neighbor of neighbors) {
|
|
315
345
|
if (!visiting.get(neighbor)) {
|
|
316
346
|
path.push(neighbor);
|
|
317
|
-
|
|
318
|
-
(0, utils_1.arrayRemove)(path,
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
try {
|
|
322
|
-
for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
|
|
323
|
-
var neighbor = neighbors_3_1.value;
|
|
324
|
-
_loop_2(neighbor);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
328
|
-
finally {
|
|
329
|
-
try {
|
|
330
|
-
if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3);
|
|
347
|
+
dfs(neighbor, dest, visiting, path);
|
|
348
|
+
(0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
|
|
331
349
|
}
|
|
332
|
-
finally { if (e_6) throw e_6.error; }
|
|
333
350
|
}
|
|
334
351
|
visiting.set(cur, false);
|
|
335
352
|
};
|
|
336
|
-
|
|
337
|
-
return
|
|
353
|
+
dfs(vertex1, vertex2, new Map(), []);
|
|
354
|
+
return minPath;
|
|
338
355
|
}
|
|
339
|
-
}
|
|
356
|
+
}
|
|
340
357
|
/**
|
|
341
358
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
342
|
-
*
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
*
|
|
359
|
+
* /
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
363
|
+
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
364
|
+
* a graph without using a heap data structure.
|
|
365
|
+
* @param {V | VertexId} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
366
|
+
* vertex object or a vertex ID.
|
|
367
|
+
* @param {V | VertexId | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
368
|
+
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
369
|
+
* identifier. If no destination is provided, the value is set to `null`.
|
|
370
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
371
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
372
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
373
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
374
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
375
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
376
|
+
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
|
|
346
377
|
*/
|
|
347
|
-
|
|
348
|
-
var e_7, _a, e_8, _b;
|
|
378
|
+
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
349
379
|
if (getMinDist === undefined)
|
|
350
380
|
getMinDist = false;
|
|
351
381
|
if (genPaths === undefined)
|
|
352
382
|
genPaths = false;
|
|
353
383
|
if (dest === undefined)
|
|
354
384
|
dest = null;
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
385
|
+
let minDist = Infinity;
|
|
386
|
+
let minDest = null;
|
|
387
|
+
let minPath = [];
|
|
388
|
+
const paths = [];
|
|
389
|
+
const vertices = this._vertices;
|
|
390
|
+
const distMap = new Map();
|
|
391
|
+
const seen = new Set();
|
|
392
|
+
const preMap = new Map(); // predecessor
|
|
393
|
+
const srcVertex = this._getVertex(src);
|
|
394
|
+
const destVertex = dest ? this._getVertex(dest) : null;
|
|
365
395
|
if (!srcVertex) {
|
|
366
396
|
return null;
|
|
367
397
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
if (vertexOrId instanceof AbstractVertex)
|
|
373
|
-
distMap.set(vertexOrId, Infinity);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
377
|
-
finally {
|
|
378
|
-
try {
|
|
379
|
-
if (vertices_2_1 && !vertices_2_1.done && (_a = vertices_2.return)) _a.call(vertices_2);
|
|
380
|
-
}
|
|
381
|
-
finally { if (e_7) throw e_7.error; }
|
|
398
|
+
for (const vertex of vertices) {
|
|
399
|
+
const vertexOrId = vertex[1];
|
|
400
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
401
|
+
distMap.set(vertexOrId, Infinity);
|
|
382
402
|
}
|
|
383
403
|
distMap.set(srcVertex, 0);
|
|
384
404
|
preMap.set(srcVertex, null);
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (val < min) {
|
|
394
|
-
min = val;
|
|
395
|
-
minV = key;
|
|
396
|
-
}
|
|
405
|
+
const getMinOfNoSeen = () => {
|
|
406
|
+
let min = Infinity;
|
|
407
|
+
let minV = null;
|
|
408
|
+
for (const [key, val] of distMap) {
|
|
409
|
+
if (!seen.has(key)) {
|
|
410
|
+
if (val < min) {
|
|
411
|
+
min = val;
|
|
412
|
+
minV = key;
|
|
397
413
|
}
|
|
398
414
|
}
|
|
399
415
|
}
|
|
400
|
-
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
401
|
-
finally {
|
|
402
|
-
try {
|
|
403
|
-
if (distMap_1_1 && !distMap_1_1.done && (_a = distMap_1.return)) _a.call(distMap_1);
|
|
404
|
-
}
|
|
405
|
-
finally { if (e_9) throw e_9.error; }
|
|
406
|
-
}
|
|
407
416
|
return minV;
|
|
408
417
|
};
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
while (parent) {
|
|
419
|
-
path.push(parent);
|
|
420
|
-
parent = preMap.get(parent);
|
|
421
|
-
}
|
|
422
|
-
var reversed = path.reverse();
|
|
423
|
-
if (vertex[1] === minV)
|
|
424
|
-
minPath = reversed;
|
|
425
|
-
paths.push(reversed);
|
|
418
|
+
const getPaths = (minV) => {
|
|
419
|
+
for (const vertex of vertices) {
|
|
420
|
+
const vertexOrId = vertex[1];
|
|
421
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
422
|
+
const path = [vertexOrId];
|
|
423
|
+
let parent = preMap.get(vertexOrId);
|
|
424
|
+
while (parent) {
|
|
425
|
+
path.push(parent);
|
|
426
|
+
parent = preMap.get(parent);
|
|
426
427
|
}
|
|
428
|
+
const reversed = path.reverse();
|
|
429
|
+
if (vertex[1] === minV)
|
|
430
|
+
minPath = reversed;
|
|
431
|
+
paths.push(reversed);
|
|
427
432
|
}
|
|
428
433
|
}
|
|
429
|
-
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
430
|
-
finally {
|
|
431
|
-
try {
|
|
432
|
-
if (vertices_3_1 && !vertices_3_1.done && (_a = vertices_3.return)) _a.call(vertices_3);
|
|
433
|
-
}
|
|
434
|
-
finally { if (e_10) throw e_10.error; }
|
|
435
|
-
}
|
|
436
434
|
};
|
|
437
|
-
for (
|
|
438
|
-
|
|
435
|
+
for (let i = 1; i < vertices.size; i++) {
|
|
436
|
+
const cur = getMinOfNoSeen();
|
|
439
437
|
if (cur) {
|
|
440
438
|
seen.add(cur);
|
|
441
439
|
if (destVertex && destVertex === cur) {
|
|
@@ -445,38 +443,28 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
445
443
|
if (genPaths) {
|
|
446
444
|
getPaths(destVertex);
|
|
447
445
|
}
|
|
448
|
-
return { distMap
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
if (
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
distMap.set(neighbor, edge.weight + curFromMap);
|
|
463
|
-
preMap.set(neighbor, cur);
|
|
464
|
-
}
|
|
446
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
447
|
+
}
|
|
448
|
+
const neighbors = this.getNeighbors(cur);
|
|
449
|
+
for (const neighbor of neighbors) {
|
|
450
|
+
if (!seen.has(neighbor)) {
|
|
451
|
+
const edge = this.getEdge(cur, neighbor);
|
|
452
|
+
if (edge) {
|
|
453
|
+
const curFromMap = distMap.get(cur);
|
|
454
|
+
const neighborFromMap = distMap.get(neighbor);
|
|
455
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
456
|
+
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
457
|
+
if (edge.weight + curFromMap < neighborFromMap) {
|
|
458
|
+
distMap.set(neighbor, edge.weight + curFromMap);
|
|
459
|
+
preMap.set(neighbor, cur);
|
|
465
460
|
}
|
|
466
461
|
}
|
|
467
462
|
}
|
|
468
463
|
}
|
|
469
464
|
}
|
|
470
|
-
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
471
|
-
finally {
|
|
472
|
-
try {
|
|
473
|
-
if (neighbors_4_1 && !neighbors_4_1.done && (_b = neighbors_4.return)) _b.call(neighbors_4);
|
|
474
|
-
}
|
|
475
|
-
finally { if (e_8) throw e_8.error; }
|
|
476
|
-
}
|
|
477
465
|
}
|
|
478
466
|
}
|
|
479
|
-
getMinDist && distMap.forEach(
|
|
467
|
+
getMinDist && distMap.forEach((d, v) => {
|
|
480
468
|
if (v !== srcVertex) {
|
|
481
469
|
if (d < minDist) {
|
|
482
470
|
minDist = d;
|
|
@@ -486,88 +474,89 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
486
474
|
}
|
|
487
475
|
});
|
|
488
476
|
genPaths && getPaths(minDest);
|
|
489
|
-
return { distMap
|
|
490
|
-
}
|
|
477
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
478
|
+
}
|
|
491
479
|
/**
|
|
492
480
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
481
|
+
*
|
|
482
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
483
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
484
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
485
|
+
*
|
|
486
|
+
* /
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
490
|
+
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
491
|
+
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
492
|
+
* @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
493
|
+
* start. It can be either a vertex object or a vertex ID.
|
|
494
|
+
* @param {V | VertexId | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
495
|
+
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
496
|
+
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
497
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
498
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
499
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
500
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
501
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
502
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
503
|
+
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
497
504
|
*/
|
|
498
|
-
|
|
499
|
-
var
|
|
500
|
-
var _c;
|
|
505
|
+
dijkstra(src, dest, getMinDist, genPaths) {
|
|
506
|
+
var _a;
|
|
501
507
|
if (getMinDist === undefined)
|
|
502
508
|
getMinDist = false;
|
|
503
509
|
if (genPaths === undefined)
|
|
504
510
|
genPaths = false;
|
|
505
511
|
if (dest === undefined)
|
|
506
512
|
dest = null;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
if (!srcVertex)
|
|
513
|
+
let minDist = Infinity;
|
|
514
|
+
let minDest = null;
|
|
515
|
+
let minPath = [];
|
|
516
|
+
const paths = [];
|
|
517
|
+
const vertices = this._vertices;
|
|
518
|
+
const distMap = new Map();
|
|
519
|
+
const seen = new Set();
|
|
520
|
+
const preMap = new Map(); // predecessor
|
|
521
|
+
const srcVertex = this._getVertex(src);
|
|
522
|
+
const destVertex = dest ? this._getVertex(dest) : null;
|
|
523
|
+
if (!srcVertex)
|
|
518
524
|
return null;
|
|
525
|
+
for (const vertex of vertices) {
|
|
526
|
+
const vertexOrId = vertex[1];
|
|
527
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
528
|
+
distMap.set(vertexOrId, Infinity);
|
|
519
529
|
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
var vertex = vertices_4_1.value;
|
|
523
|
-
var vertexOrId = vertex[1];
|
|
524
|
-
if (vertexOrId instanceof AbstractVertex)
|
|
525
|
-
distMap.set(vertexOrId, Infinity);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
529
|
-
finally {
|
|
530
|
-
try {
|
|
531
|
-
if (vertices_4_1 && !vertices_4_1.done && (_a = vertices_4.return)) _a.call(vertices_4);
|
|
532
|
-
}
|
|
533
|
-
finally { if (e_11) throw e_11.error; }
|
|
534
|
-
}
|
|
535
|
-
var heap = new priority_queue_1.PriorityQueue({ comparator: function (a, b) { return a.id - b.id; } });
|
|
536
|
-
heap.offer({ id: 0, val: srcVertex });
|
|
530
|
+
const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.id - b.id });
|
|
531
|
+
heap.add({ id: 0, val: srcVertex });
|
|
537
532
|
distMap.set(srcVertex, 0);
|
|
538
533
|
preMap.set(srcVertex, null);
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
if (vertex[1] === minV)
|
|
554
|
-
minPath = reversed;
|
|
555
|
-
paths.push(reversed);
|
|
534
|
+
/**
|
|
535
|
+
* The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
|
|
536
|
+
* @param {V | null} minV - The parameter `minV` is of type `V | null`. It represents the minimum vertex value or
|
|
537
|
+
* null.
|
|
538
|
+
*/
|
|
539
|
+
const getPaths = (minV) => {
|
|
540
|
+
for (const vertex of vertices) {
|
|
541
|
+
const vertexOrId = vertex[1];
|
|
542
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
543
|
+
const path = [vertexOrId];
|
|
544
|
+
let parent = preMap.get(vertexOrId);
|
|
545
|
+
while (parent) {
|
|
546
|
+
path.push(parent);
|
|
547
|
+
parent = preMap.get(parent);
|
|
556
548
|
}
|
|
549
|
+
const reversed = path.reverse();
|
|
550
|
+
if (vertex[1] === minV)
|
|
551
|
+
minPath = reversed;
|
|
552
|
+
paths.push(reversed);
|
|
557
553
|
}
|
|
558
554
|
}
|
|
559
|
-
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
560
|
-
finally {
|
|
561
|
-
try {
|
|
562
|
-
if (vertices_5_1 && !vertices_5_1.done && (_a = vertices_5.return)) _a.call(vertices_5);
|
|
563
|
-
}
|
|
564
|
-
finally { if (e_13) throw e_13.error; }
|
|
565
|
-
}
|
|
566
555
|
};
|
|
567
556
|
while (heap.size > 0) {
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
557
|
+
const curHeapNode = heap.poll();
|
|
558
|
+
const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
|
|
559
|
+
const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
|
|
571
560
|
if (dist !== undefined) {
|
|
572
561
|
if (cur) {
|
|
573
562
|
seen.add(cur);
|
|
@@ -578,39 +567,29 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
578
567
|
if (genPaths) {
|
|
579
568
|
getPaths(destVertex);
|
|
580
569
|
}
|
|
581
|
-
return { distMap
|
|
570
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
582
571
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
if (
|
|
588
|
-
|
|
589
|
-
if (
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
preMap.set(neighbor, cur);
|
|
595
|
-
distMap.set(neighbor, dist + weight);
|
|
596
|
-
}
|
|
572
|
+
const neighbors = this.getNeighbors(cur);
|
|
573
|
+
for (const neighbor of neighbors) {
|
|
574
|
+
if (!seen.has(neighbor)) {
|
|
575
|
+
const weight = (_a = this.getEdge(cur, neighbor)) === null || _a === void 0 ? void 0 : _a.weight;
|
|
576
|
+
if (typeof weight === 'number') {
|
|
577
|
+
const distSrcToNeighbor = distMap.get(neighbor);
|
|
578
|
+
if (distSrcToNeighbor) {
|
|
579
|
+
if (dist + weight < distSrcToNeighbor) {
|
|
580
|
+
heap.add({ id: dist + weight, val: neighbor });
|
|
581
|
+
preMap.set(neighbor, cur);
|
|
582
|
+
distMap.set(neighbor, dist + weight);
|
|
597
583
|
}
|
|
598
584
|
}
|
|
599
585
|
}
|
|
600
586
|
}
|
|
601
587
|
}
|
|
602
|
-
catch (e_12_1) { e_12 = { error: e_12_1 }; }
|
|
603
|
-
finally {
|
|
604
|
-
try {
|
|
605
|
-
if (neighbors_5_1 && !neighbors_5_1.done && (_b = neighbors_5.return)) _b.call(neighbors_5);
|
|
606
|
-
}
|
|
607
|
-
finally { if (e_12) throw e_12.error; }
|
|
608
|
-
}
|
|
609
588
|
}
|
|
610
589
|
}
|
|
611
590
|
}
|
|
612
591
|
if (getMinDist) {
|
|
613
|
-
distMap.forEach(
|
|
592
|
+
distMap.forEach((d, v) => {
|
|
614
593
|
if (v !== srcVertex) {
|
|
615
594
|
if (d < minDist) {
|
|
616
595
|
minDist = d;
|
|
@@ -623,50 +602,62 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
623
602
|
if (genPaths) {
|
|
624
603
|
getPaths(minDest);
|
|
625
604
|
}
|
|
626
|
-
return { distMap
|
|
627
|
-
}
|
|
605
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
606
|
+
}
|
|
628
607
|
/**
|
|
629
608
|
* BellmanFord time:O(VE) space:O(V)
|
|
630
609
|
* one to rest pairs
|
|
631
|
-
*
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
*
|
|
610
|
+
* /
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
614
|
+
* one to rest pairs
|
|
615
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
616
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
617
|
+
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
618
|
+
* @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
619
|
+
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
620
|
+
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
621
|
+
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
622
|
+
* calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
|
|
623
|
+
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
624
|
+
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
|
|
625
|
+
* vertex.
|
|
626
|
+
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
635
627
|
*/
|
|
636
|
-
|
|
637
|
-
var e_14, _a;
|
|
628
|
+
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
638
629
|
if (getMin === undefined)
|
|
639
630
|
getMin = false;
|
|
640
631
|
if (genPath === undefined)
|
|
641
632
|
genPath = false;
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
633
|
+
const srcVertex = this._getVertex(src);
|
|
634
|
+
const paths = [];
|
|
635
|
+
const distMap = new Map();
|
|
636
|
+
const preMap = new Map(); // predecessor
|
|
637
|
+
let min = Infinity;
|
|
638
|
+
let minPath = [];
|
|
648
639
|
// TODO
|
|
649
|
-
|
|
640
|
+
let hasNegativeCycle;
|
|
650
641
|
if (scanNegativeCycle)
|
|
651
642
|
hasNegativeCycle = false;
|
|
652
643
|
if (!srcVertex)
|
|
653
|
-
return { hasNegativeCycle
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
this._vertices.forEach(
|
|
644
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
645
|
+
const vertices = this._vertices;
|
|
646
|
+
const numOfVertices = vertices.size;
|
|
647
|
+
const edges = this.edgeSet();
|
|
648
|
+
const numOfEdges = edges.length;
|
|
649
|
+
this._vertices.forEach(vertex => {
|
|
659
650
|
distMap.set(vertex, Infinity);
|
|
660
651
|
});
|
|
661
652
|
distMap.set(srcVertex, 0);
|
|
662
|
-
for (
|
|
663
|
-
for (
|
|
664
|
-
|
|
653
|
+
for (let i = 1; i < numOfVertices; ++i) {
|
|
654
|
+
for (let j = 0; j < numOfEdges; ++j) {
|
|
655
|
+
const ends = this.getEndsOfEdge(edges[j]);
|
|
665
656
|
if (ends) {
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
657
|
+
const [s, d] = ends;
|
|
658
|
+
const weight = edges[j].weight;
|
|
659
|
+
const sWeight = distMap.get(s);
|
|
660
|
+
const dWeight = distMap.get(d);
|
|
670
661
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
671
662
|
if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
|
|
672
663
|
distMap.set(d, sWeight + weight);
|
|
@@ -676,9 +667,9 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
676
667
|
}
|
|
677
668
|
}
|
|
678
669
|
}
|
|
679
|
-
|
|
670
|
+
let minDest = null;
|
|
680
671
|
if (getMin) {
|
|
681
|
-
distMap.forEach(
|
|
672
|
+
distMap.forEach((d, v) => {
|
|
682
673
|
if (v !== srcVertex) {
|
|
683
674
|
if (d < min) {
|
|
684
675
|
min = d;
|
|
@@ -689,72 +680,93 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
689
680
|
});
|
|
690
681
|
}
|
|
691
682
|
if (genPath) {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
path.push(parent);
|
|
701
|
-
parent = preMap.get(parent);
|
|
702
|
-
}
|
|
703
|
-
var reversed = path.reverse();
|
|
704
|
-
if (vertex[1] === minDest)
|
|
705
|
-
minPath = reversed;
|
|
706
|
-
paths.push(reversed);
|
|
683
|
+
for (const vertex of vertices) {
|
|
684
|
+
const vertexOrId = vertex[1];
|
|
685
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
686
|
+
const path = [vertexOrId];
|
|
687
|
+
let parent = preMap.get(vertexOrId);
|
|
688
|
+
while (parent !== undefined) {
|
|
689
|
+
path.push(parent);
|
|
690
|
+
parent = preMap.get(parent);
|
|
707
691
|
}
|
|
692
|
+
const reversed = path.reverse();
|
|
693
|
+
if (vertex[1] === minDest)
|
|
694
|
+
minPath = reversed;
|
|
695
|
+
paths.push(reversed);
|
|
708
696
|
}
|
|
709
697
|
}
|
|
710
|
-
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
711
|
-
finally {
|
|
712
|
-
try {
|
|
713
|
-
if (vertices_6_1 && !vertices_6_1.done && (_a = vertices_6.return)) _a.call(vertices_6);
|
|
714
|
-
}
|
|
715
|
-
finally { if (e_14) throw e_14.error; }
|
|
716
|
-
}
|
|
717
698
|
}
|
|
718
|
-
for (
|
|
719
|
-
|
|
699
|
+
for (let j = 0; j < numOfEdges; ++j) {
|
|
700
|
+
const ends = this.getEndsOfEdge(edges[j]);
|
|
720
701
|
if (ends) {
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
702
|
+
const [s] = ends;
|
|
703
|
+
const weight = edges[j].weight;
|
|
704
|
+
const sWeight = distMap.get(s);
|
|
724
705
|
if (sWeight) {
|
|
725
706
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
726
707
|
hasNegativeCycle = true;
|
|
727
708
|
}
|
|
728
709
|
}
|
|
729
710
|
}
|
|
730
|
-
return { hasNegativeCycle
|
|
731
|
-
}
|
|
711
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
715
|
+
* /
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
719
|
+
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
720
|
+
*/
|
|
721
|
+
/**
|
|
722
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
723
|
+
* one to rest pairs
|
|
724
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
725
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
726
|
+
*/
|
|
732
727
|
/**
|
|
733
728
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
734
729
|
* all pairs
|
|
730
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
735
731
|
*/
|
|
736
|
-
|
|
732
|
+
/**
|
|
733
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
734
|
+
* all pairs
|
|
735
|
+
* /
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
739
|
+
* all pairs
|
|
740
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
741
|
+
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
742
|
+
* graph.
|
|
743
|
+
* @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
744
|
+
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
745
|
+
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
746
|
+
* path between vertices in the
|
|
747
|
+
*/
|
|
748
|
+
floyd() {
|
|
737
749
|
var _a;
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
750
|
+
const idAndVertices = [...this._vertices];
|
|
751
|
+
const n = idAndVertices.length;
|
|
752
|
+
const costs = [];
|
|
753
|
+
const predecessor = [];
|
|
742
754
|
// successors
|
|
743
|
-
for (
|
|
755
|
+
for (let i = 0; i < n; i++) {
|
|
744
756
|
costs[i] = [];
|
|
745
757
|
predecessor[i] = [];
|
|
746
|
-
for (
|
|
758
|
+
for (let j = 0; j < n; j++) {
|
|
747
759
|
predecessor[i][j] = null;
|
|
748
760
|
}
|
|
749
761
|
}
|
|
750
|
-
for (
|
|
751
|
-
for (
|
|
762
|
+
for (let i = 0; i < n; i++) {
|
|
763
|
+
for (let j = 0; j < n; j++) {
|
|
752
764
|
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
|
|
753
765
|
}
|
|
754
766
|
}
|
|
755
|
-
for (
|
|
756
|
-
for (
|
|
757
|
-
for (
|
|
767
|
+
for (let k = 0; k < n; k++) {
|
|
768
|
+
for (let i = 0; i < n; i++) {
|
|
769
|
+
for (let j = 0; j < n; j++) {
|
|
758
770
|
if (costs[i][j] > costs[i][k] + costs[k][j]) {
|
|
759
771
|
costs[i][j] = costs[i][k] + costs[k][j];
|
|
760
772
|
predecessor[i][j] = idAndVertices[k][1];
|
|
@@ -762,22 +774,42 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
762
774
|
}
|
|
763
775
|
}
|
|
764
776
|
}
|
|
765
|
-
return { costs
|
|
766
|
-
}
|
|
767
|
-
/**--- start find cycles --- */
|
|
777
|
+
return { costs, predecessor };
|
|
778
|
+
}
|
|
768
779
|
/**
|
|
769
780
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
770
781
|
* Tarjan can find cycles in directed or undirected graph
|
|
771
782
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
772
783
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
773
784
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
785
|
+
* /
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
789
|
+
* Tarjan can find cycles in directed or undirected graph
|
|
790
|
+
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
791
|
+
* Tarjan solve the bi-connected components of undirected graphs;
|
|
792
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
793
|
+
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
794
|
+
* strongly connected components (SCCs), and cycles in a graph.
|
|
795
|
+
* @param {boolean} [needArticulationPoints] - A boolean value indicating whether or not to calculate and return the
|
|
796
|
+
* articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
|
|
797
|
+
* number of connected components in the graph.
|
|
798
|
+
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
799
|
+
* (edges whose removal would increase the number of connected components in the graph).
|
|
800
|
+
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
801
|
+
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
802
|
+
* SCCs will not be calculated or returned.
|
|
803
|
+
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
804
|
+
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
805
|
+
* are arrays of vertices that form cycles within the SCCs.
|
|
806
|
+
* @returns The function `tarjan` returns an object with the following properties:
|
|
774
807
|
*/
|
|
775
|
-
|
|
808
|
+
tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
776
809
|
// !! in undirected graph we will not let child visit parent when DFS
|
|
777
810
|
// !! articulation point(in DFS search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
778
811
|
// !! bridge: low(child) > dfn(cur)
|
|
779
|
-
|
|
780
|
-
var defaultConfig = false;
|
|
812
|
+
const defaultConfig = false;
|
|
781
813
|
if (needArticulationPoints === undefined)
|
|
782
814
|
needArticulationPoints = defaultConfig;
|
|
783
815
|
if (needBridges === undefined)
|
|
@@ -786,71 +818,60 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
786
818
|
needSCCs = defaultConfig;
|
|
787
819
|
if (needCycles === undefined)
|
|
788
820
|
needCycles = defaultConfig;
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
vertices.forEach(
|
|
821
|
+
const dfnMap = new Map();
|
|
822
|
+
const lowMap = new Map();
|
|
823
|
+
const vertices = this._vertices;
|
|
824
|
+
vertices.forEach(v => {
|
|
793
825
|
dfnMap.set(v, -1);
|
|
794
826
|
lowMap.set(v, Infinity);
|
|
795
827
|
});
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
var e_15, _a;
|
|
828
|
+
const [root] = vertices.values();
|
|
829
|
+
const articulationPoints = [];
|
|
830
|
+
const bridges = [];
|
|
831
|
+
let dfn = 0;
|
|
832
|
+
const dfs = (cur, parent) => {
|
|
802
833
|
dfn++;
|
|
803
834
|
dfnMap.set(cur, dfn);
|
|
804
835
|
lowMap.set(cur, dfn);
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
826
|
-
articulationPoints.push(cur);
|
|
827
|
-
}
|
|
836
|
+
const neighbors = this.getNeighbors(cur);
|
|
837
|
+
let childCount = 0; // child in DFS tree not child in graph
|
|
838
|
+
for (const neighbor of neighbors) {
|
|
839
|
+
if (neighbor !== parent) {
|
|
840
|
+
if (dfnMap.get(neighbor) === -1) {
|
|
841
|
+
childCount++;
|
|
842
|
+
dfs(neighbor, cur);
|
|
843
|
+
}
|
|
844
|
+
const childLow = lowMap.get(neighbor);
|
|
845
|
+
const curLow = lowMap.get(cur);
|
|
846
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
847
|
+
if (curLow !== undefined && childLow !== undefined) {
|
|
848
|
+
lowMap.set(cur, Math.min(curLow, childLow));
|
|
849
|
+
}
|
|
850
|
+
const curFromMap = dfnMap.get(cur);
|
|
851
|
+
if (childLow !== undefined && curFromMap !== undefined) {
|
|
852
|
+
if (needArticulationPoints) {
|
|
853
|
+
if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
|
|
854
|
+
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
855
|
+
articulationPoints.push(cur);
|
|
828
856
|
}
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
857
|
+
}
|
|
858
|
+
if (needBridges) {
|
|
859
|
+
if (childLow > curFromMap) {
|
|
860
|
+
const edgeCurToNeighbor = this.getEdge(cur, neighbor);
|
|
861
|
+
if (edgeCurToNeighbor) {
|
|
862
|
+
bridges.push(edgeCurToNeighbor);
|
|
835
863
|
}
|
|
836
864
|
}
|
|
837
865
|
}
|
|
838
866
|
}
|
|
839
867
|
}
|
|
840
868
|
}
|
|
841
|
-
catch (e_15_1) { e_15 = { error: e_15_1 }; }
|
|
842
|
-
finally {
|
|
843
|
-
try {
|
|
844
|
-
if (neighbors_6_1 && !neighbors_6_1.done && (_a = neighbors_6.return)) _a.call(neighbors_6);
|
|
845
|
-
}
|
|
846
|
-
finally { if (e_15) throw e_15.error; }
|
|
847
|
-
}
|
|
848
869
|
};
|
|
849
870
|
dfs(root, null);
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
lowMap.forEach(
|
|
871
|
+
let SCCs = new Map();
|
|
872
|
+
const getSCCs = () => {
|
|
873
|
+
const SCCs = new Map();
|
|
874
|
+
lowMap.forEach((low, vertex) => {
|
|
854
875
|
var _a;
|
|
855
876
|
if (!SCCs.has(low)) {
|
|
856
877
|
SCCs.set(low, [vertex]);
|
|
@@ -864,20 +885,37 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
864
885
|
if (needSCCs) {
|
|
865
886
|
SCCs = getSCCs();
|
|
866
887
|
}
|
|
867
|
-
|
|
888
|
+
const cycles = new Map();
|
|
868
889
|
if (needCycles) {
|
|
869
|
-
|
|
870
|
-
if (
|
|
871
|
-
|
|
890
|
+
let SCCs = new Map();
|
|
891
|
+
if (SCCs.size < 1) {
|
|
892
|
+
SCCs = getSCCs();
|
|
872
893
|
}
|
|
873
|
-
|
|
894
|
+
SCCs.forEach((SCC, low) => {
|
|
874
895
|
if (SCC.length > 1) {
|
|
875
896
|
cycles.set(low, SCC);
|
|
876
897
|
}
|
|
877
898
|
});
|
|
878
899
|
}
|
|
879
|
-
return { dfnMap
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
|
|
900
|
+
return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
|
|
901
|
+
}
|
|
902
|
+
_addVertexOnly(newVertex) {
|
|
903
|
+
if (this.hasVertex(newVertex)) {
|
|
904
|
+
return false;
|
|
905
|
+
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
906
|
+
}
|
|
907
|
+
this._vertices.set(newVertex.id, newVertex);
|
|
908
|
+
return true;
|
|
909
|
+
}
|
|
910
|
+
_getVertex(vertexOrId) {
|
|
911
|
+
const vertexId = this._getVertexId(vertexOrId);
|
|
912
|
+
return this._vertices.get(vertexId) || null;
|
|
913
|
+
}
|
|
914
|
+
_getVertexId(vertexOrId) {
|
|
915
|
+
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
916
|
+
}
|
|
917
|
+
_setVertices(value) {
|
|
918
|
+
this._vertices = value;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
883
921
|
exports.AbstractGraph = AbstractGraph;
|