data-structure-typed 1.32.1 → 1.32.9

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