data-structure-typed 1.19.2 → 1.19.4

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