data-structure-typed 0.8.18 → 1.12.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 (287) hide show
  1. package/.dependency-cruiser.js +449 -0
  2. package/.idea/data-structure-typed.iml +2 -0
  3. package/.idea/modules.xml +1 -1
  4. package/README.md +298 -2
  5. package/dist/data-structures/binary-tree/aa-tree.js +5 -2
  6. package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
  7. package/dist/data-structures/binary-tree/avl-tree.js +150 -46
  8. package/dist/data-structures/binary-tree/b-tree.js +5 -2
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
  12. package/dist/data-structures/binary-tree/binary-tree.js +747 -369
  13. package/dist/data-structures/binary-tree/bst.d.ts +20 -8
  14. package/dist/data-structures/binary-tree/bst.js +164 -107
  15. package/dist/data-structures/binary-tree/rb-tree.js +5 -2
  16. package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
  17. package/dist/data-structures/binary-tree/segment-tree.js +95 -61
  18. package/dist/data-structures/binary-tree/splay-tree.js +5 -2
  19. package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
  20. package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
  21. package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
  22. package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
  23. package/dist/data-structures/graph/abstract-graph.js +712 -323
  24. package/dist/data-structures/graph/directed-graph.d.ts +114 -12
  25. package/dist/data-structures/graph/directed-graph.js +372 -128
  26. package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
  27. package/dist/data-structures/graph/undirected-graph.js +233 -81
  28. package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
  29. package/dist/data-structures/hash/coordinate-map.js +70 -20
  30. package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
  31. package/dist/data-structures/hash/coordinate-set.js +58 -15
  32. package/dist/data-structures/hash/index.d.ts +5 -0
  33. package/dist/data-structures/hash/index.js +5 -0
  34. package/dist/data-structures/heap/heap.d.ts +26 -37
  35. package/dist/data-structures/heap/heap.js +56 -60
  36. package/dist/data-structures/heap/max-heap.d.ts +8 -2
  37. package/dist/data-structures/heap/max-heap.js +32 -9
  38. package/dist/data-structures/heap/min-heap.d.ts +9 -2
  39. package/dist/data-structures/heap/min-heap.js +33 -9
  40. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
  41. package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
  42. package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
  43. package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
  44. package/dist/data-structures/matrix/matrix.d.ts +9 -0
  45. package/dist/data-structures/matrix/matrix.js +19 -7
  46. package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
  47. package/dist/data-structures/matrix/matrix2d.js +158 -61
  48. package/dist/data-structures/matrix/navigator.d.ts +34 -16
  49. package/dist/data-structures/matrix/navigator.js +65 -18
  50. package/dist/data-structures/matrix/vector2d.d.ts +153 -29
  51. package/dist/data-structures/matrix/vector2d.js +249 -102
  52. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
  53. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
  54. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
  55. package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
  56. package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
  57. package/dist/data-structures/priority-queue/priority-queue.js +285 -116
  58. package/dist/data-structures/queue/deque.d.ts +69 -0
  59. package/dist/data-structures/queue/deque.js +151 -56
  60. package/dist/data-structures/queue/queue.d.ts +34 -37
  61. package/dist/data-structures/queue/queue.js +59 -61
  62. package/dist/data-structures/stack/stack.d.ts +29 -35
  63. package/dist/data-structures/stack/stack.js +51 -56
  64. package/dist/data-structures/trie/trie.d.ts +36 -6
  65. package/dist/data-structures/trie/trie.js +256 -83
  66. package/dist/data-structures/types/abstract-graph.d.ts +29 -0
  67. package/dist/data-structures/types/abstract-graph.js +2 -0
  68. package/dist/data-structures/types/avl-tree.d.ts +5 -0
  69. package/dist/data-structures/types/avl-tree.js +2 -0
  70. package/dist/data-structures/types/binary-tree.d.ts +16 -0
  71. package/dist/data-structures/types/binary-tree.js +2 -0
  72. package/dist/data-structures/types/bst.d.ts +7 -0
  73. package/dist/data-structures/types/bst.js +2 -0
  74. package/dist/data-structures/types/directed-graph.d.ts +10 -0
  75. package/dist/data-structures/types/directed-graph.js +2 -0
  76. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
  77. package/dist/data-structures/types/doubly-linked-list.js +2 -0
  78. package/dist/data-structures/types/heap.d.ts +7 -0
  79. package/dist/data-structures/types/heap.js +2 -0
  80. package/dist/data-structures/types/index.d.ts +13 -0
  81. package/dist/data-structures/types/index.js +29 -0
  82. package/dist/data-structures/types/navigator.d.ts +14 -0
  83. package/dist/data-structures/types/navigator.js +2 -0
  84. package/dist/data-structures/types/priority-queue.d.ts +7 -0
  85. package/dist/data-structures/types/priority-queue.js +2 -0
  86. package/dist/data-structures/types/segment-tree.d.ts +1 -0
  87. package/dist/data-structures/types/segment-tree.js +2 -0
  88. package/dist/data-structures/types/singly-linked-list.js +2 -0
  89. package/dist/data-structures/types/tree-multiset.d.ts +5 -0
  90. package/dist/data-structures/types/tree-multiset.js +2 -0
  91. package/dist/utils/trampoline.d.ts +14 -0
  92. package/dist/utils/trampoline.js +130 -0
  93. package/dist/utils/types/index.js +17 -0
  94. package/dist/{types → utils}/types/utils.d.ts +15 -1
  95. package/dist/{types → utils/types}/utils.js +21 -19
  96. package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
  97. package/dist/utils/utils.js +651 -0
  98. package/docs/.nojekyll +1 -0
  99. package/docs/assets/highlight.css +85 -0
  100. package/docs/assets/main.js +58 -0
  101. package/docs/assets/search.js +1 -0
  102. package/docs/assets/style.css +1367 -0
  103. package/docs/classes/AVLTree.html +2046 -0
  104. package/docs/classes/AVLTreeNode.html +423 -0
  105. package/docs/classes/AaTree.html +117 -0
  106. package/docs/classes/AbstractEdge.html +198 -0
  107. package/docs/classes/AbstractGraph.html +891 -0
  108. package/docs/classes/AbstractVertex.html +164 -0
  109. package/docs/classes/ArrayDeque.html +384 -0
  110. package/docs/classes/BST.html +1893 -0
  111. package/docs/classes/BSTNode.html +425 -0
  112. package/docs/classes/BTree.html +117 -0
  113. package/docs/classes/BinaryIndexedTree.html +244 -0
  114. package/docs/classes/BinaryTree.html +1754 -0
  115. package/docs/classes/BinaryTreeNode.html +396 -0
  116. package/docs/classes/Character.html +165 -0
  117. package/docs/classes/CoordinateMap.html +394 -0
  118. package/docs/classes/CoordinateSet.html +355 -0
  119. package/docs/classes/Deque.html +617 -0
  120. package/docs/classes/DirectedEdge.html +247 -0
  121. package/docs/classes/DirectedGraph.html +1207 -0
  122. package/docs/classes/DirectedVertex.html +154 -0
  123. package/docs/classes/DoublyLinkedList.html +619 -0
  124. package/docs/classes/DoublyLinkedListNode.html +160 -0
  125. package/docs/classes/Heap.html +315 -0
  126. package/docs/classes/Matrix2D.html +447 -0
  127. package/docs/classes/MatrixNTI2D.html +181 -0
  128. package/docs/classes/MaxHeap.html +325 -0
  129. package/docs/classes/MaxPriorityQueue.html +668 -0
  130. package/docs/classes/MinHeap.html +326 -0
  131. package/docs/classes/MinPriorityQueue.html +668 -0
  132. package/docs/classes/Navigator.html +285 -0
  133. package/docs/classes/ObjectDeque.html +289 -0
  134. package/docs/classes/PriorityQueue.html +643 -0
  135. package/docs/classes/Queue.html +337 -0
  136. package/docs/classes/RBTree.html +117 -0
  137. package/docs/classes/SegmentTree.html +234 -0
  138. package/docs/classes/SegmentTreeNode.html +302 -0
  139. package/docs/classes/SinglyLinkedList.html +1035 -0
  140. package/docs/classes/SinglyLinkedListNode.html +304 -0
  141. package/docs/classes/SplayTree.html +117 -0
  142. package/docs/classes/Stack.html +313 -0
  143. package/docs/classes/TreeMultiSet.html +1897 -0
  144. package/docs/classes/Trie.html +317 -0
  145. package/docs/classes/TrieNode.html +221 -0
  146. package/docs/classes/TwoThreeTree.html +117 -0
  147. package/docs/classes/UndirectedEdge.html +220 -0
  148. package/docs/classes/UndirectedGraph.html +1006 -0
  149. package/docs/classes/UndirectedVertex.html +154 -0
  150. package/docs/classes/Vector2D.html +746 -0
  151. package/docs/enums/CP.html +126 -0
  152. package/docs/enums/FamilyPosition.html +126 -0
  153. package/docs/enums/LoopType.html +119 -0
  154. package/docs/index.html +288 -0
  155. package/docs/modules.html +146 -0
  156. package/jest.config.js +5 -0
  157. package/package.json +33 -47
  158. package/rename_clear_files.sh +29 -0
  159. package/src/assets/complexities-diff.jpg +0 -0
  160. package/src/assets/data-structure-complexities.jpg +0 -0
  161. package/src/data-structures/binary-tree/avl-tree.ts +58 -6
  162. package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
  163. package/src/data-structures/binary-tree/binary-tree.ts +460 -145
  164. package/src/data-structures/binary-tree/bst.ts +31 -25
  165. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  166. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  167. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  168. package/src/data-structures/binary-tree/segment-tree.ts +25 -12
  169. package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
  170. package/src/data-structures/diagrams/README.md +5 -0
  171. package/src/data-structures/graph/abstract-graph.ts +224 -108
  172. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  173. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  174. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  175. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  176. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  177. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  178. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  179. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  180. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  181. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  182. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  183. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  184. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  185. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  186. package/src/data-structures/graph/directed-graph.ts +132 -26
  187. package/src/data-structures/graph/undirected-graph.ts +78 -11
  188. package/src/data-structures/hash/coordinate-map.ts +33 -1
  189. package/src/data-structures/hash/coordinate-set.ts +25 -0
  190. package/src/data-structures/hash/index.ts +5 -0
  191. package/src/data-structures/heap/heap.ts +27 -41
  192. package/src/data-structures/heap/max-heap.ts +8 -2
  193. package/src/data-structures/heap/min-heap.ts +9 -2
  194. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
  195. package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
  196. package/src/data-structures/matrix/matrix.ts +11 -0
  197. package/src/data-structures/matrix/matrix2d.ts +90 -10
  198. package/src/data-structures/matrix/navigator.ts +34 -14
  199. package/src/data-structures/matrix/vector2d.ts +187 -63
  200. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
  201. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
  202. package/src/data-structures/priority-queue/priority-queue.ts +200 -78
  203. package/src/data-structures/queue/deque.ts +71 -2
  204. package/src/data-structures/queue/queue.ts +37 -40
  205. package/src/data-structures/stack/stack.ts +32 -38
  206. package/src/data-structures/trie/trie.ts +83 -14
  207. package/src/data-structures/types/abstract-graph.ts +51 -0
  208. package/src/data-structures/types/avl-tree.ts +6 -0
  209. package/src/data-structures/types/binary-tree.ts +15 -0
  210. package/src/data-structures/types/bst.ts +5 -0
  211. package/src/data-structures/types/directed-graph.ts +18 -0
  212. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  213. package/src/data-structures/types/heap.ts +8 -0
  214. package/src/data-structures/types/index.ts +13 -0
  215. package/src/data-structures/types/navigator.ts +13 -0
  216. package/src/data-structures/types/priority-queue.ts +9 -0
  217. package/src/data-structures/types/segment-tree.ts +1 -0
  218. package/src/data-structures/types/singly-linked-list.ts +1 -0
  219. package/src/data-structures/types/tree-multiset.ts +3 -0
  220. package/src/utils/index.ts +1 -0
  221. package/src/utils/trampoline.ts +51 -0
  222. package/src/utils/types/index.ts +1 -0
  223. package/src/{types → utils/types}/utils.ts +27 -5
  224. package/src/{utils.ts → utils/utils.ts} +41 -131
  225. package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
  226. package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
  227. package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
  228. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
  229. package/tsconfig.json +9 -6
  230. package/dist/data-structures/trampoline.d.ts +0 -25
  231. package/dist/data-structures/trampoline.js +0 -52
  232. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  233. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  234. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  235. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  236. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  237. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  238. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  239. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  240. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  241. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  242. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  243. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  244. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  245. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  246. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  247. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  248. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  249. package/dist/types/data-structures/hash/index.d.ts +0 -1
  250. package/dist/types/data-structures/hash/pair.d.ts +0 -1
  251. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  252. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  253. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  254. package/dist/types/data-structures/heap/index.d.ts +0 -3
  255. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  256. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  257. package/dist/types/data-structures/index.d.ts +0 -9
  258. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  259. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  260. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  261. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
  262. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  263. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  264. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  265. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  266. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  267. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  268. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  269. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  270. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  271. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  272. package/dist/types/data-structures/queue/index.d.ts +0 -1
  273. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  274. package/dist/types/data-structures/stack/index.d.ts +0 -1
  275. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  276. package/dist/types/data-structures/trampoline.d.ts +0 -25
  277. package/dist/types/data-structures/trie/index.d.ts +0 -1
  278. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  279. package/dist/types/utils.d.ts +0 -46
  280. package/dist/utils.js +0 -569
  281. package/src/data-structures/trampoline.ts +0 -91
  282. package/src/types/index.ts +0 -1
  283. /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
  284. /package/dist/{types → utils}/index.d.ts +0 -0
  285. /package/dist/{types → utils}/index.js +0 -0
  286. /package/dist/{types → utils}/types/index.d.ts +0 -0
  287. /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
