data-structure-typed 0.8.18 → 1.3.0
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 +690 -2
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- 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 -18
- package/dist/data-structures/binary-tree/avl-tree.js +110 -37
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +40 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +44 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -138
- package/dist/data-structures/binary-tree/binary-tree.js +27 -979
- package/dist/data-structures/binary-tree/bst.d.ts +118 -28
- package/dist/data-structures/binary-tree/bst.js +162 -124
- 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 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +63 -13
- package/dist/data-structures/binary-tree/segment-tree.js +80 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +682 -9
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -64
- package/dist/data-structures/graph/abstract-graph.js +365 -92
- package/dist/data-structures/graph/directed-graph.d.ts +175 -26
- package/dist/data-structures/graph/directed-graph.js +249 -95
- 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 -8
- package/dist/data-structures/graph/undirected-graph.js +154 -44
- package/dist/data-structures/hash/coordinate-map.d.ts +39 -2
- package/dist/data-structures/hash/coordinate-map.js +44 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +34 -0
- 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/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -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 -51
- package/dist/data-structures/heap/heap.js +106 -63
- package/dist/data-structures/heap/max-heap.d.ts +13 -4
- package/dist/data-structures/heap/max-heap.js +10 -2
- package/dist/data-structures/heap/min-heap.d.ts +14 -4
- package/dist/data-structures/heap/min-heap.js +11 -2
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +461 -194
- 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 -319
- package/dist/data-structures/linked-list/singly-linked-list.js +338 -557
- 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 +14 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +87 -4
- package/dist/data-structures/matrix/matrix2d.js +91 -8
- package/dist/data-structures/matrix/navigator.d.ts +37 -16
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/vector2d.d.ts +156 -29
- package/dist/data-structures/matrix/vector2d.js +184 -55
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +28 -4
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +29 -4
- package/dist/data-structures/priority-queue/priority-queue.d.ts +166 -22
- package/dist/data-structures/priority-queue/priority-queue.js +219 -75
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +151 -7
- package/dist/data-structures/queue/queue.d.ts +68 -42
- package/dist/data-structures/queue/queue.js +95 -51
- package/dist/data-structures/stack/stack.d.ts +30 -36
- package/dist/data-structures/stack/stack.js +31 -37
- 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/{types/utils.js → data-structures/tree/tree.js} +26 -19
- package/dist/data-structures/trie/trie.d.ts +39 -6
- package/dist/data-structures/trie/trie.js +81 -12
- 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-binary-tree.js +2 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/abstract-graph.js +2 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/avl-tree.js +2 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/bst.js +2 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/dist/interfaces/directed-graph.js +2 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/index.js +31 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/rb-tree.js +2 -0
- package/dist/interfaces/segment-tree.js +2 -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.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +13 -7
- package/dist/types/data-structures/index.js +31 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- 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.d.ts +1 -0
- package/dist/types/data-structures/segment-tree.js +2 -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 +2 -0
- package/dist/types/index.js +2 -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/index.js +17 -0
- package/dist/utils/utils.d.ts +19 -0
- package/dist/{data-structures/trampoline.js → utils/utils.js} +26 -12
- package/package.json +106 -55
- 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 -25
- 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/index.d.ts +0 -3
- 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/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/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/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/types/utils.d.ts +0 -46
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.d.ts +0 -122
- package/dist/utils.js +0 -569
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -232
- 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 -1088
- package/src/data-structures/binary-tree/bst.ts +0 -404
- 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 -164
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -21
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/graph/abstract-graph.ts +0 -789
- package/src/data-structures/graph/directed-graph.ts +0 -322
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -154
- 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/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -136
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -22
- package/src/data-structures/heap/min-heap.ts +0 -24
- package/src/data-structures/index.ts +0 -11
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -258
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -750
- 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 -99
- 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 -12
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/priority-queue.ts +0 -208
- 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 -123
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -104
- package/src/data-structures/trampoline.ts +0 -91
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -153
- package/src/index.ts +0 -1
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +0 -158
- package/src/utils.ts +0 -605
- package/tsconfig.json +0 -53
- /package/dist/{types/data-structures/hash/hash-table.d.ts → interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/pair.d.ts → interfaces/heap.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-map.d.ts → interfaces/navigator.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-set.d.ts → interfaces/priority-queue.d.ts} +0 -0
- /package/dist/{types/data-structures/linked-list/skip-linked-list.d.ts → interfaces/segment-tree.d.ts} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/singly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/types/data-structures/doubly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/types/data-structures/singly-linked-list.d.ts} +0 -0
- /package/dist/{types/types → utils}/index.d.ts +0 -0
|
@@ -1,21 +1,62 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
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
|
+
*/
|
|
4
11
|
const utils_1 = require("../../utils");
|
|
5
12
|
const priority_queue_1 = require("../priority-queue");
|
|
6
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) {
|
|
22
|
+
this._id = id;
|
|
23
|
+
this._val = val;
|
|
24
|
+
}
|
|
7
25
|
get id() {
|
|
8
26
|
return this._id;
|
|
9
27
|
}
|
|
10
28
|
set id(v) {
|
|
11
29
|
this._id = v;
|
|
12
30
|
}
|
|
13
|
-
|
|
14
|
-
this.
|
|
31
|
+
get val() {
|
|
32
|
+
return this._val;
|
|
33
|
+
}
|
|
34
|
+
set val(value) {
|
|
35
|
+
this._val = value;
|
|
15
36
|
}
|
|
16
37
|
}
|
|
17
38
|
exports.AbstractVertex = AbstractVertex;
|
|
18
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;
|
|
52
|
+
this._hashCode = (0, utils_1.uuidV4)();
|
|
53
|
+
}
|
|
54
|
+
get val() {
|
|
55
|
+
return this._val;
|
|
56
|
+
}
|
|
57
|
+
set val(value) {
|
|
58
|
+
this._val = value;
|
|
59
|
+
}
|
|
19
60
|
get weight() {
|
|
20
61
|
return this._weight;
|
|
21
62
|
}
|
|
@@ -25,51 +66,72 @@ class AbstractEdge {
|
|
|
25
66
|
get hashCode() {
|
|
26
67
|
return this._hashCode;
|
|
27
68
|
}
|
|
28
|
-
|
|
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) {
|
|
29
79
|
this._hashCode = v;
|
|
30
80
|
}
|
|
31
|
-
constructor(weight) {
|
|
32
|
-
if (weight === undefined)
|
|
33
|
-
weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
|
|
34
|
-
this._weight = weight;
|
|
35
|
-
this._hashCode = (0, utils_1.uuidV4)();
|
|
36
|
-
}
|
|
37
81
|
}
|
|
38
82
|
exports.AbstractEdge = AbstractEdge;
|
|
39
|
-
AbstractEdge.DEFAULT_EDGE_WEIGHT = 1;
|
|
40
|
-
// Connected Component === Largest Connected Sub-Graph
|
|
41
83
|
class AbstractGraph {
|
|
42
84
|
constructor() {
|
|
43
85
|
this._vertices = new Map();
|
|
44
|
-
// unionFind() {
|
|
45
|
-
// }
|
|
46
|
-
/**--- end find cycles --- */
|
|
47
|
-
// Minimum Spanning Tree
|
|
48
86
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return this._vertices.get(vertexId) || null;
|
|
52
|
-
}
|
|
53
|
-
getVertexId(vertexOrId) {
|
|
54
|
-
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
87
|
+
get vertices() {
|
|
88
|
+
return this._vertices;
|
|
55
89
|
}
|
|
56
|
-
|
|
57
|
-
|
|
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;
|
|
58
99
|
}
|
|
59
|
-
|
|
60
|
-
|
|
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));
|
|
61
108
|
}
|
|
62
|
-
addVertex(
|
|
63
|
-
if (
|
|
64
|
-
return
|
|
109
|
+
addVertex(idOrVertex, val) {
|
|
110
|
+
if (idOrVertex instanceof AbstractVertex) {
|
|
111
|
+
return this._addVertexOnly(idOrVertex);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
const newVertex = this.createVertex(idOrVertex, val);
|
|
115
|
+
return this._addVertexOnly(newVertex);
|
|
65
116
|
}
|
|
66
|
-
this._vertices.set(newVertex.id, newVertex);
|
|
67
|
-
return true;
|
|
68
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
|
+
*/
|
|
69
124
|
removeVertex(vertexOrId) {
|
|
70
|
-
const vertexId = this.
|
|
125
|
+
const vertexId = this._getVertexId(vertexOrId);
|
|
71
126
|
return this._vertices.delete(vertexId);
|
|
72
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
|
+
*/
|
|
73
135
|
removeAllVertices(vertices) {
|
|
74
136
|
const removed = [];
|
|
75
137
|
for (const v of vertices) {
|
|
@@ -77,10 +139,49 @@ class AbstractGraph {
|
|
|
77
139
|
}
|
|
78
140
|
return removed.length > 0;
|
|
79
141
|
}
|
|
80
|
-
|
|
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) {
|
|
81
151
|
const edge = this.getEdge(v1, v2);
|
|
82
152
|
return !!edge;
|
|
83
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
|
+
*/
|
|
84
185
|
setEdgeWeight(srcOrId, destOrId, weight) {
|
|
85
186
|
const edge = this.getEdge(srcOrId, destOrId);
|
|
86
187
|
if (edge) {
|
|
@@ -91,10 +192,17 @@ class AbstractGraph {
|
|
|
91
192
|
return false;
|
|
92
193
|
}
|
|
93
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
|
+
*/
|
|
94
202
|
getAllPathsBetween(v1, v2) {
|
|
95
203
|
const paths = [];
|
|
96
|
-
const vertex1 = this.
|
|
97
|
-
const vertex2 = this.
|
|
204
|
+
const vertex1 = this._getVertex(v1);
|
|
205
|
+
const vertex2 = this._getVertex(v2);
|
|
98
206
|
if (!(vertex1 && vertex2)) {
|
|
99
207
|
return [];
|
|
100
208
|
}
|
|
@@ -108,7 +216,7 @@ class AbstractGraph {
|
|
|
108
216
|
if (!visiting.get(neighbor)) {
|
|
109
217
|
path.push(neighbor);
|
|
110
218
|
dfs(neighbor, dest, visiting, path);
|
|
111
|
-
(0, utils_1.arrayRemove)(path, vertex => vertex === neighbor);
|
|
219
|
+
(0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
|
|
112
220
|
}
|
|
113
221
|
}
|
|
114
222
|
visiting.set(cur, false);
|
|
@@ -116,6 +224,11 @@ class AbstractGraph {
|
|
|
116
224
|
dfs(vertex1, vertex2, new Map(), []);
|
|
117
225
|
return paths;
|
|
118
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
|
+
*/
|
|
119
232
|
getPathSumWeight(path) {
|
|
120
233
|
var _a;
|
|
121
234
|
let sum = 0;
|
|
@@ -124,6 +237,20 @@ class AbstractGraph {
|
|
|
124
237
|
}
|
|
125
238
|
return sum;
|
|
126
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
|
+
*/
|
|
127
254
|
getMinCostBetween(v1, v2, isWeight) {
|
|
128
255
|
if (isWeight === undefined)
|
|
129
256
|
isWeight = false;
|
|
@@ -137,8 +264,8 @@ class AbstractGraph {
|
|
|
137
264
|
}
|
|
138
265
|
else {
|
|
139
266
|
// BFS
|
|
140
|
-
const vertex2 = this.
|
|
141
|
-
const vertex1 = this.
|
|
267
|
+
const vertex2 = this._getVertex(v2);
|
|
268
|
+
const vertex1 = this._getVertex(v1);
|
|
142
269
|
if (!(vertex1 && vertex2)) {
|
|
143
270
|
return null;
|
|
144
271
|
}
|
|
@@ -168,6 +295,19 @@ class AbstractGraph {
|
|
|
168
295
|
return null;
|
|
169
296
|
}
|
|
170
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
|
+
*/
|
|
171
311
|
getMinPathBetween(v1, v2, isWeight) {
|
|
172
312
|
if (isWeight === undefined)
|
|
173
313
|
isWeight = false;
|
|
@@ -189,8 +329,8 @@ class AbstractGraph {
|
|
|
189
329
|
else {
|
|
190
330
|
// BFS
|
|
191
331
|
let minPath = [];
|
|
192
|
-
const vertex1 = this.
|
|
193
|
-
const vertex2 = this.
|
|
332
|
+
const vertex1 = this._getVertex(v1);
|
|
333
|
+
const vertex2 = this._getVertex(v2);
|
|
194
334
|
if (!(vertex1 && vertex2)) {
|
|
195
335
|
return [];
|
|
196
336
|
}
|
|
@@ -205,7 +345,7 @@ class AbstractGraph {
|
|
|
205
345
|
if (!visiting.get(neighbor)) {
|
|
206
346
|
path.push(neighbor);
|
|
207
347
|
dfs(neighbor, dest, visiting, path);
|
|
208
|
-
(0, utils_1.arrayRemove)(path, vertex => vertex === neighbor);
|
|
348
|
+
(0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
|
|
209
349
|
}
|
|
210
350
|
}
|
|
211
351
|
visiting.set(cur, false);
|
|
@@ -216,10 +356,24 @@ class AbstractGraph {
|
|
|
216
356
|
}
|
|
217
357
|
/**
|
|
218
358
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
219
|
-
*
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
*
|
|
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>`.
|
|
223
377
|
*/
|
|
224
378
|
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
225
379
|
if (getMinDist === undefined)
|
|
@@ -236,13 +390,15 @@ class AbstractGraph {
|
|
|
236
390
|
const distMap = new Map();
|
|
237
391
|
const seen = new Set();
|
|
238
392
|
const preMap = new Map(); // predecessor
|
|
239
|
-
const srcVertex = this.
|
|
240
|
-
const destVertex = dest ? this.
|
|
393
|
+
const srcVertex = this._getVertex(src);
|
|
394
|
+
const destVertex = dest ? this._getVertex(dest) : null;
|
|
241
395
|
if (!srcVertex) {
|
|
242
396
|
return null;
|
|
243
397
|
}
|
|
244
398
|
for (const vertex of vertices) {
|
|
245
|
-
|
|
399
|
+
const vertexOrId = vertex[1];
|
|
400
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
401
|
+
distMap.set(vertexOrId, Infinity);
|
|
246
402
|
}
|
|
247
403
|
distMap.set(srcVertex, 0);
|
|
248
404
|
preMap.set(srcVertex, null);
|
|
@@ -261,16 +417,19 @@ class AbstractGraph {
|
|
|
261
417
|
};
|
|
262
418
|
const getPaths = (minV) => {
|
|
263
419
|
for (const vertex of vertices) {
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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);
|
|
427
|
+
}
|
|
428
|
+
const reversed = path.reverse();
|
|
429
|
+
if (vertex[1] === minV)
|
|
430
|
+
minPath = reversed;
|
|
431
|
+
paths.push(reversed);
|
|
269
432
|
}
|
|
270
|
-
const reversed = path.reverse();
|
|
271
|
-
if (vertex[1] === minV)
|
|
272
|
-
minPath = reversed;
|
|
273
|
-
paths.push(reversed);
|
|
274
433
|
}
|
|
275
434
|
};
|
|
276
435
|
for (let i = 1; i < vertices.size; i++) {
|
|
@@ -319,10 +478,29 @@ class AbstractGraph {
|
|
|
319
478
|
}
|
|
320
479
|
/**
|
|
321
480
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
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>`.
|
|
326
504
|
*/
|
|
327
505
|
dijkstra(src, dest, getMinDist, genPaths) {
|
|
328
506
|
var _a;
|
|
@@ -340,30 +518,39 @@ class AbstractGraph {
|
|
|
340
518
|
const distMap = new Map();
|
|
341
519
|
const seen = new Set();
|
|
342
520
|
const preMap = new Map(); // predecessor
|
|
343
|
-
const srcVertex = this.
|
|
344
|
-
const destVertex = dest ? this.
|
|
345
|
-
if (!srcVertex)
|
|
521
|
+
const srcVertex = this._getVertex(src);
|
|
522
|
+
const destVertex = dest ? this._getVertex(dest) : null;
|
|
523
|
+
if (!srcVertex)
|
|
346
524
|
return null;
|
|
347
|
-
}
|
|
348
525
|
for (const vertex of vertices) {
|
|
349
|
-
|
|
526
|
+
const vertexOrId = vertex[1];
|
|
527
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
528
|
+
distMap.set(vertexOrId, Infinity);
|
|
350
529
|
}
|
|
351
530
|
const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.id - b.id });
|
|
352
|
-
heap.
|
|
531
|
+
heap.add({ id: 0, val: srcVertex });
|
|
353
532
|
distMap.set(srcVertex, 0);
|
|
354
533
|
preMap.set(srcVertex, null);
|
|
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
|
+
*/
|
|
355
539
|
const getPaths = (minV) => {
|
|
356
540
|
for (const vertex of vertices) {
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
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);
|
|
548
|
+
}
|
|
549
|
+
const reversed = path.reverse();
|
|
550
|
+
if (vertex[1] === minV)
|
|
551
|
+
minPath = reversed;
|
|
552
|
+
paths.push(reversed);
|
|
362
553
|
}
|
|
363
|
-
const reversed = path.reverse();
|
|
364
|
-
if (vertex[1] === minV)
|
|
365
|
-
minPath = reversed;
|
|
366
|
-
paths.push(reversed);
|
|
367
554
|
}
|
|
368
555
|
};
|
|
369
556
|
while (heap.size > 0) {
|
|
@@ -390,7 +577,7 @@ class AbstractGraph {
|
|
|
390
577
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
391
578
|
if (distSrcToNeighbor) {
|
|
392
579
|
if (dist + weight < distSrcToNeighbor) {
|
|
393
|
-
heap.
|
|
580
|
+
heap.add({ id: dist + weight, val: neighbor });
|
|
394
581
|
preMap.set(neighbor, cur);
|
|
395
582
|
distMap.set(neighbor, dist + weight);
|
|
396
583
|
}
|
|
@@ -420,24 +607,37 @@ class AbstractGraph {
|
|
|
420
607
|
/**
|
|
421
608
|
* BellmanFord time:O(VE) space:O(V)
|
|
422
609
|
* one to rest pairs
|
|
423
|
-
*
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
*
|
|
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:
|
|
427
627
|
*/
|
|
428
628
|
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
429
629
|
if (getMin === undefined)
|
|
430
630
|
getMin = false;
|
|
431
631
|
if (genPath === undefined)
|
|
432
632
|
genPath = false;
|
|
433
|
-
const srcVertex = this.
|
|
633
|
+
const srcVertex = this._getVertex(src);
|
|
434
634
|
const paths = [];
|
|
435
635
|
const distMap = new Map();
|
|
436
636
|
const preMap = new Map(); // predecessor
|
|
437
637
|
let min = Infinity;
|
|
438
638
|
let minPath = [];
|
|
439
639
|
// TODO
|
|
440
|
-
let hasNegativeCycle
|
|
640
|
+
let hasNegativeCycle;
|
|
441
641
|
if (scanNegativeCycle)
|
|
442
642
|
hasNegativeCycle = false;
|
|
443
643
|
if (!srcVertex)
|
|
@@ -481,16 +681,19 @@ class AbstractGraph {
|
|
|
481
681
|
}
|
|
482
682
|
if (genPath) {
|
|
483
683
|
for (const vertex of vertices) {
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
parent
|
|
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);
|
|
691
|
+
}
|
|
692
|
+
const reversed = path.reverse();
|
|
693
|
+
if (vertex[1] === minDest)
|
|
694
|
+
minPath = reversed;
|
|
695
|
+
paths.push(reversed);
|
|
489
696
|
}
|
|
490
|
-
const reversed = path.reverse();
|
|
491
|
-
if (vertex[1] === minDest)
|
|
492
|
-
minPath = reversed;
|
|
493
|
-
paths.push(reversed);
|
|
494
697
|
}
|
|
495
698
|
}
|
|
496
699
|
for (let j = 0; j < numOfEdges; ++j) {
|
|
@@ -507,9 +710,40 @@ class AbstractGraph {
|
|
|
507
710
|
}
|
|
508
711
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
509
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
|
+
*/
|
|
727
|
+
/**
|
|
728
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
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.
|
|
731
|
+
*/
|
|
510
732
|
/**
|
|
511
733
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
512
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
|
|
513
747
|
*/
|
|
514
748
|
floyd() {
|
|
515
749
|
var _a;
|
|
@@ -542,13 +776,34 @@ class AbstractGraph {
|
|
|
542
776
|
}
|
|
543
777
|
return { costs, predecessor };
|
|
544
778
|
}
|
|
545
|
-
/**--- start find cycles --- */
|
|
546
779
|
/**
|
|
547
780
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
548
781
|
* Tarjan can find cycles in directed or undirected graph
|
|
549
782
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
550
783
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
551
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:
|
|
552
807
|
*/
|
|
553
808
|
tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
554
809
|
// !! in undirected graph we will not let child visit parent when DFS
|
|
@@ -644,5 +899,23 @@ class AbstractGraph {
|
|
|
644
899
|
}
|
|
645
900
|
return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
|
|
646
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
|
+
}
|
|
647
920
|
}
|
|
648
921
|
exports.AbstractGraph = AbstractGraph;
|