min-heap-typed 1.40.0-rc → 1.40.0

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 (347) hide show
  1. package/.dependency-cruiser.js +422 -422
  2. package/.eslintrc.js +59 -59
  3. package/.prettierrc.js +14 -14
  4. package/README.md +20 -3
  5. package/coverage/clover.xml +11 -7
  6. package/coverage/coverage-final.json +95 -1
  7. package/coverage/coverage-summary.json +59 -2
  8. package/coverage/lcov-report/base.css +278 -99
  9. package/coverage/lcov-report/index.html +69 -65
  10. package/coverage/lcov-report/index.ts.html +36 -35
  11. package/coverage/lcov-report/sorter.js +15 -5
  12. package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
  13. package/dist/data-structures/binary-tree/avl-tree.js +347 -0
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
  15. package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
  16. package/dist/data-structures/binary-tree/binary-tree.d.ts +355 -0
  17. package/dist/data-structures/binary-tree/binary-tree.js +1115 -0
  18. package/dist/data-structures/binary-tree/bst.d.ts +167 -0
  19. package/dist/data-structures/binary-tree/bst.js +512 -0
  20. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  21. package/dist/data-structures/binary-tree/index.js +23 -0
  22. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -0
  23. package/dist/data-structures/binary-tree/rb-tree.js +21 -0
  24. package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
  25. package/dist/data-structures/binary-tree/segment-tree.js +180 -0
  26. package/dist/data-structures/binary-tree/tree-multiset.d.ts +126 -0
  27. package/dist/data-structures/binary-tree/tree-multiset.js +355 -0
  28. package/dist/data-structures/graph/abstract-graph.d.ts +313 -0
  29. package/dist/data-structures/graph/abstract-graph.js +884 -0
  30. package/dist/data-structures/graph/directed-graph.d.ts +194 -0
  31. package/dist/data-structures/graph/directed-graph.js +404 -0
  32. package/dist/data-structures/graph/index.d.ts +4 -0
  33. package/dist/data-structures/graph/index.js +20 -0
  34. package/dist/data-structures/graph/map-graph.d.ts +73 -0
  35. package/dist/data-structures/graph/map-graph.js +93 -0
  36. package/dist/data-structures/graph/undirected-graph.d.ts +120 -0
  37. package/dist/data-structures/graph/undirected-graph.js +239 -0
  38. package/dist/data-structures/hash/coordinate-map.d.ts +44 -0
  39. package/dist/data-structures/hash/coordinate-map.js +62 -0
  40. package/dist/data-structures/hash/coordinate-set.d.ts +36 -0
  41. package/dist/data-structures/hash/coordinate-set.js +52 -0
  42. package/dist/data-structures/hash/hash-map.d.ts +50 -0
  43. package/dist/data-structures/hash/hash-map.js +153 -0
  44. package/dist/data-structures/hash/hash-table.d.ts +103 -0
  45. package/dist/data-structures/hash/hash-table.js +236 -0
  46. package/dist/data-structures/hash/index.d.ts +6 -0
  47. package/dist/data-structures/hash/index.js +22 -0
  48. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  49. package/dist/data-structures/hash/tree-map.js +6 -0
  50. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  51. package/dist/data-structures/hash/tree-set.js +6 -0
  52. package/dist/data-structures/heap/heap.d.ts +235 -0
  53. package/dist/data-structures/heap/heap.js +515 -0
  54. package/dist/data-structures/heap/index.d.ts +3 -0
  55. package/dist/data-structures/heap/index.js +19 -0
  56. package/dist/data-structures/heap/max-heap.d.ts +15 -0
  57. package/dist/data-structures/heap/max-heap.js +26 -0
  58. package/dist/data-structures/heap/min-heap.d.ts +15 -0
  59. package/dist/data-structures/heap/min-heap.js +26 -0
  60. package/dist/data-structures/index.d.ts +11 -0
  61. package/dist/data-structures/index.js +27 -0
  62. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +253 -0
  63. package/dist/data-structures/linked-list/doubly-linked-list.js +569 -0
  64. package/dist/data-structures/linked-list/index.d.ts +3 -0
  65. package/dist/data-structures/linked-list/index.js +19 -0
  66. package/dist/data-structures/linked-list/singly-linked-list.d.ts +232 -0
  67. package/dist/data-structures/linked-list/singly-linked-list.js +533 -0
  68. package/dist/data-structures/linked-list/skip-linked-list.d.ts +80 -0
  69. package/dist/data-structures/linked-list/skip-linked-list.js +187 -0
  70. package/dist/data-structures/matrix/index.d.ts +4 -0
  71. package/dist/data-structures/matrix/index.js +20 -0
  72. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  73. package/dist/data-structures/matrix/matrix.js +28 -0
  74. package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
  75. package/dist/data-structures/matrix/matrix2d.js +199 -0
  76. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  77. package/dist/data-structures/matrix/navigator.js +106 -0
  78. package/dist/data-structures/matrix/vector2d.d.ts +200 -0
  79. package/dist/data-structures/matrix/vector2d.js +290 -0
  80. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  81. package/dist/data-structures/priority-queue/index.js +19 -0
  82. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  83. package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
  84. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  85. package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
  86. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
  87. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  88. package/dist/data-structures/queue/deque.d.ts +161 -0
  89. package/dist/data-structures/queue/deque.js +264 -0
  90. package/dist/data-structures/queue/index.d.ts +2 -0
  91. package/dist/data-structures/queue/index.js +18 -0
  92. package/dist/data-structures/queue/queue.d.ts +122 -0
  93. package/dist/data-structures/queue/queue.js +187 -0
  94. package/dist/data-structures/stack/index.d.ts +1 -0
  95. package/dist/data-structures/stack/index.js +17 -0
  96. package/dist/data-structures/stack/stack.d.ts +64 -0
  97. package/dist/data-structures/stack/stack.js +94 -0
  98. package/dist/data-structures/tree/index.d.ts +1 -0
  99. package/dist/data-structures/tree/index.js +17 -0
  100. package/dist/data-structures/tree/tree.d.ts +8 -0
  101. package/dist/data-structures/tree/tree.js +40 -0
  102. package/dist/data-structures/trie/index.d.ts +1 -0
  103. package/dist/data-structures/trie/index.js +17 -0
  104. package/dist/data-structures/trie/trie.d.ts +79 -0
  105. package/dist/data-structures/trie/trie.js +251 -0
  106. package/dist/index.d.ts +3 -1
  107. package/dist/index.js +18 -4
  108. package/dist/interfaces/binary-tree.d.ts +7 -0
  109. package/dist/interfaces/binary-tree.js +2 -0
  110. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  111. package/dist/interfaces/doubly-linked-list.js +2 -0
  112. package/dist/interfaces/graph.d.ts +5 -0
  113. package/dist/interfaces/graph.js +2 -0
  114. package/dist/interfaces/heap.d.ts +1 -0
  115. package/dist/interfaces/heap.js +2 -0
  116. package/dist/interfaces/index.d.ts +8 -0
  117. package/dist/interfaces/index.js +24 -0
  118. package/dist/interfaces/navigator.d.ts +1 -0
  119. package/dist/interfaces/navigator.js +2 -0
  120. package/dist/interfaces/priority-queue.d.ts +1 -0
  121. package/dist/interfaces/priority-queue.js +2 -0
  122. package/dist/interfaces/segment-tree.d.ts +1 -0
  123. package/dist/interfaces/segment-tree.js +2 -0
  124. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  125. package/dist/interfaces/singly-linked-list.js +2 -0
  126. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
  127. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  128. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  129. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  130. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  131. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  132. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
  133. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  134. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  135. package/dist/types/data-structures/binary-tree/index.js +22 -0
  136. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
  137. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  138. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  139. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  140. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  141. package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
  142. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  143. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  144. package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
  145. package/dist/types/data-structures/graph/directed-graph.js +9 -0
  146. package/dist/types/data-structures/graph/index.d.ts +3 -0
  147. package/dist/types/data-structures/graph/index.js +19 -0
  148. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  149. package/dist/types/data-structures/graph/map-graph.js +2 -0
  150. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  151. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  152. package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
  153. package/dist/types/data-structures/hash/coordinate-map.js +2 -0
  154. package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
  155. package/dist/types/data-structures/hash/coordinate-set.js +2 -0
  156. package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
  157. package/dist/types/data-structures/hash/hash-map.js +2 -0
  158. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  159. package/dist/types/data-structures/hash/hash-table.js +2 -0
  160. package/dist/types/data-structures/hash/index.d.ts +1 -0
  161. package/dist/types/data-structures/hash/index.js +2 -0
  162. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  163. package/dist/types/data-structures/hash/tree-map.js +2 -0
  164. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  165. package/dist/types/data-structures/hash/tree-set.js +2 -0
  166. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  167. package/dist/types/data-structures/heap/heap.js +2 -0
  168. package/dist/types/data-structures/heap/index.d.ts +1 -0
  169. package/dist/types/data-structures/heap/index.js +17 -0
  170. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  171. package/dist/types/data-structures/heap/max-heap.js +2 -0
  172. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  173. package/dist/types/data-structures/heap/min-heap.js +2 -0
  174. package/dist/types/data-structures/index.d.ts +11 -0
  175. package/dist/types/data-structures/index.js +27 -0
  176. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  177. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  178. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  179. package/dist/types/data-structures/linked-list/index.js +18 -0
  180. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  181. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  182. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  183. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  184. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  185. package/dist/types/data-structures/matrix/index.js +17 -0
  186. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  187. package/dist/types/data-structures/matrix/matrix.js +2 -0
  188. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  189. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  190. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  191. package/dist/types/data-structures/matrix/navigator.js +2 -0
  192. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  193. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  194. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  195. package/dist/types/data-structures/priority-queue/index.js +19 -0
  196. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  197. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  198. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  199. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  200. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  201. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  202. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  203. package/dist/types/data-structures/queue/deque.js +2 -0
  204. package/dist/types/data-structures/queue/index.d.ts +2 -0
  205. package/dist/types/data-structures/queue/index.js +18 -0
  206. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  207. package/dist/types/data-structures/queue/queue.js +2 -0
  208. package/dist/types/data-structures/stack/index.d.ts +1 -0
  209. package/dist/types/data-structures/stack/index.js +17 -0
  210. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  211. package/dist/types/data-structures/stack/stack.js +2 -0
  212. package/dist/types/data-structures/tree/index.d.ts +1 -0
  213. package/dist/types/data-structures/tree/index.js +17 -0
  214. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  215. package/dist/types/data-structures/tree/tree.js +2 -0
  216. package/dist/types/data-structures/trie/index.d.ts +1 -0
  217. package/dist/types/data-structures/trie/index.js +17 -0
  218. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  219. package/dist/types/data-structures/trie/trie.js +2 -0
  220. package/dist/types/helpers.d.ts +8 -0
  221. package/dist/types/helpers.js +9 -0
  222. package/dist/types/index.d.ts +3 -0
  223. package/dist/types/index.js +19 -0
  224. package/dist/types/utils/index.d.ts +2 -0
  225. package/dist/types/utils/index.js +18 -0
  226. package/dist/types/utils/utils.d.ts +7 -0
  227. package/dist/types/utils/utils.js +2 -0
  228. package/dist/types/utils/validate-type.d.ts +19 -0
  229. package/dist/types/utils/validate-type.js +2 -0
  230. package/dist/utils/index.d.ts +1 -0
  231. package/dist/utils/index.js +17 -0
  232. package/dist/utils/utils.d.ts +20 -0
  233. package/dist/utils/utils.js +73 -0
  234. package/jest.config.js +6 -6
  235. package/package.json +2 -2
  236. package/src/data-structures/binary-tree/avl-tree.ts +350 -0
  237. package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
  238. package/src/data-structures/binary-tree/binary-tree.ts +1262 -0
  239. package/src/data-structures/binary-tree/bst.ts +522 -0
  240. package/src/data-structures/binary-tree/index.ts +7 -0
  241. package/src/data-structures/binary-tree/rb-tree.ts +358 -0
  242. package/src/data-structures/binary-tree/segment-tree.ts +190 -0
  243. package/src/data-structures/binary-tree/tree-multiset.ts +379 -0
  244. package/src/data-structures/graph/abstract-graph.ts +1000 -0
  245. package/src/data-structures/graph/directed-graph.ts +449 -0
  246. package/src/data-structures/graph/index.ts +4 -0
  247. package/src/data-structures/graph/map-graph.ts +106 -0
  248. package/src/data-structures/graph/undirected-graph.ts +259 -0
  249. package/src/data-structures/hash/coordinate-map.ts +63 -0
  250. package/src/data-structures/hash/coordinate-set.ts +52 -0
  251. package/src/data-structures/hash/hash-map.ts +185 -0
  252. package/src/data-structures/hash/hash-table.ts +268 -0
  253. package/src/data-structures/hash/index.ts +6 -0
  254. package/src/data-structures/hash/tree-map.ts +2 -0
  255. package/src/data-structures/hash/tree-set.ts +2 -0
  256. package/src/data-structures/heap/heap.ts +589 -0
  257. package/src/data-structures/heap/index.ts +3 -0
  258. package/src/data-structures/heap/max-heap.ts +26 -0
  259. package/src/data-structures/heap/min-heap.ts +26 -0
  260. package/src/data-structures/index.ts +11 -0
  261. package/src/data-structures/linked-list/doubly-linked-list.ts +605 -0
  262. package/src/data-structures/linked-list/index.ts +3 -0
  263. package/src/data-structures/linked-list/singly-linked-list.ts +576 -0
  264. package/src/data-structures/linked-list/skip-linked-list.ts +221 -0
  265. package/src/data-structures/matrix/index.ts +4 -0
  266. package/src/data-structures/matrix/matrix.ts +27 -0
  267. package/src/data-structures/matrix/matrix2d.ts +211 -0
  268. package/src/data-structures/matrix/navigator.ts +121 -0
  269. package/src/data-structures/matrix/vector2d.ts +315 -0
  270. package/src/data-structures/priority-queue/index.ts +3 -0
  271. package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
  272. package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
  273. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  274. package/src/data-structures/queue/deque.ts +282 -0
  275. package/src/data-structures/queue/index.ts +2 -0
  276. package/src/data-structures/queue/queue.ts +209 -0
  277. package/src/data-structures/stack/index.ts +1 -0
  278. package/src/data-structures/stack/stack.ts +102 -0
  279. package/src/data-structures/tree/index.ts +1 -0
  280. package/src/data-structures/tree/tree.ts +41 -0
  281. package/src/data-structures/trie/index.ts +1 -0
  282. package/src/data-structures/trie/trie.ts +262 -0
  283. package/src/index.ts +4 -1
  284. package/src/interfaces/binary-tree.ts +10 -0
  285. package/src/interfaces/doubly-linked-list.ts +1 -0
  286. package/src/interfaces/graph.ts +7 -0
  287. package/src/interfaces/heap.ts +1 -0
  288. package/src/interfaces/index.ts +8 -0
  289. package/src/interfaces/navigator.ts +1 -0
  290. package/src/interfaces/priority-queue.ts +1 -0
  291. package/src/interfaces/segment-tree.ts +1 -0
  292. package/src/interfaces/singly-linked-list.ts +1 -0
  293. package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
  294. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  295. package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
  296. package/src/types/data-structures/binary-tree/bst.ts +11 -0
  297. package/src/types/data-structures/binary-tree/index.ts +6 -0
  298. package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
  299. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  300. package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
  301. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  302. package/src/types/data-structures/graph/directed-graph.ts +8 -0
  303. package/src/types/data-structures/graph/index.ts +3 -0
  304. package/src/types/data-structures/graph/map-graph.ts +1 -0
  305. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  306. package/src/types/data-structures/hash/coordinate-map.ts +1 -0
  307. package/src/types/data-structures/hash/coordinate-set.ts +1 -0
  308. package/src/types/data-structures/hash/hash-map.ts +1 -0
  309. package/src/types/data-structures/hash/hash-table.ts +1 -0
  310. package/src/types/data-structures/hash/index.ts +1 -0
  311. package/src/types/data-structures/hash/tree-map.ts +1 -0
  312. package/src/types/data-structures/hash/tree-set.ts +1 -0
  313. package/src/types/data-structures/heap/heap.ts +1 -0
  314. package/src/types/data-structures/heap/index.ts +1 -0
  315. package/src/types/data-structures/heap/max-heap.ts +1 -0
  316. package/src/types/data-structures/heap/min-heap.ts +1 -0
  317. package/src/types/data-structures/index.ts +11 -0
  318. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  319. package/src/types/data-structures/linked-list/index.ts +2 -0
  320. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  321. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  322. package/src/types/data-structures/matrix/index.ts +1 -0
  323. package/src/types/data-structures/matrix/matrix.ts +1 -0
  324. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  325. package/src/types/data-structures/matrix/navigator.ts +14 -0
  326. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  327. package/src/types/data-structures/priority-queue/index.ts +3 -0
  328. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  329. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  330. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  331. package/src/types/data-structures/queue/deque.ts +1 -0
  332. package/src/types/data-structures/queue/index.ts +2 -0
  333. package/src/types/data-structures/queue/queue.ts +1 -0
  334. package/src/types/data-structures/stack/index.ts +1 -0
  335. package/src/types/data-structures/stack/stack.ts +1 -0
  336. package/src/types/data-structures/tree/index.ts +1 -0
  337. package/src/types/data-structures/tree/tree.ts +1 -0
  338. package/src/types/data-structures/trie/index.ts +1 -0
  339. package/src/types/data-structures/trie/trie.ts +1 -0
  340. package/src/types/helpers.ts +11 -0
  341. package/src/types/index.ts +3 -0
  342. package/src/types/utils/index.ts +2 -0
  343. package/src/types/utils/utils.ts +6 -0
  344. package/src/types/utils/validate-type.ts +35 -0
  345. package/src/utils/index.ts +1 -0
  346. package/src/utils/utils.ts +86 -0
  347. package/tsconfig.json +1 -2
