data-structure-typed 2.0.4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/COMMANDS.md +17 -0
  3. package/README.md +11 -11
  4. package/benchmark/report.html +13 -77
  5. package/benchmark/report.json +158 -190
  6. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +186 -83
  7. package/dist/cjs/data-structures/base/iterable-element-base.js +149 -107
  8. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  9. package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +95 -119
  10. package/dist/cjs/data-structures/base/iterable-entry-base.js +59 -116
  11. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
  12. package/dist/cjs/data-structures/base/linear-base.d.ts +250 -192
  13. package/dist/cjs/data-structures/base/linear-base.js +137 -274
  14. package/dist/cjs/data-structures/base/linear-base.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +171 -205
  17. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  19. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  20. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +138 -149
  22. package/dist/cjs/data-structures/binary-tree/avl-tree.js +208 -195
  23. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +476 -632
  25. package/dist/cjs/data-structures/binary-tree/binary-tree.js +608 -875
  26. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/bst.d.ts +258 -306
  28. package/dist/cjs/data-structures/binary-tree/bst.js +505 -481
  29. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  30. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  31. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +114 -209
  32. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
  33. package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +132 -154
  34. package/dist/cjs/data-structures/binary-tree/tree-counter.js +172 -203
  35. package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
  36. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  37. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -85
  38. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  39. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +238 -233
  40. package/dist/cjs/data-structures/graph/abstract-graph.js +267 -237
  41. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  42. package/dist/cjs/data-structures/graph/directed-graph.d.ts +108 -224
  43. package/dist/cjs/data-structures/graph/directed-graph.js +146 -233
  44. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  45. package/dist/cjs/data-structures/graph/map-graph.d.ts +49 -55
  46. package/dist/cjs/data-structures/graph/map-graph.js +56 -59
  47. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  48. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +103 -146
  49. package/dist/cjs/data-structures/graph/undirected-graph.js +129 -149
  50. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  51. package/dist/cjs/data-structures/hash/hash-map.d.ts +164 -338
  52. package/dist/cjs/data-structures/hash/hash-map.js +270 -457
  53. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  54. package/dist/cjs/data-structures/heap/heap.d.ts +214 -289
  55. package/dist/cjs/data-structures/heap/heap.js +340 -349
  56. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  57. package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -47
  58. package/dist/cjs/data-structures/heap/max-heap.js +11 -66
  59. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  60. package/dist/cjs/data-structures/heap/min-heap.d.ts +12 -47
  61. package/dist/cjs/data-structures/heap/min-heap.js +11 -66
  62. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  63. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  64. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +368 -494
  65. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  66. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  67. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +447 -466
  68. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  69. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  70. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -100
  71. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  72. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  73. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +11 -78
  74. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  75. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  76. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +10 -79
  77. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  78. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -61
  79. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -83
  80. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  81. package/dist/cjs/data-structures/queue/deque.d.ts +227 -254
  82. package/dist/cjs/data-structures/queue/deque.js +309 -348
  83. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  84. package/dist/cjs/data-structures/queue/queue.d.ts +180 -201
  85. package/dist/cjs/data-structures/queue/queue.js +265 -248
  86. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  87. package/dist/cjs/data-structures/stack/stack.d.ts +124 -102
  88. package/dist/cjs/data-structures/stack/stack.js +181 -125
  89. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  90. package/dist/cjs/data-structures/trie/trie.d.ts +164 -165
  91. package/dist/cjs/data-structures/trie/trie.js +189 -172
  92. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  93. package/dist/cjs/interfaces/binary-tree.d.ts +56 -6
  94. package/dist/cjs/interfaces/graph.d.ts +16 -0
  95. package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
  96. package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +4 -0
  97. package/dist/cjs/types/utils/utils.d.ts +6 -6
  98. package/dist/cjs/utils/utils.d.ts +110 -49
  99. package/dist/cjs/utils/utils.js +148 -73
  100. package/dist/cjs/utils/utils.js.map +1 -1
  101. package/dist/esm/data-structures/base/iterable-element-base.d.ts +186 -83
  102. package/dist/esm/data-structures/base/iterable-element-base.js +155 -107
  103. package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
  104. package/dist/esm/data-structures/base/iterable-entry-base.d.ts +95 -119
  105. package/dist/esm/data-structures/base/iterable-entry-base.js +59 -116
  106. package/dist/esm/data-structures/base/iterable-entry-base.js.map +1 -1
  107. package/dist/esm/data-structures/base/linear-base.d.ts +250 -192
  108. package/dist/esm/data-structures/base/linear-base.js +137 -274
  109. package/dist/esm/data-structures/base/linear-base.js.map +1 -1
  110. package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  111. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +171 -212
  112. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  113. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  114. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +133 -94
  115. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  116. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +138 -149
  117. package/dist/esm/data-structures/binary-tree/avl-tree.js +206 -200
  118. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
  119. package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +476 -632
  120. package/dist/esm/data-structures/binary-tree/binary-tree.js +613 -885
  121. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  122. package/dist/esm/data-structures/binary-tree/bst.d.ts +258 -306
  123. package/dist/esm/data-structures/binary-tree/bst.js +507 -487
  124. package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
  125. package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  126. package/dist/esm/data-structures/binary-tree/red-black-tree.js +114 -215
  127. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
  128. package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +132 -154
  129. package/dist/esm/data-structures/binary-tree/tree-counter.js +175 -209
  130. package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
  131. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  132. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +103 -92
  133. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  134. package/dist/esm/data-structures/graph/abstract-graph.d.ts +238 -233
  135. package/dist/esm/data-structures/graph/abstract-graph.js +267 -237
  136. package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
  137. package/dist/esm/data-structures/graph/directed-graph.d.ts +108 -224
  138. package/dist/esm/data-structures/graph/directed-graph.js +145 -233
  139. package/dist/esm/data-structures/graph/directed-graph.js.map +1 -1
  140. package/dist/esm/data-structures/graph/map-graph.d.ts +49 -55
  141. package/dist/esm/data-structures/graph/map-graph.js +56 -59
  142. package/dist/esm/data-structures/graph/map-graph.js.map +1 -1
  143. package/dist/esm/data-structures/graph/undirected-graph.d.ts +103 -146
  144. package/dist/esm/data-structures/graph/undirected-graph.js +128 -149
  145. package/dist/esm/data-structures/graph/undirected-graph.js.map +1 -1
  146. package/dist/esm/data-structures/hash/hash-map.d.ts +164 -338
  147. package/dist/esm/data-structures/hash/hash-map.js +270 -457
  148. package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
  149. package/dist/esm/data-structures/heap/heap.d.ts +214 -289
  150. package/dist/esm/data-structures/heap/heap.js +329 -349
  151. package/dist/esm/data-structures/heap/heap.js.map +1 -1
  152. package/dist/esm/data-structures/heap/max-heap.d.ts +11 -47
  153. package/dist/esm/data-structures/heap/max-heap.js +11 -66
  154. package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
  155. package/dist/esm/data-structures/heap/min-heap.d.ts +12 -47
  156. package/dist/esm/data-structures/heap/min-heap.js +11 -66
  157. package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
  158. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  159. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +368 -495
  160. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  161. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  162. package/dist/esm/data-structures/linked-list/singly-linked-list.js +448 -467
  163. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
  164. package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  165. package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -100
  166. package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +1 -1
  167. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  168. package/dist/esm/data-structures/priority-queue/max-priority-queue.js +11 -78
  169. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  170. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  171. package/dist/esm/data-structures/priority-queue/min-priority-queue.js +10 -79
  172. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  173. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -61
  174. package/dist/esm/data-structures/priority-queue/priority-queue.js +8 -83
  175. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
  176. package/dist/esm/data-structures/queue/deque.d.ts +227 -254
  177. package/dist/esm/data-structures/queue/deque.js +313 -348
  178. package/dist/esm/data-structures/queue/deque.js.map +1 -1
  179. package/dist/esm/data-structures/queue/queue.d.ts +180 -201
  180. package/dist/esm/data-structures/queue/queue.js +263 -248
  181. package/dist/esm/data-structures/queue/queue.js.map +1 -1
  182. package/dist/esm/data-structures/stack/stack.d.ts +124 -102
  183. package/dist/esm/data-structures/stack/stack.js +181 -125
  184. package/dist/esm/data-structures/stack/stack.js.map +1 -1
  185. package/dist/esm/data-structures/trie/trie.d.ts +164 -165
  186. package/dist/esm/data-structures/trie/trie.js +193 -172
  187. package/dist/esm/data-structures/trie/trie.js.map +1 -1
  188. package/dist/esm/interfaces/binary-tree.d.ts +56 -6
  189. package/dist/esm/interfaces/graph.d.ts +16 -0
  190. package/dist/esm/types/data-structures/base/base.d.ts +1 -1
  191. package/dist/esm/types/data-structures/graph/abstract-graph.d.ts +4 -0
  192. package/dist/esm/types/utils/utils.d.ts +6 -6
  193. package/dist/esm/utils/utils.d.ts +110 -49
  194. package/dist/esm/utils/utils.js +139 -68
  195. package/dist/esm/utils/utils.js.map +1 -1
  196. package/dist/umd/data-structure-typed.js +4737 -6525
  197. package/dist/umd/data-structure-typed.min.js +8 -6
  198. package/dist/umd/data-structure-typed.min.js.map +1 -1
  199. package/package.json +3 -4
  200. package/src/data-structures/base/iterable-element-base.ts +238 -115
  201. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  202. package/src/data-structures/base/linear-base.ts +271 -277
  203. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  204. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  205. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  206. package/src/data-structures/binary-tree/binary-tree.ts +677 -901
  207. package/src/data-structures/binary-tree/bst.ts +568 -570
  208. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  209. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  210. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  211. package/src/data-structures/graph/abstract-graph.ts +339 -264
  212. package/src/data-structures/graph/directed-graph.ts +146 -236
  213. package/src/data-structures/graph/map-graph.ts +63 -60
  214. package/src/data-structures/graph/undirected-graph.ts +129 -152
  215. package/src/data-structures/hash/hash-map.ts +274 -496
  216. package/src/data-structures/heap/heap.ts +389 -402
  217. package/src/data-structures/heap/max-heap.ts +12 -76
  218. package/src/data-structures/heap/min-heap.ts +13 -76
  219. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  220. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  221. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  222. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  223. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  224. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  225. package/src/data-structures/queue/deque.ts +381 -357
  226. package/src/data-structures/queue/queue.ts +310 -264
  227. package/src/data-structures/stack/stack.ts +217 -131
  228. package/src/data-structures/trie/trie.ts +240 -175
  229. package/src/interfaces/binary-tree.ts +240 -6
  230. package/src/interfaces/graph.ts +37 -0
  231. package/src/types/data-structures/base/base.ts +5 -5
  232. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  233. package/src/types/utils/utils.ts +9 -5
  234. package/src/utils/utils.ts +152 -86
  235. package/test/integration/index.html +1 -1
  236. package/test/performance/benchmark-runner.ts +528 -0
  237. package/test/performance/reportor.mjs +43 -43
  238. package/test/performance/runner-config.json +39 -0
  239. package/test/performance/single-suite-runner.ts +69 -0
  240. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +3 -3
  241. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +5 -5
  242. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
  243. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +350 -90
  244. package/test/unit/data-structures/binary-tree/bst.test.ts +84 -5
  245. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +2 -2
  246. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +25 -24
  247. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +3 -3
  248. package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -4
  249. package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
  250. package/test/unit/data-structures/heap/heap.test.ts +14 -21
  251. package/test/unit/data-structures/heap/max-heap.test.ts +5 -9
  252. package/test/unit/data-structures/heap/min-heap.test.ts +1 -4
  253. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
  254. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -7
  255. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +8 -11
  256. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -4
  257. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +1 -4
  258. package/test/unit/data-structures/queue/queue.test.ts +4 -5
  259. package/test/unit/utils/utils.test.ts +0 -1
  260. package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -71
  261. package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -81
