data-structure-typed 1.34.7 → 1.34.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +64 -90
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +12 -31
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +157 -244
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +20 -44
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +351 -585
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +152 -282
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +47 -88
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +91 -180
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +33 -46
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
- package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
- package/lib/data-structures/binary-tree/avl-tree.js +35 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +86 -89
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
- package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
- package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
- package/lib/data-structures/graph/abstract-graph.js +86 -86
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +6 -4
- package/lib/interfaces/bst.d.ts +10 -9
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +6 -6
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
- package/src/data-structures/binary-tree/avl-tree.ts +43 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +101 -96
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
- package/src/data-structures/graph/abstract-graph.ts +114 -109
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +37 -50
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +6 -4
- package/src/interfaces/bst.ts +9 -9
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -4
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -1,164 +1,91 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __values = (this && this.__values) || function(o) {
|
|
3
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
-
if (m) return m.call(o);
|
|
5
|
-
if (o && typeof o.length === "number") return {
|
|
6
|
-
next: function () {
|
|
7
|
-
if (o && i >= o.length) o = void 0;
|
|
8
|
-
return { value: o && o[i++], done: !o };
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
-
};
|
|
13
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
-
if (!m) return o;
|
|
16
|
-
var i = m.call(o), r, ar = [], e;
|
|
17
|
-
try {
|
|
18
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
-
}
|
|
20
|
-
catch (error) { e = { error: error }; }
|
|
21
|
-
finally {
|
|
22
|
-
try {
|
|
23
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
-
}
|
|
25
|
-
finally { if (e) throw e.error; }
|
|
26
|
-
}
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
-
if (ar || !(i in from)) {
|
|
32
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
-
ar[i] = from[i];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
this.
|
|
4
|
+
const utils_1 = require("../../utils");
|
|
5
|
+
const priority_queue_1 = require("../priority-queue");
|
|
6
|
+
class AbstractVertex {
|
|
7
|
+
constructor(key, val) {
|
|
8
|
+
this._key = key;
|
|
45
9
|
this._val = val;
|
|
46
10
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
},
|
|
61
|
-
set: function (value) {
|
|
62
|
-
this._val = value;
|
|
63
|
-
},
|
|
64
|
-
enumerable: false,
|
|
65
|
-
configurable: true
|
|
66
|
-
});
|
|
67
|
-
return AbstractVertex;
|
|
68
|
-
}());
|
|
11
|
+
get key() {
|
|
12
|
+
return this._key;
|
|
13
|
+
}
|
|
14
|
+
set key(v) {
|
|
15
|
+
this._key = v;
|
|
16
|
+
}
|
|
17
|
+
get val() {
|
|
18
|
+
return this._val;
|
|
19
|
+
}
|
|
20
|
+
set val(value) {
|
|
21
|
+
this._val = value;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
69
24
|
exports.AbstractVertex = AbstractVertex;
|
|
70
|
-
|
|
71
|
-
|
|
25
|
+
class AbstractEdge {
|
|
26
|
+
constructor(weight, val) {
|
|
72
27
|
this._weight = weight !== undefined ? weight : 1;
|
|
73
28
|
this._val = val;
|
|
74
29
|
this._hashCode = (0, utils_1.uuidV4)();
|
|
75
30
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},
|
|
93
|
-
enumerable: false,
|
|
94
|
-
configurable: true
|
|
95
|
-
});
|
|
96
|
-
Object.defineProperty(AbstractEdge.prototype, "hashCode", {
|
|
97
|
-
get: function () {
|
|
98
|
-
return this._hashCode;
|
|
99
|
-
},
|
|
100
|
-
enumerable: false,
|
|
101
|
-
configurable: true
|
|
102
|
-
});
|
|
103
|
-
AbstractEdge.prototype._setHashCode = function (v) {
|
|
31
|
+
get val() {
|
|
32
|
+
return this._val;
|
|
33
|
+
}
|
|
34
|
+
set val(value) {
|
|
35
|
+
this._val = value;
|
|
36
|
+
}
|
|
37
|
+
get weight() {
|
|
38
|
+
return this._weight;
|
|
39
|
+
}
|
|
40
|
+
set weight(v) {
|
|
41
|
+
this._weight = v;
|
|
42
|
+
}
|
|
43
|
+
get hashCode() {
|
|
44
|
+
return this._hashCode;
|
|
45
|
+
}
|
|
46
|
+
_setHashCode(v) {
|
|
104
47
|
this._hashCode = v;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
}());
|
|
48
|
+
}
|
|
49
|
+
}
|
|
108
50
|
exports.AbstractEdge = AbstractEdge;
|
|
109
|
-
|
|
110
|
-
|
|
51
|
+
class AbstractGraph {
|
|
52
|
+
constructor() {
|
|
111
53
|
this._vertices = new Map();
|
|
112
54
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
};
|
|
126
|
-
AbstractGraph.prototype.addVertex = function (idOrVertex, val) {
|
|
127
|
-
if (idOrVertex instanceof AbstractVertex) {
|
|
128
|
-
return this._addVertexOnly(idOrVertex);
|
|
55
|
+
get vertices() {
|
|
56
|
+
return this._vertices;
|
|
57
|
+
}
|
|
58
|
+
getVertex(vertexKey) {
|
|
59
|
+
return this._vertices.get(vertexKey) || null;
|
|
60
|
+
}
|
|
61
|
+
hasVertex(vertexOrKey) {
|
|
62
|
+
return this._vertices.has(this._getVertexKey(vertexOrKey));
|
|
63
|
+
}
|
|
64
|
+
addVertex(keyOrVertex, val) {
|
|
65
|
+
if (keyOrVertex instanceof AbstractVertex) {
|
|
66
|
+
return this._addVertexOnly(keyOrVertex);
|
|
129
67
|
}
|
|
130
68
|
else {
|
|
131
|
-
|
|
69
|
+
const newVertex = this.createVertex(keyOrVertex, val);
|
|
132
70
|
return this._addVertexOnly(newVertex);
|
|
133
71
|
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return this._vertices.delete(
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
for (var vertices_1 = __values(vertices), vertices_1_1 = vertices_1.next(); !vertices_1_1.done; vertices_1_1 = vertices_1.next()) {
|
|
144
|
-
var v = vertices_1_1.value;
|
|
145
|
-
removed.push(this.removeVertex(v));
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
149
|
-
finally {
|
|
150
|
-
try {
|
|
151
|
-
if (vertices_1_1 && !vertices_1_1.done && (_a = vertices_1.return)) _a.call(vertices_1);
|
|
152
|
-
}
|
|
153
|
-
finally { if (e_1) throw e_1.error; }
|
|
72
|
+
}
|
|
73
|
+
removeVertex(vertexOrKey) {
|
|
74
|
+
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
75
|
+
return this._vertices.delete(vertexKey);
|
|
76
|
+
}
|
|
77
|
+
removeAllVertices(vertices) {
|
|
78
|
+
const removed = [];
|
|
79
|
+
for (const v of vertices) {
|
|
80
|
+
removed.push(this.removeVertex(v));
|
|
154
81
|
}
|
|
155
82
|
return removed.length > 0;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
83
|
+
}
|
|
84
|
+
hasEdge(v1, v2) {
|
|
85
|
+
const edge = this.getEdge(v1, v2);
|
|
159
86
|
return !!edge;
|
|
160
|
-
}
|
|
161
|
-
|
|
87
|
+
}
|
|
88
|
+
addEdge(srcOrEdge, dest, weight, val) {
|
|
162
89
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
163
90
|
return this._addEdgeOnly(srcOrEdge);
|
|
164
91
|
}
|
|
@@ -167,19 +94,19 @@ var AbstractGraph = (function () {
|
|
|
167
94
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
|
|
168
95
|
return false;
|
|
169
96
|
if (srcOrEdge instanceof AbstractVertex)
|
|
170
|
-
srcOrEdge = srcOrEdge.
|
|
97
|
+
srcOrEdge = srcOrEdge.key;
|
|
171
98
|
if (dest instanceof AbstractVertex)
|
|
172
|
-
dest = dest.
|
|
173
|
-
|
|
99
|
+
dest = dest.key;
|
|
100
|
+
const newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
174
101
|
return this._addEdgeOnly(newEdge);
|
|
175
102
|
}
|
|
176
103
|
else {
|
|
177
|
-
throw new Error('dest must be a Vertex or vertex
|
|
104
|
+
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
178
105
|
}
|
|
179
106
|
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
107
|
+
}
|
|
108
|
+
setEdgeWeight(srcOrKey, destOrKey, weight) {
|
|
109
|
+
const edge = this.getEdge(srcOrKey, destOrKey);
|
|
183
110
|
if (edge) {
|
|
184
111
|
edge.weight = weight;
|
|
185
112
|
return true;
|
|
@@ -187,281 +114,186 @@ var AbstractGraph = (function () {
|
|
|
187
114
|
else {
|
|
188
115
|
return false;
|
|
189
116
|
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
var vertex2 = this._getVertex(v2);
|
|
117
|
+
}
|
|
118
|
+
getAllPathsBetween(v1, v2) {
|
|
119
|
+
const paths = [];
|
|
120
|
+
const vertex1 = this._getVertex(v1);
|
|
121
|
+
const vertex2 = this._getVertex(v2);
|
|
196
122
|
if (!(vertex1 && vertex2)) {
|
|
197
123
|
return [];
|
|
198
124
|
}
|
|
199
|
-
|
|
200
|
-
var e_2, _a;
|
|
125
|
+
const dfs = (cur, dest, visiting, path) => {
|
|
201
126
|
visiting.set(cur, true);
|
|
202
127
|
if (cur === dest) {
|
|
203
|
-
paths.push(
|
|
128
|
+
paths.push([vertex1, ...path]);
|
|
204
129
|
}
|
|
205
|
-
|
|
206
|
-
|
|
130
|
+
const neighbors = this.getNeighbors(cur);
|
|
131
|
+
for (const neighbor of neighbors) {
|
|
207
132
|
if (!visiting.get(neighbor)) {
|
|
208
133
|
path.push(neighbor);
|
|
209
134
|
dfs(neighbor, dest, visiting, path);
|
|
210
|
-
(0, utils_1.arrayRemove)(path,
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
try {
|
|
214
|
-
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
|
|
215
|
-
var neighbor = neighbors_1_1.value;
|
|
216
|
-
_loop_1(neighbor);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
220
|
-
finally {
|
|
221
|
-
try {
|
|
222
|
-
if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
|
|
135
|
+
(0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
|
|
223
136
|
}
|
|
224
|
-
finally { if (e_2) throw e_2.error; }
|
|
225
137
|
}
|
|
226
138
|
visiting.set(cur, false);
|
|
227
139
|
};
|
|
228
140
|
dfs(vertex1, vertex2, new Map(), []);
|
|
229
141
|
return paths;
|
|
230
|
-
}
|
|
231
|
-
|
|
142
|
+
}
|
|
143
|
+
getPathSumWeight(path) {
|
|
232
144
|
var _a;
|
|
233
|
-
|
|
234
|
-
for (
|
|
145
|
+
let sum = 0;
|
|
146
|
+
for (let i = 0; i < path.length; i++) {
|
|
235
147
|
sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
|
|
236
148
|
}
|
|
237
149
|
return sum;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
var e_3, _a, e_4, _b;
|
|
150
|
+
}
|
|
151
|
+
getMinCostBetween(v1, v2, isWeight) {
|
|
241
152
|
if (isWeight === undefined)
|
|
242
153
|
isWeight = false;
|
|
243
154
|
if (isWeight) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
var path = allPaths_1_1.value;
|
|
249
|
-
min = Math.min(this.getPathSumWeight(path), min);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
253
|
-
finally {
|
|
254
|
-
try {
|
|
255
|
-
if (allPaths_1_1 && !allPaths_1_1.done && (_a = allPaths_1.return)) _a.call(allPaths_1);
|
|
256
|
-
}
|
|
257
|
-
finally { if (e_3) throw e_3.error; }
|
|
155
|
+
const allPaths = this.getAllPathsBetween(v1, v2);
|
|
156
|
+
let min = Infinity;
|
|
157
|
+
for (const path of allPaths) {
|
|
158
|
+
min = Math.min(this.getPathSumWeight(path), min);
|
|
258
159
|
}
|
|
259
160
|
return min;
|
|
260
161
|
}
|
|
261
162
|
else {
|
|
262
|
-
|
|
263
|
-
|
|
163
|
+
const vertex2 = this._getVertex(v2);
|
|
164
|
+
const vertex1 = this._getVertex(v1);
|
|
264
165
|
if (!(vertex1 && vertex2)) {
|
|
265
166
|
return null;
|
|
266
167
|
}
|
|
267
|
-
|
|
268
|
-
|
|
168
|
+
const visited = new Map();
|
|
169
|
+
const queue = [vertex1];
|
|
269
170
|
visited.set(vertex1, true);
|
|
270
|
-
|
|
171
|
+
let cost = 0;
|
|
271
172
|
while (queue.length > 0) {
|
|
272
|
-
for (
|
|
273
|
-
|
|
173
|
+
for (let i = 0; i < queue.length; i++) {
|
|
174
|
+
const cur = queue.shift();
|
|
274
175
|
if (cur === vertex2) {
|
|
275
176
|
return cost;
|
|
276
177
|
}
|
|
277
178
|
if (cur !== undefined) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
visited.set(neighbor, true);
|
|
284
|
-
queue.push(neighbor);
|
|
285
|
-
}
|
|
179
|
+
const neighbors = this.getNeighbors(cur);
|
|
180
|
+
for (const neighbor of neighbors) {
|
|
181
|
+
if (!visited.has(neighbor)) {
|
|
182
|
+
visited.set(neighbor, true);
|
|
183
|
+
queue.push(neighbor);
|
|
286
184
|
}
|
|
287
185
|
}
|
|
288
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
289
|
-
finally {
|
|
290
|
-
try {
|
|
291
|
-
if (neighbors_2_1 && !neighbors_2_1.done && (_b = neighbors_2.return)) _b.call(neighbors_2);
|
|
292
|
-
}
|
|
293
|
-
finally { if (e_4) throw e_4.error; }
|
|
294
|
-
}
|
|
295
186
|
}
|
|
296
187
|
}
|
|
297
188
|
cost++;
|
|
298
189
|
}
|
|
299
190
|
return null;
|
|
300
191
|
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
var e_5, _a;
|
|
304
|
-
var _this = this;
|
|
192
|
+
}
|
|
193
|
+
getMinPathBetween(v1, v2, isWeight) {
|
|
305
194
|
if (isWeight === undefined)
|
|
306
195
|
isWeight = false;
|
|
307
196
|
if (isWeight) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
320
|
-
index++;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
324
|
-
finally {
|
|
325
|
-
try {
|
|
326
|
-
if (allPaths_2_1 && !allPaths_2_1.done && (_a = allPaths_2.return)) _a.call(allPaths_2);
|
|
327
|
-
}
|
|
328
|
-
finally { if (e_5) throw e_5.error; }
|
|
197
|
+
const allPaths = this.getAllPathsBetween(v1, v2);
|
|
198
|
+
let min = Infinity;
|
|
199
|
+
let minIndex = -1;
|
|
200
|
+
let index = 0;
|
|
201
|
+
for (const path of allPaths) {
|
|
202
|
+
const pathSumWeight = this.getPathSumWeight(path);
|
|
203
|
+
if (pathSumWeight < min) {
|
|
204
|
+
min = pathSumWeight;
|
|
205
|
+
minIndex = index;
|
|
206
|
+
}
|
|
207
|
+
index++;
|
|
329
208
|
}
|
|
330
209
|
return allPaths[minIndex] || null;
|
|
331
210
|
}
|
|
332
211
|
else {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
if (!(
|
|
212
|
+
let minPath = [];
|
|
213
|
+
const vertex1 = this._getVertex(v1);
|
|
214
|
+
const vertex2 = this._getVertex(v2);
|
|
215
|
+
if (!(vertex1 && vertex2)) {
|
|
337
216
|
return [];
|
|
338
217
|
}
|
|
339
|
-
|
|
340
|
-
var e_6, _a;
|
|
218
|
+
const dfs = (cur, dest, visiting, path) => {
|
|
341
219
|
visiting.set(cur, true);
|
|
342
220
|
if (cur === dest) {
|
|
343
|
-
|
|
221
|
+
minPath = [vertex1, ...path];
|
|
344
222
|
return;
|
|
345
223
|
}
|
|
346
|
-
|
|
347
|
-
|
|
224
|
+
const neighbors = this.getNeighbors(cur);
|
|
225
|
+
for (const neighbor of neighbors) {
|
|
348
226
|
if (!visiting.get(neighbor)) {
|
|
349
227
|
path.push(neighbor);
|
|
350
|
-
|
|
351
|
-
(0, utils_1.arrayRemove)(path,
|
|
352
|
-
}
|
|
353
|
-
};
|
|
354
|
-
try {
|
|
355
|
-
for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
|
|
356
|
-
var neighbor = neighbors_3_1.value;
|
|
357
|
-
_loop_2(neighbor);
|
|
228
|
+
dfs(neighbor, dest, visiting, path);
|
|
229
|
+
(0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
|
|
358
230
|
}
|
|
359
231
|
}
|
|
360
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
361
|
-
finally {
|
|
362
|
-
try {
|
|
363
|
-
if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3);
|
|
364
|
-
}
|
|
365
|
-
finally { if (e_6) throw e_6.error; }
|
|
366
|
-
}
|
|
367
232
|
visiting.set(cur, false);
|
|
368
233
|
};
|
|
369
|
-
|
|
370
|
-
return
|
|
234
|
+
dfs(vertex1, vertex2, new Map(), []);
|
|
235
|
+
return minPath;
|
|
371
236
|
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
var e_7, _a, e_8, _b;
|
|
237
|
+
}
|
|
238
|
+
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
375
239
|
if (getMinDist === undefined)
|
|
376
240
|
getMinDist = false;
|
|
377
241
|
if (genPaths === undefined)
|
|
378
242
|
genPaths = false;
|
|
379
243
|
if (dest === undefined)
|
|
380
244
|
dest = null;
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
245
|
+
let minDist = Infinity;
|
|
246
|
+
let minDest = null;
|
|
247
|
+
let minPath = [];
|
|
248
|
+
const paths = [];
|
|
249
|
+
const vertices = this._vertices;
|
|
250
|
+
const distMap = new Map();
|
|
251
|
+
const seen = new Set();
|
|
252
|
+
const preMap = new Map();
|
|
253
|
+
const srcVertex = this._getVertex(src);
|
|
254
|
+
const destVertex = dest ? this._getVertex(dest) : null;
|
|
391
255
|
if (!srcVertex) {
|
|
392
256
|
return null;
|
|
393
257
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
if (vertexOrId instanceof AbstractVertex)
|
|
399
|
-
distMap.set(vertexOrId, Infinity);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
403
|
-
finally {
|
|
404
|
-
try {
|
|
405
|
-
if (vertices_2_1 && !vertices_2_1.done && (_a = vertices_2.return)) _a.call(vertices_2);
|
|
406
|
-
}
|
|
407
|
-
finally { if (e_7) throw e_7.error; }
|
|
258
|
+
for (const vertex of vertices) {
|
|
259
|
+
const vertexOrKey = vertex[1];
|
|
260
|
+
if (vertexOrKey instanceof AbstractVertex)
|
|
261
|
+
distMap.set(vertexOrKey, Infinity);
|
|
408
262
|
}
|
|
409
263
|
distMap.set(srcVertex, 0);
|
|
410
264
|
preMap.set(srcVertex, null);
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
if (val < min) {
|
|
420
|
-
min = val;
|
|
421
|
-
minV = key;
|
|
422
|
-
}
|
|
265
|
+
const getMinOfNoSeen = () => {
|
|
266
|
+
let min = Infinity;
|
|
267
|
+
let minV = null;
|
|
268
|
+
for (const [key, val] of distMap) {
|
|
269
|
+
if (!seen.has(key)) {
|
|
270
|
+
if (val < min) {
|
|
271
|
+
min = val;
|
|
272
|
+
minV = key;
|
|
423
273
|
}
|
|
424
274
|
}
|
|
425
275
|
}
|
|
426
|
-
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
427
|
-
finally {
|
|
428
|
-
try {
|
|
429
|
-
if (distMap_1_1 && !distMap_1_1.done && (_a = distMap_1.return)) _a.call(distMap_1);
|
|
430
|
-
}
|
|
431
|
-
finally { if (e_9) throw e_9.error; }
|
|
432
|
-
}
|
|
433
276
|
return minV;
|
|
434
277
|
};
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
while (parent) {
|
|
445
|
-
path.push(parent);
|
|
446
|
-
parent = preMap.get(parent);
|
|
447
|
-
}
|
|
448
|
-
var reversed = path.reverse();
|
|
449
|
-
if (vertex[1] === minV)
|
|
450
|
-
minPath = reversed;
|
|
451
|
-
paths.push(reversed);
|
|
278
|
+
const getPaths = (minV) => {
|
|
279
|
+
for (const vertex of vertices) {
|
|
280
|
+
const vertexOrKey = vertex[1];
|
|
281
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
282
|
+
const path = [vertexOrKey];
|
|
283
|
+
let parent = preMap.get(vertexOrKey);
|
|
284
|
+
while (parent) {
|
|
285
|
+
path.push(parent);
|
|
286
|
+
parent = preMap.get(parent);
|
|
452
287
|
}
|
|
288
|
+
const reversed = path.reverse();
|
|
289
|
+
if (vertex[1] === minV)
|
|
290
|
+
minPath = reversed;
|
|
291
|
+
paths.push(reversed);
|
|
453
292
|
}
|
|
454
293
|
}
|
|
455
|
-
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
456
|
-
finally {
|
|
457
|
-
try {
|
|
458
|
-
if (vertices_3_1 && !vertices_3_1.done && (_a = vertices_3.return)) _a.call(vertices_3);
|
|
459
|
-
}
|
|
460
|
-
finally { if (e_10) throw e_10.error; }
|
|
461
|
-
}
|
|
462
294
|
};
|
|
463
|
-
for (
|
|
464
|
-
|
|
295
|
+
for (let i = 1; i < vertices.size; i++) {
|
|
296
|
+
const cur = getMinOfNoSeen();
|
|
465
297
|
if (cur) {
|
|
466
298
|
seen.add(cur);
|
|
467
299
|
if (destVertex && destVertex === cur) {
|
|
@@ -471,38 +303,28 @@ var AbstractGraph = (function () {
|
|
|
471
303
|
if (genPaths) {
|
|
472
304
|
getPaths(destVertex);
|
|
473
305
|
}
|
|
474
|
-
return { distMap
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
if (
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
distMap.set(neighbor, edge.weight + curFromMap);
|
|
488
|
-
preMap.set(neighbor, cur);
|
|
489
|
-
}
|
|
306
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
307
|
+
}
|
|
308
|
+
const neighbors = this.getNeighbors(cur);
|
|
309
|
+
for (const neighbor of neighbors) {
|
|
310
|
+
if (!seen.has(neighbor)) {
|
|
311
|
+
const edge = this.getEdge(cur, neighbor);
|
|
312
|
+
if (edge) {
|
|
313
|
+
const curFromMap = distMap.get(cur);
|
|
314
|
+
const neighborFromMap = distMap.get(neighbor);
|
|
315
|
+
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
316
|
+
if (edge.weight + curFromMap < neighborFromMap) {
|
|
317
|
+
distMap.set(neighbor, edge.weight + curFromMap);
|
|
318
|
+
preMap.set(neighbor, cur);
|
|
490
319
|
}
|
|
491
320
|
}
|
|
492
321
|
}
|
|
493
322
|
}
|
|
494
323
|
}
|
|
495
|
-
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
496
|
-
finally {
|
|
497
|
-
try {
|
|
498
|
-
if (neighbors_4_1 && !neighbors_4_1.done && (_b = neighbors_4.return)) _b.call(neighbors_4);
|
|
499
|
-
}
|
|
500
|
-
finally { if (e_8) throw e_8.error; }
|
|
501
|
-
}
|
|
502
324
|
}
|
|
503
325
|
}
|
|
504
326
|
getMinDist &&
|
|
505
|
-
distMap.forEach(
|
|
327
|
+
distMap.forEach((d, v) => {
|
|
506
328
|
if (v !== srcVertex) {
|
|
507
329
|
if (d < minDist) {
|
|
508
330
|
minDist = d;
|
|
@@ -512,82 +334,60 @@ var AbstractGraph = (function () {
|
|
|
512
334
|
}
|
|
513
335
|
});
|
|
514
336
|
genPaths && getPaths(minDest);
|
|
515
|
-
return { distMap
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
var
|
|
519
|
-
var _c;
|
|
337
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
338
|
+
}
|
|
339
|
+
dijkstra(src, dest, getMinDist, genPaths) {
|
|
340
|
+
var _a;
|
|
520
341
|
if (getMinDist === undefined)
|
|
521
342
|
getMinDist = false;
|
|
522
343
|
if (genPaths === undefined)
|
|
523
344
|
genPaths = false;
|
|
524
345
|
if (dest === undefined)
|
|
525
346
|
dest = null;
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
347
|
+
let minDist = Infinity;
|
|
348
|
+
let minDest = null;
|
|
349
|
+
let minPath = [];
|
|
350
|
+
const paths = [];
|
|
351
|
+
const vertices = this._vertices;
|
|
352
|
+
const distMap = new Map();
|
|
353
|
+
const seen = new Set();
|
|
354
|
+
const preMap = new Map();
|
|
355
|
+
const srcVertex = this._getVertex(src);
|
|
356
|
+
const destVertex = dest ? this._getVertex(dest) : null;
|
|
536
357
|
if (!srcVertex)
|
|
537
358
|
return null;
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
if (vertexOrId instanceof AbstractVertex)
|
|
543
|
-
distMap.set(vertexOrId, Infinity);
|
|
544
|
-
}
|
|
359
|
+
for (const vertex of vertices) {
|
|
360
|
+
const vertexOrKey = vertex[1];
|
|
361
|
+
if (vertexOrKey instanceof AbstractVertex)
|
|
362
|
+
distMap.set(vertexOrKey, Infinity);
|
|
545
363
|
}
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
try {
|
|
549
|
-
if (vertices_4_1 && !vertices_4_1.done && (_a = vertices_4.return)) _a.call(vertices_4);
|
|
550
|
-
}
|
|
551
|
-
finally { if (e_11) throw e_11.error; }
|
|
552
|
-
}
|
|
553
|
-
var heap = new priority_queue_1.PriorityQueue({
|
|
554
|
-
comparator: function (a, b) { return a.id - b.id; }
|
|
364
|
+
const heap = new priority_queue_1.PriorityQueue({
|
|
365
|
+
comparator: (a, b) => a.key - b.key
|
|
555
366
|
});
|
|
556
|
-
heap.add({
|
|
367
|
+
heap.add({ key: 0, val: srcVertex });
|
|
557
368
|
distMap.set(srcVertex, 0);
|
|
558
369
|
preMap.set(srcVertex, null);
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
while (parent) {
|
|
569
|
-
path.push(parent);
|
|
570
|
-
parent = preMap.get(parent);
|
|
571
|
-
}
|
|
572
|
-
var reversed = path.reverse();
|
|
573
|
-
if (vertex[1] === minV)
|
|
574
|
-
minPath = reversed;
|
|
575
|
-
paths.push(reversed);
|
|
370
|
+
const getPaths = (minV) => {
|
|
371
|
+
for (const vertex of vertices) {
|
|
372
|
+
const vertexOrKey = vertex[1];
|
|
373
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
374
|
+
const path = [vertexOrKey];
|
|
375
|
+
let parent = preMap.get(vertexOrKey);
|
|
376
|
+
while (parent) {
|
|
377
|
+
path.push(parent);
|
|
378
|
+
parent = preMap.get(parent);
|
|
576
379
|
}
|
|
380
|
+
const reversed = path.reverse();
|
|
381
|
+
if (vertex[1] === minV)
|
|
382
|
+
minPath = reversed;
|
|
383
|
+
paths.push(reversed);
|
|
577
384
|
}
|
|
578
385
|
}
|
|
579
|
-
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
580
|
-
finally {
|
|
581
|
-
try {
|
|
582
|
-
if (vertices_5_1 && !vertices_5_1.done && (_a = vertices_5.return)) _a.call(vertices_5);
|
|
583
|
-
}
|
|
584
|
-
finally { if (e_13) throw e_13.error; }
|
|
585
|
-
}
|
|
586
386
|
};
|
|
587
387
|
while (heap.size > 0) {
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
388
|
+
const curHeapNode = heap.poll();
|
|
389
|
+
const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.key;
|
|
390
|
+
const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
|
|
591
391
|
if (dist !== undefined) {
|
|
592
392
|
if (cur) {
|
|
593
393
|
seen.add(cur);
|
|
@@ -598,39 +398,29 @@ var AbstractGraph = (function () {
|
|
|
598
398
|
if (genPaths) {
|
|
599
399
|
getPaths(destVertex);
|
|
600
400
|
}
|
|
601
|
-
return { distMap
|
|
401
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
602
402
|
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
if (
|
|
608
|
-
|
|
609
|
-
if (
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
preMap.set(neighbor, cur);
|
|
615
|
-
distMap.set(neighbor, dist + weight);
|
|
616
|
-
}
|
|
403
|
+
const neighbors = this.getNeighbors(cur);
|
|
404
|
+
for (const neighbor of neighbors) {
|
|
405
|
+
if (!seen.has(neighbor)) {
|
|
406
|
+
const weight = (_a = this.getEdge(cur, neighbor)) === null || _a === void 0 ? void 0 : _a.weight;
|
|
407
|
+
if (typeof weight === 'number') {
|
|
408
|
+
const distSrcToNeighbor = distMap.get(neighbor);
|
|
409
|
+
if (distSrcToNeighbor) {
|
|
410
|
+
if (dist + weight < distSrcToNeighbor) {
|
|
411
|
+
heap.add({ key: dist + weight, val: neighbor });
|
|
412
|
+
preMap.set(neighbor, cur);
|
|
413
|
+
distMap.set(neighbor, dist + weight);
|
|
617
414
|
}
|
|
618
415
|
}
|
|
619
416
|
}
|
|
620
417
|
}
|
|
621
418
|
}
|
|
622
|
-
catch (e_12_1) { e_12 = { error: e_12_1 }; }
|
|
623
|
-
finally {
|
|
624
|
-
try {
|
|
625
|
-
if (neighbors_5_1 && !neighbors_5_1.done && (_b = neighbors_5.return)) _b.call(neighbors_5);
|
|
626
|
-
}
|
|
627
|
-
finally { if (e_12) throw e_12.error; }
|
|
628
|
-
}
|
|
629
419
|
}
|
|
630
420
|
}
|
|
631
421
|
}
|
|
632
422
|
if (getMinDist) {
|
|
633
|
-
distMap.forEach(
|
|
423
|
+
distMap.forEach((d, v) => {
|
|
634
424
|
if (v !== srcVertex) {
|
|
635
425
|
if (d < minDist) {
|
|
636
426
|
minDist = d;
|
|
@@ -643,41 +433,40 @@ var AbstractGraph = (function () {
|
|
|
643
433
|
if (genPaths) {
|
|
644
434
|
getPaths(minDest);
|
|
645
435
|
}
|
|
646
|
-
return { distMap
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
var e_14, _a;
|
|
436
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
437
|
+
}
|
|
438
|
+
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
650
439
|
if (getMin === undefined)
|
|
651
440
|
getMin = false;
|
|
652
441
|
if (genPath === undefined)
|
|
653
442
|
genPath = false;
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
443
|
+
const srcVertex = this._getVertex(src);
|
|
444
|
+
const paths = [];
|
|
445
|
+
const distMap = new Map();
|
|
446
|
+
const preMap = new Map();
|
|
447
|
+
let min = Infinity;
|
|
448
|
+
let minPath = [];
|
|
449
|
+
let hasNegativeCycle;
|
|
661
450
|
if (scanNegativeCycle)
|
|
662
451
|
hasNegativeCycle = false;
|
|
663
452
|
if (!srcVertex)
|
|
664
|
-
return { hasNegativeCycle
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
this._vertices.forEach(
|
|
453
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
454
|
+
const vertices = this._vertices;
|
|
455
|
+
const numOfVertices = vertices.size;
|
|
456
|
+
const edges = this.edgeSet();
|
|
457
|
+
const numOfEdges = edges.length;
|
|
458
|
+
this._vertices.forEach(vertex => {
|
|
670
459
|
distMap.set(vertex, Infinity);
|
|
671
460
|
});
|
|
672
461
|
distMap.set(srcVertex, 0);
|
|
673
|
-
for (
|
|
674
|
-
for (
|
|
675
|
-
|
|
462
|
+
for (let i = 1; i < numOfVertices; ++i) {
|
|
463
|
+
for (let j = 0; j < numOfEdges; ++j) {
|
|
464
|
+
const ends = this.getEndsOfEdge(edges[j]);
|
|
676
465
|
if (ends) {
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
466
|
+
const [s, d] = ends;
|
|
467
|
+
const weight = edges[j].weight;
|
|
468
|
+
const sWeight = distMap.get(s);
|
|
469
|
+
const dWeight = distMap.get(d);
|
|
681
470
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
682
471
|
if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
|
|
683
472
|
distMap.set(d, sWeight + weight);
|
|
@@ -687,9 +476,9 @@ var AbstractGraph = (function () {
|
|
|
687
476
|
}
|
|
688
477
|
}
|
|
689
478
|
}
|
|
690
|
-
|
|
479
|
+
let minDest = null;
|
|
691
480
|
if (getMin) {
|
|
692
|
-
distMap.forEach(
|
|
481
|
+
distMap.forEach((d, v) => {
|
|
693
482
|
if (v !== srcVertex) {
|
|
694
483
|
if (d < min) {
|
|
695
484
|
min = d;
|
|
@@ -700,67 +489,57 @@ var AbstractGraph = (function () {
|
|
|
700
489
|
});
|
|
701
490
|
}
|
|
702
491
|
if (genPath) {
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
path.push(parent);
|
|
712
|
-
parent = preMap.get(parent);
|
|
713
|
-
}
|
|
714
|
-
var reversed = path.reverse();
|
|
715
|
-
if (vertex[1] === minDest)
|
|
716
|
-
minPath = reversed;
|
|
717
|
-
paths.push(reversed);
|
|
492
|
+
for (const vertex of vertices) {
|
|
493
|
+
const vertexOrKey = vertex[1];
|
|
494
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
495
|
+
const path = [vertexOrKey];
|
|
496
|
+
let parent = preMap.get(vertexOrKey);
|
|
497
|
+
while (parent !== undefined) {
|
|
498
|
+
path.push(parent);
|
|
499
|
+
parent = preMap.get(parent);
|
|
718
500
|
}
|
|
501
|
+
const reversed = path.reverse();
|
|
502
|
+
if (vertex[1] === minDest)
|
|
503
|
+
minPath = reversed;
|
|
504
|
+
paths.push(reversed);
|
|
719
505
|
}
|
|
720
506
|
}
|
|
721
|
-
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
722
|
-
finally {
|
|
723
|
-
try {
|
|
724
|
-
if (vertices_6_1 && !vertices_6_1.done && (_a = vertices_6.return)) _a.call(vertices_6);
|
|
725
|
-
}
|
|
726
|
-
finally { if (e_14) throw e_14.error; }
|
|
727
|
-
}
|
|
728
507
|
}
|
|
729
|
-
for (
|
|
730
|
-
|
|
508
|
+
for (let j = 0; j < numOfEdges; ++j) {
|
|
509
|
+
const ends = this.getEndsOfEdge(edges[j]);
|
|
731
510
|
if (ends) {
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
511
|
+
const [s] = ends;
|
|
512
|
+
const weight = edges[j].weight;
|
|
513
|
+
const sWeight = distMap.get(s);
|
|
735
514
|
if (sWeight) {
|
|
736
515
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
737
516
|
hasNegativeCycle = true;
|
|
738
517
|
}
|
|
739
518
|
}
|
|
740
519
|
}
|
|
741
|
-
return { hasNegativeCycle
|
|
742
|
-
}
|
|
743
|
-
|
|
520
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
521
|
+
}
|
|
522
|
+
floyd() {
|
|
744
523
|
var _a;
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
for (
|
|
524
|
+
const idAndVertices = [...this._vertices];
|
|
525
|
+
const n = idAndVertices.length;
|
|
526
|
+
const costs = [];
|
|
527
|
+
const predecessor = [];
|
|
528
|
+
for (let i = 0; i < n; i++) {
|
|
750
529
|
costs[i] = [];
|
|
751
530
|
predecessor[i] = [];
|
|
752
|
-
for (
|
|
531
|
+
for (let j = 0; j < n; j++) {
|
|
753
532
|
predecessor[i][j] = null;
|
|
754
533
|
}
|
|
755
534
|
}
|
|
756
|
-
for (
|
|
757
|
-
for (
|
|
535
|
+
for (let i = 0; i < n; i++) {
|
|
536
|
+
for (let j = 0; j < n; j++) {
|
|
758
537
|
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
|
|
759
538
|
}
|
|
760
539
|
}
|
|
761
|
-
for (
|
|
762
|
-
for (
|
|
763
|
-
for (
|
|
540
|
+
for (let k = 0; k < n; k++) {
|
|
541
|
+
for (let i = 0; i < n; i++) {
|
|
542
|
+
for (let j = 0; j < n; j++) {
|
|
764
543
|
if (costs[i][j] > costs[i][k] + costs[k][j]) {
|
|
765
544
|
costs[i][j] = costs[i][k] + costs[k][j];
|
|
766
545
|
predecessor[i][j] = idAndVertices[k][1];
|
|
@@ -768,11 +547,10 @@ var AbstractGraph = (function () {
|
|
|
768
547
|
}
|
|
769
548
|
}
|
|
770
549
|
}
|
|
771
|
-
return { costs
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
var defaultConfig = false;
|
|
550
|
+
return { costs, predecessor };
|
|
551
|
+
}
|
|
552
|
+
tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
553
|
+
const defaultConfig = false;
|
|
776
554
|
if (needArticulationPoints === undefined)
|
|
777
555
|
needArticulationPoints = defaultConfig;
|
|
778
556
|
if (needBridges === undefined)
|
|
@@ -781,69 +559,58 @@ var AbstractGraph = (function () {
|
|
|
781
559
|
needSCCs = defaultConfig;
|
|
782
560
|
if (needCycles === undefined)
|
|
783
561
|
needCycles = defaultConfig;
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
vertices.forEach(
|
|
562
|
+
const dfnMap = new Map();
|
|
563
|
+
const lowMap = new Map();
|
|
564
|
+
const vertices = this._vertices;
|
|
565
|
+
vertices.forEach(v => {
|
|
788
566
|
dfnMap.set(v, -1);
|
|
789
567
|
lowMap.set(v, Infinity);
|
|
790
568
|
});
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
var e_15, _a;
|
|
569
|
+
const [root] = vertices.values();
|
|
570
|
+
const articulationPoints = [];
|
|
571
|
+
const bridges = [];
|
|
572
|
+
let dfn = 0;
|
|
573
|
+
const dfs = (cur, parent) => {
|
|
797
574
|
dfn++;
|
|
798
575
|
dfnMap.set(cur, dfn);
|
|
799
576
|
lowMap.set(cur, dfn);
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
|
|
819
|
-
articulationPoints.push(cur);
|
|
820
|
-
}
|
|
577
|
+
const neighbors = this.getNeighbors(cur);
|
|
578
|
+
let childCount = 0;
|
|
579
|
+
for (const neighbor of neighbors) {
|
|
580
|
+
if (neighbor !== parent) {
|
|
581
|
+
if (dfnMap.get(neighbor) === -1) {
|
|
582
|
+
childCount++;
|
|
583
|
+
dfs(neighbor, cur);
|
|
584
|
+
}
|
|
585
|
+
const childLow = lowMap.get(neighbor);
|
|
586
|
+
const curLow = lowMap.get(cur);
|
|
587
|
+
if (curLow !== undefined && childLow !== undefined) {
|
|
588
|
+
lowMap.set(cur, Math.min(curLow, childLow));
|
|
589
|
+
}
|
|
590
|
+
const curFromMap = dfnMap.get(cur);
|
|
591
|
+
if (childLow !== undefined && curFromMap !== undefined) {
|
|
592
|
+
if (needArticulationPoints) {
|
|
593
|
+
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
|
|
594
|
+
articulationPoints.push(cur);
|
|
821
595
|
}
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
596
|
+
}
|
|
597
|
+
if (needBridges) {
|
|
598
|
+
if (childLow > curFromMap) {
|
|
599
|
+
const edgeCurToNeighbor = this.getEdge(cur, neighbor);
|
|
600
|
+
if (edgeCurToNeighbor) {
|
|
601
|
+
bridges.push(edgeCurToNeighbor);
|
|
828
602
|
}
|
|
829
603
|
}
|
|
830
604
|
}
|
|
831
605
|
}
|
|
832
606
|
}
|
|
833
607
|
}
|
|
834
|
-
catch (e_15_1) { e_15 = { error: e_15_1 }; }
|
|
835
|
-
finally {
|
|
836
|
-
try {
|
|
837
|
-
if (neighbors_6_1 && !neighbors_6_1.done && (_a = neighbors_6.return)) _a.call(neighbors_6);
|
|
838
|
-
}
|
|
839
|
-
finally { if (e_15) throw e_15.error; }
|
|
840
|
-
}
|
|
841
608
|
};
|
|
842
609
|
dfs(root, null);
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
lowMap.forEach(
|
|
610
|
+
let SCCs = new Map();
|
|
611
|
+
const getSCCs = () => {
|
|
612
|
+
const SCCs = new Map();
|
|
613
|
+
lowMap.forEach((low, vertex) => {
|
|
847
614
|
var _a;
|
|
848
615
|
if (!SCCs.has(low)) {
|
|
849
616
|
SCCs.set(low, [vertex]);
|
|
@@ -857,38 +624,37 @@ var AbstractGraph = (function () {
|
|
|
857
624
|
if (needSCCs) {
|
|
858
625
|
SCCs = getSCCs();
|
|
859
626
|
}
|
|
860
|
-
|
|
627
|
+
const cycles = new Map();
|
|
861
628
|
if (needCycles) {
|
|
862
|
-
|
|
863
|
-
if (
|
|
864
|
-
|
|
629
|
+
let SCCs = new Map();
|
|
630
|
+
if (SCCs.size < 1) {
|
|
631
|
+
SCCs = getSCCs();
|
|
865
632
|
}
|
|
866
|
-
|
|
633
|
+
SCCs.forEach((SCC, low) => {
|
|
867
634
|
if (SCC.length > 1) {
|
|
868
635
|
cycles.set(low, SCC);
|
|
869
636
|
}
|
|
870
637
|
});
|
|
871
638
|
}
|
|
872
|
-
return { dfnMap
|
|
873
|
-
}
|
|
874
|
-
|
|
639
|
+
return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
|
|
640
|
+
}
|
|
641
|
+
_addVertexOnly(newVertex) {
|
|
875
642
|
if (this.hasVertex(newVertex)) {
|
|
876
643
|
return false;
|
|
877
644
|
}
|
|
878
|
-
this._vertices.set(newVertex.
|
|
645
|
+
this._vertices.set(newVertex.key, newVertex);
|
|
879
646
|
return true;
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
return this._vertices.get(
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
return
|
|
887
|
-
}
|
|
888
|
-
|
|
647
|
+
}
|
|
648
|
+
_getVertex(vertexOrKey) {
|
|
649
|
+
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
650
|
+
return this._vertices.get(vertexKey) || null;
|
|
651
|
+
}
|
|
652
|
+
_getVertexKey(vertexOrKey) {
|
|
653
|
+
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
654
|
+
}
|
|
655
|
+
_setVertices(value) {
|
|
889
656
|
this._vertices = value;
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
}());
|
|
657
|
+
}
|
|
658
|
+
}
|
|
893
659
|
exports.AbstractGraph = AbstractGraph;
|
|
894
660
|
//# sourceMappingURL=abstract-graph.js.map
|