@@ -0,0 +1,884 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Kirk Qi
8
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
9
+ * @license MIT License
10
+ */
11
+ const utils_1 = require("../../utils");
12
+ const priority_queue_1 = require("../priority-queue");
13
+ const queue_1 = require("../queue");
14
+ class AbstractVertex {
15
+ /**
16
+ * The function is a protected constructor that takes an key and an optional value as parameters.
17
+ * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
18
+ * used to uniquely identify the vertex object.
19
+ * @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
20
+ * vertex. If no value is provided, it will be set to undefined.
21
+ */
22
+ constructor(key, value) {
23
+ this.key = key;
24
+ this.value = value;
25
+ }
26
+ }
27
+ exports.AbstractVertex = AbstractVertex;
28
+ class AbstractEdge {
29
+ /**
30
+ * The above function is a protected constructor that initializes the weight, value, and hash code properties of an
31
+ * object.
32
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
33
+ * a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
34
+ * will be assigned.
35
+ * @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
36
+ * meaning it can be omitted when creating an instance of the class.
37
+ */
38
+ constructor(weight, value) {
39
+ this.weight = weight !== undefined ? weight : 1;
40
+ this.value = value;
41
+ this._hashCode = (0, utils_1.uuidV4)();
42
+ }
43
+ get hashCode() {
44
+ return this._hashCode;
45
+ }
46
+ }
47
+ exports.AbstractEdge = AbstractEdge;
48
+ class AbstractGraph {
49
+ constructor() {
50
+ this._vertices = new Map();
51
+ }
52
+ get vertices() {
53
+ return this._vertices;
54
+ }
55
+ /**
56
+ * The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
57
+ * @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
58
+ * the `_vertices` map.
59
+ * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
60
+ * map. If the vertex does not exist, it returns `null`.
61
+ */
62
+ getVertex(vertexKey) {
63
+ return this._vertices.get(vertexKey) || null;
64
+ }
65
+ /**
66
+ * The function checks if a vertex exists in a graph.
67
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
68
+ * (`VertexKey`).
69
+ * @returns a boolean value.
70
+ */
71
+ hasVertex(vertexOrKey) {
72
+ return this._vertices.has(this._getVertexKey(vertexOrKey));
73
+ }
74
+ addVertex(keyOrVertex, value) {
75
+ if (keyOrVertex instanceof AbstractVertex) {
76
+ return this._addVertexOnly(keyOrVertex);
77
+ }
78
+ else {
79
+ const newVertex = this.createVertex(keyOrVertex, value);
80
+ return this._addVertexOnly(newVertex);
81
+ }
82
+ }
83
+ /**
84
+ * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
85
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
86
+ * (`VertexKey`).
87
+ * @returns The method is returning a boolean value.
88
+ */
89
+ deleteVertex(vertexOrKey) {
90
+ const vertexKey = this._getVertexKey(vertexOrKey);
91
+ return this._vertices.delete(vertexKey);
92
+ }
93
+ /**
94
+ * The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
95
+ * @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
96
+ * of vertex IDs (`VertexKey[]`).
97
+ * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
98
+ * were removed.
99
+ */
100
+ removeManyVertices(vertices) {
101
+ const removed = [];
102
+ for (const v of vertices) {
103
+ removed.push(this.deleteVertex(v));
104
+ }
105
+ return removed.length > 0;
106
+ }
107
+ /**
108
+ * The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
109
+ * @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
110
+ * identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
111
+ * @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
112
+ * `VertexKey` or a `VO` type, which represents the type of the vertex.
113
+ * @returns A boolean value is being returned.
114
+ */
115
+ hasEdge(v1, v2) {
116
+ const edge = this.getEdge(v1, v2);
117
+ return !!edge;
118
+ }
119
+ addEdge(srcOrEdge, dest, weight, value) {
120
+ if (srcOrEdge instanceof AbstractEdge) {
121
+ return this._addEdgeOnly(srcOrEdge);
122
+ }
123
+ else {
124
+ if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
125
+ if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
126
+ return false;
127
+ if (srcOrEdge instanceof AbstractVertex)
128
+ srcOrEdge = srcOrEdge.key;
129
+ if (dest instanceof AbstractVertex)
130
+ dest = dest.key;
131
+ const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
132
+ return this._addEdgeOnly(newEdge);
133
+ }
134
+ else {
135
+ throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
136
+ }
137
+ }
138
+ }
139
+ /**
140
+ * The function sets the weight of an edge between two vertices in a graph.
141
+ * @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
142
+ * the source vertex of the edge.
143
+ * @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
144
+ * either a `VertexKey` or a vertex object `VO`.
145
+ * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
146
+ * and the destination vertex (destOrKey).
147
+ * @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
148
+ * the weight of the edge and return true. If the edge does not exist, the function will return false.
149
+ */
150
+ setEdgeWeight(srcOrKey, destOrKey, weight) {
151
+ const edge = this.getEdge(srcOrKey, destOrKey);
152
+ if (edge) {
153
+ edge.weight = weight;
154
+ return true;
155
+ }
156
+ else {
157
+ return false;
158
+ }
159
+ }
160
+ /**
161
+ * The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
162
+ * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
163
+ * It is the starting vertex for finding paths.
164
+ * @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
165
+ * @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`VO[][]`).
166
+ */
167
+ getAllPathsBetween(v1, v2) {
168
+ const paths = [];
169
+ const vertex1 = this._getVertex(v1);
170
+ const vertex2 = this._getVertex(v2);
171
+ if (!(vertex1 && vertex2)) {
172
+ return [];
173
+ }
174
+ const dfs = (cur, dest, visiting, path) => {
175
+ visiting.set(cur, true);
176
+ if (cur === dest) {
177
+ paths.push([vertex1, ...path]);
178
+ }
179
+ const neighbors = this.getNeighbors(cur);
180
+ for (const neighbor of neighbors) {
181
+ if (!visiting.get(neighbor)) {
182
+ path.push(neighbor);
183
+ dfs(neighbor, dest, visiting, path);
184
+ (0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
185
+ }
186
+ }
187
+ visiting.set(cur, false);
188
+ };
189
+ dfs(vertex1, vertex2, new Map(), []);
190
+ return paths;
191
+ }
192
+ /**
193
+ * The function calculates the sum of weights along a given path.
194
+ * @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
195
+ * @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
196
+ */
197
+ getPathSumWeight(path) {
198
+ var _a;
199
+ let sum = 0;
200
+ for (let i = 0; i < path.length; i++) {
201
+ sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
202
+ }
203
+ return sum;
204
+ }
205
+ /**
206
+ * The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
207
+ * weights or using a breadth-first search algorithm.
208
+ * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
209
+ * @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
210
+ * you want to find the minimum cost or weight from the source vertex `v1`.
211
+ * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
212
+ * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
213
+ * the edges. If isWeight is set to false or not provided, the function will calculate the
214
+ * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
215
+ * and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
216
+ * vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
217
+ * minimum number of
218
+ */
219
+ getMinCostBetween(v1, v2, isWeight) {
220
+ if (isWeight === undefined)
221
+ isWeight = false;
222
+ if (isWeight) {
223
+ const allPaths = this.getAllPathsBetween(v1, v2);
224
+ let min = Infinity;
225
+ for (const path of allPaths) {
226
+ min = Math.min(this.getPathSumWeight(path), min);
227
+ }
228
+ return min;
229
+ }
230
+ else {
231
+ // BFS
232
+ const vertex2 = this._getVertex(v2);
233
+ const vertex1 = this._getVertex(v1);
234
+ if (!(vertex1 && vertex2)) {
235
+ return null;
236
+ }
237
+ const visited = new Map();
238
+ const queue = new queue_1.Queue([vertex1]);
239
+ visited.set(vertex1, true);
240
+ let cost = 0;
241
+ while (queue.size > 0) {
242
+ for (let i = 0; i < queue.size; i++) {
243
+ const cur = queue.shift();
244
+ if (cur === vertex2) {
245
+ return cost;
246
+ }
247
+ // TODO consider optimizing to AbstractGraph
248
+ if (cur !== undefined) {
249
+ const neighbors = this.getNeighbors(cur);
250
+ for (const neighbor of neighbors) {
251
+ if (!visited.has(neighbor)) {
252
+ visited.set(neighbor, true);
253
+ queue.push(neighbor);
254
+ }
255
+ }
256
+ }
257
+ }
258
+ cost++;
259
+ }
260
+ return null;
261
+ }
262
+ }
263
+ /**
264
+ * The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
265
+ * using a breadth-first search algorithm.
266
+ * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
267
+ * object (`VO`) or a vertex ID (`VertexKey`).
268
+ * @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
269
+ * path.
270
+ * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
271
+ * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
272
+ * to false, the function will use breadth-first search (BFS) to find the minimum path.
273
+ * @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
274
+ * two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
275
+ */
276
+ getMinPathBetween(v1, v2, isWeight) {
277
+ if (isWeight === undefined)
278
+ isWeight = false;
279
+ if (isWeight) {
280
+ const allPaths = this.getAllPathsBetween(v1, v2);
281
+ let min = Infinity;
282
+ let minIndex = -1;
283
+ let index = 0;
284
+ for (const path of allPaths) {
285
+ const pathSumWeight = this.getPathSumWeight(path);
286
+ if (pathSumWeight < min) {
287
+ min = pathSumWeight;
288
+ minIndex = index;
289
+ }
290
+ index++;
291
+ }
292
+ return allPaths[minIndex] || null;
293
+ }
294
+ else {
295
+ // BFS
296
+ let minPath = [];
297
+ const vertex1 = this._getVertex(v1);
298
+ const vertex2 = this._getVertex(v2);
299
+ if (!(vertex1 && vertex2)) {
300
+ return [];
301
+ }
302
+ const dfs = (cur, dest, visiting, path) => {
303
+ visiting.set(cur, true);
304
+ if (cur === dest) {
305
+ minPath = [vertex1, ...path];
306
+ return;
307
+ }
308
+ const neighbors = this.getNeighbors(cur);
309
+ for (const neighbor of neighbors) {
310
+ if (!visiting.get(neighbor)) {
311
+ path.push(neighbor);
312
+ dfs(neighbor, dest, visiting, path);
313
+ (0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
314
+ }
315
+ }
316
+ visiting.set(cur, false);
317
+ };
318
+ dfs(vertex1, vertex2, new Map(), []);
319
+ return minPath;
320
+ }
321
+ }
322
+ /**
323
+ * Dijkstra algorithm time: O(VE) space: O(VO + EO)
324
+ * /
325
+
326
+ /**
327
+ * Dijkstra algorithm time: O(VE) space: O(VO + EO)
328
+ * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
329
+ * a graph without using a heap data structure.
330
+ * @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
331
+ * vertex object or a vertex ID.
332
+ * @param {VO | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
333
+ * parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
334
+ * identifier. If no destination is provided, the value is set to `null`.
335
+ * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
336
+ * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
337
+ * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
338
+ * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
339
+ * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
340
+ * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
341
+ * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
342
+ */
343
+ dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
344
+ if (getMinDist === undefined)
345
+ getMinDist = false;
346
+ if (genPaths === undefined)
347
+ genPaths = false;
348
+ if (dest === undefined)
349
+ dest = null;
350
+ let minDist = Infinity;
351
+ let minDest = null;
352
+ let minPath = [];
353
+ const paths = [];
354
+ const vertices = this._vertices;
355
+ const distMap = new Map();
356
+ const seen = new Set();
357
+ const preMap = new Map(); // predecessor
358
+ const srcVertex = this._getVertex(src);
359
+ const destVertex = dest ? this._getVertex(dest) : null;
360
+ if (!srcVertex) {
361
+ return null;
362
+ }
363
+ for (const vertex of vertices) {
364
+ const vertexOrKey = vertex[1];
365
+ if (vertexOrKey instanceof AbstractVertex)
366
+ distMap.set(vertexOrKey, Infinity);
367
+ }
368
+ distMap.set(srcVertex, 0);
369
+ preMap.set(srcVertex, null);
370
+ const getMinOfNoSeen = () => {
371
+ let min = Infinity;
372
+ let minV = null;
373
+ for (const [key, value] of distMap) {
374
+ if (!seen.has(key)) {
375
+ if (value < min) {
376
+ min = value;
377
+ minV = key;
378
+ }
379
+ }
380
+ }
381
+ return minV;
382
+ };
383
+ const getPaths = (minV) => {
384
+ for (const vertex of vertices) {
385
+ const vertexOrKey = vertex[1];
386
+ if (vertexOrKey instanceof AbstractVertex) {
387
+ const path = [vertexOrKey];
388
+ let parent = preMap.get(vertexOrKey);
389
+ while (parent) {
390
+ path.push(parent);
391
+ parent = preMap.get(parent);
392
+ }
393
+ const reversed = path.reverse();
394
+ if (vertex[1] === minV)
395
+ minPath = reversed;
396
+ paths.push(reversed);
397
+ }
398
+ }
399
+ };
400
+ for (let i = 1; i < vertices.size; i++) {
401
+ const cur = getMinOfNoSeen();
402
+ if (cur) {
403
+ seen.add(cur);
404
+ if (destVertex && destVertex === cur) {
405
+ if (getMinDist) {
406
+ minDist = distMap.get(destVertex) || Infinity;
407
+ }
408
+ if (genPaths) {
409
+ getPaths(destVertex);
410
+ }
411
+ return { distMap, preMap, seen, paths, minDist, minPath };
412
+ }
413
+ const neighbors = this.getNeighbors(cur);
414
+ for (const neighbor of neighbors) {
415
+ if (!seen.has(neighbor)) {
416
+ const edge = this.getEdge(cur, neighbor);
417
+ if (edge) {
418
+ const curFromMap = distMap.get(cur);
419
+ const neighborFromMap = distMap.get(neighbor);
420
+ // TODO after no-non-null-assertion not ensure the logic
421
+ if (curFromMap !== undefined && neighborFromMap !== undefined) {
422
+ if (edge.weight + curFromMap < neighborFromMap) {
423
+ distMap.set(neighbor, edge.weight + curFromMap);
424
+ preMap.set(neighbor, cur);
425
+ }
426
+ }
427
+ }
428
+ }
429
+ }
430
+ }
431
+ }
432
+ getMinDist &&
433
+ distMap.forEach((d, v) => {
434
+ if (v !== srcVertex) {
435
+ if (d < minDist) {
436
+ minDist = d;
437
+ if (genPaths)
438
+ minDest = v;
439
+ }
440
+ }
441
+ });
442
+ genPaths && getPaths(minDest);
443
+ return { distMap, preMap, seen, paths, minDist, minPath };
444
+ }
445
+ /**
446
+ * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
447
+ *
448
+ * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
449
+ * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
450
+ * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
451
+ *
452
+ * /
453
+
454
+ /**
455
+ * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
456
+ * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
457
+ * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
458
+ * @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
459
+ * start. It can be either a vertex object or a vertex ID.
460
+ * @param {VO | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
461
+ * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
462
+ * will calculate the shortest paths to all other vertices from the source vertex.
463
+ * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
464
+ * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
465
+ * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
466
+ * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
467
+ * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
468
+ * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
469
+ * @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
470
+ */
471
+ dijkstra(src, dest, getMinDist, genPaths) {
472
+ var _a;
473
+ if (getMinDist === undefined)
474
+ getMinDist = false;
475
+ if (genPaths === undefined)
476
+ genPaths = false;
477
+ if (dest === undefined)
478
+ dest = null;
479
+ let minDist = Infinity;
480
+ let minDest = null;
481
+ let minPath = [];
482
+ const paths = [];
483
+ const vertices = this._vertices;
484
+ const distMap = new Map();
485
+ const seen = new Set();
486
+ const preMap = new Map(); // predecessor
487
+ const srcVertex = this._getVertex(src);
488
+ const destVertex = dest ? this._getVertex(dest) : null;
489
+ if (!srcVertex)
490
+ return null;
491
+ for (const vertex of vertices) {
492
+ const vertexOrKey = vertex[1];
493
+ if (vertexOrKey instanceof AbstractVertex)
494
+ distMap.set(vertexOrKey, Infinity);
495
+ }
496
+ const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.key - b.key });
497
+ heap.add({ key: 0, value: srcVertex });
498
+ distMap.set(srcVertex, 0);
499
+ preMap.set(srcVertex, null);
500
+ /**
501
+ * The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
502
+ * @param {VO | null} minV - The parameter `minV` is of type `VO | null`. It represents the minimum vertex value or
503
+ * null.
504
+ */
505
+ const getPaths = (minV) => {
506
+ for (const vertex of vertices) {
507
+ const vertexOrKey = vertex[1];
508
+ if (vertexOrKey instanceof AbstractVertex) {
509
+ const path = [vertexOrKey];
510
+ let parent = preMap.get(vertexOrKey);
511
+ while (parent) {
512
+ path.push(parent);
513
+ parent = preMap.get(parent);
514
+ }
515
+ const reversed = path.reverse();
516
+ if (vertex[1] === minV)
517
+ minPath = reversed;
518
+ paths.push(reversed);
519
+ }
520
+ }
521
+ };
522
+ while (heap.size > 0) {
523
+ const curHeapNode = heap.poll();
524
+ const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.key;
525
+ const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.value;
526
+ if (dist !== undefined) {
527
+ if (cur) {
528
+ seen.add(cur);
529
+ if (destVertex && destVertex === cur) {
530
+ if (getMinDist) {
531
+ minDist = distMap.get(destVertex) || Infinity;
532
+ }
533
+ if (genPaths) {
534
+ getPaths(destVertex);
535
+ }
536
+ return { distMap, preMap, seen, paths, minDist, minPath };
537
+ }
538
+ const neighbors = this.getNeighbors(cur);
539
+ for (const neighbor of neighbors) {
540
+ if (!seen.has(neighbor)) {
541
+ const weight = (_a = this.getEdge(cur, neighbor)) === null || _a === void 0 ? void 0 : _a.weight;
542
+ if (typeof weight === 'number') {
543
+ const distSrcToNeighbor = distMap.get(neighbor);
544
+ if (distSrcToNeighbor) {
545
+ if (dist + weight < distSrcToNeighbor) {
546
+ heap.add({ key: dist + weight, value: neighbor });
547
+ preMap.set(neighbor, cur);
548
+ distMap.set(neighbor, dist + weight);
549
+ }
550
+ }
551
+ }
552
+ }
553
+ }
554
+ }
555
+ }
556
+ }
557
+ if (getMinDist) {
558
+ distMap.forEach((d, v) => {
559
+ if (v !== srcVertex) {
560
+ if (d < minDist) {
561
+ minDist = d;
562
+ if (genPaths)
563
+ minDest = v;
564
+ }
565
+ }
566
+ });
567
+ }
568
+ if (genPaths) {
569
+ getPaths(minDest);
570
+ }
571
+ return { distMap, preMap, seen, paths, minDist, minPath };
572
+ }
573
+ /**
574
+ * BellmanFord time:O(VE) space:O(VO)
575
+ * one to rest pairs
576
+ * /
577
+
578
+ /**
579
+ * BellmanFord time:O(VE) space:O(VO)
580
+ * one to rest pairs
581
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
582
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
583
+ * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
584
+ * @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
585
+ * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
586
+ * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
587
+ * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
588
+ * calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
589
+ * `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
590
+ * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
591
+ * vertex.
592
+ * @returns The function `bellmanFord` returns an object with the following properties:
593
+ */
594
+ bellmanFord(src, scanNegativeCycle, getMin, genPath) {
595
+ if (getMin === undefined)
596
+ getMin = false;
597
+ if (genPath === undefined)
598
+ genPath = false;
599
+ const srcVertex = this._getVertex(src);
600
+ const paths = [];
601
+ const distMap = new Map();
602
+ const preMap = new Map(); // predecessor
603
+ let min = Infinity;
604
+ let minPath = [];
605
+ // TODO
606
+ let hasNegativeCycle;
607
+ if (scanNegativeCycle)
608
+ hasNegativeCycle = false;
609
+ if (!srcVertex)
610
+ return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
611
+ const vertices = this._vertices;
612
+ const numOfVertices = vertices.size;
613
+ const edges = this.edgeSet();
614
+ const numOfEdges = edges.length;
615
+ this._vertices.forEach(vertex => {
616
+ distMap.set(vertex, Infinity);
617
+ });
618
+ distMap.set(srcVertex, 0);
619
+ for (let i = 1; i < numOfVertices; ++i) {
620
+ for (let j = 0; j < numOfEdges; ++j) {
621
+ const ends = this.getEndsOfEdge(edges[j]);
622
+ if (ends) {
623
+ const [s, d] = ends;
624
+ const weight = edges[j].weight;
625
+ const sWeight = distMap.get(s);
626
+ const dWeight = distMap.get(d);
627
+ if (sWeight !== undefined && dWeight !== undefined) {
628
+ if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
629
+ distMap.set(d, sWeight + weight);
630
+ genPath && preMap.set(d, s);
631
+ }
632
+ }
633
+ }
634
+ }
635
+ }
636
+ let minDest = null;
637
+ if (getMin) {
638
+ distMap.forEach((d, v) => {
639
+ if (v !== srcVertex) {
640
+ if (d < min) {
641
+ min = d;
642
+ if (genPath)
643
+ minDest = v;
644
+ }
645
+ }
646
+ });
647
+ }
648
+ if (genPath) {
649
+ for (const vertex of vertices) {
650
+ const vertexOrKey = vertex[1];
651
+ if (vertexOrKey instanceof AbstractVertex) {
652
+ const path = [vertexOrKey];
653
+ let parent = preMap.get(vertexOrKey);
654
+ while (parent !== undefined) {
655
+ path.push(parent);
656
+ parent = preMap.get(parent);
657
+ }
658
+ const reversed = path.reverse();
659
+ if (vertex[1] === minDest)
660
+ minPath = reversed;
661
+ paths.push(reversed);
662
+ }
663
+ }
664
+ }
665
+ for (let j = 0; j < numOfEdges; ++j) {
666
+ const ends = this.getEndsOfEdge(edges[j]);
667
+ if (ends) {
668
+ const [s] = ends;
669
+ const weight = edges[j].weight;
670
+ const sWeight = distMap.get(s);
671
+ if (sWeight) {
672
+ if (sWeight !== Infinity && sWeight + weight < sWeight)
673
+ hasNegativeCycle = true;
674
+ }
675
+ }
676
+ }
677
+ return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
678
+ }
679
+ /**
680
+ * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
681
+ * /
682
+
683
+ /**
684
+ * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
685
+ * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
686
+ */
687
+ /**
688
+ * BellmanFord time:O(VE) space:O(VO)
689
+ * one to rest pairs
690
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
691
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
692
+ */
693
+ /**
694
+ * Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
695
+ * all pairs
696
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
697
+ */
698
+ /**
699
+ * Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
700
+ * all pairs
701
+ * /
702
+
703
+ /**
704
+ * Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
705
+ * all pairs
706
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
707
+ * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
708
+ * graph.
709
+ * @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
710
+ * property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
711
+ * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
712
+ * path between vertices in the
713
+ */
714
+ floyd() {
715
+ var _a;
716
+ const idAndVertices = [...this._vertices];
717
+ const n = idAndVertices.length;
718
+ const costs = [];
719
+ const predecessor = [];
720
+ // successors
721
+ for (let i = 0; i < n; i++) {
722
+ costs[i] = [];
723
+ predecessor[i] = [];
724
+ for (let j = 0; j < n; j++) {
725
+ predecessor[i][j] = null;
726
+ }
727
+ }
728
+ for (let i = 0; i < n; i++) {
729
+ for (let j = 0; j < n; j++) {
730
+ costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
731
+ }
732
+ }
733
+ for (let k = 0; k < n; k++) {
734
+ for (let i = 0; i < n; i++) {
735
+ for (let j = 0; j < n; j++) {
736
+ if (costs[i][j] > costs[i][k] + costs[k][j]) {
737
+ costs[i][j] = costs[i][k] + costs[k][j];
738
+ predecessor[i][j] = idAndVertices[k][1];
739
+ }
740
+ }
741
+ }
742
+ }
743
+ return { costs, predecessor };
744
+ }
745
+ /**
746
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
747
+ * Tarjan can find cycles in directed or undirected graph
748
+ * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
749
+ * Tarjan solve the bi-connected components of undirected graphs;
750
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
751
+ * /
752
+
753
+ /**
754
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
755
+ * Tarjan can find cycles in directed or undirected graph
756
+ * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
757
+ * Tarjan solve the bi-connected components of undirected graphs;
758
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
759
+ * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
760
+ * strongly connected components (SCCs), and cycles in a graph.
761
+ * @param {boolean} [needArticulationPoints] - A boolean value indicating whether or not to calculate and return the
762
+ * articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
763
+ * number of connected components in the graph.
764
+ * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
765
+ * (edges whose removal would increase the number of connected components in the graph).
766
+ * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
767
+ * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
768
+ * SCCs will not be calculated or returned.
769
+ * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
770
+ * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
771
+ * are arrays of vertices that form cycles within the SCCs.
772
+ * @returns The function `tarjan` returns an object with the following properties:
773
+ */
774
+ tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
775
+ // !! in undirected graph we will not let child visit parent when dfs
776
+ // !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
777
+ // !! bridge: low(child) > dfn(cur)
778
+ const defaultConfig = false;
779
+ if (needArticulationPoints === undefined)
780
+ needArticulationPoints = defaultConfig;
781
+ if (needBridges === undefined)
782
+ needBridges = defaultConfig;
783
+ if (needSCCs === undefined)
784
+ needSCCs = defaultConfig;
785
+ if (needCycles === undefined)
786
+ needCycles = defaultConfig;
787
+ const dfnMap = new Map();
788
+ const lowMap = new Map();
789
+ const vertices = this._vertices;
790
+ vertices.forEach(v => {
791
+ dfnMap.set(v, -1);
792
+ lowMap.set(v, Infinity);
793
+ });
794
+ const [root] = vertices.values();
795
+ const articulationPoints = [];
796
+ const bridges = [];
797
+ let dfn = 0;
798
+ const dfs = (cur, parent) => {
799
+ dfn++;
800
+ dfnMap.set(cur, dfn);
801
+ lowMap.set(cur, dfn);
802
+ const neighbors = this.getNeighbors(cur);
803
+ let childCount = 0; // child in dfs tree not child in graph
804
+ for (const neighbor of neighbors) {
805
+ if (neighbor !== parent) {
806
+ if (dfnMap.get(neighbor) === -1) {
807
+ childCount++;
808
+ dfs(neighbor, cur);
809
+ }
810
+ const childLow = lowMap.get(neighbor);
811
+ const curLow = lowMap.get(cur);
812
+ // TODO after no-non-null-assertion not ensure the logic
813
+ if (curLow !== undefined && childLow !== undefined) {
814
+ lowMap.set(cur, Math.min(curLow, childLow));
815
+ }
816
+ const curFromMap = dfnMap.get(cur);
817
+ if (childLow !== undefined && curFromMap !== undefined) {
818
+ if (needArticulationPoints) {
819
+ if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
820
+ // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
821
+ articulationPoints.push(cur);
822
+ }
823
+ }
824
+ if (needBridges) {
825
+ if (childLow > curFromMap) {
826
+ const edgeCurToNeighbor = this.getEdge(cur, neighbor);
827
+ if (edgeCurToNeighbor) {
828
+ bridges.push(edgeCurToNeighbor);
829
+ }
830
+ }
831
+ }
832
+ }
833
+ }
834
+ }
835
+ };
836
+ dfs(root, null);
837
+ let SCCs = new Map();
838
+ const getSCCs = () => {
839
+ const SCCs = new Map();
840
+ lowMap.forEach((low, vertex) => {
841
+ var _a;
842
+ if (!SCCs.has(low)) {
843
+ SCCs.set(low, [vertex]);
844
+ }
845
+ else {
846
+ (_a = SCCs.get(low)) === null || _a === void 0 ? void 0 : _a.push(vertex);
847
+ }
848
+ });
849
+ return SCCs;
850
+ };
851
+ if (needSCCs) {
852
+ SCCs = getSCCs();
853
+ }
854
+ const cycles = new Map();
855
+ if (needCycles) {
856
+ let SCCs = new Map();
857
+ if (SCCs.size < 1) {
858
+ SCCs = getSCCs();
859
+ }
860
+ SCCs.forEach((SCC, low) => {
861
+ if (SCC.length > 1) {
862
+ cycles.set(low, SCC);
863
+ }
864
+ });
865
+ }
866
+ return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
867
+ }
868
+ _addVertexOnly(newVertex) {
869
+ if (this.hasVertex(newVertex)) {
870
+ return false;
871
+ // throw (new Error('Duplicated vertex key is not allowed'));
872
+ }
873
+ this._vertices.set(newVertex.key, newVertex);
874
+ return true;
875
+ }
876
+ _getVertex(vertexOrKey) {
877
+ const vertexKey = this._getVertexKey(vertexOrKey);
878
+ return this._vertices.get(vertexKey) || null;
879
+ }
880
+ _getVertexKey(vertexOrKey) {
881
+ return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
882
+ }
883
+ }
884
+ exports.AbstractGraph = AbstractGraph;