data-structure-typed 1.34.8 → 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.
Files changed (126) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +1 -1
  3. package/CONTRIBUTING.md +0 -0
  4. package/README.md +8 -8
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +280 -423
  8. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/avl-tree.js +63 -89
  10. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  12. package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
  13. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  15. package/dist/data-structures/binary-tree/binary-tree.js +11 -30
  16. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  17. package/dist/data-structures/binary-tree/bst.js +127 -214
  18. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  19. package/dist/data-structures/binary-tree/rb-tree.js +19 -43
  20. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  21. package/dist/data-structures/binary-tree/segment-tree.js +80 -122
  22. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
  25. package/dist/data-structures/binary-tree/tree-multiset.js +147 -218
  26. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  27. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  28. package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
  29. package/dist/data-structures/graph/abstract-graph.js +338 -572
  30. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  31. package/dist/data-structures/graph/directed-graph.js +145 -275
  32. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  33. package/dist/data-structures/graph/map-graph.js +46 -87
  34. package/dist/data-structures/graph/map-graph.js.map +1 -1
  35. package/dist/data-structures/graph/undirected-graph.js +90 -179
  36. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  37. package/dist/data-structures/hash/coordinate-map.js +23 -45
  38. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  39. package/dist/data-structures/hash/coordinate-set.js +20 -42
  40. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  41. package/dist/data-structures/hash/hash-map.js +85 -247
  42. package/dist/data-structures/hash/hash-map.js.map +1 -1
  43. package/dist/data-structures/hash/hash-table.js +87 -128
  44. package/dist/data-structures/hash/hash-table.js.map +1 -1
  45. package/dist/data-structures/hash/pair.js +2 -5
  46. package/dist/data-structures/hash/pair.js.map +1 -1
  47. package/dist/data-structures/hash/tree-map.js +2 -5
  48. package/dist/data-structures/hash/tree-map.js.map +1 -1
  49. package/dist/data-structures/hash/tree-set.js +2 -5
  50. package/dist/data-structures/hash/tree-set.js.map +1 -1
  51. package/dist/data-structures/heap/heap.js +56 -80
  52. package/dist/data-structures/heap/heap.js.map +1 -1
  53. package/dist/data-structures/heap/max-heap.js +8 -26
  54. package/dist/data-structures/heap/max-heap.js.map +1 -1
  55. package/dist/data-structures/heap/min-heap.js +8 -26
  56. package/dist/data-structures/heap/min-heap.js.map +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
  58. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  59. package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
  60. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  61. package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
  62. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  63. package/dist/data-structures/matrix/matrix.js +7 -8
  64. package/dist/data-structures/matrix/matrix.js.map +1 -1
  65. package/dist/data-structures/matrix/matrix2d.js +57 -70
  66. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  67. package/dist/data-structures/matrix/navigator.js +18 -37
  68. package/dist/data-structures/matrix/navigator.js.map +1 -1
  69. package/dist/data-structures/matrix/vector2d.js +63 -84
  70. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  71. package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
  72. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  73. package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
  74. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  75. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  76. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  77. package/dist/data-structures/queue/deque.js +82 -128
  78. package/dist/data-structures/queue/deque.js.map +1 -1
  79. package/dist/data-structures/queue/queue.js +57 -157
  80. package/dist/data-structures/queue/queue.js.map +1 -1
  81. package/dist/data-structures/stack/stack.js +21 -22
  82. package/dist/data-structures/stack/stack.js.map +1 -1
  83. package/dist/data-structures/tree/tree.js +32 -45
  84. package/dist/data-structures/tree/tree.js.map +1 -1
  85. package/dist/data-structures/trie/trie.js +93 -200
  86. package/dist/data-structures/trie/trie.js.map +1 -1
  87. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  88. package/dist/utils/utils.js +22 -107
  89. package/dist/utils/utils.js.map +1 -1
  90. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +79 -78
  91. package/lib/data-structures/binary-tree/abstract-binary-tree.js +61 -141
  92. package/lib/data-structures/binary-tree/avl-tree.d.ts +9 -0
  93. package/lib/data-structures/binary-tree/avl-tree.js +22 -0
  94. package/lib/data-structures/binary-tree/bst.d.ts +3 -3
  95. package/lib/data-structures/binary-tree/bst.js +12 -15
  96. package/lib/data-structures/binary-tree/tree-multiset.d.ts +16 -18
  97. package/lib/data-structures/binary-tree/tree-multiset.js +26 -36
  98. package/lib/data-structures/graph/abstract-graph.d.ts +2 -2
  99. package/lib/data-structures/graph/abstract-graph.js +5 -5
  100. package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
  101. package/lib/data-structures/priority-queue/priority-queue.js +3 -3
  102. package/lib/interfaces/abstract-binary-tree.d.ts +23 -28
  103. package/lib/interfaces/avl-tree.d.ts +3 -1
  104. package/lib/interfaces/bst.d.ts +2 -1
  105. package/lib/types/data-structures/abstract-binary-tree.js +0 -1
  106. package/package.json +6 -6
  107. package/src/data-structures/binary-tree/abstract-binary-tree.ts +151 -203
  108. package/src/data-structures/binary-tree/avl-tree.ts +29 -0
  109. package/src/data-structures/binary-tree/bst.ts +12 -15
  110. package/src/data-structures/binary-tree/tree-multiset.ts +27 -39
  111. package/src/data-structures/graph/abstract-graph.ts +5 -5
  112. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  113. package/src/interfaces/abstract-binary-tree.ts +23 -36
  114. package/src/interfaces/avl-tree.ts +3 -1
  115. package/src/interfaces/bst.ts +1 -1
  116. package/src/types/data-structures/abstract-binary-tree.ts +0 -1
  117. package/test/integration/avl-tree.test.ts +24 -24
  118. package/test/integration/bst.test.ts +71 -71
  119. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
  120. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -9
  121. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -8
  122. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  123. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +9 -9
  124. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
  125. package/umd/bundle.min.js +1 -1
  126. 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
