red-black-tree-typed 1.47.3

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 (341) hide show
  1. package/.eslintrc.js +61 -0
  2. package/.prettierignore +6 -0
  3. package/.prettierrc.js +16 -0
  4. package/LICENSE +21 -0
  5. package/README.md +713 -0
  6. package/coverage/clover.xml +13 -0
  7. package/coverage/coverage-final.json +96 -0
  8. package/coverage/coverage-summary.json +60 -0
  9. package/coverage/lcov-report/base.css +403 -0
  10. package/coverage/lcov-report/block-navigation.js +87 -0
  11. package/coverage/lcov-report/favicon.png +0 -0
  12. package/coverage/lcov-report/index.html +119 -0
  13. package/coverage/lcov-report/index.ts.html +109 -0
  14. package/coverage/lcov-report/prettify.css +1 -0
  15. package/coverage/lcov-report/prettify.js +2 -0
  16. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  17. package/coverage/lcov-report/sorter.js +206 -0
  18. package/coverage/lcov.info +14 -0
  19. package/dist/data-structures/binary-tree/avl-tree.d.ts +173 -0
  20. package/dist/data-structures/binary-tree/avl-tree.js +429 -0
  21. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
  22. package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
  23. package/dist/data-structures/binary-tree/binary-tree.d.ts +515 -0
  24. package/dist/data-structures/binary-tree/binary-tree.js +1665 -0
  25. package/dist/data-structures/binary-tree/bst.d.ts +253 -0
  26. package/dist/data-structures/binary-tree/bst.js +651 -0
  27. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  28. package/dist/data-structures/binary-tree/index.js +23 -0
  29. package/dist/data-structures/binary-tree/rb-tree.d.ts +169 -0
  30. package/dist/data-structures/binary-tree/rb-tree.js +524 -0
  31. package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
  32. package/dist/data-structures/binary-tree/segment-tree.js +180 -0
  33. package/dist/data-structures/binary-tree/tree-multimap.d.ts +162 -0
  34. package/dist/data-structures/binary-tree/tree-multimap.js +407 -0
  35. package/dist/data-structures/graph/abstract-graph.d.ts +450 -0
  36. package/dist/data-structures/graph/abstract-graph.js +1047 -0
  37. package/dist/data-structures/graph/directed-graph.d.ts +320 -0
  38. package/dist/data-structures/graph/directed-graph.js +530 -0
  39. package/dist/data-structures/graph/index.d.ts +4 -0
  40. package/dist/data-structures/graph/index.js +20 -0
  41. package/dist/data-structures/graph/map-graph.d.ts +73 -0
  42. package/dist/data-structures/graph/map-graph.js +93 -0
  43. package/dist/data-structures/graph/undirected-graph.d.ts +183 -0
  44. package/dist/data-structures/graph/undirected-graph.js +302 -0
  45. package/dist/data-structures/hash/hash-map.d.ts +186 -0
  46. package/dist/data-structures/hash/hash-map.js +367 -0
  47. package/dist/data-structures/hash/hash-table.d.ts +103 -0
  48. package/dist/data-structures/hash/hash-table.js +236 -0
  49. package/dist/data-structures/hash/index.d.ts +2 -0
  50. package/dist/data-structures/hash/index.js +18 -0
  51. package/dist/data-structures/heap/heap.d.ts +410 -0
  52. package/dist/data-structures/heap/heap.js +697 -0
  53. package/dist/data-structures/heap/index.d.ts +3 -0
  54. package/dist/data-structures/heap/index.js +19 -0
  55. package/dist/data-structures/heap/max-heap.d.ts +15 -0
  56. package/dist/data-structures/heap/max-heap.js +26 -0
  57. package/dist/data-structures/heap/min-heap.d.ts +15 -0
  58. package/dist/data-structures/heap/min-heap.js +26 -0
  59. package/dist/data-structures/index.d.ts +11 -0
  60. package/dist/data-structures/index.js +27 -0
  61. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +456 -0
  62. package/dist/data-structures/linked-list/doubly-linked-list.js +772 -0
  63. package/dist/data-structures/linked-list/index.d.ts +3 -0
  64. package/dist/data-structures/linked-list/index.js +19 -0
  65. package/dist/data-structures/linked-list/singly-linked-list.d.ts +414 -0
  66. package/dist/data-structures/linked-list/singly-linked-list.js +715 -0
  67. package/dist/data-structures/linked-list/skip-linked-list.d.ts +144 -0
  68. package/dist/data-structures/linked-list/skip-linked-list.js +251 -0
  69. package/dist/data-structures/matrix/index.d.ts +4 -0
  70. package/dist/data-structures/matrix/index.js +20 -0
  71. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  72. package/dist/data-structures/matrix/matrix.js +28 -0
  73. package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
  74. package/dist/data-structures/matrix/matrix2d.js +199 -0
  75. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  76. package/dist/data-structures/matrix/navigator.js +106 -0
  77. package/dist/data-structures/matrix/vector2d.d.ts +200 -0
  78. package/dist/data-structures/matrix/vector2d.js +290 -0
  79. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  80. package/dist/data-structures/priority-queue/index.js +19 -0
  81. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  82. package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
  83. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  84. package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
  85. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
  86. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  87. package/dist/data-structures/queue/deque.d.ts +572 -0
  88. package/dist/data-structures/queue/deque.js +990 -0
  89. package/dist/data-structures/queue/index.d.ts +2 -0
  90. package/dist/data-structures/queue/index.js +18 -0
  91. package/dist/data-structures/queue/queue.d.ts +209 -0
  92. package/dist/data-structures/queue/queue.js +274 -0
  93. package/dist/data-structures/stack/index.d.ts +1 -0
  94. package/dist/data-structures/stack/index.js +17 -0
  95. package/dist/data-structures/stack/stack.d.ts +106 -0
  96. package/dist/data-structures/stack/stack.js +136 -0
  97. package/dist/data-structures/tree/index.d.ts +1 -0
  98. package/dist/data-structures/tree/index.js +17 -0
  99. package/dist/data-structures/tree/tree.d.ts +8 -0
  100. package/dist/data-structures/tree/tree.js +40 -0
  101. package/dist/data-structures/trie/index.d.ts +1 -0
  102. package/dist/data-structures/trie/index.js +17 -0
  103. package/dist/data-structures/trie/trie.d.ts +155 -0
  104. package/dist/data-structures/trie/trie.js +326 -0
  105. package/dist/index.d.ts +10 -0
  106. package/dist/index.js +27 -0
  107. package/dist/interfaces/binary-tree.d.ts +7 -0
  108. package/dist/interfaces/binary-tree.js +2 -0
  109. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  110. package/dist/interfaces/doubly-linked-list.js +2 -0
  111. package/dist/interfaces/graph.d.ts +5 -0
  112. package/dist/interfaces/graph.js +2 -0
  113. package/dist/interfaces/heap.d.ts +1 -0
  114. package/dist/interfaces/heap.js +2 -0
  115. package/dist/interfaces/index.d.ts +8 -0
  116. package/dist/interfaces/index.js +24 -0
  117. package/dist/interfaces/navigator.d.ts +1 -0
  118. package/dist/interfaces/navigator.js +2 -0
  119. package/dist/interfaces/priority-queue.d.ts +1 -0
  120. package/dist/interfaces/priority-queue.js +2 -0
  121. package/dist/interfaces/segment-tree.d.ts +1 -0
  122. package/dist/interfaces/segment-tree.js +2 -0
  123. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  124. package/dist/interfaces/singly-linked-list.js +2 -0
  125. package/dist/types/common.d.ts +20 -0
  126. package/dist/types/common.js +9 -0
  127. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +5 -0
  128. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  129. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  130. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  131. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
  132. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  133. package/dist/types/data-structures/binary-tree/bst.d.ts +8 -0
  134. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  135. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  136. package/dist/types/data-structures/binary-tree/index.js +22 -0
  137. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +9 -0
  138. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  139. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  140. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  141. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +5 -0
  142. package/dist/types/data-structures/binary-tree/tree-multimap.js +2 -0
  143. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  144. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  145. package/dist/types/data-structures/graph/directed-graph.d.ts +1 -0
  146. package/dist/types/data-structures/graph/directed-graph.js +2 -0
  147. package/dist/types/data-structures/graph/index.d.ts +3 -0
  148. package/dist/types/data-structures/graph/index.js +19 -0
  149. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  150. package/dist/types/data-structures/graph/map-graph.js +2 -0
  151. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  152. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  153. package/dist/types/data-structures/hash/hash-map.d.ts +11 -0
  154. package/dist/types/data-structures/hash/hash-map.js +2 -0
  155. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  156. package/dist/types/data-structures/hash/hash-table.js +2 -0
  157. package/dist/types/data-structures/hash/index.d.ts +3 -0
  158. package/dist/types/data-structures/hash/index.js +18 -0
  159. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  160. package/dist/types/data-structures/heap/heap.js +2 -0
  161. package/dist/types/data-structures/heap/index.d.ts +1 -0
  162. package/dist/types/data-structures/heap/index.js +17 -0
  163. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  164. package/dist/types/data-structures/heap/max-heap.js +2 -0
  165. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  166. package/dist/types/data-structures/heap/min-heap.js +2 -0
  167. package/dist/types/data-structures/index.d.ts +11 -0
  168. package/dist/types/data-structures/index.js +27 -0
  169. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  170. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  171. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  172. package/dist/types/data-structures/linked-list/index.js +18 -0
  173. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  174. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  175. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  176. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  177. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  178. package/dist/types/data-structures/matrix/index.js +17 -0
  179. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  180. package/dist/types/data-structures/matrix/matrix.js +2 -0
  181. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  182. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  183. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  184. package/dist/types/data-structures/matrix/navigator.js +2 -0
  185. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  186. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  187. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  188. package/dist/types/data-structures/priority-queue/index.js +19 -0
  189. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  190. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  191. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  192. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  193. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  194. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  195. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  196. package/dist/types/data-structures/queue/deque.js +2 -0
  197. package/dist/types/data-structures/queue/index.d.ts +2 -0
  198. package/dist/types/data-structures/queue/index.js +18 -0
  199. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  200. package/dist/types/data-structures/queue/queue.js +2 -0
  201. package/dist/types/data-structures/stack/index.d.ts +1 -0
  202. package/dist/types/data-structures/stack/index.js +17 -0
  203. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  204. package/dist/types/data-structures/stack/stack.js +2 -0
  205. package/dist/types/data-structures/tree/index.d.ts +1 -0
  206. package/dist/types/data-structures/tree/index.js +17 -0
  207. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  208. package/dist/types/data-structures/tree/tree.js +2 -0
  209. package/dist/types/data-structures/trie/index.d.ts +1 -0
  210. package/dist/types/data-structures/trie/index.js +17 -0
  211. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  212. package/dist/types/data-structures/trie/trie.js +2 -0
  213. package/dist/types/index.d.ts +3 -0
  214. package/dist/types/index.js +19 -0
  215. package/dist/types/utils/index.d.ts +2 -0
  216. package/dist/types/utils/index.js +18 -0
  217. package/dist/types/utils/utils.d.ts +7 -0
  218. package/dist/types/utils/utils.js +2 -0
  219. package/dist/types/utils/validate-type.d.ts +19 -0
  220. package/dist/types/utils/validate-type.js +2 -0
  221. package/dist/utils/index.d.ts +1 -0
  222. package/dist/utils/index.js +17 -0
  223. package/dist/utils/utils.d.ts +24 -0
  224. package/dist/utils/utils.js +89 -0
  225. package/docs/.nojekyll +1 -0
  226. package/docs/assets/highlight.css +92 -0
  227. package/docs/assets/main.js +59 -0
  228. package/docs/assets/navigation.js +1 -0
  229. package/docs/assets/search.js +1 -0
  230. package/docs/assets/style.css +1383 -0
  231. package/docs/classes/AVLTree.html +2046 -0
  232. package/docs/classes/AVLTreeNode.html +263 -0
  233. package/docs/index.html +523 -0
  234. package/docs/modules.html +45 -0
  235. package/jest.config.js +8 -0
  236. package/package.json +147 -0
  237. package/src/data-structures/binary-tree/avl-tree.ts +443 -0
  238. package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
  239. package/src/data-structures/binary-tree/binary-tree.ts +1974 -0
  240. package/src/data-structures/binary-tree/bst.ts +676 -0
  241. package/src/data-structures/binary-tree/index.ts +7 -0
  242. package/src/data-structures/binary-tree/rb-tree.ts +585 -0
  243. package/src/data-structures/binary-tree/segment-tree.ts +190 -0
  244. package/src/data-structures/binary-tree/tree-multimap.ts +435 -0
  245. package/src/data-structures/graph/abstract-graph.ts +1181 -0
  246. package/src/data-structures/graph/directed-graph.ts +593 -0
  247. package/src/data-structures/graph/index.ts +4 -0
  248. package/src/data-structures/graph/map-graph.ts +106 -0
  249. package/src/data-structures/graph/undirected-graph.ts +331 -0
  250. package/src/data-structures/hash/hash-map.ts +401 -0
  251. package/src/data-structures/hash/hash-table.ts +268 -0
  252. package/src/data-structures/hash/index.ts +2 -0
  253. package/src/data-structures/heap/heap.ts +790 -0
  254. package/src/data-structures/heap/index.ts +3 -0
  255. package/src/data-structures/heap/max-heap.ts +26 -0
  256. package/src/data-structures/heap/min-heap.ts +26 -0
  257. package/src/data-structures/index.ts +11 -0
  258. package/src/data-structures/linked-list/doubly-linked-list.ts +837 -0
  259. package/src/data-structures/linked-list/index.ts +3 -0
  260. package/src/data-structures/linked-list/singly-linked-list.ts +784 -0
  261. package/src/data-structures/linked-list/skip-linked-list.ts +295 -0
  262. package/src/data-structures/matrix/index.ts +4 -0
  263. package/src/data-structures/matrix/matrix.ts +27 -0
  264. package/src/data-structures/matrix/matrix2d.ts +211 -0
  265. package/src/data-structures/matrix/navigator.ts +121 -0
  266. package/src/data-structures/matrix/vector2d.ts +315 -0
  267. package/src/data-structures/priority-queue/index.ts +3 -0
  268. package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
  269. package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
  270. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  271. package/src/data-structures/queue/deque.ts +1073 -0
  272. package/src/data-structures/queue/index.ts +2 -0
  273. package/src/data-structures/queue/queue.ts +308 -0
  274. package/src/data-structures/stack/index.ts +1 -0
  275. package/src/data-structures/stack/stack.ts +150 -0
  276. package/src/data-structures/tree/index.ts +1 -0
  277. package/src/data-structures/tree/tree.ts +41 -0
  278. package/src/data-structures/trie/index.ts +1 -0
  279. package/src/data-structures/trie/trie.ts +345 -0
  280. package/src/index.ts +11 -0
  281. package/src/interfaces/binary-tree.ts +10 -0
  282. package/src/interfaces/doubly-linked-list.ts +1 -0
  283. package/src/interfaces/graph.ts +7 -0
  284. package/src/interfaces/heap.ts +1 -0
  285. package/src/interfaces/index.ts +8 -0
  286. package/src/interfaces/navigator.ts +1 -0
  287. package/src/interfaces/priority-queue.ts +1 -0
  288. package/src/interfaces/segment-tree.ts +1 -0
  289. package/src/interfaces/singly-linked-list.ts +1 -0
  290. package/src/types/common.ts +23 -0
  291. package/src/types/data-structures/binary-tree/avl-tree.ts +9 -0
  292. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  293. package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
  294. package/src/types/data-structures/binary-tree/bst.ts +13 -0
  295. package/src/types/data-structures/binary-tree/index.ts +6 -0
  296. package/src/types/data-structures/binary-tree/rb-tree.ts +10 -0
  297. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  298. package/src/types/data-structures/binary-tree/tree-multimap.ts +8 -0
  299. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  300. package/src/types/data-structures/graph/directed-graph.ts +2 -0
  301. package/src/types/data-structures/graph/index.ts +3 -0
  302. package/src/types/data-structures/graph/map-graph.ts +1 -0
  303. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  304. package/src/types/data-structures/hash/hash-map.ts +12 -0
  305. package/src/types/data-structures/hash/hash-table.ts +1 -0
  306. package/src/types/data-structures/hash/index.ts +4 -0
  307. package/src/types/data-structures/heap/heap.ts +1 -0
  308. package/src/types/data-structures/heap/index.ts +1 -0
  309. package/src/types/data-structures/heap/max-heap.ts +1 -0
  310. package/src/types/data-structures/heap/min-heap.ts +1 -0
  311. package/src/types/data-structures/index.ts +11 -0
  312. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  313. package/src/types/data-structures/linked-list/index.ts +2 -0
  314. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  315. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  316. package/src/types/data-structures/matrix/index.ts +1 -0
  317. package/src/types/data-structures/matrix/matrix.ts +1 -0
  318. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  319. package/src/types/data-structures/matrix/navigator.ts +14 -0
  320. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  321. package/src/types/data-structures/priority-queue/index.ts +3 -0
  322. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  323. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  324. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  325. package/src/types/data-structures/queue/deque.ts +1 -0
  326. package/src/types/data-structures/queue/index.ts +2 -0
  327. package/src/types/data-structures/queue/queue.ts +1 -0
  328. package/src/types/data-structures/stack/index.ts +1 -0
  329. package/src/types/data-structures/stack/stack.ts +1 -0
  330. package/src/types/data-structures/tree/index.ts +1 -0
  331. package/src/types/data-structures/tree/tree.ts +1 -0
  332. package/src/types/data-structures/trie/index.ts +1 -0
  333. package/src/types/data-structures/trie/trie.ts +1 -0
  334. package/src/types/index.ts +3 -0
  335. package/src/types/utils/index.ts +2 -0
  336. package/src/types/utils/utils.ts +6 -0
  337. package/src/types/utils/validate-type.ts +35 -0
  338. package/src/utils/index.ts +1 -0
  339. package/src/utils/utils.ts +101 -0
  340. package/test/index.test.ts +111 -0
  341. package/tsconfig.json +38 -0
