data-structure-typed 1.19.2 → 1.19.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
- package/dist/data-structures/binary-tree/avl-tree.js +46 -90
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -31
- package/dist/data-structures/binary-tree/bst.js +96 -139
- package/dist/data-structures/binary-tree/rb-tree.js +32 -56
- package/dist/data-structures/binary-tree/segment-tree.js +78 -120
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.js +340 -574
- package/dist/data-structures/graph/directed-graph.js +146 -276
- package/dist/data-structures/graph/undirected-graph.js +87 -176
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/hash-table.js +2 -5
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/heap/heap.js +53 -77
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
- package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix2d.js +76 -93
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/vector2d.js +80 -101
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/queue.js +24 -25
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/utils/utils.js +22 -107
- package/dist/utils/validate-type.js +2 -2
- package/package.json +1 -1
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
- package/src/data-structures/binary-tree/avl-tree.ts +297 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/src/data-structures/binary-tree/binary-tree.ts +40 -0
- package/src/data-structures/binary-tree/bst.ts +435 -0
- 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 +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +102 -0
- package/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +1032 -0
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +472 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +270 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +3 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +3 -0
- package/src/data-structures/hash/tree-map.ts +3 -0
- package/src/data-structures/hash/tree-set.ts +3 -0
- package/src/data-structures/heap/heap.ts +183 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +34 -0
- package/src/data-structures/index.ts +15 -0
- package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
- package/src/data-structures/interfaces/abstract-graph.ts +40 -0
- package/src/data-structures/interfaces/avl-tree.ts +28 -0
- package/src/data-structures/interfaces/binary-tree.ts +8 -0
- package/src/data-structures/interfaces/bst.ts +32 -0
- package/src/data-structures/interfaces/directed-graph.ts +20 -0
- package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/heap.ts +1 -0
- package/src/data-structures/interfaces/index.ts +15 -0
- package/src/data-structures/interfaces/navigator.ts +1 -0
- package/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/src/data-structures/interfaces/rb-tree.ts +11 -0
- package/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/tree-multiset.ts +12 -0
- package/src/data-structures/interfaces/undirected-graph.ts +6 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +208 -0
- package/src/data-structures/matrix/navigator.ts +122 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/src/data-structures/queue/deque.ts +251 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +120 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +227 -0
- package/src/data-structures/types/abstract-binary-tree.ts +42 -0
- package/src/data-structures/types/abstract-graph.ts +5 -0
- package/src/data-structures/types/avl-tree.ts +5 -0
- package/src/data-structures/types/binary-tree.ts +9 -0
- package/src/data-structures/types/bst.ts +12 -0
- package/src/data-structures/types/directed-graph.ts +8 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +5 -0
- package/src/data-structures/types/helpers.ts +1 -0
- package/src/data-structures/types/index.ts +15 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/rb-tree.ts +8 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +8 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/types/index.ts +2 -0
- package/src/utils/types/utils.ts +6 -0
- package/src/utils/types/validate-type.ts +25 -0
- package/src/utils/utils.ts +78 -0
- package/src/utils/validate-type.ts +69 -0
- package/tsconfig.json +1 -1
|
@@ -1,40 +1,4 @@
|
|
|
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
4
|
/**
|
|
@@ -44,9 +8,9 @@ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
|
44
8
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
45
9
|
* @license MIT License
|
|
46
10
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
11
|
+
const utils_1 = require("../../utils");
|
|
12
|
+
const priority_queue_1 = require("../priority-queue");
|
|
13
|
+
class AbstractVertex {
|
|
50
14
|
/**
|
|
51
15
|
* The function is a protected constructor that takes an id and an optional value as parameters.
|
|
52
16
|
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
@@ -54,34 +18,25 @@ var AbstractVertex = /** @class */ (function () {
|
|
|
54
18
|
* @param {T} [val] - The parameter "val" is an optional parameter of type T. It is used to assign a value to the
|
|
55
19
|
* vertex. If no value is provided, it will be set to undefined.
|
|
56
20
|
*/
|
|
57
|
-
|
|
21
|
+
constructor(id, val) {
|
|
58
22
|
this._id = id;
|
|
59
23
|
this._val = val;
|
|
60
24
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
},
|
|
75
|
-
set: function (value) {
|
|
76
|
-
this._val = value;
|
|
77
|
-
},
|
|
78
|
-
enumerable: false,
|
|
79
|
-
configurable: true
|
|
80
|
-
});
|
|
81
|
-
return AbstractVertex;
|
|
82
|
-
}());
|
|
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
|
+
}
|
|
83
38
|
exports.AbstractVertex = AbstractVertex;
|
|
84
|
-
|
|
39
|
+
class AbstractEdge {
|
|
85
40
|
/**
|
|
86
41
|
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
87
42
|
* object.
|
|
@@ -91,38 +46,26 @@ var AbstractEdge = /** @class */ (function () {
|
|
|
91
46
|
* @param {T} [val] - The `val` parameter is of type `T`, which means it can be any type. It is an optional parameter,
|
|
92
47
|
* meaning it can be omitted when creating an instance of the class.
|
|
93
48
|
*/
|
|
94
|
-
|
|
49
|
+
constructor(weight, val) {
|
|
95
50
|
this._weight = weight !== undefined ? weight : 1;
|
|
96
51
|
this._val = val;
|
|
97
52
|
this._hashCode = (0, utils_1.uuidV4)();
|
|
98
53
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
this._weight = v;
|
|
115
|
-
},
|
|
116
|
-
enumerable: false,
|
|
117
|
-
configurable: true
|
|
118
|
-
});
|
|
119
|
-
Object.defineProperty(AbstractEdge.prototype, "hashCode", {
|
|
120
|
-
get: function () {
|
|
121
|
-
return this._hashCode;
|
|
122
|
-
},
|
|
123
|
-
enumerable: false,
|
|
124
|
-
configurable: true
|
|
125
|
-
});
|
|
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
|
+
}
|
|
126
69
|
/**
|
|
127
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.
|
|
128
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.
|
|
@@ -132,23 +75,18 @@ var AbstractEdge = /** @class */ (function () {
|
|
|
132
75
|
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
133
76
|
* "_hashCode" property.
|
|
134
77
|
*/
|
|
135
|
-
|
|
78
|
+
_setHashCode(v) {
|
|
136
79
|
this._hashCode = v;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
}());
|
|
80
|
+
}
|
|
81
|
+
}
|
|
140
82
|
exports.AbstractEdge = AbstractEdge;
|
|
141
|
-
|
|
142
|
-
|
|
83
|
+
class AbstractGraph {
|
|
84
|
+
constructor() {
|
|
143
85
|
this._vertices = new Map();
|
|
144
86
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
},
|
|
149
|
-
enumerable: false,
|
|
150
|
-
configurable: true
|
|
151
|
-
});
|
|
87
|
+
get vertices() {
|
|
88
|
+
return this._vertices;
|
|
89
|
+
}
|
|
152
90
|
/**
|
|
153
91
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
154
92
|
* @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
|
|
@@ -156,37 +94,37 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
156
94
|
* @returns The method `getVertex` returns the vertex with the specified `vertexId` if it exists in the `_vertices`
|
|
157
95
|
* map. If the vertex does not exist, it returns `null`.
|
|
158
96
|
*/
|
|
159
|
-
|
|
97
|
+
getVertex(vertexId) {
|
|
160
98
|
return this._vertices.get(vertexId) || null;
|
|
161
|
-
}
|
|
99
|
+
}
|
|
162
100
|
/**
|
|
163
101
|
* The function checks if a vertex exists in a graph.
|
|
164
102
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
165
103
|
* (`VertexId`).
|
|
166
104
|
* @returns a boolean value.
|
|
167
105
|
*/
|
|
168
|
-
|
|
106
|
+
hasVertex(vertexOrId) {
|
|
169
107
|
return this._vertices.has(this._getVertexId(vertexOrId));
|
|
170
|
-
}
|
|
171
|
-
|
|
108
|
+
}
|
|
109
|
+
addVertex(idOrVertex, val) {
|
|
172
110
|
if (idOrVertex instanceof AbstractVertex) {
|
|
173
111
|
return this._addVertexOnly(idOrVertex);
|
|
174
112
|
}
|
|
175
113
|
else {
|
|
176
|
-
|
|
114
|
+
const newVertex = this.createVertex(idOrVertex, val);
|
|
177
115
|
return this._addVertexOnly(newVertex);
|
|
178
116
|
}
|
|
179
|
-
}
|
|
117
|
+
}
|
|
180
118
|
/**
|
|
181
119
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
182
120
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
183
121
|
* (`VertexId`).
|
|
184
122
|
* @returns The method is returning a boolean value.
|
|
185
123
|
*/
|
|
186
|
-
|
|
187
|
-
|
|
124
|
+
removeVertex(vertexOrId) {
|
|
125
|
+
const vertexId = this._getVertexId(vertexOrId);
|
|
188
126
|
return this._vertices.delete(vertexId);
|
|
189
|
-
}
|
|
127
|
+
}
|
|
190
128
|
/**
|
|
191
129
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
192
130
|
* @param {V[] | VertexId[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
|
|
@@ -194,24 +132,13 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
194
132
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
195
133
|
* were removed.
|
|
196
134
|
*/
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
for (var vertices_1 = __values(vertices), vertices_1_1 = vertices_1.next(); !vertices_1_1.done; vertices_1_1 = vertices_1.next()) {
|
|
202
|
-
var v = vertices_1_1.value;
|
|
203
|
-
removed.push(this.removeVertex(v));
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
207
|
-
finally {
|
|
208
|
-
try {
|
|
209
|
-
if (vertices_1_1 && !vertices_1_1.done && (_a = vertices_1.return)) _a.call(vertices_1);
|
|
210
|
-
}
|
|
211
|
-
finally { if (e_1) throw e_1.error; }
|
|
135
|
+
removeAllVertices(vertices) {
|
|
136
|
+
const removed = [];
|
|
137
|
+
for (const v of vertices) {
|
|
138
|
+
removed.push(this.removeVertex(v));
|
|
212
139
|
}
|
|
213
140
|
return removed.length > 0;
|
|
214
|
-
}
|
|
141
|
+
}
|
|
215
142
|
/**
|
|
216
143
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
217
144
|
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the unique
|
|
@@ -220,11 +147,11 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
220
147
|
* `VertexId` or a `V` type, which represents the type of the vertex.
|
|
221
148
|
* @returns A boolean value is being returned.
|
|
222
149
|
*/
|
|
223
|
-
|
|
224
|
-
|
|
150
|
+
hasEdge(v1, v2) {
|
|
151
|
+
const edge = this.getEdge(v1, v2);
|
|
225
152
|
return !!edge;
|
|
226
|
-
}
|
|
227
|
-
|
|
153
|
+
}
|
|
154
|
+
addEdge(srcOrEdge, dest, weight, val) {
|
|
228
155
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
229
156
|
return this._addEdgeOnly(srcOrEdge);
|
|
230
157
|
}
|
|
@@ -236,14 +163,14 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
236
163
|
srcOrEdge = srcOrEdge.id;
|
|
237
164
|
if (dest instanceof AbstractVertex)
|
|
238
165
|
dest = dest.id;
|
|
239
|
-
|
|
166
|
+
const newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
240
167
|
return this._addEdgeOnly(newEdge);
|
|
241
168
|
}
|
|
242
169
|
else {
|
|
243
170
|
throw new Error('dest must be a Vertex or vertex id while srcOrEdge is an Edge');
|
|
244
171
|
}
|
|
245
172
|
}
|
|
246
|
-
}
|
|
173
|
+
}
|
|
247
174
|
/**
|
|
248
175
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
249
176
|
* @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
|
|
@@ -255,8 +182,8 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
255
182
|
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
256
183
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
257
184
|
*/
|
|
258
|
-
|
|
259
|
-
|
|
185
|
+
setEdgeWeight(srcOrId, destOrId, weight) {
|
|
186
|
+
const edge = this.getEdge(srcOrId, destOrId);
|
|
260
187
|
if (edge) {
|
|
261
188
|
edge.weight = weight;
|
|
262
189
|
return true;
|
|
@@ -264,7 +191,7 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
264
191
|
else {
|
|
265
192
|
return false;
|
|
266
193
|
}
|
|
267
|
-
}
|
|
194
|
+
}
|
|
268
195
|
/**
|
|
269
196
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
270
197
|
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
@@ -272,59 +199,44 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
272
199
|
* @param {V | VertexId} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
273
200
|
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
274
201
|
*/
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
var vertex2 = this._getVertex(v2);
|
|
202
|
+
getAllPathsBetween(v1, v2) {
|
|
203
|
+
const paths = [];
|
|
204
|
+
const vertex1 = this._getVertex(v1);
|
|
205
|
+
const vertex2 = this._getVertex(v2);
|
|
280
206
|
if (!(vertex1 && vertex2)) {
|
|
281
207
|
return [];
|
|
282
208
|
}
|
|
283
|
-
|
|
284
|
-
var e_2, _a;
|
|
209
|
+
const dfs = (cur, dest, visiting, path) => {
|
|
285
210
|
visiting.set(cur, true);
|
|
286
211
|
if (cur === dest) {
|
|
287
|
-
paths.push(
|
|
212
|
+
paths.push([vertex1, ...path]);
|
|
288
213
|
}
|
|
289
|
-
|
|
290
|
-
|
|
214
|
+
const neighbors = this.getNeighbors(cur);
|
|
215
|
+
for (const neighbor of neighbors) {
|
|
291
216
|
if (!visiting.get(neighbor)) {
|
|
292
217
|
path.push(neighbor);
|
|
293
218
|
dfs(neighbor, dest, visiting, path);
|
|
294
|
-
(0, utils_1.arrayRemove)(path,
|
|
295
|
-
}
|
|
296
|
-
};
|
|
297
|
-
try {
|
|
298
|
-
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
|
|
299
|
-
var neighbor = neighbors_1_1.value;
|
|
300
|
-
_loop_1(neighbor);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
304
|
-
finally {
|
|
305
|
-
try {
|
|
306
|
-
if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
|
|
219
|
+
(0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
|
|
307
220
|
}
|
|
308
|
-
finally { if (e_2) throw e_2.error; }
|
|
309
221
|
}
|
|
310
222
|
visiting.set(cur, false);
|
|
311
223
|
};
|
|
312
224
|
dfs(vertex1, vertex2, new Map(), []);
|
|
313
225
|
return paths;
|
|
314
|
-
}
|
|
226
|
+
}
|
|
315
227
|
/**
|
|
316
228
|
* The function calculates the sum of weights along a given path.
|
|
317
229
|
* @param {V[]} path - An array of vertices (V) representing a path in a graph.
|
|
318
230
|
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
319
231
|
*/
|
|
320
|
-
|
|
232
|
+
getPathSumWeight(path) {
|
|
321
233
|
var _a;
|
|
322
|
-
|
|
323
|
-
for (
|
|
234
|
+
let sum = 0;
|
|
235
|
+
for (let i = 0; i < path.length; i++) {
|
|
324
236
|
sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
|
|
325
237
|
}
|
|
326
238
|
return sum;
|
|
327
|
-
}
|
|
239
|
+
}
|
|
328
240
|
/**
|
|
329
241
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
330
242
|
* weights or using a breadth-first search algorithm.
|
|
@@ -339,63 +251,42 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
339
251
|
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
340
252
|
* minimum number of
|
|
341
253
|
*/
|
|
342
|
-
|
|
343
|
-
var e_3, _a, e_4, _b;
|
|
254
|
+
getMinCostBetween(v1, v2, isWeight) {
|
|
344
255
|
if (isWeight === undefined)
|
|
345
256
|
isWeight = false;
|
|
346
257
|
if (isWeight) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
var path = allPaths_1_1.value;
|
|
352
|
-
min = Math.min(this.getPathSumWeight(path), min);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
356
|
-
finally {
|
|
357
|
-
try {
|
|
358
|
-
if (allPaths_1_1 && !allPaths_1_1.done && (_a = allPaths_1.return)) _a.call(allPaths_1);
|
|
359
|
-
}
|
|
360
|
-
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);
|
|
361
262
|
}
|
|
362
263
|
return min;
|
|
363
264
|
}
|
|
364
265
|
else {
|
|
365
266
|
// BFS
|
|
366
|
-
|
|
367
|
-
|
|
267
|
+
const vertex2 = this._getVertex(v2);
|
|
268
|
+
const vertex1 = this._getVertex(v1);
|
|
368
269
|
if (!(vertex1 && vertex2)) {
|
|
369
270
|
return null;
|
|
370
271
|
}
|
|
371
|
-
|
|
372
|
-
|
|
272
|
+
const visited = new Map();
|
|
273
|
+
const queue = [vertex1];
|
|
373
274
|
visited.set(vertex1, true);
|
|
374
|
-
|
|
275
|
+
let cost = 0;
|
|
375
276
|
while (queue.length > 0) {
|
|
376
|
-
for (
|
|
377
|
-
|
|
277
|
+
for (let i = 0; i < queue.length; i++) {
|
|
278
|
+
const cur = queue.shift();
|
|
378
279
|
if (cur === vertex2) {
|
|
379
280
|
return cost;
|
|
380
281
|
}
|
|
381
282
|
// TODO consider optimizing to AbstractGraph
|
|
382
283
|
if (cur !== undefined) {
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
visited.set(neighbor, true);
|
|
389
|
-
queue.push(neighbor);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
394
|
-
finally {
|
|
395
|
-
try {
|
|
396
|
-
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);
|
|
397
289
|
}
|
|
398
|
-
finally { if (e_4) throw e_4.error; }
|
|
399
290
|
}
|
|
400
291
|
}
|
|
401
292
|
}
|
|
@@ -403,7 +294,7 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
403
294
|
}
|
|
404
295
|
return null;
|
|
405
296
|
}
|
|
406
|
-
}
|
|
297
|
+
}
|
|
407
298
|
/**
|
|
408
299
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
409
300
|
* using a breadth-first search algorithm.
|
|
@@ -417,78 +308,52 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
417
308
|
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
418
309
|
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
419
310
|
*/
|
|
420
|
-
|
|
421
|
-
var e_5, _a;
|
|
422
|
-
var _this = this;
|
|
311
|
+
getMinPathBetween(v1, v2, isWeight) {
|
|
423
312
|
if (isWeight === undefined)
|
|
424
313
|
isWeight = false;
|
|
425
314
|
if (isWeight) {
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
index++;
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
442
|
-
finally {
|
|
443
|
-
try {
|
|
444
|
-
if (allPaths_2_1 && !allPaths_2_1.done && (_a = allPaths_2.return)) _a.call(allPaths_2);
|
|
445
|
-
}
|
|
446
|
-
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++;
|
|
447
326
|
}
|
|
448
327
|
return allPaths[minIndex] || null;
|
|
449
328
|
}
|
|
450
329
|
else {
|
|
451
330
|
// BFS
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
if (!(
|
|
331
|
+
let minPath = [];
|
|
332
|
+
const vertex1 = this._getVertex(v1);
|
|
333
|
+
const vertex2 = this._getVertex(v2);
|
|
334
|
+
if (!(vertex1 && vertex2)) {
|
|
456
335
|
return [];
|
|
457
336
|
}
|
|
458
|
-
|
|
459
|
-
var e_6, _a;
|
|
337
|
+
const dfs = (cur, dest, visiting, path) => {
|
|
460
338
|
visiting.set(cur, true);
|
|
461
339
|
if (cur === dest) {
|
|
462
|
-
|
|
340
|
+
minPath = [vertex1, ...path];
|
|
463
341
|
return;
|
|
464
342
|
}
|
|
465
|
-
|
|
466
|
-
|
|
343
|
+
const neighbors = this.getNeighbors(cur);
|
|
344
|
+
for (const neighbor of neighbors) {
|
|
467
345
|
if (!visiting.get(neighbor)) {
|
|
468
346
|
path.push(neighbor);
|
|
469
|
-
|
|
470
|
-
(0, utils_1.arrayRemove)(path,
|
|
471
|
-
}
|
|
472
|
-
};
|
|
473
|
-
try {
|
|
474
|
-
for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
|
|
475
|
-
var neighbor = neighbors_3_1.value;
|
|
476
|
-
_loop_2(neighbor);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
480
|
-
finally {
|
|
481
|
-
try {
|
|
482
|
-
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);
|
|
483
349
|
}
|
|
484
|
-
finally { if (e_6) throw e_6.error; }
|
|
485
350
|
}
|
|
486
351
|
visiting.set(cur, false);
|
|
487
352
|
};
|
|
488
|
-
|
|
489
|
-
return
|
|
353
|
+
dfs(vertex1, vertex2, new Map(), []);
|
|
354
|
+
return minPath;
|
|
490
355
|
}
|
|
491
|
-
}
|
|
356
|
+
}
|
|
492
357
|
/**
|
|
493
358
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
494
359
|
* /
|
|
@@ -510,98 +375,65 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
510
375
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
511
376
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
|
|
512
377
|
*/
|
|
513
|
-
|
|
514
|
-
var e_7, _a, e_8, _b;
|
|
378
|
+
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
515
379
|
if (getMinDist === undefined)
|
|
516
380
|
getMinDist = false;
|
|
517
381
|
if (genPaths === undefined)
|
|
518
382
|
genPaths = false;
|
|
519
383
|
if (dest === undefined)
|
|
520
384
|
dest = null;
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
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;
|
|
531
395
|
if (!srcVertex) {
|
|
532
396
|
return null;
|
|
533
397
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
if (vertexOrId instanceof AbstractVertex)
|
|
539
|
-
distMap.set(vertexOrId, Infinity);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
543
|
-
finally {
|
|
544
|
-
try {
|
|
545
|
-
if (vertices_2_1 && !vertices_2_1.done && (_a = vertices_2.return)) _a.call(vertices_2);
|
|
546
|
-
}
|
|
547
|
-
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);
|
|
548
402
|
}
|
|
549
403
|
distMap.set(srcVertex, 0);
|
|
550
404
|
preMap.set(srcVertex, null);
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
if (val < min) {
|
|
560
|
-
min = val;
|
|
561
|
-
minV = key;
|
|
562
|
-
}
|
|
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;
|
|
563
413
|
}
|
|
564
414
|
}
|
|
565
415
|
}
|
|
566
|
-
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
567
|
-
finally {
|
|
568
|
-
try {
|
|
569
|
-
if (distMap_1_1 && !distMap_1_1.done && (_a = distMap_1.return)) _a.call(distMap_1);
|
|
570
|
-
}
|
|
571
|
-
finally { if (e_9) throw e_9.error; }
|
|
572
|
-
}
|
|
573
416
|
return minV;
|
|
574
417
|
};
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
while (parent) {
|
|
585
|
-
path.push(parent);
|
|
586
|
-
parent = preMap.get(parent);
|
|
587
|
-
}
|
|
588
|
-
var reversed = path.reverse();
|
|
589
|
-
if (vertex[1] === minV)
|
|
590
|
-
minPath = reversed;
|
|
591
|
-
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);
|
|
592
427
|
}
|
|
428
|
+
const reversed = path.reverse();
|
|
429
|
+
if (vertex[1] === minV)
|
|
430
|
+
minPath = reversed;
|
|
431
|
+
paths.push(reversed);
|
|
593
432
|
}
|
|
594
433
|
}
|
|
595
|
-
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
596
|
-
finally {
|
|
597
|
-
try {
|
|
598
|
-
if (vertices_3_1 && !vertices_3_1.done && (_a = vertices_3.return)) _a.call(vertices_3);
|
|
599
|
-
}
|
|
600
|
-
finally { if (e_10) throw e_10.error; }
|
|
601
|
-
}
|
|
602
434
|
};
|
|
603
|
-
for (
|
|
604
|
-
|
|
435
|
+
for (let i = 1; i < vertices.size; i++) {
|
|
436
|
+
const cur = getMinOfNoSeen();
|
|
605
437
|
if (cur) {
|
|
606
438
|
seen.add(cur);
|
|
607
439
|
if (destVertex && destVertex === cur) {
|
|
@@ -611,38 +443,28 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
611
443
|
if (genPaths) {
|
|
612
444
|
getPaths(destVertex);
|
|
613
445
|
}
|
|
614
|
-
return { distMap
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
if (
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
distMap.set(neighbor, edge.weight + curFromMap);
|
|
629
|
-
preMap.set(neighbor, cur);
|
|
630
|
-
}
|
|
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);
|
|
631
460
|
}
|
|
632
461
|
}
|
|
633
462
|
}
|
|
634
463
|
}
|
|
635
464
|
}
|
|
636
|
-
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
637
|
-
finally {
|
|
638
|
-
try {
|
|
639
|
-
if (neighbors_4_1 && !neighbors_4_1.done && (_b = neighbors_4.return)) _b.call(neighbors_4);
|
|
640
|
-
}
|
|
641
|
-
finally { if (e_8) throw e_8.error; }
|
|
642
|
-
}
|
|
643
465
|
}
|
|
644
466
|
}
|
|
645
|
-
getMinDist && distMap.forEach(
|
|
467
|
+
getMinDist && distMap.forEach((d, v) => {
|
|
646
468
|
if (v !== srcVertex) {
|
|
647
469
|
if (d < minDist) {
|
|
648
470
|
minDist = d;
|
|
@@ -652,8 +474,8 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
652
474
|
}
|
|
653
475
|
});
|
|
654
476
|
genPaths && getPaths(minDest);
|
|
655
|
-
return { distMap
|
|
656
|
-
}
|
|
477
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
478
|
+
}
|
|
657
479
|
/**
|
|
658
480
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
659
481
|
*
|
|
@@ -680,79 +502,57 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
680
502
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
681
503
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
682
504
|
*/
|
|
683
|
-
|
|
684
|
-
var
|
|
685
|
-
var _c;
|
|
505
|
+
dijkstra(src, dest, getMinDist, genPaths) {
|
|
506
|
+
var _a;
|
|
686
507
|
if (getMinDist === undefined)
|
|
687
508
|
getMinDist = false;
|
|
688
509
|
if (genPaths === undefined)
|
|
689
510
|
genPaths = false;
|
|
690
511
|
if (dest === undefined)
|
|
691
512
|
dest = null;
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
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;
|
|
702
523
|
if (!srcVertex) {
|
|
703
524
|
return null;
|
|
704
525
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
if (vertexOrId instanceof AbstractVertex)
|
|
710
|
-
distMap.set(vertexOrId, Infinity);
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
714
|
-
finally {
|
|
715
|
-
try {
|
|
716
|
-
if (vertices_4_1 && !vertices_4_1.done && (_a = vertices_4.return)) _a.call(vertices_4);
|
|
717
|
-
}
|
|
718
|
-
finally { if (e_11) throw e_11.error; }
|
|
526
|
+
for (const vertex of vertices) {
|
|
527
|
+
const vertexOrId = vertex[1];
|
|
528
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
529
|
+
distMap.set(vertexOrId, Infinity);
|
|
719
530
|
}
|
|
720
|
-
|
|
531
|
+
const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.id - b.id });
|
|
721
532
|
heap.add({ id: 0, val: srcVertex });
|
|
722
533
|
distMap.set(srcVertex, 0);
|
|
723
534
|
preMap.set(srcVertex, null);
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
while (parent) {
|
|
734
|
-
path.push(parent);
|
|
735
|
-
parent = preMap.get(parent);
|
|
736
|
-
}
|
|
737
|
-
var reversed = path.reverse();
|
|
738
|
-
if (vertex[1] === minV)
|
|
739
|
-
minPath = reversed;
|
|
740
|
-
paths.push(reversed);
|
|
535
|
+
const getPaths = (minV) => {
|
|
536
|
+
for (const vertex of vertices) {
|
|
537
|
+
const vertexOrId = vertex[1];
|
|
538
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
539
|
+
const path = [vertexOrId];
|
|
540
|
+
let parent = preMap.get(vertexOrId);
|
|
541
|
+
while (parent) {
|
|
542
|
+
path.push(parent);
|
|
543
|
+
parent = preMap.get(parent);
|
|
741
544
|
}
|
|
545
|
+
const reversed = path.reverse();
|
|
546
|
+
if (vertex[1] === minV)
|
|
547
|
+
minPath = reversed;
|
|
548
|
+
paths.push(reversed);
|
|
742
549
|
}
|
|
743
550
|
}
|
|
744
|
-
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
745
|
-
finally {
|
|
746
|
-
try {
|
|
747
|
-
if (vertices_5_1 && !vertices_5_1.done && (_a = vertices_5.return)) _a.call(vertices_5);
|
|
748
|
-
}
|
|
749
|
-
finally { if (e_13) throw e_13.error; }
|
|
750
|
-
}
|
|
751
551
|
};
|
|
752
552
|
while (heap.size > 0) {
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
553
|
+
const curHeapNode = heap.poll();
|
|
554
|
+
const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
|
|
555
|
+
const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
|
|
756
556
|
if (dist !== undefined) {
|
|
757
557
|
if (cur) {
|
|
758
558
|
seen.add(cur);
|
|
@@ -763,39 +563,29 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
763
563
|
if (genPaths) {
|
|
764
564
|
getPaths(destVertex);
|
|
765
565
|
}
|
|
766
|
-
return { distMap
|
|
566
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
767
567
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
if (
|
|
773
|
-
|
|
774
|
-
if (
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
preMap.set(neighbor, cur);
|
|
780
|
-
distMap.set(neighbor, dist + weight);
|
|
781
|
-
}
|
|
568
|
+
const neighbors = this.getNeighbors(cur);
|
|
569
|
+
for (const neighbor of neighbors) {
|
|
570
|
+
if (!seen.has(neighbor)) {
|
|
571
|
+
const weight = (_a = this.getEdge(cur, neighbor)) === null || _a === void 0 ? void 0 : _a.weight;
|
|
572
|
+
if (typeof weight === 'number') {
|
|
573
|
+
const distSrcToNeighbor = distMap.get(neighbor);
|
|
574
|
+
if (distSrcToNeighbor) {
|
|
575
|
+
if (dist + weight < distSrcToNeighbor) {
|
|
576
|
+
heap.add({ id: dist + weight, val: neighbor });
|
|
577
|
+
preMap.set(neighbor, cur);
|
|
578
|
+
distMap.set(neighbor, dist + weight);
|
|
782
579
|
}
|
|
783
580
|
}
|
|
784
581
|
}
|
|
785
582
|
}
|
|
786
583
|
}
|
|
787
|
-
catch (e_12_1) { e_12 = { error: e_12_1 }; }
|
|
788
|
-
finally {
|
|
789
|
-
try {
|
|
790
|
-
if (neighbors_5_1 && !neighbors_5_1.done && (_b = neighbors_5.return)) _b.call(neighbors_5);
|
|
791
|
-
}
|
|
792
|
-
finally { if (e_12) throw e_12.error; }
|
|
793
|
-
}
|
|
794
584
|
}
|
|
795
585
|
}
|
|
796
586
|
}
|
|
797
587
|
if (getMinDist) {
|
|
798
|
-
distMap.forEach(
|
|
588
|
+
distMap.forEach((d, v) => {
|
|
799
589
|
if (v !== srcVertex) {
|
|
800
590
|
if (d < minDist) {
|
|
801
591
|
minDist = d;
|
|
@@ -808,8 +598,8 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
808
598
|
if (genPaths) {
|
|
809
599
|
getPaths(minDest);
|
|
810
600
|
}
|
|
811
|
-
return { distMap
|
|
812
|
-
}
|
|
601
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
602
|
+
}
|
|
813
603
|
/**
|
|
814
604
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
815
605
|
* /
|
|
@@ -850,40 +640,39 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
850
640
|
* vertex.
|
|
851
641
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
852
642
|
*/
|
|
853
|
-
|
|
854
|
-
var e_14, _a;
|
|
643
|
+
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
855
644
|
if (getMin === undefined)
|
|
856
645
|
getMin = false;
|
|
857
646
|
if (genPath === undefined)
|
|
858
647
|
genPath = false;
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
648
|
+
const srcVertex = this._getVertex(src);
|
|
649
|
+
const paths = [];
|
|
650
|
+
const distMap = new Map();
|
|
651
|
+
const preMap = new Map(); // predecessor
|
|
652
|
+
let min = Infinity;
|
|
653
|
+
let minPath = [];
|
|
865
654
|
// TODO
|
|
866
|
-
|
|
655
|
+
let hasNegativeCycle;
|
|
867
656
|
if (scanNegativeCycle)
|
|
868
657
|
hasNegativeCycle = false;
|
|
869
658
|
if (!srcVertex)
|
|
870
|
-
return { hasNegativeCycle
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
this._vertices.forEach(
|
|
659
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
660
|
+
const vertices = this._vertices;
|
|
661
|
+
const numOfVertices = vertices.size;
|
|
662
|
+
const edges = this.edgeSet();
|
|
663
|
+
const numOfEdges = edges.length;
|
|
664
|
+
this._vertices.forEach(vertex => {
|
|
876
665
|
distMap.set(vertex, Infinity);
|
|
877
666
|
});
|
|
878
667
|
distMap.set(srcVertex, 0);
|
|
879
|
-
for (
|
|
880
|
-
for (
|
|
881
|
-
|
|
668
|
+
for (let i = 1; i < numOfVertices; ++i) {
|
|
669
|
+
for (let j = 0; j < numOfEdges; ++j) {
|
|
670
|
+
const ends = this.getEndsOfEdge(edges[j]);
|
|
882
671
|
if (ends) {
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
672
|
+
const [s, d] = ends;
|
|
673
|
+
const weight = edges[j].weight;
|
|
674
|
+
const sWeight = distMap.get(s);
|
|
675
|
+
const dWeight = distMap.get(d);
|
|
887
676
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
888
677
|
if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
|
|
889
678
|
distMap.set(d, sWeight + weight);
|
|
@@ -893,9 +682,9 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
893
682
|
}
|
|
894
683
|
}
|
|
895
684
|
}
|
|
896
|
-
|
|
685
|
+
let minDest = null;
|
|
897
686
|
if (getMin) {
|
|
898
|
-
distMap.forEach(
|
|
687
|
+
distMap.forEach((d, v) => {
|
|
899
688
|
if (v !== srcVertex) {
|
|
900
689
|
if (d < min) {
|
|
901
690
|
min = d;
|
|
@@ -906,46 +695,36 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
906
695
|
});
|
|
907
696
|
}
|
|
908
697
|
if (genPath) {
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
path.push(parent);
|
|
918
|
-
parent = preMap.get(parent);
|
|
919
|
-
}
|
|
920
|
-
var reversed = path.reverse();
|
|
921
|
-
if (vertex[1] === minDest)
|
|
922
|
-
minPath = reversed;
|
|
923
|
-
paths.push(reversed);
|
|
698
|
+
for (const vertex of vertices) {
|
|
699
|
+
const vertexOrId = vertex[1];
|
|
700
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
701
|
+
const path = [vertexOrId];
|
|
702
|
+
let parent = preMap.get(vertexOrId);
|
|
703
|
+
while (parent !== undefined) {
|
|
704
|
+
path.push(parent);
|
|
705
|
+
parent = preMap.get(parent);
|
|
924
706
|
}
|
|
707
|
+
const reversed = path.reverse();
|
|
708
|
+
if (vertex[1] === minDest)
|
|
709
|
+
minPath = reversed;
|
|
710
|
+
paths.push(reversed);
|
|
925
711
|
}
|
|
926
712
|
}
|
|
927
|
-
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
928
|
-
finally {
|
|
929
|
-
try {
|
|
930
|
-
if (vertices_6_1 && !vertices_6_1.done && (_a = vertices_6.return)) _a.call(vertices_6);
|
|
931
|
-
}
|
|
932
|
-
finally { if (e_14) throw e_14.error; }
|
|
933
|
-
}
|
|
934
713
|
}
|
|
935
|
-
for (
|
|
936
|
-
|
|
714
|
+
for (let j = 0; j < numOfEdges; ++j) {
|
|
715
|
+
const ends = this.getEndsOfEdge(edges[j]);
|
|
937
716
|
if (ends) {
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
717
|
+
const [s] = ends;
|
|
718
|
+
const weight = edges[j].weight;
|
|
719
|
+
const sWeight = distMap.get(s);
|
|
941
720
|
if (sWeight) {
|
|
942
721
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
943
722
|
hasNegativeCycle = true;
|
|
944
723
|
}
|
|
945
724
|
}
|
|
946
725
|
}
|
|
947
|
-
return { hasNegativeCycle
|
|
948
|
-
}
|
|
726
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
727
|
+
}
|
|
949
728
|
/**
|
|
950
729
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
951
730
|
* all pairs
|
|
@@ -962,28 +741,28 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
962
741
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
963
742
|
* path between vertices in the
|
|
964
743
|
*/
|
|
965
|
-
|
|
744
|
+
floyd() {
|
|
966
745
|
var _a;
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
746
|
+
const idAndVertices = [...this._vertices];
|
|
747
|
+
const n = idAndVertices.length;
|
|
748
|
+
const costs = [];
|
|
749
|
+
const predecessor = [];
|
|
971
750
|
// successors
|
|
972
|
-
for (
|
|
751
|
+
for (let i = 0; i < n; i++) {
|
|
973
752
|
costs[i] = [];
|
|
974
753
|
predecessor[i] = [];
|
|
975
|
-
for (
|
|
754
|
+
for (let j = 0; j < n; j++) {
|
|
976
755
|
predecessor[i][j] = null;
|
|
977
756
|
}
|
|
978
757
|
}
|
|
979
|
-
for (
|
|
980
|
-
for (
|
|
758
|
+
for (let i = 0; i < n; i++) {
|
|
759
|
+
for (let j = 0; j < n; j++) {
|
|
981
760
|
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
|
|
982
761
|
}
|
|
983
762
|
}
|
|
984
|
-
for (
|
|
985
|
-
for (
|
|
986
|
-
for (
|
|
763
|
+
for (let k = 0; k < n; k++) {
|
|
764
|
+
for (let i = 0; i < n; i++) {
|
|
765
|
+
for (let j = 0; j < n; j++) {
|
|
987
766
|
if (costs[i][j] > costs[i][k] + costs[k][j]) {
|
|
988
767
|
costs[i][j] = costs[i][k] + costs[k][j];
|
|
989
768
|
predecessor[i][j] = idAndVertices[k][1];
|
|
@@ -991,8 +770,8 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
991
770
|
}
|
|
992
771
|
}
|
|
993
772
|
}
|
|
994
|
-
return { costs
|
|
995
|
-
}
|
|
773
|
+
return { costs, predecessor };
|
|
774
|
+
}
|
|
996
775
|
/**
|
|
997
776
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
998
777
|
* Tarjan can find cycles in directed or undirected graph
|
|
@@ -1022,12 +801,11 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
1022
801
|
* are arrays of vertices that form cycles within the SCCs.
|
|
1023
802
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
1024
803
|
*/
|
|
1025
|
-
|
|
804
|
+
tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
1026
805
|
// !! in undirected graph we will not let child visit parent when DFS
|
|
1027
806
|
// !! articulation point(in DFS search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
1028
807
|
// !! bridge: low(child) > dfn(cur)
|
|
1029
|
-
|
|
1030
|
-
var defaultConfig = false;
|
|
808
|
+
const defaultConfig = false;
|
|
1031
809
|
if (needArticulationPoints === undefined)
|
|
1032
810
|
needArticulationPoints = defaultConfig;
|
|
1033
811
|
if (needBridges === undefined)
|
|
@@ -1036,71 +814,60 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
1036
814
|
needSCCs = defaultConfig;
|
|
1037
815
|
if (needCycles === undefined)
|
|
1038
816
|
needCycles = defaultConfig;
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
vertices.forEach(
|
|
817
|
+
const dfnMap = new Map();
|
|
818
|
+
const lowMap = new Map();
|
|
819
|
+
const vertices = this._vertices;
|
|
820
|
+
vertices.forEach(v => {
|
|
1043
821
|
dfnMap.set(v, -1);
|
|
1044
822
|
lowMap.set(v, Infinity);
|
|
1045
823
|
});
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
var e_15, _a;
|
|
824
|
+
const [root] = vertices.values();
|
|
825
|
+
const articulationPoints = [];
|
|
826
|
+
const bridges = [];
|
|
827
|
+
let dfn = 0;
|
|
828
|
+
const dfs = (cur, parent) => {
|
|
1052
829
|
dfn++;
|
|
1053
830
|
dfnMap.set(cur, dfn);
|
|
1054
831
|
lowMap.set(cur, dfn);
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
1076
|
-
articulationPoints.push(cur);
|
|
1077
|
-
}
|
|
832
|
+
const neighbors = this.getNeighbors(cur);
|
|
833
|
+
let childCount = 0; // child in DFS tree not child in graph
|
|
834
|
+
for (const neighbor of neighbors) {
|
|
835
|
+
if (neighbor !== parent) {
|
|
836
|
+
if (dfnMap.get(neighbor) === -1) {
|
|
837
|
+
childCount++;
|
|
838
|
+
dfs(neighbor, cur);
|
|
839
|
+
}
|
|
840
|
+
const childLow = lowMap.get(neighbor);
|
|
841
|
+
const curLow = lowMap.get(cur);
|
|
842
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
843
|
+
if (curLow !== undefined && childLow !== undefined) {
|
|
844
|
+
lowMap.set(cur, Math.min(curLow, childLow));
|
|
845
|
+
}
|
|
846
|
+
const curFromMap = dfnMap.get(cur);
|
|
847
|
+
if (childLow !== undefined && curFromMap !== undefined) {
|
|
848
|
+
if (needArticulationPoints) {
|
|
849
|
+
if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
|
|
850
|
+
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
851
|
+
articulationPoints.push(cur);
|
|
1078
852
|
}
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
853
|
+
}
|
|
854
|
+
if (needBridges) {
|
|
855
|
+
if (childLow > curFromMap) {
|
|
856
|
+
const edgeCurToNeighbor = this.getEdge(cur, neighbor);
|
|
857
|
+
if (edgeCurToNeighbor) {
|
|
858
|
+
bridges.push(edgeCurToNeighbor);
|
|
1085
859
|
}
|
|
1086
860
|
}
|
|
1087
861
|
}
|
|
1088
862
|
}
|
|
1089
863
|
}
|
|
1090
864
|
}
|
|
1091
|
-
catch (e_15_1) { e_15 = { error: e_15_1 }; }
|
|
1092
|
-
finally {
|
|
1093
|
-
try {
|
|
1094
|
-
if (neighbors_6_1 && !neighbors_6_1.done && (_a = neighbors_6.return)) _a.call(neighbors_6);
|
|
1095
|
-
}
|
|
1096
|
-
finally { if (e_15) throw e_15.error; }
|
|
1097
|
-
}
|
|
1098
865
|
};
|
|
1099
866
|
dfs(root, null);
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
lowMap.forEach(
|
|
867
|
+
let SCCs = new Map();
|
|
868
|
+
const getSCCs = () => {
|
|
869
|
+
const SCCs = new Map();
|
|
870
|
+
lowMap.forEach((low, vertex) => {
|
|
1104
871
|
var _a;
|
|
1105
872
|
if (!SCCs.has(low)) {
|
|
1106
873
|
SCCs.set(low, [vertex]);
|
|
@@ -1114,38 +881,37 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
1114
881
|
if (needSCCs) {
|
|
1115
882
|
SCCs = getSCCs();
|
|
1116
883
|
}
|
|
1117
|
-
|
|
884
|
+
const cycles = new Map();
|
|
1118
885
|
if (needCycles) {
|
|
1119
|
-
|
|
1120
|
-
if (
|
|
1121
|
-
|
|
886
|
+
let SCCs = new Map();
|
|
887
|
+
if (SCCs.size < 1) {
|
|
888
|
+
SCCs = getSCCs();
|
|
1122
889
|
}
|
|
1123
|
-
|
|
890
|
+
SCCs.forEach((SCC, low) => {
|
|
1124
891
|
if (SCC.length > 1) {
|
|
1125
892
|
cycles.set(low, SCC);
|
|
1126
893
|
}
|
|
1127
894
|
});
|
|
1128
895
|
}
|
|
1129
|
-
return { dfnMap
|
|
1130
|
-
}
|
|
1131
|
-
|
|
896
|
+
return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
|
|
897
|
+
}
|
|
898
|
+
_addVertexOnly(newVertex) {
|
|
1132
899
|
if (this.hasVertex(newVertex)) {
|
|
1133
900
|
return false;
|
|
1134
901
|
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
1135
902
|
}
|
|
1136
903
|
this._vertices.set(newVertex.id, newVertex);
|
|
1137
904
|
return true;
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
|
|
905
|
+
}
|
|
906
|
+
_getVertex(vertexOrId) {
|
|
907
|
+
const vertexId = this._getVertexId(vertexOrId);
|
|
1141
908
|
return this._vertices.get(vertexId) || null;
|
|
1142
|
-
}
|
|
1143
|
-
|
|
909
|
+
}
|
|
910
|
+
_getVertexId(vertexOrId) {
|
|
1144
911
|
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
1145
|
-
}
|
|
1146
|
-
|
|
912
|
+
}
|
|
913
|
+
_setVertices(value) {
|
|
1147
914
|
this._vertices = value;
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
}());
|
|
915
|
+
}
|
|
916
|
+
}
|
|
1151
917
|
exports.AbstractGraph = AbstractGraph;
|