- var utils_1 = require("../../utils");
41
- var priority_queue_1 = require("../priority-queue");
42
- var AbstractVertex = (function () {
43
- function AbstractVertex(key, val) {
4
+ const utils_1 = require("../../utils");
5
+ const priority_queue_1 = require("../priority-queue");
6
+ class AbstractVertex {
7
+ constructor(key, val) {
44
8
  this._key = key;
45
9
  this._val = val;
46
10
  }
47
- Object.defineProperty(AbstractVertex.prototype, "key", {
48
- get: function () {
49
- return this._key;
50
- },
51
- set: function (v) {
52
- this._key = v;
53
- },
54
- enumerable: false,
55
- configurable: true
56
- });
57
- Object.defineProperty(AbstractVertex.prototype, "val", {
58
- get: function () {
59
- return this._val;
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
- var AbstractEdge = (function () {
71
- function AbstractEdge(weight, val) {
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
- Object.defineProperty(AbstractEdge.prototype, "val", {
77
- get: function () {
78
- return this._val;
79
- },
80
- set: function (value) {
81
- this._val = value;
82
- },
83
- enumerable: false,
84
- configurable: true
85
- });
86
- Object.defineProperty(AbstractEdge.prototype, "weight", {
87
- get: function () {
88
- return this._weight;
89
- },
90
- set: function (v) {
91
- this._weight = v;
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
- return AbstractEdge;
107
- }());
48
+ }
49
+ }
108
50
  exports.AbstractEdge = AbstractEdge;
109
- var AbstractGraph = (function () {
110
- function AbstractGraph() {
51
+ class AbstractGraph {
52
+ constructor() {
111
53
  this._vertices = new Map();
112
54
  }
113
- Object.defineProperty(AbstractGraph.prototype, "vertices", {
114
- get: function () {
115
- return this._vertices;
116
- },
117
- enumerable: false,
118
- configurable: true
119
- });
120
- AbstractGraph.prototype.getVertex = function (vertexKey) {
55
+ get vertices() {
56
+ return this._vertices;
57
+ }
58
+ getVertex(vertexKey) {
121
59
  return this._vertices.get(vertexKey) || null;
122
- };
123
- AbstractGraph.prototype.hasVertex = function (vertexOrKey) {
60
+ }
61
+ hasVertex(vertexOrKey) {
124
62
  return this._vertices.has(this._getVertexKey(vertexOrKey));
125
- };
126
- AbstractGraph.prototype.addVertex = function (keyOrVertex, val) {
63
+ }
64
+ addVertex(keyOrVertex, val) {
127
65
  if (keyOrVertex instanceof AbstractVertex) {
128
66
  return this._addVertexOnly(keyOrVertex);
129
67
  }
130
68
  else {
131
- var newVertex = this.createVertex(keyOrVertex, val);
69
+ const newVertex = this.createVertex(keyOrVertex, val);
132
70
  return this._addVertexOnly(newVertex);
133
71
  }
134
- };
135
- AbstractGraph.prototype.removeVertex = function (vertexOrKey) {
136
- var vertexKey = this._getVertexKey(vertexOrKey);
72
+ }
73
+ removeVertex(vertexOrKey) {
74
+ const vertexKey = this._getVertexKey(vertexOrKey);
137
75
  return this._vertices.delete(vertexKey);
138
- };
139
- AbstractGraph.prototype.removeAllVertices = function (vertices) {
140
- var e_1, _a;
141
- var removed = [];
142
- try {
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; }
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
- AbstractGraph.prototype.hasEdge = function (v1, v2) {
158
- var edge = this.getEdge(v1, v2);
83
+ }
84
+ hasEdge(v1, v2) {
85
+ const edge = this.getEdge(v1, v2);
159
86
  return !!edge;
160
- };
161
- AbstractGraph.prototype.addEdge = function (srcOrEdge, dest, weight, val) {
87
+ }
88
+ addEdge(srcOrEdge, dest, weight, val) {
162
89
  if (srcOrEdge instanceof AbstractEdge) {
163
90
  return this._addEdgeOnly(srcOrEdge);
164
91
  }
@@ -170,16 +97,16 @@ var AbstractGraph = (function () {
170
97
  srcOrEdge = srcOrEdge.key;
171
98
  if (dest instanceof AbstractVertex)
172
99
  dest = dest.key;
173
- var newEdge = this.createEdge(srcOrEdge, dest, weight, val);
100
+ const newEdge = this.createEdge(srcOrEdge, dest, weight, val);
174
101
  return this._addEdgeOnly(newEdge);
175
102
  }
176
103
  else {
177
104
  throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
178
105
  }
179
106
  }
180
- };
181
- AbstractGraph.prototype.setEdgeWeight = function (srcOrKey, destOrKey, weight) {
182
- var edge = this.getEdge(srcOrKey, destOrKey);
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
- AbstractGraph.prototype.getAllPathsBetween = function (v1, v2) {
192
- var _this = this;
193
- var paths = [];
194
- var vertex1 = this._getVertex(v1);
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
- var dfs = function (cur, dest, visiting, path) {
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(__spreadArray([vertex1], __read(path), false));
128
+ paths.push([vertex1, ...path]);
204
129
  }
205
- var neighbors = _this.getNeighbors(cur);
206
- var _loop_1 = function (neighbor) {
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, function (vertex) { return vertex === neighbor; });
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
- AbstractGraph.prototype.getPathSumWeight = function (path) {
142
+ }
143
+ getPathSumWeight(path) {
232
144
  var _a;
233
- var sum = 0;
234
- for (var i = 0; i < path.length; i++) {
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
- AbstractGraph.prototype.getMinCostBetween = function (v1, v2, isWeight) {
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
- var allPaths = this.getAllPathsBetween(v1, v2);
245
- var min = Infinity;
246
- try {
247
- for (var allPaths_1 = __values(allPaths), allPaths_1_1 = allPaths_1.next(); !allPaths_1_1.done; allPaths_1_1 = allPaths_1.next()) {
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
- var vertex2 = this._getVertex(v2);
263
- var vertex1 = this._getVertex(v1);
163
+ const vertex2 = this._getVertex(v2);
164
+ const vertex1 = this._getVertex(v1);
264
165
  if (!(vertex1 && vertex2)) {
265
166
  return null;
266
167
  }
267
- var visited = new Map();
268
- var queue = [vertex1];
168
+ const visited = new Map();
169
+ const queue = [vertex1];
269
170
  visited.set(vertex1, true);
270
- var cost = 0;
171
+ let cost = 0;
271
172
  while (queue.length > 0) {
272
- for (var i = 0; i < queue.length; i++) {
273
- var cur = queue.shift();
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
- var neighbors = this.getNeighbors(cur);
279
- try {
280
- for (var neighbors_2 = (e_4 = void 0, __values(neighbors)), neighbors_2_1 = neighbors_2.next(); !neighbors_2_1.done; neighbors_2_1 = neighbors_2.next()) {
281
- var neighbor = neighbors_2_1.value;
282
- if (!visited.has(neighbor)) {
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
- AbstractGraph.prototype.getMinPathBetween = function (v1, v2, isWeight) {
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
- var allPaths = this.getAllPathsBetween(v1, v2);
309
- var min = Infinity;
310
- var minIndex = -1;
311
- var index = 0;
312
- try {
313
- for (var allPaths_2 = __values(allPaths), allPaths_2_1 = allPaths_2.next(); !allPaths_2_1.done; allPaths_2_1 = allPaths_2.next()) {
314
- var path = allPaths_2_1.value;
315
- var pathSumWeight = this.getPathSumWeight(path);
316
- if (pathSumWeight < min) {
317
- min = pathSumWeight;
318
- minIndex = index;
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
- var minPath_1 = [];
334
- var vertex1_1 = this._getVertex(v1);
335
- var vertex2 = this._getVertex(v2);
336
- if (!(vertex1_1 && vertex2)) {
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
- var dfs_1 = function (cur, dest, visiting, path) {
340
- var e_6, _a;
218
+ const dfs = (cur, dest, visiting, path) => {
341
219
  visiting.set(cur, true);
342
220
  if (cur === dest) {
343
- minPath_1 = __spreadArray([vertex1_1], __read(path), false);
221
+ minPath = [vertex1, ...path];
344
222
  return;
345
223
  }
346
- var neighbors = _this.getNeighbors(cur);
347
- var _loop_2 = function (neighbor) {
224
+ const neighbors = this.getNeighbors(cur);
225
+ for (const neighbor of neighbors) {
348
226
  if (!visiting.get(neighbor)) {
349
227
  path.push(neighbor);
350
- dfs_1(neighbor, dest, visiting, path);
351
- (0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
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
- dfs_1(vertex1_1, vertex2, new Map(), []);
370
- return minPath_1;
234
+ dfs(vertex1, vertex2, new Map(), []);
235
+ return minPath;
371
236
  }
372
- };
373
- AbstractGraph.prototype.dijkstraWithoutHeap = function (src, dest, getMinDist, genPaths) {
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
- var minDist = Infinity;
382
- var minDest = null;
383
- var minPath = [];
384
- var paths = [];
385
- var vertices = this._vertices;
386
- var distMap = new Map();
387
- var seen = new Set();
388
- var preMap = new Map();
389
- var srcVertex = this._getVertex(src);
390
- var destVertex = dest ? this._getVertex(dest) : null;
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
- try {
395
- for (var vertices_2 = __values(vertices), vertices_2_1 = vertices_2.next(); !vertices_2_1.done; vertices_2_1 = vertices_2.next()) {
396
- var vertex = vertices_2_1.value;
397
- var vertexOrKey = vertex[1];
398
- if (vertexOrKey instanceof AbstractVertex)
399
- distMap.set(vertexOrKey, 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
- var getMinOfNoSeen = function () {
412
- var e_9, _a;
413
- var min = Infinity;
414
- var minV = null;
415
- try {
416
- for (var distMap_1 = __values(distMap), distMap_1_1 = distMap_1.next(); !distMap_1_1.done; distMap_1_1 = distMap_1.next()) {
417
- var _b = __read(distMap_1_1.value, 2), key = _b[0], val = _b[1];
418
- if (!seen.has(key)) {
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
- var getPaths = function (minV) {
436
- var e_10, _a;
437
- try {
438
- for (var vertices_3 = __values(vertices), vertices_3_1 = vertices_3.next(); !vertices_3_1.done; vertices_3_1 = vertices_3.next()) {
439
- var vertex = vertices_3_1.value;
440
- var vertexOrKey = vertex[1];
441
- if (vertexOrKey instanceof AbstractVertex) {
442
- var path = [vertexOrKey];
443
- var parent = preMap.get(vertexOrKey);
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 (var i = 1; i < vertices.size; i++) {
464
- var cur = getMinOfNoSeen();
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: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
475
- }
476
- var neighbors = this.getNeighbors(cur);
477
- try {
478
- for (var neighbors_4 = (e_8 = void 0, __values(neighbors)), neighbors_4_1 = neighbors_4.next(); !neighbors_4_1.done; neighbors_4_1 = neighbors_4.next()) {
479
- var neighbor = neighbors_4_1.value;
480
- if (!seen.has(neighbor)) {
481
- var edge = this.getEdge(cur, neighbor);
482
- if (edge) {
483
- var curFromMap = distMap.get(cur);
484
- var neighborFromMap = distMap.get(neighbor);
485
- if (curFromMap !== undefined && neighborFromMap !== undefined) {
486
- if (edge.weight + curFromMap < neighborFromMap) {
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(function (d, v) {
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: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
516
- };
517
- AbstractGraph.prototype.dijkstra = function (src, dest, getMinDist, genPaths) {
518
- var e_11, _a, e_12, _b;
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
- var minDist = Infinity;
527
- var minDest = null;
528
- var minPath = [];
529
- var paths = [];
530
- var vertices = this._vertices;
531
- var distMap = new Map();
532
- var seen = new Set();
533
- var preMap = new Map();
534
- var srcVertex = this._getVertex(src);
535
- var destVertex = dest ? this._getVertex(dest) : null;
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
- try {
539
- for (var vertices_4 = __values(vertices), vertices_4_1 = vertices_4.next(); !vertices_4_1.done; vertices_4_1 = vertices_4.next()) {
540
- var vertex = vertices_4_1.value;
541
- var vertexOrKey = vertex[1];
542
- if (vertexOrKey instanceof AbstractVertex)
543
- distMap.set(vertexOrKey, 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
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
547
- finally {
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.key - b.key; }
364
+ const heap = new priority_queue_1.PriorityQueue({
365
+ comparator: (a, b) => a.key - b.key
555
366
  });
556
367
  heap.add({ key: 0, val: srcVertex });
557
368
  distMap.set(srcVertex, 0);
558
369
  preMap.set(srcVertex, null);
559
- var getPaths = function (minV) {
560
- var e_13, _a;
561
- try {
562
- for (var vertices_5 = __values(vertices), vertices_5_1 = vertices_5.next(); !vertices_5_1.done; vertices_5_1 = vertices_5.next()) {
563
- var vertex = vertices_5_1.value;
564
- var vertexOrKey = vertex[1];
565
- if (vertexOrKey instanceof AbstractVertex) {
566
- var path = [vertexOrKey];
567
- var parent = preMap.get(vertexOrKey);
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
- var curHeapNode = heap.poll();
589
- var dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.key;
590
- var cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
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: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
401
+ return { distMap, preMap, seen, paths, minDist, minPath };
602
402
  }
603
- var neighbors = this.getNeighbors(cur);
604
- try {
605
- for (var neighbors_5 = (e_12 = void 0, __values(neighbors)), neighbors_5_1 = neighbors_5.next(); !neighbors_5_1.done; neighbors_5_1 = neighbors_5.next()) {
606
- var neighbor = neighbors_5_1.value;
607
- if (!seen.has(neighbor)) {
608
- var weight = (_c = this.getEdge(cur, neighbor)) === null || _c === void 0 ? void 0 : _c.weight;
609
- if (typeof weight === 'number') {
610
- var distSrcToNeighbor = distMap.get(neighbor);
611
- if (distSrcToNeighbor) {
612
- if (dist + weight < distSrcToNeighbor) {
613
- heap.add({ key: dist + weight, val: neighbor });
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(function (d, v) {
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: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
647
- };
648
- AbstractGraph.prototype.bellmanFord = function (src, scanNegativeCycle, getMin, genPath) {
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
- var srcVertex = this._getVertex(src);
655
- var paths = [];
656
- var distMap = new Map();
657
- var preMap = new Map();
658
- var min = Infinity;
659
- var minPath = [];
660
- var hasNegativeCycle;
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: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
665
- var vertices = this._vertices;
666
- var numOfVertices = vertices.size;
667
- var edges = this.edgeSet();
668
- var numOfEdges = edges.length;
669
- this._vertices.forEach(function (vertex) {
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 (var i = 1; i < numOfVertices; ++i) {
674
- for (var j = 0; j < numOfEdges; ++j) {
675
- var ends = this.getEndsOfEdge(edges[j]);
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
- var _b = __read(ends, 2), s = _b[0], d = _b[1];
678
- var weight = edges[j].weight;
679
- var sWeight = distMap.get(s);
680
- var dWeight = distMap.get(d);
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
- var minDest = null;
479
+ let minDest = null;
691
480
  if (getMin) {
692
- distMap.forEach(function (d, v) {
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
- try {
704
- for (var vertices_6 = __values(vertices), vertices_6_1 = vertices_6.next(); !vertices_6_1.done; vertices_6_1 = vertices_6.next()) {
705
- var vertex = vertices_6_1.value;
706
- var vertexOrKey = vertex[1];
707
- if (vertexOrKey instanceof AbstractVertex) {
708
- var path = [vertexOrKey];
709
- var parent = preMap.get(vertexOrKey);
710
- while (parent !== undefined) {
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 (var j = 0; j < numOfEdges; ++j) {
730
- var ends = this.getEndsOfEdge(edges[j]);
508
+ for (let j = 0; j < numOfEdges; ++j) {
509
+ const ends = this.getEndsOfEdge(edges[j]);
731
510
  if (ends) {
732
- var _c = __read(ends, 1), s = _c[0];
733
- var weight = edges[j].weight;
734
- var sWeight = distMap.get(s);
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: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
742
- };
743
- AbstractGraph.prototype.floyd = function () {
520
+ return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
521
+ }
522
+ floyd() {
744
523
  var _a;
745
- var idAndVertices = __spreadArray([], __read(this._vertices), false);
746
- var n = idAndVertices.length;
747
- var costs = [];
748
- var predecessor = [];
749
- for (var i = 0; i < n; i++) {
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 (var j = 0; j < n; j++) {
531
+ for (let j = 0; j < n; j++) {
753
532
  predecessor[i][j] = null;
754
533
  }
755
534
  }
756
- for (var i = 0; i < n; i++) {
757
- for (var j = 0; j < n; j++) {
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 (var k = 0; k < n; k++) {
762
- for (var i = 0; i < n; i++) {
763
- for (var j = 0; j < n; j++) {
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: costs, predecessor: predecessor };
772
- };
773
- AbstractGraph.prototype.tarjan = function (needArticulationPoints, needBridges, needSCCs, needCycles) {
774
- var _this = this;
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
- var dfnMap = new Map();
785
- var lowMap = new Map();
786
- var vertices = this._vertices;
787
- vertices.forEach(function (v) {
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
- var _a = __read(vertices.values(), 1), root = _a[0];
792
- var articulationPoints = [];
793
- var bridges = [];
794
- var dfn = 0;
795
- var dfs = function (cur, parent) {
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
- var neighbors = _this.getNeighbors(cur);
801
- var childCount = 0;
802
- try {
803
- for (var neighbors_6 = __values(neighbors), neighbors_6_1 = neighbors_6.next(); !neighbors_6_1.done; neighbors_6_1 = neighbors_6.next()) {
804
- var neighbor = neighbors_6_1.value;
805
- if (neighbor !== parent) {
806
- if (dfnMap.get(neighbor) === -1) {
807
- childCount++;
808
- dfs(neighbor, cur);
809
- }
810
- var childLow = lowMap.get(neighbor);
811
- var curLow = lowMap.get(cur);
812
- if (curLow !== undefined && childLow !== undefined) {
813
- lowMap.set(cur, Math.min(curLow, childLow));
814
- }
815
- var curFromMap = dfnMap.get(cur);
816
- if (childLow !== undefined && curFromMap !== undefined) {
817
- if (needArticulationPoints) {
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
- if (needBridges) {
823
- if (childLow > curFromMap) {
824
- var edgeCurToNeighbor = _this.getEdge(cur, neighbor);
825
- if (edgeCurToNeighbor) {
826
- bridges.push(edgeCurToNeighbor);
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
- var SCCs = new Map();
844
- var getSCCs = function () {
845
- var SCCs = new Map();
846
- lowMap.forEach(function (low, vertex) {
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
- var cycles = new Map();
627
+ const cycles = new Map();
861
628
  if (needCycles) {
862
- var SCCs_1 = new Map();
863
- if (SCCs_1.size < 1) {
864
- SCCs_1 = getSCCs();
629
+ let SCCs = new Map();
630
+ if (SCCs.size < 1) {
631
+ SCCs = getSCCs();
865
632
  }
866
- SCCs_1.forEach(function (SCC, low) {
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: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
873
- };
874
- AbstractGraph.prototype._addVertexOnly = function (newVertex) {
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
645
  this._vertices.set(newVertex.key, newVertex);
879
646
  return true;
880
- };
881
- AbstractGraph.prototype._getVertex = function (vertexOrKey) {
882
- var vertexKey = this._getVertexKey(vertexOrKey);
647
+ }
648
+ _getVertex(vertexOrKey) {
649
+ const vertexKey = this._getVertexKey(vertexOrKey);
883
650
  return this._vertices.get(vertexKey) || null;
884
- };
885
- AbstractGraph.prototype._getVertexKey = function (vertexOrKey) {
651
+ }
652
+ _getVertexKey(vertexOrKey) {
886
653
  return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
887
- };
888
- AbstractGraph.prototype._setVertices = function (value) {
654
+ }
655
+ _setVertices(value) {
889
656
  this._vertices = value;
890
- };
891
- return AbstractGraph;
892
- }());
657
+ }
658
+ }
893
659
  exports.AbstractGraph = AbstractGraph;
894
660
  //# sourceMappingURL=abstract-graph.js.map