@@ -1,88 +1,216 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
4
- const utils_1 = require("../../utils");
5
- const priority_queue_1 = require("../priority-queue");
6
- class AbstractVertex {
7
- get id() {
8
- return this._id;
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; }
9
26
  }
10
- set id(v) {
11
- this._id = v;
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
+ }
12
35
  }
13
- constructor(id) {
36
+ return to.concat(ar || Array.prototype.slice.call(from));
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
40
+ /**
41
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
42
+ * @license MIT
43
+ */
44
+ var utils_1 = require("../../utils");
45
+ var priority_queue_1 = require("../priority-queue");
46
+ var AbstractVertex = /** @class */ (function () {
47
+ function AbstractVertex(id) {
14
48
  this._id = id;
15
49
  }
16
- }
50
+ Object.defineProperty(AbstractVertex.prototype, "id", {
51
+ get: function () {
52
+ return this._id;
53
+ },
54
+ set: function (v) {
55
+ this._id = v;
56
+ },
57
+ enumerable: false,
58
+ configurable: true
59
+ });
60
+ return AbstractVertex;
61
+ }());
17
62
  exports.AbstractVertex = AbstractVertex;
18
- class AbstractEdge {
19
- get weight() {
20
- return this._weight;
21
- }
22
- set weight(v) {
23
- this._weight = v;
24
- }
25
- get hashCode() {
26
- return this._hashCode;
27
- }
28
- set hashCode(v) {
29
- this._hashCode = v;
30
- }
31
- constructor(weight) {
63
+ var AbstractEdge = /** @class */ (function () {
64
+ function AbstractEdge(weight) {
32
65
  if (weight === undefined)
33
66
  weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
34
67
  this._weight = weight;
35
68
  this._hashCode = (0, utils_1.uuidV4)();
36
69
  }
37
- }
70
+ Object.defineProperty(AbstractEdge.prototype, "weight", {
71
+ get: function () {
72
+ return this._weight;
73
+ },
74
+ set: function (v) {
75
+ this._weight = v;
76
+ },
77
+ enumerable: false,
78
+ configurable: true
79
+ });
80
+ Object.defineProperty(AbstractEdge.prototype, "hashCode", {
81
+ get: function () {
82
+ return this._hashCode;
83
+ },
84
+ set: function (v) {
85
+ this._hashCode = v;
86
+ },
87
+ enumerable: false,
88
+ configurable: true
89
+ });
90
+ AbstractEdge.DEFAULT_EDGE_WEIGHT = 1;
91
+ return AbstractEdge;
92
+ }());
38
93
  exports.AbstractEdge = AbstractEdge;
39
- AbstractEdge.DEFAULT_EDGE_WEIGHT = 1;
40
94
  // Connected Component === Largest Connected Sub-Graph
41
- class AbstractGraph {
42
- constructor() {
95
+ var AbstractGraph = /** @class */ (function () {
96
+ function AbstractGraph() {
43
97
  this._vertices = new Map();
44
98
  // unionFind() {
45
99
  // }
46
100
  /**--- end find cycles --- */
47
101
  // Minimum Spanning Tree
48
102
  }
49
- getVertex(vertexOrId) {
50
- const vertexId = this.getVertexId(vertexOrId);
103
+ /**
104
+ * The function `getVertex` returns the vertex object associated with a given vertex ID or vertex object, or null if it
105
+ * does not exist.
106
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
107
+ * @returns The function `getVertex` returns the vertex object (`V`) corresponding to the given `vertexOrId` parameter.
108
+ * If the vertex is found in the `_vertices` map, it is returned. Otherwise, `null` is returned.
109
+ */
110
+ AbstractGraph.prototype.getVertex = function (vertexOrId) {
111
+ var vertexId = this.getVertexId(vertexOrId);
51
112
  return this._vertices.get(vertexId) || null;
52
- }
53
- getVertexId(vertexOrId) {
113
+ };
114
+ /**
115
+ * The function `getVertexId` returns the id of a vertex, whether it is passed as an instance of `AbstractVertex` or as
116
+ * a `VertexId`.
117
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
118
+ * (`VertexId`).
119
+ * @returns the id of the vertex.
120
+ */
121
+ AbstractGraph.prototype.getVertexId = function (vertexOrId) {
54
122
  return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
55
- }
56
- containsVertex(vertexOrId) {
123
+ };
124
+ /**
125
+ * The function checks if a vertex exists in a graph.
126
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
127
+ * (`VertexId`).
128
+ * @returns The method `containsVertex` returns a boolean value.
129
+ */
130
+ AbstractGraph.prototype.containsVertex = function (vertexOrId) {
57
131
  return this._vertices.has(this.getVertexId(vertexOrId));
58
- }
59
- vertexSet() {
132
+ };
133
+ /**
134
+ * The function `vertexSet()` returns a map of vertices.
135
+ * @returns The method `vertexSet()` returns a map of vertex IDs to vertex objects.
136
+ */
137
+ AbstractGraph.prototype.vertexSet = function () {
60
138
  return this._vertices;
61
- }
62
- addVertex(newVertex) {
139
+ };
140
+ /**
141
+ * The addVertex function adds a new vertex to a graph if it does not already exist.
142
+ * @param {V} newVertex - The parameter "newVertex" is of type V, which represents a vertex in a graph.
143
+ * @returns The method is returning a boolean value. If the newVertex is already contained in the graph, it will return
144
+ * false. Otherwise, it will add the newVertex to the graph and return true.
145
+ */
146
+ AbstractGraph.prototype.addVertex = function (newVertex) {
63
147
  if (this.containsVertex(newVertex)) {
64
148
  return false;
65
149
  }
66
150
  this._vertices.set(newVertex.id, newVertex);
67
151
  return true;
68
- }
69
- removeVertex(vertexOrId) {
70
- const vertexId = this.getVertexId(vertexOrId);
152
+ };
153
+ /**
154
+ * The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
155
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
156
+ * (`VertexId`).
157
+ * @returns The method `removeVertex` returns a boolean value.
158
+ */
159
+ AbstractGraph.prototype.removeVertex = function (vertexOrId) {
160
+ var vertexId = this.getVertexId(vertexOrId);
71
161
  return this._vertices.delete(vertexId);
72
- }
73
- removeAllVertices(vertices) {
74
- const removed = [];
75
- for (const v of vertices) {
76
- removed.push(this.removeVertex(v));
162
+ };
163
+ /**
164
+ * The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
165
+ * @param {V[] | VertexId[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
166
+ * of vertex IDs (`VertexId[]`).
167
+ * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
168
+ * were removed.
169
+ */
170
+ AbstractGraph.prototype.removeAllVertices = function (vertices) {
171
+ var e_1, _a;
172
+ var removed = [];
173
+ try {
174
+ for (var vertices_1 = __values(vertices), vertices_1_1 = vertices_1.next(); !vertices_1_1.done; vertices_1_1 = vertices_1.next()) {
175
+ var v = vertices_1_1.value;
176
+ removed.push(this.removeVertex(v));
177
+ }
178
+ }
179
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
180
+ finally {
181
+ try {
182
+ if (vertices_1_1 && !vertices_1_1.done && (_a = vertices_1.return)) _a.call(vertices_1);
183
+ }
184
+ finally { if (e_1) throw e_1.error; }
77
185
  }
78
186
  return removed.length > 0;
79
- }
80
- containsEdge(v1, v2) {
81
- const edge = this.getEdge(v1, v2);
187
+ };
188
+ /**
189
+ * The function checks if there is an edge between two vertices in a graph.
190
+ * @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the identifier of
191
+ * a vertex in a graph, while V represents the type of the vertex itself.
192
+ * @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId`
193
+ * or a `V` type.
194
+ * @returns The function `containsEdge` returns a boolean value. It returns `true` if there is an edge between the
195
+ * vertices `v1` and `v2`, and `false` otherwise.
196
+ */
197
+ AbstractGraph.prototype.containsEdge = function (v1, v2) {
198
+ var edge = this.getEdge(v1, v2);
82
199
  return !!edge;
83
- }
84
- setEdgeWeight(srcOrId, destOrId, weight) {
85
- const edge = this.getEdge(srcOrId, destOrId);
200
+ };
201
+ /**
202
+ * The function sets the weight of an edge between two vertices in a graph.
203
+ * @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
204
+ * the source vertex of the edge.
205
+ * @param {VertexId | V} destOrId - The `destOrId` parameter represents the destination vertex of the edge. It can be
206
+ * either a `VertexId` or a vertex object `V`.
207
+ * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrId)
208
+ * and the destination vertex (destOrId).
209
+ * @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
210
+ * the weight of the edge and return true. If the edge does not exist, the function will return false.
211
+ */
212
+ AbstractGraph.prototype.setEdgeWeight = function (srcOrId, destOrId, weight) {
213
+ var edge = this.getEdge(srcOrId, destOrId);
86
214
  if (edge) {
87
215
  edge.weight = weight;
88
216
  return true;
@@ -90,76 +218,140 @@ class AbstractGraph {
90
218
  else {
91
219
  return false;
92
220
  }
93
- }
94
- getAllPathsBetween(v1, v2) {
95
- const paths = [];
96
- const vertex1 = this.getVertex(v1);
97
- const vertex2 = this.getVertex(v2);
221
+ };
222
+ /**
223
+ * The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
224
+ * @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
225
+ * It is the starting vertex for finding paths.
226
+ * @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex that we
227
+ * want to find paths to from the starting vertex `v1`.
228
+ * @returns an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1
229
+ * and v2).
230
+ */
231
+ AbstractGraph.prototype.getAllPathsBetween = function (v1, v2) {
232
+ var _this = this;
233
+ var paths = [];
234
+ var vertex1 = this.getVertex(v1);
235
+ var vertex2 = this.getVertex(v2);
98
236
  if (!(vertex1 && vertex2)) {
99
237
  return [];
100
238
  }
101
- const dfs = (cur, dest, visiting, path) => {
239
+ var dfs = function (cur, dest, visiting, path) {
240
+ var e_2, _a;
102
241
  visiting.set(cur, true);
103
242
  if (cur === dest) {
104
- paths.push([vertex1, ...path]);
243
+ paths.push(__spreadArray([vertex1], __read(path), false));
105
244
  }
106
- const neighbors = this.getNeighbors(cur);
107
- for (const neighbor of neighbors) {
245
+ var neighbors = _this.getNeighbors(cur);
246
+ var _loop_1 = function (neighbor) {
108
247
  if (!visiting.get(neighbor)) {
109
248
  path.push(neighbor);
110
249
  dfs(neighbor, dest, visiting, path);
111
- (0, utils_1.arrayRemove)(path, vertex => vertex === neighbor);
250
+ (0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
251
+ }
252
+ };
253
+ try {
254
+ for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
255
+ var neighbor = neighbors_1_1.value;
256
+ _loop_1(neighbor);
257
+ }
258
+ }
259
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
260
+ finally {
261
+ try {
262
+ if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
112
263
  }
264
+ finally { if (e_2) throw e_2.error; }
113
265
  }
114
266
  visiting.set(cur, false);
115
267
  };
116
268
  dfs(vertex1, vertex2, new Map(), []);
117
269
  return paths;
118
- }
119
- getPathSumWeight(path) {
270
+ };
271
+ /**
272
+ * The function calculates the sum of weights along a given path.
273
+ * @param {V[]} path - An array of vertices (V) representing a path in a graph.
274
+ * @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
275
+ */
276
+ AbstractGraph.prototype.getPathSumWeight = function (path) {
120
277
  var _a;
121
- let sum = 0;
122
- for (let i = 0; i < path.length; i++) {
278
+ var sum = 0;
279
+ for (var i = 0; i < path.length; i++) {
123
280
  sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
124
281
  }
125
282
  return sum;
126
- }
127
- getMinCostBetween(v1, v2, isWeight) {
283
+ };
284
+ /**
285
+ * The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
286
+ * weights or using a breadth-first search algorithm.
287
+ * @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or vertex ID of the graph.
288
+ * @param {V | VertexId} v2 - The parameter `v2` represents the second vertex in the graph. It can be either a vertex
289
+ * object or a vertex ID.
290
+ * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
291
+ * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
292
+ * the edges. If isWeight is set to false or not provided, the function will calculate the
293
+ * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
294
+ * and `v2`) in a graph. If the `isWeight` parameter is `true`, it calculates the minimum weight between the vertices.
295
+ * If `isWeight` is `false` or not provided, it calculates the minimum number of edges between the vertices. If the
296
+ * vertices are not
297
+ */
298
+ AbstractGraph.prototype.getMinCostBetween = function (v1, v2, isWeight) {
299
+ var e_3, _a, e_4, _b;
128
300
  if (isWeight === undefined)
129
301
  isWeight = false;
130
302
  if (isWeight) {
131
- const allPaths = this.getAllPathsBetween(v1, v2);
132
- let min = Infinity;
133
- for (const path of allPaths) {
134
- min = Math.min(this.getPathSumWeight(path), min);
303
+ var allPaths = this.getAllPathsBetween(v1, v2);
304
+ var min = Infinity;
305
+ try {
306
+ for (var allPaths_1 = __values(allPaths), allPaths_1_1 = allPaths_1.next(); !allPaths_1_1.done; allPaths_1_1 = allPaths_1.next()) {
307
+ var path = allPaths_1_1.value;
308
+ min = Math.min(this.getPathSumWeight(path), min);
309
+ }
310
+ }
311
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
312
+ finally {
313
+ try {
314
+ if (allPaths_1_1 && !allPaths_1_1.done && (_a = allPaths_1.return)) _a.call(allPaths_1);
315
+ }
316
+ finally { if (e_3) throw e_3.error; }
135
317
  }
136
318
  return min;
137
319
  }
138
320
  else {
139
321
  // BFS
140
- const vertex2 = this.getVertex(v2);
141
- const vertex1 = this.getVertex(v1);
322
+ var vertex2 = this.getVertex(v2);
323
+ var vertex1 = this.getVertex(v1);
142
324
  if (!(vertex1 && vertex2)) {
143
325
  return null;
144
326
  }
145
- const visited = new Map();
146
- const queue = [vertex1];
327
+ var visited = new Map();
328
+ var queue = [vertex1];
147
329
  visited.set(vertex1, true);
148
- let cost = 0;
330
+ var cost = 0;
149
331
  while (queue.length > 0) {
150
- for (let i = 0; i < queue.length; i++) {
151
- const cur = queue.shift();
332
+ for (var i = 0; i < queue.length; i++) {
333
+ var cur = queue.shift();
152
334
  if (cur === vertex2) {
153
335
  return cost;
154
336
  }
155
337
  // TODO consider optimizing to AbstractGraph
156
338
  if (cur !== undefined) {
157
- const neighbors = this.getNeighbors(cur);
158
- for (const neighbor of neighbors) {
159
- if (!visited.has(neighbor)) {
160
- visited.set(neighbor, true);
161
- queue.push(neighbor);
339
+ var neighbors = this.getNeighbors(cur);
340
+ try {
341
+ 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()) {
342
+ var neighbor = neighbors_2_1.value;
343
+ if (!visited.has(neighbor)) {
344
+ visited.set(neighbor, true);
345
+ queue.push(neighbor);
346
+ }
347
+ }
348
+ }
349
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
350
+ finally {
351
+ try {
352
+ if (neighbors_2_1 && !neighbors_2_1.done && (_b = neighbors_2.return)) _b.call(neighbors_2);
162
353
  }
354
+ finally { if (e_4) throw e_4.error; }
163
355
  }
164
356
  }
165
357
  }
@@ -167,114 +359,200 @@ class AbstractGraph {
167
359
  }
168
360
  return null;
169
361
  }
170
- }
171
- getMinPathBetween(v1, v2, isWeight) {
362
+ };
363
+ /**
364
+ * The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
365
+ * using a breadth-first search algorithm.
366
+ * @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
367
+ * @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex that we
368
+ * want to find the minimum path to from the source vertex `v1`.
369
+ * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
370
+ * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
371
+ * to false, the function will use breadth-first search (BFS) to find the minimum path. If
372
+ * @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
373
+ * two vertices (`v1` and `v2`). If no path is found, it returns `null`.
374
+ */
375
+ AbstractGraph.prototype.getMinPathBetween = function (v1, v2, isWeight) {
376
+ var e_5, _a;
377
+ var _this = this;
172
378
  if (isWeight === undefined)
173
379
  isWeight = false;
174
380
  if (isWeight) {
175
- const allPaths = this.getAllPathsBetween(v1, v2);
176
- let min = Infinity;
177
- let minIndex = -1;
178
- let index = 0;
179
- for (const path of allPaths) {
180
- const pathSumWeight = this.getPathSumWeight(path);
181
- if (pathSumWeight < min) {
182
- min = pathSumWeight;
183
- minIndex = index;
184
- }
185
- index++;
381
+ var allPaths = this.getAllPathsBetween(v1, v2);
382
+ var min = Infinity;
383
+ var minIndex = -1;
384
+ var index = 0;
385
+ try {
386
+ for (var allPaths_2 = __values(allPaths), allPaths_2_1 = allPaths_2.next(); !allPaths_2_1.done; allPaths_2_1 = allPaths_2.next()) {
387
+ var path = allPaths_2_1.value;
388
+ var pathSumWeight = this.getPathSumWeight(path);
389
+ if (pathSumWeight < min) {
390
+ min = pathSumWeight;
391
+ minIndex = index;
392
+ }
393
+ index++;
394
+ }
395
+ }
396
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
397
+ finally {
398
+ try {
399
+ if (allPaths_2_1 && !allPaths_2_1.done && (_a = allPaths_2.return)) _a.call(allPaths_2);
400
+ }
401
+ finally { if (e_5) throw e_5.error; }
186
402
  }
187
403
  return allPaths[minIndex] || null;
188
404
  }
189
405
  else {
190
406
  // BFS
191
- let minPath = [];
192
- const vertex1 = this.getVertex(v1);
193
- const vertex2 = this.getVertex(v2);
194
- if (!(vertex1 && vertex2)) {
407
+ var minPath_1 = [];
408
+ var vertex1_1 = this.getVertex(v1);
409
+ var vertex2 = this.getVertex(v2);
410
+ if (!(vertex1_1 && vertex2)) {
195
411
  return [];
196
412
  }
197
- const dfs = (cur, dest, visiting, path) => {
413
+ var dfs_1 = function (cur, dest, visiting, path) {
414
+ var e_6, _a;
198
415
  visiting.set(cur, true);
199
416
  if (cur === dest) {
200
- minPath = [vertex1, ...path];
417
+ minPath_1 = __spreadArray([vertex1_1], __read(path), false);
201
418
  return;
202
419
  }
203
- const neighbors = this.getNeighbors(cur);
204
- for (const neighbor of neighbors) {
420
+ var neighbors = _this.getNeighbors(cur);
421
+ var _loop_2 = function (neighbor) {
205
422
  if (!visiting.get(neighbor)) {
206
423
  path.push(neighbor);
207
- dfs(neighbor, dest, visiting, path);
208
- (0, utils_1.arrayRemove)(path, vertex => vertex === neighbor);
424
+ dfs_1(neighbor, dest, visiting, path);
425
+ (0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
209
426
  }
427
+ };
428
+ try {
429
+ for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
430
+ var neighbor = neighbors_3_1.value;
431
+ _loop_2(neighbor);
432
+ }
433
+ }
434
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
435
+ finally {
436
+ try {
437
+ if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3);
438
+ }
439
+ finally { if (e_6) throw e_6.error; }
210
440
  }
211
441
  visiting.set(cur, false);
212
442
  };
213
- dfs(vertex1, vertex2, new Map(), []);
214
- return minPath;
443
+ dfs_1(vertex1_1, vertex2, new Map(), []);
444
+ return minPath_1;
215
445
  }
216
- }
446
+ };
217
447
  /**
218
448
  * Dijkstra algorithm time: O(VE) space: O(V + E)
219
- * @param src
220
- * @param dest
221
- * @param getMinDist
222
- * @param genPaths
449
+ * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
450
+ * a graph without using a heap data structure.
451
+ * @param {V | VertexId} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
452
+ * vertex object or a vertex ID.
453
+ * @param {V | VertexId | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
454
+ * parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
455
+ * identifier. If no destination is provided, the value is set to `null`.
456
+ * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
457
+ * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
458
+ * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
459
+ * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
460
+ * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
461
+ * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
462
+ * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
223
463
  */
224
- dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
464
+ AbstractGraph.prototype.dijkstraWithoutHeap = function (src, dest, getMinDist, genPaths) {
465
+ var e_7, _a, e_8, _b;
225
466
  if (getMinDist === undefined)
226
467
  getMinDist = false;
227
468
  if (genPaths === undefined)
228
469
  genPaths = false;
229
470
  if (dest === undefined)
230
471
  dest = null;
231
- let minDist = Infinity;
232
- let minDest = null;
233
- let minPath = [];
234
- const paths = [];
235
- const vertices = this._vertices;
236
- const distMap = new Map();
237
- const seen = new Set();
238
- const preMap = new Map(); // predecessor
239
- const srcVertex = this.getVertex(src);
240
- const destVertex = dest ? this.getVertex(dest) : null;
472
+ var minDist = Infinity;
473
+ var minDest = null;
474
+ var minPath = [];
475
+ var paths = [];
476
+ var vertices = this._vertices;
477
+ var distMap = new Map();
478
+ var seen = new Set();
479
+ var preMap = new Map(); // predecessor
480
+ var srcVertex = this.getVertex(src);
481
+ var destVertex = dest ? this.getVertex(dest) : null;
241
482
  if (!srcVertex) {
242
483
  return null;
243
484
  }
244
- for (const vertex of vertices) {
245
- distMap.set(vertex[1], Infinity);
485
+ try {
486
+ for (var vertices_2 = __values(vertices), vertices_2_1 = vertices_2.next(); !vertices_2_1.done; vertices_2_1 = vertices_2.next()) {
487
+ var vertex = vertices_2_1.value;
488
+ var vertexOrId = vertex[1];
489
+ if (vertexOrId instanceof AbstractVertex)
490
+ distMap.set(vertexOrId, Infinity);
491
+ }
492
+ }
493
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
494
+ finally {
495
+ try {
496
+ if (vertices_2_1 && !vertices_2_1.done && (_a = vertices_2.return)) _a.call(vertices_2);
497
+ }
498
+ finally { if (e_7) throw e_7.error; }
246
499
  }
247
500
  distMap.set(srcVertex, 0);
248
501
  preMap.set(srcVertex, null);
249
- const getMinOfNoSeen = () => {
250
- let min = Infinity;
251
- let minV = null;
252
- for (const [key, val] of distMap) {
253
- if (!seen.has(key)) {
254
- if (val < min) {
255
- min = val;
256
- minV = key;
502
+ var getMinOfNoSeen = function () {
503
+ var e_9, _a;
504
+ var min = Infinity;
505
+ var minV = null;
506
+ try {
507
+ for (var distMap_1 = __values(distMap), distMap_1_1 = distMap_1.next(); !distMap_1_1.done; distMap_1_1 = distMap_1.next()) {
508
+ var _b = __read(distMap_1_1.value, 2), key = _b[0], val = _b[1];
509
+ if (!seen.has(key)) {
510
+ if (val < min) {
511
+ min = val;
512
+ minV = key;
513
+ }
257
514
  }
258
515
  }
259
516
  }
517
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
518
+ finally {
519
+ try {
520
+ if (distMap_1_1 && !distMap_1_1.done && (_a = distMap_1.return)) _a.call(distMap_1);
521
+ }
522
+ finally { if (e_9) throw e_9.error; }
523
+ }
260
524
  return minV;
261
525
  };
262
- const getPaths = (minV) => {
263
- for (const vertex of vertices) {
264
- const path = [vertex[1]];
265
- let parent = preMap.get(vertex[1]);
266
- while (parent) {
267
- path.push(parent);
268
- parent = preMap.get(parent);
269
- }
270
- const reversed = path.reverse();
271
- if (vertex[1] === minV)
272
- minPath = reversed;
273
- paths.push(reversed);
526
+ var getPaths = function (minV) {
527
+ var e_10, _a;
528
+ try {
529
+ for (var vertices_3 = __values(vertices), vertices_3_1 = vertices_3.next(); !vertices_3_1.done; vertices_3_1 = vertices_3.next()) {
530
+ var vertex = vertices_3_1.value;
531
+ var vertexOrId = vertex[1];
532
+ if (vertexOrId instanceof AbstractVertex) {
533
+ var path = [vertexOrId];
534
+ var parent = preMap.get(vertexOrId);
535
+ while (parent) {
536
+ path.push(parent);
537
+ parent = preMap.get(parent);
538
+ }
539
+ var reversed = path.reverse();
540
+ if (vertex[1] === minV)
541
+ minPath = reversed;
542
+ paths.push(reversed);
543
+ }
544
+ }
545
+ }
546
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
547
+ finally {
548
+ try {
549
+ if (vertices_3_1 && !vertices_3_1.done && (_a = vertices_3.return)) _a.call(vertices_3);
550
+ }
551
+ finally { if (e_10) throw e_10.error; }
274
552
  }
275
553
  };
276
- for (let i = 1; i < vertices.size; i++) {
277
- const cur = getMinOfNoSeen();
554
+ for (var i = 1; i < vertices.size; i++) {
555
+ var cur = getMinOfNoSeen();
278
556
  if (cur) {
279
557
  seen.add(cur);
280
558
  if (destVertex && destVertex === cur) {
@@ -284,28 +562,38 @@ class AbstractGraph {
284
562
  if (genPaths) {
285
563
  getPaths(destVertex);
286
564
  }
287
- return { distMap, preMap, seen, paths, minDist, minPath };
288
- }
289
- const neighbors = this.getNeighbors(cur);
290
- for (const neighbor of neighbors) {
291
- if (!seen.has(neighbor)) {
292
- const edge = this.getEdge(cur, neighbor);
293
- if (edge) {
294
- const curFromMap = distMap.get(cur);
295
- const neighborFromMap = distMap.get(neighbor);
296
- // TODO after no-non-null-assertion not ensure the logic
297
- if (curFromMap !== undefined && neighborFromMap !== undefined) {
298
- if (edge.weight + curFromMap < neighborFromMap) {
299
- distMap.set(neighbor, edge.weight + curFromMap);
300
- preMap.set(neighbor, cur);
565
+ return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
566
+ }
567
+ var neighbors = this.getNeighbors(cur);
568
+ try {
569
+ 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()) {
570
+ var neighbor = neighbors_4_1.value;
571
+ if (!seen.has(neighbor)) {
572
+ var edge = this.getEdge(cur, neighbor);
573
+ if (edge) {
574
+ var curFromMap = distMap.get(cur);
575
+ var neighborFromMap = distMap.get(neighbor);
576
+ // TODO after no-non-null-assertion not ensure the logic
577
+ if (curFromMap !== undefined && neighborFromMap !== undefined) {
578
+ if (edge.weight + curFromMap < neighborFromMap) {
579
+ distMap.set(neighbor, edge.weight + curFromMap);
580
+ preMap.set(neighbor, cur);
581
+ }
301
582
  }
302
583
  }
303
584
  }
304
585
  }
305
586
  }
587
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
588
+ finally {
589
+ try {
590
+ if (neighbors_4_1 && !neighbors_4_1.done && (_b = neighbors_4.return)) _b.call(neighbors_4);
591
+ }
592
+ finally { if (e_8) throw e_8.error; }
593
+ }
306
594
  }
307
595
  }
308
- getMinDist && distMap.forEach((d, v) => {
596
+ getMinDist && distMap.forEach(function (d, v) {
309
597
  if (v !== srcVertex) {
310
598
  if (d < minDist) {
311
599
  minDist = d;
@@ -315,61 +603,98 @@ class AbstractGraph {
315
603
  }
316
604
  });
317
605
  genPaths && getPaths(minDest);
318
- return { distMap, preMap, seen, paths, minDist, minPath };
319
- }
606
+ return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
607
+ };
320
608
  /**
321
609
  * Dijkstra algorithm time: O(logVE) space: O(V + E)
322
- * @param src
323
- * @param dest
324
- * @param getMinDist
325
- * @param genPaths
610
+ * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
611
+ * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
612
+ * @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
613
+ * start. It can be either a vertex object or a vertex ID.
614
+ * @param {V | VertexId | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
615
+ * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
616
+ * will calculate the shortest paths to all other vertices from the source vertex.
617
+ * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
618
+ * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
619
+ * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
620
+ * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
621
+ * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
622
+ * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
623
+ * @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
326
624
  */
327
- dijkstra(src, dest, getMinDist, genPaths) {
328
- var _a;
625
+ AbstractGraph.prototype.dijkstra = function (src, dest, getMinDist, genPaths) {
626
+ var e_11, _a, e_12, _b;
627
+ var _c;
329
628
  if (getMinDist === undefined)
330
629
  getMinDist = false;
331
630
  if (genPaths === undefined)
332
631
  genPaths = false;
333
632
  if (dest === undefined)
334
633
  dest = null;
335
- let minDist = Infinity;
336
- let minDest = null;
337
- let minPath = [];
338
- const paths = [];
339
- const vertices = this._vertices;
340
- const distMap = new Map();
341
- const seen = new Set();
342
- const preMap = new Map(); // predecessor
343
- const srcVertex = this.getVertex(src);
344
- const destVertex = dest ? this.getVertex(dest) : null;
634
+ var minDist = Infinity;
635
+ var minDest = null;
636
+ var minPath = [];
637
+ var paths = [];
638
+ var vertices = this._vertices;
639
+ var distMap = new Map();
640
+ var seen = new Set();
641
+ var preMap = new Map(); // predecessor
642
+ var srcVertex = this.getVertex(src);
643
+ var destVertex = dest ? this.getVertex(dest) : null;
345
644
  if (!srcVertex) {
346
645
  return null;
347
646
  }
348
- for (const vertex of vertices) {
349
- distMap.set(vertex[1], Infinity);
647
+ try {
648
+ for (var vertices_4 = __values(vertices), vertices_4_1 = vertices_4.next(); !vertices_4_1.done; vertices_4_1 = vertices_4.next()) {
649
+ var vertex = vertices_4_1.value;
650
+ var vertexOrId = vertex[1];
651
+ if (vertexOrId instanceof AbstractVertex)
652
+ distMap.set(vertexOrId, Infinity);
653
+ }
654
+ }
655
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
656
+ finally {
657
+ try {
658
+ if (vertices_4_1 && !vertices_4_1.done && (_a = vertices_4.return)) _a.call(vertices_4);
659
+ }
660
+ finally { if (e_11) throw e_11.error; }
350
661
  }
351
- const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.id - b.id });
662
+ var heap = new priority_queue_1.PriorityQueue({ comparator: function (a, b) { return a.id - b.id; } });
352
663
  heap.offer({ id: 0, val: srcVertex });
353
664
  distMap.set(srcVertex, 0);
354
665
  preMap.set(srcVertex, null);
355
- const getPaths = (minV) => {
356
- for (const vertex of vertices) {
357
- const path = [vertex[1]];
358
- let parent = preMap.get(vertex[1]);
359
- while (parent) {
360
- path.push(parent);
361
- parent = preMap.get(parent);
362
- }
363
- const reversed = path.reverse();
364
- if (vertex[1] === minV)
365
- minPath = reversed;
366
- paths.push(reversed);
666
+ var getPaths = function (minV) {
667
+ var e_13, _a;
668
+ try {
669
+ for (var vertices_5 = __values(vertices), vertices_5_1 = vertices_5.next(); !vertices_5_1.done; vertices_5_1 = vertices_5.next()) {
670
+ var vertex = vertices_5_1.value;
671
+ var vertexOrId = vertex[1];
672
+ if (vertexOrId instanceof AbstractVertex) {
673
+ var path = [vertexOrId];
674
+ var parent = preMap.get(vertexOrId);
675
+ while (parent) {
676
+ path.push(parent);
677
+ parent = preMap.get(parent);
678
+ }
679
+ var reversed = path.reverse();
680
+ if (vertex[1] === minV)
681
+ minPath = reversed;
682
+ paths.push(reversed);
683
+ }
684
+ }
685
+ }
686
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
687
+ finally {
688
+ try {
689
+ if (vertices_5_1 && !vertices_5_1.done && (_a = vertices_5.return)) _a.call(vertices_5);
690
+ }
691
+ finally { if (e_13) throw e_13.error; }
367
692
  }
368
693
  };
369
694
  while (heap.size > 0) {
370
- const curHeapNode = heap.poll();
371
- const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
372
- const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
695
+ var curHeapNode = heap.poll();
696
+ var dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
697
+ var cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
373
698
  if (dist !== undefined) {
374
699
  if (cur) {
375
700
  seen.add(cur);
@@ -380,29 +705,39 @@ class AbstractGraph {
380
705
  if (genPaths) {
381
706
  getPaths(destVertex);
382
707
  }
383
- return { distMap, preMap, seen, paths, minDist, minPath };
708
+ return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
384
709
  }
385
- const neighbors = this.getNeighbors(cur);
386
- for (const neighbor of neighbors) {
387
- if (!seen.has(neighbor)) {
388
- const weight = (_a = this.getEdge(cur, neighbor)) === null || _a === void 0 ? void 0 : _a.weight;
389
- if (typeof weight === 'number') {
390
- const distSrcToNeighbor = distMap.get(neighbor);
391
- if (distSrcToNeighbor) {
392
- if (dist + weight < distSrcToNeighbor) {
393
- heap.offer({ id: dist + weight, val: neighbor });
394
- preMap.set(neighbor, cur);
395
- distMap.set(neighbor, dist + weight);
710
+ var neighbors = this.getNeighbors(cur);
711
+ try {
712
+ 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()) {
713
+ var neighbor = neighbors_5_1.value;
714
+ if (!seen.has(neighbor)) {
715
+ var weight = (_c = this.getEdge(cur, neighbor)) === null || _c === void 0 ? void 0 : _c.weight;
716
+ if (typeof weight === 'number') {
717
+ var distSrcToNeighbor = distMap.get(neighbor);
718
+ if (distSrcToNeighbor) {
719
+ if (dist + weight < distSrcToNeighbor) {
720
+ heap.offer({ id: dist + weight, val: neighbor });
721
+ preMap.set(neighbor, cur);
722
+ distMap.set(neighbor, dist + weight);
723
+ }
396
724
  }
397
725
  }
398
726
  }
399
727
  }
400
728
  }
729
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
730
+ finally {
731
+ try {
732
+ if (neighbors_5_1 && !neighbors_5_1.done && (_b = neighbors_5.return)) _b.call(neighbors_5);
733
+ }
734
+ finally { if (e_12) throw e_12.error; }
735
+ }
401
736
  }
402
737
  }
403
738
  }
404
739
  if (getMinDist) {
405
- distMap.forEach((d, v) => {
740
+ distMap.forEach(function (d, v) {
406
741
  if (v !== srcVertex) {
407
742
  if (d < minDist) {
408
743
  minDist = d;
@@ -415,49 +750,57 @@ class AbstractGraph {
415
750
  if (genPaths) {
416
751
  getPaths(minDest);
417
752
  }
418
- return { distMap, preMap, seen, paths, minDist, minPath };
419
- }
753
+ return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
754
+ };
420
755
  /**
421
756
  * BellmanFord time:O(VE) space:O(V)
422
757
  * one to rest pairs
423
- * @param src
424
- * @param scanNegativeCycle
425
- * @param getMin
426
- * @param genPath
758
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
759
+ * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
760
+ * @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
761
+ * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
762
+ * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
763
+ * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
764
+ * calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
765
+ * `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
766
+ * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
767
+ * vertex.
768
+ * @returns The function `bellmanFord` returns an object with the following properties:
427
769
  */
428
- bellmanFord(src, scanNegativeCycle, getMin, genPath) {
770
+ AbstractGraph.prototype.bellmanFord = function (src, scanNegativeCycle, getMin, genPath) {
771
+ var e_14, _a;
429
772
  if (getMin === undefined)
430
773
  getMin = false;
431
774
  if (genPath === undefined)
432
775
  genPath = false;
433
- const srcVertex = this.getVertex(src);
434
- const paths = [];
435
- const distMap = new Map();
436
- const preMap = new Map(); // predecessor
437
- let min = Infinity;
438
- let minPath = [];
776
+ var srcVertex = this.getVertex(src);
777
+ var paths = [];
778
+ var distMap = new Map();
779
+ var preMap = new Map(); // predecessor
780
+ var min = Infinity;
781
+ var minPath = [];
439
782
  // TODO
440
- let hasNegativeCycle = undefined;
783
+ var hasNegativeCycle;
441
784
  if (scanNegativeCycle)
442
785
  hasNegativeCycle = false;
443
786
  if (!srcVertex)
444
- return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
445
- const vertices = this._vertices;
446
- const numOfVertices = vertices.size;
447
- const edges = this.edgeSet();
448
- const numOfEdges = edges.length;
449
- this._vertices.forEach(vertex => {
787
+ return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
788
+ var vertices = this._vertices;
789
+ var numOfVertices = vertices.size;
790
+ var edges = this.edgeSet();
791
+ var numOfEdges = edges.length;
792
+ this._vertices.forEach(function (vertex) {
450
793
  distMap.set(vertex, Infinity);
451
794
  });
452
795
  distMap.set(srcVertex, 0);
453
- for (let i = 1; i < numOfVertices; ++i) {
454
- for (let j = 0; j < numOfEdges; ++j) {
455
- const ends = this.getEndsOfEdge(edges[j]);
796
+ for (var i = 1; i < numOfVertices; ++i) {
797
+ for (var j = 0; j < numOfEdges; ++j) {
798
+ var ends = this.getEndsOfEdge(edges[j]);
456
799
  if (ends) {
457
- const [s, d] = ends;
458
- const weight = edges[j].weight;
459
- const sWeight = distMap.get(s);
460
- const dWeight = distMap.get(d);
800
+ var _b = __read(ends, 2), s = _b[0], d = _b[1];
801
+ var weight = edges[j].weight;
802
+ var sWeight = distMap.get(s);
803
+ var dWeight = distMap.get(d);
461
804
  if (sWeight !== undefined && dWeight !== undefined) {
462
805
  if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
463
806
  distMap.set(d, sWeight + weight);
@@ -467,9 +810,9 @@ class AbstractGraph {
467
810
  }
468
811
  }
469
812
  }
470
- let minDest = null;
813
+ var minDest = null;
471
814
  if (getMin) {
472
- distMap.forEach((d, v) => {
815
+ distMap.forEach(function (d, v) {
473
816
  if (v !== srcVertex) {
474
817
  if (d < min) {
475
818
  min = d;
@@ -480,59 +823,78 @@ class AbstractGraph {
480
823
  });
481
824
  }
482
825
  if (genPath) {
483
- for (const vertex of vertices) {
484
- const path = [vertex[1]];
485
- let parent = preMap.get(vertex[1]);
486
- while (parent !== undefined) {
487
- path.push(parent);
488
- parent = preMap.get(parent);
826
+ try {
827
+ for (var vertices_6 = __values(vertices), vertices_6_1 = vertices_6.next(); !vertices_6_1.done; vertices_6_1 = vertices_6.next()) {
828
+ var vertex = vertices_6_1.value;
829
+ var vertexOrId = vertex[1];
830
+ if (vertexOrId instanceof AbstractVertex) {
831
+ var path = [vertexOrId];
832
+ var parent = preMap.get(vertexOrId);
833
+ while (parent !== undefined) {
834
+ path.push(parent);
835
+ parent = preMap.get(parent);
836
+ }
837
+ var reversed = path.reverse();
838
+ if (vertex[1] === minDest)
839
+ minPath = reversed;
840
+ paths.push(reversed);
841
+ }
842
+ }
843
+ }
844
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
845
+ finally {
846
+ try {
847
+ if (vertices_6_1 && !vertices_6_1.done && (_a = vertices_6.return)) _a.call(vertices_6);
489
848
  }
490
- const reversed = path.reverse();
491
- if (vertex[1] === minDest)
492
- minPath = reversed;
493
- paths.push(reversed);
849
+ finally { if (e_14) throw e_14.error; }
494
850
  }
495
851
  }
496
- for (let j = 0; j < numOfEdges; ++j) {
497
- const ends = this.getEndsOfEdge(edges[j]);
852
+ for (var j = 0; j < numOfEdges; ++j) {
853
+ var ends = this.getEndsOfEdge(edges[j]);
498
854
  if (ends) {
499
- const [s] = ends;
500
- const weight = edges[j].weight;
501
- const sWeight = distMap.get(s);
855
+ var _c = __read(ends, 1), s = _c[0];
856
+ var weight = edges[j].weight;
857
+ var sWeight = distMap.get(s);
502
858
  if (sWeight) {
503
859
  if (sWeight !== Infinity && sWeight + weight < sWeight)
504
860
  hasNegativeCycle = true;
505
861
  }
506
862
  }
507
863
  }
508
- return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
509
- }
864
+ return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
865
+ };
510
866
  /**
511
867
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
512
868
  * all pairs
869
+ * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
870
+ * graph.
871
+ * @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
872
+ * property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
873
+ * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
874
+ * path between vertices in the
513
875
  */
514
- floyd() {
876
+ AbstractGraph.prototype.floyd = function () {
515
877
  var _a;
516
- const idAndVertices = [...this._vertices];
517
- const n = idAndVertices.length;
518
- const costs = [];
519
- const predecessor = [];
878
+ var idAndVertices = __spreadArray([], __read(this._vertices), false);
879
+ var n = idAndVertices.length;
880
+ var costs = [];
881
+ var predecessor = [];
520
882
  // successors
521
- for (let i = 0; i < n; i++) {
883
+ for (var i = 0; i < n; i++) {
522
884
  costs[i] = [];
523
885
  predecessor[i] = [];
524
- for (let j = 0; j < n; j++) {
886
+ for (var j = 0; j < n; j++) {
525
887
  predecessor[i][j] = null;
526
888
  }
527
889
  }
528
- for (let i = 0; i < n; i++) {
529
- for (let j = 0; j < n; j++) {
890
+ for (var i = 0; i < n; i++) {
891
+ for (var j = 0; j < n; j++) {
530
892
  costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
531
893
  }
532
894
  }
533
- for (let k = 0; k < n; k++) {
534
- for (let i = 0; i < n; i++) {
535
- for (let j = 0; j < n; j++) {
895
+ for (var k = 0; k < n; k++) {
896
+ for (var i = 0; i < n; i++) {
897
+ for (var j = 0; j < n; j++) {
536
898
  if (costs[i][j] > costs[i][k] + costs[k][j]) {
537
899
  costs[i][j] = costs[i][k] + costs[k][j];
538
900
  predecessor[i][j] = idAndVertices[k][1];
@@ -540,8 +902,8 @@ class AbstractGraph {
540
902
  }
541
903
  }
542
904
  }
543
- return { costs, predecessor };
544
- }
905
+ return { costs: costs, predecessor: predecessor };
906
+ };
545
907
  /**--- start find cycles --- */
546
908
  /**
547
909
  * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
@@ -549,12 +911,27 @@ class AbstractGraph {
549
911
  * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
550
912
  * Tarjan solve the bi-connected components of undirected graphs;
551
913
  * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
914
+ * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
915
+ * strongly connected components (SCCs), and cycles in a graph.
916
+ * @param {boolean} [needArticulationPoints] - A boolean value indicating whether or not to calculate and return the
917
+ * articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
918
+ * number of connected components in the graph.
919
+ * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
920
+ * (edges whose removal would increase the number of connected components in the graph).
921
+ * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
922
+ * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
923
+ * SCCs will not be calculated or returned.
924
+ * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
925
+ * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
926
+ * are arrays of vertices that form cycles within the SCCs.
927
+ * @returns The function `tarjan` returns an object with the following properties:
552
928
  */
553
- tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
929
+ AbstractGraph.prototype.tarjan = function (needArticulationPoints, needBridges, needSCCs, needCycles) {
554
930
  // !! in undirected graph we will not let child visit parent when DFS
555
931
  // !! articulation point(in DFS search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
556
932
  // !! bridge: low(child) > dfn(cur)
557
- const defaultConfig = false;
933
+ var _this = this;
934
+ var defaultConfig = false;
558
935
  if (needArticulationPoints === undefined)
559
936
  needArticulationPoints = defaultConfig;
560
937
  if (needBridges === undefined)
@@ -563,60 +940,71 @@ class AbstractGraph {
563
940
  needSCCs = defaultConfig;
564
941
  if (needCycles === undefined)
565
942
  needCycles = defaultConfig;
566
- const dfnMap = new Map();
567
- const lowMap = new Map();
568
- const vertices = this._vertices;
569
- vertices.forEach(v => {
943
+ var dfnMap = new Map();
944
+ var lowMap = new Map();
945
+ var vertices = this._vertices;
946
+ vertices.forEach(function (v) {
570
947
  dfnMap.set(v, -1);
571
948
  lowMap.set(v, Infinity);
572
949
  });
573
- const [root] = vertices.values();
574
- const articulationPoints = [];
575
- const bridges = [];
576
- let dfn = 0;
577
- const dfs = (cur, parent) => {
950
+ var _a = __read(vertices.values(), 1), root = _a[0];
951
+ var articulationPoints = [];
952
+ var bridges = [];
953
+ var dfn = 0;
954
+ var dfs = function (cur, parent) {
955
+ var e_15, _a;
578
956
  dfn++;
579
957
  dfnMap.set(cur, dfn);
580
958
  lowMap.set(cur, dfn);
581
- const neighbors = this.getNeighbors(cur);
582
- let childCount = 0; // child in DFS tree not child in graph
583
- for (const neighbor of neighbors) {
584
- if (neighbor !== parent) {
585
- if (dfnMap.get(neighbor) === -1) {
586
- childCount++;
587
- dfs(neighbor, cur);
588
- }
589
- const childLow = lowMap.get(neighbor);
590
- const curLow = lowMap.get(cur);
591
- // TODO after no-non-null-assertion not ensure the logic
592
- if (curLow !== undefined && childLow !== undefined) {
593
- lowMap.set(cur, Math.min(curLow, childLow));
594
- }
595
- const curFromMap = dfnMap.get(cur);
596
- if (childLow !== undefined && curFromMap !== undefined) {
597
- if (needArticulationPoints) {
598
- if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
599
- // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
600
- articulationPoints.push(cur);
601
- }
959
+ var neighbors = _this.getNeighbors(cur);
960
+ var childCount = 0; // child in DFS tree not child in graph
961
+ try {
962
+ for (var neighbors_6 = __values(neighbors), neighbors_6_1 = neighbors_6.next(); !neighbors_6_1.done; neighbors_6_1 = neighbors_6.next()) {
963
+ var neighbor = neighbors_6_1.value;
964
+ if (neighbor !== parent) {
965
+ if (dfnMap.get(neighbor) === -1) {
966
+ childCount++;
967
+ dfs(neighbor, cur);
602
968
  }
603
- if (needBridges) {
604
- if (childLow > curFromMap) {
605
- const edgeCurToNeighbor = this.getEdge(cur, neighbor);
606
- if (edgeCurToNeighbor) {
607
- bridges.push(edgeCurToNeighbor);
969
+ var childLow = lowMap.get(neighbor);
970
+ var curLow = lowMap.get(cur);
971
+ // TODO after no-non-null-assertion not ensure the logic
972
+ if (curLow !== undefined && childLow !== undefined) {
973
+ lowMap.set(cur, Math.min(curLow, childLow));
974
+ }
975
+ var curFromMap = dfnMap.get(cur);
976
+ if (childLow !== undefined && curFromMap !== undefined) {
977
+ if (needArticulationPoints) {
978
+ if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
979
+ // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
980
+ articulationPoints.push(cur);
981
+ }
982
+ }
983
+ if (needBridges) {
984
+ if (childLow > curFromMap) {
985
+ var edgeCurToNeighbor = _this.getEdge(cur, neighbor);
986
+ if (edgeCurToNeighbor) {
987
+ bridges.push(edgeCurToNeighbor);
988
+ }
608
989
  }
609
990
  }
610
991
  }
611
992
  }
612
993
  }
613
994
  }
995
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
996
+ finally {
997
+ try {
998
+ if (neighbors_6_1 && !neighbors_6_1.done && (_a = neighbors_6.return)) _a.call(neighbors_6);
999
+ }
1000
+ finally { if (e_15) throw e_15.error; }
1001
+ }
614
1002
  };
615
1003
  dfs(root, null);
616
- let SCCs = new Map();
617
- const getSCCs = () => {
618
- const SCCs = new Map();
619
- lowMap.forEach((low, vertex) => {
1004
+ var SCCs = new Map();
1005
+ var getSCCs = function () {
1006
+ var SCCs = new Map();
1007
+ lowMap.forEach(function (low, vertex) {
620
1008
  var _a;
621
1009
  if (!SCCs.has(low)) {
622
1010
  SCCs.set(low, [vertex]);
@@ -630,19 +1018,20 @@ class AbstractGraph {
630
1018
  if (needSCCs) {
631
1019
  SCCs = getSCCs();
632
1020
  }
633
- const cycles = new Map();
1021
+ var cycles = new Map();
634
1022
  if (needCycles) {
635
- let SCCs = new Map();
636
- if (SCCs.size < 1) {
637
- SCCs = getSCCs();
1023
+ var SCCs_1 = new Map();
1024
+ if (SCCs_1.size < 1) {
1025
+ SCCs_1 = getSCCs();
638
1026
  }
639
- SCCs.forEach((SCC, low) => {
1027
+ SCCs_1.forEach(function (SCC, low) {
640
1028
  if (SCC.length > 1) {
641
1029
  cycles.set(low, SCC);
642
1030
  }
643
1031
  });
644
1032
  }
645
- return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
646
- }
647
- }
1033
+ return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
1034
+ };
1035
+ return AbstractGraph;
1036
+ }());
648
1037
  exports.AbstractGraph = AbstractGraph;