@@ -1,3 +1,10 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
1
8
  import { uuidV4 } from '../../utils';
2
9
  import { IterableEntryBase } from '../base';
3
10
  import { Heap } from '../heap';
@@ -5,13 +12,6 @@ import { Queue } from '../queue';
5
12
  export class AbstractVertex {
6
13
  key;
7
14
  value;
8
- /**
9
- * The function is a protected constructor that takes an key and an optional value as parameters.
10
- * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
11
- * used to uniquely identify the vertex object.
12
- * @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
13
- * vertex. If no value is provided, it will be set to undefined.
14
- */
15
15
  constructor(key, value) {
16
16
  this.key = key;
17
17
  this.value = value;
@@ -20,15 +20,6 @@ export class AbstractVertex {
20
20
  export class AbstractEdge {
21
21
  value;
22
22
  weight;
23
- /**
24
- * The above function is a protected constructor that initializes the weight, value, and hash code properties of an
25
- * object.
26
- * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
27
- * a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
28
- * will be assigned.
29
- * @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
30
- * meaning it can be omitted when creating an instance of the class.
31
- */
32
23
  constructor(weight, value) {
33
24
  this.weight = weight !== undefined ? weight : 1;
34
25
  this.value = value;
@@ -39,9 +30,29 @@ export class AbstractEdge {
39
30
  return this._hashCode;
40
31
  }
41
32
  }
33
+ /**
34
+ * Abstract graph over vertices and edges.
35
+ * @template V - Vertex value type.
36
+ * @template E - Edge value type.
37
+ * @template VO - Concrete vertex subclass (extends AbstractVertex<V>).
38
+ * @template EO - Concrete edge subclass (extends AbstractEdge<E>).
39
+ * @remarks Time O(1), Space O(1)
40
+ * @example examples will be generated by unit test
41
+ */
42
42
  export class AbstractGraph extends IterableEntryBase {
43
- constructor() {
43
+ /**
44
+ * Construct a graph with runtime defaults.
45
+ * @param options - `GraphOptions<V>` in `options.graph` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
46
+ * @remarks Time O(1), Space O(1)
47
+ */
48
+ constructor(options) {
44
49
  super();
50
+ const graph = options?.graph;
51
+ this._options = { defaultEdgeWeight: 1, ...(graph ?? {}) };
52
+ }
53
+ _options = { defaultEdgeWeight: 1 };
54
+ get options() {
55
+ return this._options;
45
56
  }
46
57
  _vertexMap = new Map();
47
58
  get vertexMap() {
@@ -54,33 +65,29 @@ export class AbstractGraph extends IterableEntryBase {
54
65
  return this._vertexMap.size;
55
66
  }
56
67
  /**
57
- * Time Complexity: O(1) - Constant time for Map lookup.
58
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
59
- *
60
- * The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
61
- * @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
62
- * the `_vertexMap` map.
63
- * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertexMap`
64
- * map. If the vertex does not exist, it returns `undefined`.
68
+ * Get vertex instance by key.
69
+ * @param vertexKey - Vertex key.
70
+ * @returns Vertex instance or `undefined`.
71
+ * @remarks Time O(1), Space O(1)
65
72
  */
66
73
  getVertex(vertexKey) {
67
74
  return this._vertexMap.get(vertexKey) || undefined;
68
75
  }
69
76
  /**
70
- * Time Complexity: O(1) - Constant time for Map lookup.
71
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
72
- *
73
- * The function checks if a vertex exists in a graph.
74
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
75
- * (`VertexKey`).
76
- * @returns a boolean value.
77
+ * Whether a vertex exists.
78
+ * @param vertexOrKey - Vertex or key.
79
+ * @returns `true` if present, otherwise `false`.
80
+ * @remarks Time O(1) avg, Space O(1)
77
81
  */
78
82
  hasVertex(vertexOrKey) {
79
83
  return this._vertexMap.has(this._getVertexKey(vertexOrKey));
80
84
  }
81
85
  /**
82
- * Time Complexity: O(1) - Constant time for Map operations.
83
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
86
+ * Add a vertex by key/value or by pre-built vertex.
87
+ * @param keyOrVertex - Vertex key or existing vertex instance.
88
+ * @param value - Optional payload.
89
+ * @returns `true` if inserted; `false` when key already exists.
90
+ * @remarks Time O(1) avg, Space O(1)
84
91
  */
85
92
  addVertex(keyOrVertex, value) {
86
93
  if (keyOrVertex instanceof AbstractVertex) {
@@ -91,19 +98,21 @@ export class AbstractGraph extends IterableEntryBase {
91
98
  return this._addVertex(newVertex);
92
99
  }
93
100
  }
101
+ /**
102
+ * Type guard: check if a value is a valid vertex key.
103
+ * @param potentialKey - Value to test.
104
+ * @returns `true` if string/number; else `false`.
105
+ * @remarks Time O(1), Space O(1)
106
+ */
94
107
  isVertexKey(potentialKey) {
95
108
  const potentialKeyType = typeof potentialKey;
96
109
  return potentialKeyType === 'string' || potentialKeyType === 'number';
97
110
  }
98
111
  /**
99
- * Time Complexity: O(K), where K is the number of vertexMap to be removed.
100
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
101
- *
102
- * The function removes all vertexMap from a graph and returns a boolean indicating if any vertexMap were removed.
103
- * @param {VO[] | VertexKey[]} vertexMap - The `vertexMap` parameter can be either an array of vertexMap (`VO[]`) or an array
104
- * of vertex IDs (`VertexKey[]`).
105
- * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertexMap
106
- * were removed.
112
+ * Delete multiple vertices.
113
+ * @param vertexMap - Array of vertices or keys.
114
+ * @returns `true` if any vertex was removed.
115
+ * @remarks Time O(sum(deg)), Space O(1)
107
116
  */
108
117
  removeManyVertices(vertexMap) {
109
118
  const removed = [];
@@ -113,23 +122,24 @@ export class AbstractGraph extends IterableEntryBase {
113
122
  return removed.length > 0;
114
123
  }
115
124
  /**
116
- * Time Complexity: O(1) - Depends on the implementation in the concrete class.
117
- * Space Complexity: O(1) - Depends on the implementation in the concrete class.
118
- *
119
- * The function checks if there is an edge between two vertexMap and returns a boolean value indicating the result.
120
- * @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
121
- * identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
122
- * @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
123
- * `VertexKey` or a `VO` type, which represents the type of the vertex.
124
- * @returns A boolean value is being returned.
125
+ * Whether an edge exists between two vertices.
126
+ * @param v1 - Endpoint A vertex or key.
127
+ * @param v2 - Endpoint B vertex or key.
128
+ * @returns `true` if present; otherwise `false`.
129
+ * @remarks Time O(1) avg, Space O(1)
125
130
  */
126
131
  hasEdge(v1, v2) {
127
132
  const edge = this.getEdge(v1, v2);
128
133
  return !!edge;
129
134
  }
130
135
  /**
131
- * Time Complexity: O(1) - Depends on the implementation in the concrete class.
132
- * Space Complexity: O(1) - Depends on the implementation in the concrete class.
136
+ * Add an edge by instance or by `(src, dest, weight?, value?)`.
137
+ * @param srcOrEdge - Edge instance or source vertex/key.
138
+ * @param dest - Destination vertex/key (when adding by pair).
139
+ * @param weight - Edge weight.
140
+ * @param value - Edge payload.
141
+ * @returns `true` if inserted; otherwise `false`.
142
+ * @remarks Time O(1) avg, Space O(1)
133
143
  */
134
144
  addEdge(srcOrEdge, dest, weight, value) {
135
145
  if (srcOrEdge instanceof AbstractEdge) {
@@ -152,18 +162,12 @@ export class AbstractGraph extends IterableEntryBase {
152
162
  }
153
163
  }
154
164
  /**
155
- * Time Complexity: O(1) - Constant time for Map and Edge operations.
156
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
157
- *
158
- * The function sets the weight of an edge between two vertexMap in a graph.
159
- * @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
160
- * the source vertex of the edge.
161
- * @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
162
- * either a `VertexKey` or a vertex object `VO`.
163
- * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
164
- * and the destination vertex (destOrKey).
165
- * @returns a boolean value. If the edge exists between the source and destination vertexMap, the function will update
166
- * the weight of the edge and return true. If the edge does not exist, the function will return false.
165
+ * Set the weight of an existing edge.
166
+ * @param srcOrKey - Source vertex or key.
167
+ * @param destOrKey - Destination vertex or key.
168
+ * @param weight - New weight.
169
+ * @returns `true` if updated; otherwise `false`.
170
+ * @remarks Time O(1) avg, Space O(1)
167
171
  */
168
172
  setEdgeWeight(srcOrKey, destOrKey, weight) {
169
173
  const edge = this.getEdge(srcOrKey, destOrKey);
@@ -176,15 +180,12 @@ export class AbstractGraph extends IterableEntryBase {
176
180
  }
177
181
  }
178
182
  /**
179
- * Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
180
- * Space Complexity: O(P) - Linear space, where P is the number of paths found.
181
- *
182
- * The function `getAllPathsBetween` finds all paths between two vertexMap in a graph using depth-first search.
183
- * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
184
- * It is the starting vertex for finding paths.
185
- * @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
186
- * @param limit - The count of limitation of result array.
187
- * @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
183
+ * Enumerate simple paths up to a limit.
184
+ * @param v1 - Source vertex or key.
185
+ * @param v2 - Destination vertex or key.
186
+ * @param limit - Maximum number of paths to collect.
187
+ * @returns Array of paths (each path is an array of vertices).
188
+ * @remarks Time O(paths) worst-case exponential, Space O(V + paths)
188
189
  */
189
190
  getAllPathsBetween(v1, v2, limit = 1000) {
190
191
  const paths = [];
@@ -213,12 +214,10 @@ export class AbstractGraph extends IterableEntryBase {
213
214
  return paths;
214
215
  }
215
216
  /**
216
- * Time Complexity: O(L), where L is the length of the path.
217
- * Space Complexity: O(1) - Constant space.
218
- *
219
- * The function calculates the sum of weights along a given path.
220
- * @param {VO[]} path - An array of vertexMap (VO) representing a path in a graph.
221
- * @returns The function `getPathSumWeight` returns the sum of the weights of the edgeMap in the given path.
217
+ * Sum the weights along a vertex path.
218
+ * @param path - Sequence of vertices.
219
+ * @returns Path weight sum (0 if empty or edge missing).
220
+ * @remarks Time O(L), Space O(1) where L is path length
222
221
  */
223
222
  getPathSumWeight(path) {
224
223
  let sum = 0;
@@ -228,21 +227,12 @@ export class AbstractGraph extends IterableEntryBase {
228
227
  return sum;
229
228
  }
230
229
  /**
231
- * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
232
- * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
233
- *
234
- * The function `getMinCostBetween` calculates the minimum cost between two vertexMap in a graph, either based on edge
235
- * weights or using a breadth-first search algorithm.
236
- * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
237
- * @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
238
- * you want to find the minimum cost or weight from the source vertex `v1`.
239
- * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
240
- * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
241
- * the edgeMap. If isWeight is set to false or not provided, the function will calculate the
242
- * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertexMap (`v1`
243
- * and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
244
- * vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
245
- * minimum number of
230
+ * Minimum hops/weight between two vertices.
231
+ * @param v1 - Source vertex or key.
232
+ * @param v2 - Destination vertex or key.
233
+ * @param isWeight - If `true`, compare by path weight; otherwise by hop count.
234
+ * @returns Minimum cost or `undefined` if missing/unreachable.
235
+ * @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
246
236
  */
247
237
  getMinCostBetween(v1, v2, isWeight) {
248
238
  if (isWeight === undefined)
@@ -256,7 +246,6 @@ export class AbstractGraph extends IterableEntryBase {
256
246
  return min;
257
247
  }
258
248
  else {
259
- // BFS
260
249
  const vertex2 = this._getVertex(v2);
261
250
  const vertex1 = this._getVertex(v1);
262
251
  if (!(vertex1 && vertex2)) {
@@ -267,12 +256,11 @@ export class AbstractGraph extends IterableEntryBase {
267
256
  visited.set(vertex1, true);
268
257
  let cost = 0;
269
258
  while (queue.length > 0) {
270
- for (let i = 0; i < queue.length; i++) {
259
+ for (let i = 0, layerSize = queue.length; i < layerSize; i++) {
271
260
  const cur = queue.shift();
272
261
  if (cur === vertex2) {
273
262
  return cost;
274
263
  }
275
- // TODO consider optimizing to AbstractGraph
276
264
  if (cur !== undefined) {
277
265
  const neighbors = this.getNeighbors(cur);
278
266
  for (const neighbor of neighbors) {
@@ -289,23 +277,13 @@ export class AbstractGraph extends IterableEntryBase {
289
277
  }
290
278
  }
291
279
  /**
292
- * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
293
- * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
294
- *
295
- * The function `getMinPathBetween` returns the minimum path between two vertexMap in a graph, either based on weight or
296
- * using a breadth-first search algorithm.
297
- * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
298
- * object (`VO`) or a vertex ID (`VertexKey`).
299
- * @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
300
- * path.
301
- * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
302
- * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
303
- * to false, the function will use breadth-first search (BFS) to find the minimum path.
304
- * @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
305
- * followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
306
- * so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
307
- * @returns The function `getMinPathBetween` returns an array of vertexMap (`VO[]`) representing the minimum path between
308
- * two vertexMap (`v1` and `v2`). If there is no path between the vertexMap, it returns `undefined`.
280
+ * Minimum path (as vertex sequence) between two vertices.
281
+ * @param v1 - Source vertex or key.
282
+ * @param v2 - Destination vertex or key.
283
+ * @param isWeight - If `true`, compare by path weight; otherwise by hop count.
284
+ * @param isDFS - For weighted mode only: if `true`, brute-force all paths; if `false`, use Dijkstra.
285
+ * @returns Vertex sequence, or `undefined`/empty when unreachable depending on branch.
286
+ * @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
309
287
  */
310
288
  getMinPathBetween(v1, v2, isWeight, isDFS = false) {
311
289
  if (isWeight === undefined)
@@ -327,11 +305,19 @@ export class AbstractGraph extends IterableEntryBase {
327
305
  return allPaths[minIndex] || undefined;
328
306
  }
329
307
  else {
308
+ /**
309
+ * Dijkstra (binary-heap) shortest paths for non-negative weights.
310
+ * @param src - Source vertex or key.
311
+ * @param dest - Optional destination for early stop.
312
+ * @param getMinDist - If `true`, compute global minimum distance.
313
+ * @param genPaths - If `true`, also generate path arrays.
314
+ * @returns Result bag or `undefined` if source missing.
315
+ * @remarks Time O((V + E) log V), Space O(V + E)
316
+ */
330
317
  return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
331
318
  }
332
319
  }
333
320
  else {
334
- // DFS
335
321
  let minPath = [];
336
322
  const vertex1 = this._getVertex(v1);
337
323
  const vertex2 = this._getVertex(v2);
@@ -358,23 +344,13 @@ export class AbstractGraph extends IterableEntryBase {
358
344
  }
359
345
  }
360
346
  /**
361
- * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
362
- * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
363
- *
364
- * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertexMap in
365
- * a graph without using a heap data structure.
366
- * @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
367
- * vertex object or a vertex ID.
368
- * @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
369
- * parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
370
- * identifier. If no destination is provided, the value is set to `undefined`.
371
- * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
372
- * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
373
- * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
374
- * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
375
- * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
376
- * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
377
- * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
347
+ * Dijkstra without heap (array-based selection).
348
+ * @param src - Source vertex or key.
349
+ * @param dest - Optional destination for early stop.
350
+ * @param getMinDist - If `true`, compute global minimum distance.
351
+ * @param genPaths - If `true`, also generate path arrays.
352
+ * @returns Result bag or `undefined` if source missing.
353
+ * @remarks Time O(V^2 + E), Space O(V + E)
378
354
  */
379
355
  dijkstraWithoutHeap(src, dest = undefined, getMinDist = false, genPaths = false) {
380
356
  let minDist = Number.MAX_SAFE_INTEGER;
@@ -384,7 +360,7 @@ export class AbstractGraph extends IterableEntryBase {
384
360
  const vertexMap = this._vertexMap;
385
361
  const distMap = new Map();
386
362
  const seen = new Set();
387
- const preMap = new Map(); // predecessor
363
+ const preMap = new Map();
388
364
  const srcVertex = this._getVertex(src);
389
365
  const destVertex = dest ? this._getVertex(dest) : undefined;
390
366
  if (!srcVertex) {
@@ -447,7 +423,6 @@ export class AbstractGraph extends IterableEntryBase {
447
423
  if (edge) {
448
424
  const curFromMap = distMap.get(cur);
449
425
  const neighborFromMap = distMap.get(neighbor);
450
- // TODO after no-non-undefined-assertion not ensure the logic
451
426
  if (curFromMap !== undefined && neighborFromMap !== undefined) {
452
427
  if (edge.weight + curFromMap < neighborFromMap) {
453
428
  distMap.set(neighbor, edge.weight + curFromMap);
@@ -473,26 +448,6 @@ export class AbstractGraph extends IterableEntryBase {
473
448
  getPaths(minDest);
474
449
  return { distMap, preMap, seen, paths, minDist, minPath };
475
450
  }
476
- /**
477
- * Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
478
- * Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
479
- *
480
- * 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.
481
- * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
482
- * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
483
- * @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
484
- * start. It can be either a vertex object or a vertex ID.
485
- * @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
486
- * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
487
- * will calculate the shortest paths to all other vertexMap from the source vertex.
488
- * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
489
- * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
490
- * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
491
- * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
492
- * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
493
- * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
494
- * @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
495
- */
496
451
  dijkstra(src, dest = undefined, getMinDist = false, genPaths = false) {
497
452
  let minDist = Number.MAX_SAFE_INTEGER;
498
453
  let minDest = undefined;
@@ -501,7 +456,7 @@ export class AbstractGraph extends IterableEntryBase {
501
456
  const vertexMap = this._vertexMap;
502
457
  const distMap = new Map();
503
458
  const seen = new Set();
504
- const preMap = new Map(); // predecessor
459
+ const preMap = new Map();
505
460
  const srcVertex = this._getVertex(src);
506
461
  const destVertex = dest ? this._getVertex(dest) : undefined;
507
462
  if (!srcVertex)
@@ -515,11 +470,6 @@ export class AbstractGraph extends IterableEntryBase {
515
470
  heap.add({ key: 0, value: srcVertex });
516
471
  distMap.set(srcVertex, 0);
517
472
  preMap.set(srcVertex, undefined);
518
- /**
519
- * The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
520
- * @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
521
- * undefined.
522
- */
523
473
  const getPaths = (minV) => {
524
474
  for (const vertex of vertexMap) {
525
475
  const vertexOrKey = vertex[1];
@@ -559,7 +509,7 @@ export class AbstractGraph extends IterableEntryBase {
559
509
  const weight = this.getEdge(cur, neighbor)?.weight;
560
510
  if (typeof weight === 'number') {
561
511
  const distSrcToNeighbor = distMap.get(neighbor);
562
- if (distSrcToNeighbor) {
512
+ if (distSrcToNeighbor !== undefined) {
563
513
  if (dist + weight < distSrcToNeighbor) {
564
514
  heap.add({ key: dist + weight, value: neighbor });
565
515
  preMap.set(neighbor, cur);
@@ -589,22 +539,13 @@ export class AbstractGraph extends IterableEntryBase {
589
539
  return { distMap, preMap, seen, paths, minDist, minPath };
590
540
  }
591
541
  /**
592
- * Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
593
- * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
594
- *
595
- * one to rest pairs
596
- * 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 edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
597
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
598
- * all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
599
- * @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
600
- * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
601
- * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
602
- * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
603
- * calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
604
- * `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
605
- * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
606
- * vertex.
607
- * @returns The function `bellmanFord` returns an object with the following properties:
542
+ * Bellman-Ford single-source shortest paths with option to scan negative cycles.
543
+ * @param src - Source vertex or key.
544
+ * @param scanNegativeCycle - If `true`, also detect negative cycles.
545
+ * @param getMin - If `true`, compute global minimum distance.
546
+ * @param genPath - If `true`, generate path arrays via predecessor map.
547
+ * @returns Result bag including distances, predecessors, and optional cycle flag.
548
+ * @remarks Time O(V * E), Space O(V + E)
608
549
  */
609
550
  bellmanFord(src, scanNegativeCycle, getMin, genPath) {
610
551
  if (getMin === undefined)
@@ -614,10 +555,9 @@ export class AbstractGraph extends IterableEntryBase {
614
555
  const srcVertex = this._getVertex(src);
615
556
  const paths = [];
616
557
  const distMap = new Map();
617
- const preMap = new Map(); // predecessor
558
+ const preMap = new Map();
618
559
  let min = Number.MAX_SAFE_INTEGER;
619
560
  let minPath = [];
620
- // TODO
621
561
  let hasNegativeCycle;
622
562
  if (scanNegativeCycle)
623
563
  hasNegativeCycle = false;
@@ -693,38 +633,15 @@ export class AbstractGraph extends IterableEntryBase {
693
633
  return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
694
634
  }
695
635
  /**
696
- * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
697
- */
698
- /**
699
- * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
700
- * 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.
701
- */
702
- /**
703
- * BellmanFord time:O(VE) space:O(VO)
704
- * one to rest pairs
705
- * 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 edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
706
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
707
- */
708
- /**
709
- * Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
710
- * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
711
- *
712
- * Not support graph with negative weight cycle
713
- * all pairs
714
- * 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 edgeMap, and it can simultaneously compute shortest paths between any two nodes.
715
- * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
716
- * graph.
717
- * @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
718
- * property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
719
- * `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
720
- * path between vertexMap in the
636
+ * Floyd–Warshall all-pairs shortest paths.
637
+ * @returns `{ costs, predecessor }` matrices.
638
+ * @remarks Time O(V^3), Space O(V^2)
721
639
  */
722
640
  floydWarshall() {
723
641
  const idAndVertices = [...this._vertexMap];
724
642
  const n = idAndVertices.length;
725
643
  const costs = [];
726
644
  const predecessor = [];
727
- // successors
728
645
  for (let i = 0; i < n; i++) {
729
646
  costs[i] = [];
730
647
  predecessor[i] = [];
@@ -750,8 +667,10 @@ export class AbstractGraph extends IterableEntryBase {
750
667
  return { costs, predecessor };
751
668
  }
752
669
  /**
753
- * O(V+E+C)
754
- * O(V+C)
670
+ * Enumerate simple cycles (may be expensive).
671
+ * @param isInclude2Cycle - If `true`, include 2-cycles when graph semantics allow.
672
+ * @returns Array of cycles (each as array of vertex keys).
673
+ * @remarks Time exponential in worst-case, Space O(V + E)
755
674
  */
756
675
  getCycles(isInclude2Cycle = false) {
757
676
  const cycles = [];
@@ -776,7 +695,6 @@ export class AbstractGraph extends IterableEntryBase {
776
695
  for (const vertex of this.vertexMap.values()) {
777
696
  dfs(vertex, [], visited);
778
697
  }
779
- // Use a set to eliminate duplicate cycles
780
698
  const uniqueCycles = new Map();
781
699
  for (const cycle of cycles) {
782
700
  const sorted = [...cycle].sort().toString();
@@ -786,24 +704,22 @@ export class AbstractGraph extends IterableEntryBase {
786
704
  uniqueCycles.set(sorted, cycle);
787
705
  }
788
706
  }
789
- // Convert the unique cycles back to an array
707
+ /**
708
+ * Map entries to an array via callback.
709
+ * @param callback - `(key, value, index, self) => T`.
710
+ * @param thisArg - Optional `this` for callback.
711
+ * @returns Mapped results.
712
+ * @remarks Time O(V), Space O(V)
713
+ */
790
714
  return [...uniqueCycles].map(cycleString => cycleString[1]);
791
715
  }
792
716
  /**
793
- * Time Complexity: O(n)
794
- * Space Complexity: O(n)
795
- *
796
- * The `filter` function iterates over key-value pairs in a data structure and returns an array of
797
- * pairs that satisfy a given predicate.
798
- * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
799
- * `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
800
- * in the filtered array. The callback function should return `true` if the element should be
801
- * included, and `
802
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
803
- * specify the value of `this` within the `predicate` function. It is used when you want to bind a
804
- * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
805
- * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
806
- * that satisfy the given predicate function.
717
+ * Induced-subgraph filter: keep vertices where `predicate(key, value)` is true,
718
+ * and only keep edges whose endpoints both survive.
719
+ * @param predicate - `(key, value, index, self) => boolean`.
720
+ * @param thisArg - Optional `this` for callback.
721
+ * @returns A new graph of the same concrete class (`this` type).
722
+ * @remarks Time O(V + E), Space O(V + E)
807
723
  */
808
724
  filter(predicate, thisArg) {
809
725
  const filtered = [];
@@ -814,21 +730,23 @@ export class AbstractGraph extends IterableEntryBase {
814
730
  }
815
731
  index++;
816
732
  }
817
- return filtered;
733
+ return this._createLike(filtered, this._snapshotOptions());
818
734
  }
819
735
  /**
820
- * Time Complexity: O(n)
821
- * Space Complexity: O(n)
822
- *
823
- * The `map` function iterates over the elements of a collection and applies a callback function to
824
- * each element, returning an array of the results.
825
- * @param callback - The callback parameter is a function that will be called for each element in the
826
- * map. It takes four arguments:
827
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
828
- * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
829
- * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
830
- * @returns The `map` function is returning an array of type `T[]`.
736
+ * Preserve the old behavior: return filtered entries as an array.
737
+ * @remarks Time O(V), Space O(V)
831
738
  */
739
+ filterEntries(predicate, thisArg) {
740
+ const filtered = [];
741
+ let index = 0;
742
+ for (const [key, value] of this) {
743
+ if (predicate.call(thisArg, key, value, index, this)) {
744
+ filtered.push([key, value]);
745
+ }
746
+ index++;
747
+ }
748
+ return filtered;
749
+ }
832
750
  map(callback, thisArg) {
833
751
  const mapped = [];
834
752
  let index = 0;
@@ -838,23 +756,135 @@ export class AbstractGraph extends IterableEntryBase {
838
756
  }
839
757
  return mapped;
840
758
  }
759
+ /**
760
+ * Create a deep clone of the graph with the same species.
761
+ * @remarks Time O(V + E), Space O(V + E)
762
+ */
763
+ /**
764
+ * Create a deep clone of the graph with the same species.
765
+ * @returns A new graph of the same concrete class (`this` type).
766
+ * @remarks Time O(V + E), Space O(V + E)
767
+ */
768
+ clone() {
769
+ return this._createLike(undefined, this._snapshotOptions());
770
+ }
771
+ // ===== Same-species factory & cloning helpers =====
772
+ /**
773
+ * Internal iterator over `[key, value]` entries in insertion order.
774
+ * @returns Iterator of `[VertexKey, V | undefined]`.
775
+ * @remarks Time O(V), Space O(1)
776
+ */
841
777
  *_getIterator() {
842
778
  for (const vertex of this._vertexMap.values()) {
843
779
  yield [vertex.key, vertex.value];
844
780
  }
845
781
  }
782
+ /**
783
+ * Capture configuration needed to reproduce the current graph.
784
+ * Currently the graph has no runtime options, so we return an empty object.
785
+ */
786
+ /**
787
+ * Capture configuration needed to reproduce the current graph.
788
+ * @returns Options bag (opaque to callers).
789
+ * @remarks Time O(1), Space O(1)
790
+ */
791
+ _snapshotOptions() {
792
+ return { graph: { ...this._options } };
793
+ }
794
+ /**
795
+ * Create an empty graph instance of the same concrete species (Directed/Undirected/etc).
796
+ * @remarks Time O(1), Space O(1)
797
+ */
798
+ /**
799
+ * Create an empty graph instance of the same concrete species.
800
+ * @param _options - Snapshot options from `_snapshotOptions()`.
801
+ * @returns A new empty graph instance of `this` type.
802
+ * @remarks Time O(1), Space O(1)
803
+ */
804
+ _createInstance(_options) {
805
+ const Ctor = this.constructor;
806
+ const instance = new Ctor();
807
+ const graph = _options?.graph;
808
+ if (graph)
809
+ instance._options = { ...instance._options, ...graph };
810
+ else
811
+ instance._options = { ...instance._options, ...this._options };
812
+ return instance;
813
+ }
814
+ /**
815
+ * Create a same-species graph populated with the given entries.
816
+ * Also preserves edges between kept vertices from the source graph.
817
+ * @remarks Time O(V + E), Space O(V + E)
818
+ */
819
+ /**
820
+ * Create a same-species graph populated with entries; preserves edges among kept vertices.
821
+ * @param iter - Optional entries to seed the new graph.
822
+ * @param options - Snapshot options.
823
+ * @returns A new graph of `this` type.
824
+ * @remarks Time O(V + E), Space O(V + E)
825
+ */
826
+ _createLike(iter, options) {
827
+ const g = this._createInstance(options);
828
+ // 1) Add vertices
829
+ if (iter) {
830
+ for (const [k, v] of iter) {
831
+ g.addVertex(k, v);
832
+ }
833
+ }
834
+ else {
835
+ for (const [k, v] of this) {
836
+ g.addVertex(k, v);
837
+ }
838
+ }
839
+ // 2) Add edges whose endpoints exist in the new graph
840
+ const edges = this.edgeSet();
841
+ for (const e of edges) {
842
+ const ends = this.getEndsOfEdge(e);
843
+ if (!ends)
844
+ continue;
845
+ const [va, vb] = ends;
846
+ const ka = va.key;
847
+ const kb = vb.key;
848
+ const hasA = g.hasVertex ? g.hasVertex(ka) : false;
849
+ const hasB = g.hasVertex ? g.hasVertex(kb) : false;
850
+ if (hasA && hasB) {
851
+ const w = e.weight;
852
+ const val = e.value;
853
+ const newEdge = g.createEdge(ka, kb, w, val);
854
+ g._addEdge(newEdge);
855
+ }
856
+ }
857
+ return g;
858
+ }
859
+ /**
860
+ * Insert a pre-built vertex into the graph.
861
+ * @param newVertex - Concrete vertex instance.
862
+ * @returns `true` if inserted; `false` if key already exists.
863
+ * @remarks Time O(1) avg, Space O(1)
864
+ */
846
865
  _addVertex(newVertex) {
847
866
  if (this.hasVertex(newVertex)) {
848
867
  return false;
849
- // throw (new Error('Duplicated vertex key is not allowed'));
850
868
  }
851
869
  this._vertexMap.set(newVertex.key, newVertex);
852
870
  return true;
853
871
  }
872
+ /**
873
+ * Resolve a vertex key or instance to the concrete vertex instance.
874
+ * @param vertexOrKey - Vertex key or existing vertex.
875
+ * @returns Vertex instance or `undefined`.
876
+ * @remarks Time O(1), Space O(1)
877
+ */
854
878
  _getVertex(vertexOrKey) {
855
879
  const vertexKey = this._getVertexKey(vertexOrKey);
856
880
  return this._vertexMap.get(vertexKey) || undefined;
857
881
  }
882
+ /**
883
+ * Resolve a vertex key from a key or vertex instance.
884
+ * @param vertexOrKey - Vertex key or existing vertex.
885
+ * @returns The vertex key.
886
+ * @remarks Time O(1), Space O(1)
887
+ */
858
888
  _getVertexKey(vertexOrKey) {
859
889
  return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
860
890
  }