tree-multimap-typed 1.42.5

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