@@ -0,0 +1,1047 @@
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 Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@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
+ * Time Complexity: O(1) - Constant time for Map lookup.
57
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
58
+ */
59
+ /**
60
+ * Time Complexity: O(1) - Constant time for Map lookup.
61
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
62
+ *
63
+ * The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
64
+ * @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
65
+ * the `_vertices` map.
66
+ * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
67
+ * map. If the vertex does not exist, it returns `null`.
68
+ */
69
+ getVertex(vertexKey) {
70
+ return this._vertices.get(vertexKey) || null;
71
+ }
72
+ /**
73
+ * Time Complexity: O(1) - Constant time for Map lookup.
74
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
75
+ */
76
+ /**
77
+ * Time Complexity: O(1) - Constant time for Map lookup.
78
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
79
+ *
80
+ * The function checks if a vertex exists in a graph.
81
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
82
+ * (`VertexKey`).
83
+ * @returns a boolean value.
84
+ */
85
+ hasVertex(vertexOrKey) {
86
+ return this._vertices.has(this._getVertexKey(vertexOrKey));
87
+ }
88
+ /**
89
+ * Time Complexity: O(1) - Constant time for Map operations.
90
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
91
+ */
92
+ addVertex(keyOrVertex, value) {
93
+ if (keyOrVertex instanceof AbstractVertex) {
94
+ return this._addVertexOnly(keyOrVertex);
95
+ }
96
+ else {
97
+ const newVertex = this.createVertex(keyOrVertex, value);
98
+ return this._addVertexOnly(newVertex);
99
+ }
100
+ }
101
+ /**
102
+ * Time Complexity: O(1) - Constant time for Map operations.
103
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
104
+ */
105
+ /**
106
+ * Time Complexity: O(1) - Constant time for Map operations.
107
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
108
+ *
109
+ * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
110
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
111
+ * (`VertexKey`).
112
+ * @returns The method is returning a boolean value.
113
+ */
114
+ deleteVertex(vertexOrKey) {
115
+ const vertexKey = this._getVertexKey(vertexOrKey);
116
+ return this._vertices.delete(vertexKey);
117
+ }
118
+ /**
119
+ * Time Complexity: O(K), where K is the number of vertices to be removed.
120
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
121
+ */
122
+ /**
123
+ * Time Complexity: O(K), where K is the number of vertices to be removed.
124
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
125
+ *
126
+ * The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
127
+ * @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
128
+ * of vertex IDs (`VertexKey[]`).
129
+ * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
130
+ * were removed.
131
+ */
132
+ removeManyVertices(vertices) {
133
+ const removed = [];
134
+ for (const v of vertices) {
135
+ removed.push(this.deleteVertex(v));
136
+ }
137
+ return removed.length > 0;
138
+ }
139
+ /**
140
+ * Time Complexity: O(1) - Depends on the implementation in the concrete class.
141
+ * Space Complexity: O(1) - Depends on the implementation in the concrete class.
142
+ */
143
+ /**
144
+ * Time Complexity: O(1) - Depends on the implementation in the concrete class.
145
+ * Space Complexity: O(1) - Depends on the implementation in the concrete class.
146
+ *
147
+ * The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
148
+ * @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
149
+ * identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
150
+ * @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
151
+ * `VertexKey` or a `VO` type, which represents the type of the vertex.
152
+ * @returns A boolean value is being returned.
153
+ */
154
+ hasEdge(v1, v2) {
155
+ const edge = this.getEdge(v1, v2);
156
+ return !!edge;
157
+ }
158
+ /**
159
+ * Time Complexity: O(1) - Depends on the implementation in the concrete class.
160
+ * Space Complexity: O(1) - Depends on the implementation in the concrete class.
161
+ */
162
+ addEdge(srcOrEdge, dest, weight, value) {
163
+ if (srcOrEdge instanceof AbstractEdge) {
164
+ return this._addEdgeOnly(srcOrEdge);
165
+ }
166
+ else {
167
+ if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
168
+ if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
169
+ return false;
170
+ if (srcOrEdge instanceof AbstractVertex)
171
+ srcOrEdge = srcOrEdge.key;
172
+ if (dest instanceof AbstractVertex)
173
+ dest = dest.key;
174
+ const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
175
+ return this._addEdgeOnly(newEdge);
176
+ }
177
+ else {
178
+ throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
179
+ }
180
+ }
181
+ }
182
+ /**
183
+ * Time Complexity: O(1) - Constant time for Map and Edge operations.
184
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
185
+ */
186
+ /**
187
+ * Time Complexity: O(1) - Constant time for Map and Edge operations.
188
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
189
+ *
190
+ * The function sets the weight of an edge between two vertices in a graph.
191
+ * @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
192
+ * the source vertex of the edge.
193
+ * @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
194
+ * either a `VertexKey` or a vertex object `VO`.
195
+ * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
196
+ * and the destination vertex (destOrKey).
197
+ * @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
198
+ * the weight of the edge and return true. If the edge does not exist, the function will return false.
199
+ */
200
+ setEdgeWeight(srcOrKey, destOrKey, weight) {
201
+ const edge = this.getEdge(srcOrKey, destOrKey);
202
+ if (edge) {
203
+ edge.weight = weight;
204
+ return true;
205
+ }
206
+ else {
207
+ return false;
208
+ }
209
+ }
210
+ /**
211
+ * Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
212
+ * Space Complexity: O(P) - Linear space, where P is the number of paths found.
213
+ */
214
+ /**
215
+ * Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
216
+ * Space Complexity: O(P) - Linear space, where P is the number of paths found.
217
+ *
218
+ * The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
219
+ * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
220
+ * It is the starting vertex for finding paths.
221
+ * @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
222
+ * @param limit - The count of limitation of result array.
223
+ * @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`VO[][]`).
224
+ */
225
+ getAllPathsBetween(v1, v2, limit = 1000) {
226
+ const paths = [];
227
+ const vertex1 = this._getVertex(v1);
228
+ const vertex2 = this._getVertex(v2);
229
+ if (!(vertex1 && vertex2)) {
230
+ return [];
231
+ }
232
+ const stack = [];
233
+ stack.push({ vertex: vertex1, path: [vertex1] });
234
+ while (stack.length > 0) {
235
+ const { vertex, path } = stack.pop();
236
+ if (vertex === vertex2) {
237
+ paths.push(path);
238
+ if (paths.length >= limit)
239
+ return paths;
240
+ }
241
+ const neighbors = this.getNeighbors(vertex);
242
+ for (const neighbor of neighbors) {
243
+ if (!path.includes(neighbor)) {
244
+ const newPath = [...path, neighbor];
245
+ stack.push({ vertex: neighbor, path: newPath });
246
+ }
247
+ }
248
+ }
249
+ return paths;
250
+ }
251
+ /**
252
+ * Time Complexity: O(L), where L is the length of the path.
253
+ * Space Complexity: O(1) - Constant space.
254
+ */
255
+ /**
256
+ * Time Complexity: O(L), where L is the length of the path.
257
+ * Space Complexity: O(1) - Constant space.
258
+ *
259
+ * The function calculates the sum of weights along a given path.
260
+ * @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
261
+ * @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
262
+ */
263
+ getPathSumWeight(path) {
264
+ var _a;
265
+ let sum = 0;
266
+ for (let i = 0; i < path.length; i++) {
267
+ sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
268
+ }
269
+ return sum;
270
+ }
271
+ /**
272
+ * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
273
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
274
+ */
275
+ /**
276
+ * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
277
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
278
+ *
279
+ * The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
280
+ * weights or using a breadth-first search algorithm.
281
+ * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
282
+ * @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
283
+ * you want to find the minimum cost or weight from the source vertex `v1`.
284
+ * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
285
+ * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
286
+ * the edges. If isWeight is set to false or not provided, the function will calculate the
287
+ * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
288
+ * and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
289
+ * vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
290
+ * minimum number of
291
+ */
292
+ getMinCostBetween(v1, v2, isWeight) {
293
+ if (isWeight === undefined)
294
+ isWeight = false;
295
+ if (isWeight) {
296
+ const allPaths = this.getAllPathsBetween(v1, v2);
297
+ let min = Infinity;
298
+ for (const path of allPaths) {
299
+ min = Math.min(this.getPathSumWeight(path), min);
300
+ }
301
+ return min;
302
+ }
303
+ else {
304
+ // BFS
305
+ const vertex2 = this._getVertex(v2);
306
+ const vertex1 = this._getVertex(v1);
307
+ if (!(vertex1 && vertex2)) {
308
+ return null;
309
+ }
310
+ const visited = new Map();
311
+ const queue = new queue_1.Queue([vertex1]);
312
+ visited.set(vertex1, true);
313
+ let cost = 0;
314
+ while (queue.size > 0) {
315
+ for (let i = 0; i < queue.size; i++) {
316
+ const cur = queue.shift();
317
+ if (cur === vertex2) {
318
+ return cost;
319
+ }
320
+ // TODO consider optimizing to AbstractGraph
321
+ if (cur !== undefined) {
322
+ const neighbors = this.getNeighbors(cur);
323
+ for (const neighbor of neighbors) {
324
+ if (!visited.has(neighbor)) {
325
+ visited.set(neighbor, true);
326
+ queue.push(neighbor);
327
+ }
328
+ }
329
+ }
330
+ }
331
+ cost++;
332
+ }
333
+ return null;
334
+ }
335
+ }
336
+ /**
337
+ * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
338
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
339
+ */
340
+ /**
341
+ * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
342
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
343
+ *
344
+ * The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
345
+ * using a breadth-first search algorithm.
346
+ * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
347
+ * object (`VO`) or a vertex ID (`VertexKey`).
348
+ * @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
349
+ * path.
350
+ * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
351
+ * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
352
+ * to false, the function will use breadth-first search (BFS) to find the minimum path.
353
+ * @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
354
+ * followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
355
+ * so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
356
+ * @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
357
+ * two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
358
+ */
359
+ getMinPathBetween(v1, v2, isWeight, isDFS = false) {
360
+ var _a, _b;
361
+ if (isWeight === undefined)
362
+ isWeight = false;
363
+ if (isWeight) {
364
+ if (isDFS) {
365
+ const allPaths = this.getAllPathsBetween(v1, v2, 10000);
366
+ let min = Infinity;
367
+ let minIndex = -1;
368
+ let index = 0;
369
+ for (const path of allPaths) {
370
+ const pathSumWeight = this.getPathSumWeight(path);
371
+ if (pathSumWeight < min) {
372
+ min = pathSumWeight;
373
+ minIndex = index;
374
+ }
375
+ index++;
376
+ }
377
+ return allPaths[minIndex] || null;
378
+ }
379
+ else {
380
+ return (_b = (_a = this.dijkstra(v1, v2, true, true)) === null || _a === void 0 ? void 0 : _a.minPath) !== null && _b !== void 0 ? _b : [];
381
+ }
382
+ }
383
+ else {
384
+ // DFS
385
+ let minPath = [];
386
+ const vertex1 = this._getVertex(v1);
387
+ const vertex2 = this._getVertex(v2);
388
+ if (!(vertex1 && vertex2))
389
+ return [];
390
+ const dfs = (cur, dest, visiting, path) => {
391
+ visiting.add(cur);
392
+ if (cur === dest) {
393
+ minPath = [vertex1, ...path];
394
+ return;
395
+ }
396
+ const neighbors = this.getNeighbors(cur);
397
+ for (const neighbor of neighbors) {
398
+ if (!visiting.has(neighbor)) {
399
+ path.push(neighbor);
400
+ dfs(neighbor, dest, visiting, path);
401
+ path.pop();
402
+ }
403
+ }
404
+ visiting.delete(cur);
405
+ };
406
+ dfs(vertex1, vertex2, new Set(), []);
407
+ return minPath;
408
+ }
409
+ }
410
+ /**
411
+ * Dijkstra algorithm time: O(VE) space: O(VO + EO)
412
+ * /
413
+
414
+ /**
415
+ * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
416
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
417
+ */
418
+ /**
419
+ * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
420
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
421
+ *
422
+ * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
423
+ * a graph without using a heap data structure.
424
+ * @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
425
+ * vertex object or a vertex ID.
426
+ * @param {VO | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
427
+ * parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
428
+ * identifier. If no destination is provided, the value is set to `null`.
429
+ * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
430
+ * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
431
+ * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
432
+ * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
433
+ * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
434
+ * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
435
+ * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
436
+ */
437
+ dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
438
+ if (getMinDist === undefined)
439
+ getMinDist = false;
440
+ if (genPaths === undefined)
441
+ genPaths = false;
442
+ if (dest === undefined)
443
+ dest = null;
444
+ let minDist = Infinity;
445
+ let minDest = null;
446
+ let minPath = [];
447
+ const paths = [];
448
+ const vertices = this._vertices;
449
+ const distMap = new Map();
450
+ const seen = new Set();
451
+ const preMap = new Map(); // predecessor
452
+ const srcVertex = this._getVertex(src);
453
+ const destVertex = dest ? this._getVertex(dest) : null;
454
+ if (!srcVertex) {
455
+ return null;
456
+ }
457
+ for (const vertex of vertices) {
458
+ const vertexOrKey = vertex[1];
459
+ if (vertexOrKey instanceof AbstractVertex)
460
+ distMap.set(vertexOrKey, Infinity);
461
+ }
462
+ distMap.set(srcVertex, 0);
463
+ preMap.set(srcVertex, null);
464
+ const getMinOfNoSeen = () => {
465
+ let min = Infinity;
466
+ let minV = null;
467
+ for (const [key, value] of distMap) {
468
+ if (!seen.has(key)) {
469
+ if (value < min) {
470
+ min = value;
471
+ minV = key;
472
+ }
473
+ }
474
+ }
475
+ return minV;
476
+ };
477
+ const getPaths = (minV) => {
478
+ for (const vertex of vertices) {
479
+ const vertexOrKey = vertex[1];
480
+ if (vertexOrKey instanceof AbstractVertex) {
481
+ const path = [vertexOrKey];
482
+ let parent = preMap.get(vertexOrKey);
483
+ while (parent) {
484
+ path.push(parent);
485
+ parent = preMap.get(parent);
486
+ }
487
+ const reversed = path.reverse();
488
+ if (vertex[1] === minV)
489
+ minPath = reversed;
490
+ paths.push(reversed);
491
+ }
492
+ }
493
+ };
494
+ for (let i = 1; i < vertices.size; i++) {
495
+ const cur = getMinOfNoSeen();
496
+ if (cur) {
497
+ seen.add(cur);
498
+ if (destVertex && destVertex === cur) {
499
+ if (getMinDist) {
500
+ minDist = distMap.get(destVertex) || Infinity;
501
+ }
502
+ if (genPaths) {
503
+ getPaths(destVertex);
504
+ }
505
+ return { distMap, preMap, seen, paths, minDist, minPath };
506
+ }
507
+ const neighbors = this.getNeighbors(cur);
508
+ for (const neighbor of neighbors) {
509
+ if (!seen.has(neighbor)) {
510
+ const edge = this.getEdge(cur, neighbor);
511
+ if (edge) {
512
+ const curFromMap = distMap.get(cur);
513
+ const neighborFromMap = distMap.get(neighbor);
514
+ // TODO after no-non-null-assertion not ensure the logic
515
+ if (curFromMap !== undefined && neighborFromMap !== undefined) {
516
+ if (edge.weight + curFromMap < neighborFromMap) {
517
+ distMap.set(neighbor, edge.weight + curFromMap);
518
+ preMap.set(neighbor, cur);
519
+ }
520
+ }
521
+ }
522
+ }
523
+ }
524
+ }
525
+ }
526
+ getMinDist &&
527
+ distMap.forEach((d, v) => {
528
+ if (v !== srcVertex) {
529
+ if (d < minDist) {
530
+ minDist = d;
531
+ if (genPaths)
532
+ minDest = v;
533
+ }
534
+ }
535
+ });
536
+ genPaths && getPaths(minDest);
537
+ return { distMap, preMap, seen, paths, minDist, minPath };
538
+ }
539
+ /**
540
+ * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
541
+ *
542
+ * 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.
543
+ * 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.
544
+ * 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.
545
+ *
546
+ * /
547
+
548
+ /**
549
+ * Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
550
+ * Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
551
+ */
552
+ /**
553
+ * Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
554
+ * Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
555
+ *
556
+ * 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.
557
+ * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
558
+ * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
559
+ * @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
560
+ * start. It can be either a vertex object or a vertex ID.
561
+ * @param {VO | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
562
+ * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
563
+ * will calculate the shortest paths to all other vertices from the source vertex.
564
+ * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
565
+ * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
566
+ * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
567
+ * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
568
+ * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
569
+ * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
570
+ * @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
571
+ */
572
+ dijkstra(src, dest, getMinDist, genPaths) {
573
+ var _a;
574
+ if (getMinDist === undefined)
575
+ getMinDist = false;
576
+ if (genPaths === undefined)
577
+ genPaths = false;
578
+ if (dest === undefined)
579
+ dest = null;
580
+ let minDist = Infinity;
581
+ let minDest = null;
582
+ let minPath = [];
583
+ const paths = [];
584
+ const vertices = this._vertices;
585
+ const distMap = new Map();
586
+ const seen = new Set();
587
+ const preMap = new Map(); // predecessor
588
+ const srcVertex = this._getVertex(src);
589
+ const destVertex = dest ? this._getVertex(dest) : null;
590
+ if (!srcVertex)
591
+ return null;
592
+ for (const vertex of vertices) {
593
+ const vertexOrKey = vertex[1];
594
+ if (vertexOrKey instanceof AbstractVertex)
595
+ distMap.set(vertexOrKey, Infinity);
596
+ }
597
+ const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.key - b.key });
598
+ heap.add({ key: 0, value: srcVertex });
599
+ distMap.set(srcVertex, 0);
600
+ preMap.set(srcVertex, null);
601
+ /**
602
+ * The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
603
+ * @param {VO | null} minV - The parameter `minV` is of type `VO | null`. It represents the minimum vertex value or
604
+ * null.
605
+ */
606
+ const getPaths = (minV) => {
607
+ for (const vertex of vertices) {
608
+ const vertexOrKey = vertex[1];
609
+ if (vertexOrKey instanceof AbstractVertex) {
610
+ const path = [vertexOrKey];
611
+ let parent = preMap.get(vertexOrKey);
612
+ while (parent) {
613
+ path.push(parent);
614
+ parent = preMap.get(parent);
615
+ }
616
+ const reversed = path.reverse();
617
+ if (vertex[1] === minV)
618
+ minPath = reversed;
619
+ paths.push(reversed);
620
+ }
621
+ }
622
+ };
623
+ while (heap.size > 0) {
624
+ const curHeapNode = heap.poll();
625
+ const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.key;
626
+ const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.value;
627
+ if (dist !== undefined) {
628
+ if (cur) {
629
+ seen.add(cur);
630
+ if (destVertex && destVertex === cur) {
631
+ if (getMinDist) {
632
+ minDist = distMap.get(destVertex) || Infinity;
633
+ }
634
+ if (genPaths) {
635
+ getPaths(destVertex);
636
+ }
637
+ return { distMap, preMap, seen, paths, minDist, minPath };
638
+ }
639
+ const neighbors = this.getNeighbors(cur);
640
+ for (const neighbor of neighbors) {
641
+ if (!seen.has(neighbor)) {
642
+ const weight = (_a = this.getEdge(cur, neighbor)) === null || _a === void 0 ? void 0 : _a.weight;
643
+ if (typeof weight === 'number') {
644
+ const distSrcToNeighbor = distMap.get(neighbor);
645
+ if (distSrcToNeighbor) {
646
+ if (dist + weight < distSrcToNeighbor) {
647
+ heap.add({ key: dist + weight, value: neighbor });
648
+ preMap.set(neighbor, cur);
649
+ distMap.set(neighbor, dist + weight);
650
+ }
651
+ }
652
+ }
653
+ }
654
+ }
655
+ }
656
+ }
657
+ }
658
+ if (getMinDist) {
659
+ distMap.forEach((d, v) => {
660
+ if (v !== srcVertex) {
661
+ if (d < minDist) {
662
+ minDist = d;
663
+ if (genPaths)
664
+ minDest = v;
665
+ }
666
+ }
667
+ });
668
+ }
669
+ if (genPaths) {
670
+ getPaths(minDest);
671
+ }
672
+ return { distMap, preMap, seen, paths, minDist, minPath };
673
+ }
674
+ /**
675
+ * Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
676
+ * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
677
+ * one to rest pairs
678
+ * /
679
+
680
+ /**
681
+ * Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
682
+ * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
683
+ *
684
+ * one to rest pairs
685
+ * 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.
686
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
687
+ * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
688
+ * @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
689
+ * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
690
+ * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
691
+ * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
692
+ * calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
693
+ * `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
694
+ * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
695
+ * vertex.
696
+ * @returns The function `bellmanFord` returns an object with the following properties:
697
+ */
698
+ bellmanFord(src, scanNegativeCycle, getMin, genPath) {
699
+ if (getMin === undefined)
700
+ getMin = false;
701
+ if (genPath === undefined)
702
+ genPath = false;
703
+ const srcVertex = this._getVertex(src);
704
+ const paths = [];
705
+ const distMap = new Map();
706
+ const preMap = new Map(); // predecessor
707
+ let min = Infinity;
708
+ let minPath = [];
709
+ // TODO
710
+ let hasNegativeCycle;
711
+ if (scanNegativeCycle)
712
+ hasNegativeCycle = false;
713
+ if (!srcVertex)
714
+ return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
715
+ const vertices = this._vertices;
716
+ const numOfVertices = vertices.size;
717
+ const edges = this.edgeSet();
718
+ const numOfEdges = edges.length;
719
+ this._vertices.forEach(vertex => {
720
+ distMap.set(vertex, Infinity);
721
+ });
722
+ distMap.set(srcVertex, 0);
723
+ for (let i = 1; i < numOfVertices; ++i) {
724
+ for (let j = 0; j < numOfEdges; ++j) {
725
+ const ends = this.getEndsOfEdge(edges[j]);
726
+ if (ends) {
727
+ const [s, d] = ends;
728
+ const weight = edges[j].weight;
729
+ const sWeight = distMap.get(s);
730
+ const dWeight = distMap.get(d);
731
+ if (sWeight !== undefined && dWeight !== undefined) {
732
+ if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
733
+ distMap.set(d, sWeight + weight);
734
+ genPath && preMap.set(d, s);
735
+ }
736
+ }
737
+ }
738
+ }
739
+ }
740
+ let minDest = null;
741
+ if (getMin) {
742
+ distMap.forEach((d, v) => {
743
+ if (v !== srcVertex) {
744
+ if (d < min) {
745
+ min = d;
746
+ if (genPath)
747
+ minDest = v;
748
+ }
749
+ }
750
+ });
751
+ }
752
+ if (genPath) {
753
+ for (const vertex of vertices) {
754
+ const vertexOrKey = vertex[1];
755
+ if (vertexOrKey instanceof AbstractVertex) {
756
+ const path = [vertexOrKey];
757
+ let parent = preMap.get(vertexOrKey);
758
+ while (parent !== undefined) {
759
+ path.push(parent);
760
+ parent = preMap.get(parent);
761
+ }
762
+ const reversed = path.reverse();
763
+ if (vertex[1] === minDest)
764
+ minPath = reversed;
765
+ paths.push(reversed);
766
+ }
767
+ }
768
+ }
769
+ for (let j = 0; j < numOfEdges; ++j) {
770
+ const ends = this.getEndsOfEdge(edges[j]);
771
+ if (ends) {
772
+ const [s] = ends;
773
+ const weight = edges[j].weight;
774
+ const sWeight = distMap.get(s);
775
+ if (sWeight) {
776
+ if (sWeight !== Infinity && sWeight + weight < sWeight)
777
+ hasNegativeCycle = true;
778
+ }
779
+ }
780
+ }
781
+ return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
782
+ }
783
+ /**
784
+ * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
785
+ * /
786
+
787
+ /**
788
+ * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
789
+ * 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.
790
+ */
791
+ /**
792
+ * BellmanFord time:O(VE) space:O(VO)
793
+ * one to rest pairs
794
+ * 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.
795
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
796
+ */
797
+ /**
798
+ * Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
799
+ * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
800
+ * Not support graph with negative weight cycle
801
+ * all pairs
802
+ * 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.
803
+ * /
804
+
805
+ /**
806
+ * Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
807
+ * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
808
+ *
809
+ * Not support graph with negative weight cycle
810
+ * all pairs
811
+ * 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.
812
+ * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
813
+ * graph.
814
+ * @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
815
+ * property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
816
+ * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
817
+ * path between vertices in the
818
+ */
819
+ floydWarshall() {
820
+ var _a;
821
+ const idAndVertices = [...this._vertices];
822
+ const n = idAndVertices.length;
823
+ const costs = [];
824
+ const predecessor = [];
825
+ // successors
826
+ for (let i = 0; i < n; i++) {
827
+ costs[i] = [];
828
+ predecessor[i] = [];
829
+ for (let j = 0; j < n; j++) {
830
+ predecessor[i][j] = null;
831
+ }
832
+ }
833
+ for (let i = 0; i < n; i++) {
834
+ for (let j = 0; j < n; j++) {
835
+ costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
836
+ }
837
+ }
838
+ for (let k = 0; k < n; k++) {
839
+ for (let i = 0; i < n; i++) {
840
+ for (let j = 0; j < n; j++) {
841
+ if (costs[i][j] > costs[i][k] + costs[k][j]) {
842
+ costs[i][j] = costs[i][k] + costs[k][j];
843
+ predecessor[i][j] = idAndVertices[k][1];
844
+ }
845
+ }
846
+ }
847
+ }
848
+ return { costs, predecessor };
849
+ }
850
+ /**
851
+ * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
852
+ * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
853
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
854
+ * Tarjan can find cycles in directed or undirected graph
855
+ * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
856
+ * Tarjan solve the bi-connected components of undirected graphs;
857
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
858
+ * /
859
+
860
+ /**
861
+ * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
862
+ * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
863
+ *
864
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
865
+ * Tarjan can find cycles in directed or undirected graph
866
+ * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
867
+ * Tarjan solve the bi-connected components of undirected graphs;
868
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
869
+ * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
870
+ * strongly connected components (SCCs), and cycles in a graph.
871
+ * @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
872
+ * articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
873
+ * number of connected components in the graph.
874
+ * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
875
+ * (edges whose removal would increase the number of connected components in the graph).
876
+ * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
877
+ * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
878
+ * SCCs will not be calculated or returned.
879
+ * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
880
+ * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
881
+ * are arrays of vertices that form cycles within the SCCs.
882
+ * @returns The function `tarjan` returns an object with the following properties:
883
+ */
884
+ tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
885
+ // !! in undirected graph we will not let child visit parent when dfs
886
+ // !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
887
+ // !! bridge: low(child) > dfn(cur)
888
+ const defaultConfig = false;
889
+ if (needCutVertexes === undefined)
890
+ needCutVertexes = defaultConfig;
891
+ if (needBridges === undefined)
892
+ needBridges = defaultConfig;
893
+ if (needSCCs === undefined)
894
+ needSCCs = defaultConfig;
895
+ if (needCycles === undefined)
896
+ needCycles = defaultConfig;
897
+ const dfnMap = new Map();
898
+ const lowMap = new Map();
899
+ const vertices = this._vertices;
900
+ vertices.forEach(v => {
901
+ dfnMap.set(v, -1);
902
+ lowMap.set(v, Infinity);
903
+ });
904
+ const [root] = vertices.values();
905
+ const cutVertexes = [];
906
+ const bridges = [];
907
+ let dfn = 0;
908
+ const dfs = (cur, parent) => {
909
+ dfn++;
910
+ dfnMap.set(cur, dfn);
911
+ lowMap.set(cur, dfn);
912
+ const neighbors = this.getNeighbors(cur);
913
+ let childCount = 0; // child in dfs tree not child in graph
914
+ for (const neighbor of neighbors) {
915
+ if (neighbor !== parent) {
916
+ if (dfnMap.get(neighbor) === -1) {
917
+ childCount++;
918
+ dfs(neighbor, cur);
919
+ }
920
+ const childLow = lowMap.get(neighbor);
921
+ const curLow = lowMap.get(cur);
922
+ // TODO after no-non-null-assertion not ensure the logic
923
+ if (curLow !== undefined && childLow !== undefined) {
924
+ lowMap.set(cur, Math.min(curLow, childLow));
925
+ }
926
+ const curFromMap = dfnMap.get(cur);
927
+ if (childLow !== undefined && curFromMap !== undefined) {
928
+ if (needCutVertexes) {
929
+ if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
930
+ // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
931
+ cutVertexes.push(cur);
932
+ }
933
+ }
934
+ if (needBridges) {
935
+ if (childLow > curFromMap) {
936
+ const edgeCurToNeighbor = this.getEdge(cur, neighbor);
937
+ if (edgeCurToNeighbor) {
938
+ bridges.push(edgeCurToNeighbor);
939
+ }
940
+ }
941
+ }
942
+ }
943
+ }
944
+ }
945
+ };
946
+ dfs(root, null);
947
+ let SCCs = new Map();
948
+ const getSCCs = () => {
949
+ const SCCs = new Map();
950
+ lowMap.forEach((low, vertex) => {
951
+ var _a;
952
+ if (!SCCs.has(low)) {
953
+ SCCs.set(low, [vertex]);
954
+ }
955
+ else {
956
+ (_a = SCCs.get(low)) === null || _a === void 0 ? void 0 : _a.push(vertex);
957
+ }
958
+ });
959
+ return SCCs;
960
+ };
961
+ if (needSCCs) {
962
+ SCCs = getSCCs();
963
+ }
964
+ const cycles = new Map();
965
+ if (needCycles) {
966
+ let SCCs = new Map();
967
+ if (SCCs.size < 1) {
968
+ SCCs = getSCCs();
969
+ }
970
+ SCCs.forEach((SCC, low) => {
971
+ if (SCC.length > 1) {
972
+ cycles.set(low, SCC);
973
+ }
974
+ });
975
+ }
976
+ return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
977
+ }
978
+ /**
979
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
980
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
981
+ */
982
+ /**
983
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
984
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
985
+ *
986
+ * The function returns a map that associates each vertex object with its corresponding depth-first
987
+ * number.
988
+ * @returns A Map object with keys of type VO and values of type number.
989
+ */
990
+ getDFNMap() {
991
+ return this.tarjan(false, false, false, false).dfnMap;
992
+ }
993
+ /**
994
+ * The function returns a Map object that contains the low values of each vertex in a Tarjan
995
+ * algorithm.
996
+ * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
997
+ * type `number`.
998
+ */
999
+ getLowMap() {
1000
+ return this.tarjan(false, false, false, false).lowMap;
1001
+ }
1002
+ /**
1003
+ * The function `getCycles` returns a map of cycles found using the Tarjan algorithm.
1004
+ * @returns The function `getCycles()` is returning a `Map<number, VO[]>`.
1005
+ */
1006
+ getCycles() {
1007
+ return this.tarjan(false, false, false, true).cycles;
1008
+ }
1009
+ /**
1010
+ * The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
1011
+ * @returns an array of VO objects, specifically the cut vertexes.
1012
+ */
1013
+ getCutVertexes() {
1014
+ return this.tarjan(true, false, false, false).cutVertexes;
1015
+ }
1016
+ /**
1017
+ * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
1018
+ * algorithm.
1019
+ * @returns a map where the keys are numbers and the values are arrays of VO objects.
1020
+ */
1021
+ getSCCs() {
1022
+ return this.tarjan(false, false, true, false).SCCs;
1023
+ }
1024
+ /**
1025
+ * The function "getBridges" returns an array of bridges using the Tarjan algorithm.
1026
+ * @returns the bridges found using the Tarjan algorithm.
1027
+ */
1028
+ getBridges() {
1029
+ return this.tarjan(false, true, false, false).bridges;
1030
+ }
1031
+ _addVertexOnly(newVertex) {
1032
+ if (this.hasVertex(newVertex)) {
1033
+ return false;
1034
+ // throw (new Error('Duplicated vertex key is not allowed'));
1035
+ }
1036
+ this._vertices.set(newVertex.key, newVertex);
1037
+ return true;
1038
+ }
1039
+ _getVertex(vertexOrKey) {
1040
+ const vertexKey = this._getVertexKey(vertexOrKey);
1041
+ return this._vertices.get(vertexKey) || null;
1042
+ }
1043
+ _getVertexKey(vertexOrKey) {
1044
+ return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
1045
+ }
1046
+ }
1047
+ exports.AbstractGraph = AbstractGraph;