trie-typed 2.1.1 → 2.2.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 (288) hide show
  1. package/dist/cjs/index.cjs +797 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/cjs-legacy/index.cjs +795 -0
  4. package/dist/cjs-legacy/index.cjs.map +1 -0
  5. package/dist/esm/index.mjs +792 -0
  6. package/dist/esm/index.mjs.map +1 -0
  7. package/dist/esm-legacy/index.mjs +790 -0
  8. package/dist/esm-legacy/index.mjs.map +1 -0
  9. package/dist/types/data-structures/base/index.d.ts +2 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +236 -2
  11. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +197 -2
  12. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +345 -2
  13. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
  14. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
  15. package/dist/types/data-structures/binary-tree/bst.d.ts +467 -12
  16. package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +262 -3
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
  19. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +244 -2
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +332 -2
  21. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
  22. package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
  23. package/dist/types/data-structures/graph/index.d.ts +2 -1
  24. package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
  25. package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
  26. package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
  27. package/dist/types/data-structures/hash/index.d.ts +0 -1
  28. package/dist/types/data-structures/heap/heap.d.ts +503 -5
  29. package/dist/types/data-structures/heap/index.d.ts +2 -0
  30. package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
  31. package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
  32. package/dist/types/data-structures/index.d.ts +7 -7
  33. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
  34. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
  35. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
  36. package/dist/types/data-structures/matrix/index.d.ts +1 -1
  37. package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
  38. package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
  39. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
  40. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
  41. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
  42. package/dist/types/data-structures/queue/deque.d.ts +431 -4
  43. package/dist/types/data-structures/queue/queue.d.ts +308 -4
  44. package/dist/types/data-structures/stack/stack.d.ts +306 -2
  45. package/dist/types/data-structures/tree/tree.d.ts +62 -1
  46. package/dist/types/data-structures/trie/trie.d.ts +350 -4
  47. package/dist/types/index.d.ts +11 -2
  48. package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
  49. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  50. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  51. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  52. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  53. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  54. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  55. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  56. package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
  57. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  58. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  59. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  60. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  61. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  62. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  63. package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
  64. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  65. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  66. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  67. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  68. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  69. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  70. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  71. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  72. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  73. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  74. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  75. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  76. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  77. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  78. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  79. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  80. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  81. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  82. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  83. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  84. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  85. package/dist/types/types/index.d.ts +3 -0
  86. package/dist/types/types/utils/index.d.ts +2 -0
  87. package/dist/types/types/utils/utils.d.ts +22 -0
  88. package/dist/types/utils/index.d.ts +1 -1
  89. package/dist/types/utils/utils.d.ts +209 -22
  90. package/dist/umd/trie-typed.js +802 -0
  91. package/dist/umd/trie-typed.js.map +1 -0
  92. package/dist/umd/trie-typed.min.js +9 -0
  93. package/dist/umd/trie-typed.min.js.map +1 -0
  94. package/package.json +43 -5
  95. package/src/data-structures/binary-tree/avl-tree-counter.ts +106 -15
  96. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
  97. package/src/data-structures/binary-tree/avl-tree.ts +107 -16
  98. package/src/data-structures/binary-tree/binary-tree.ts +4 -4
  99. package/src/data-structures/binary-tree/bst.ts +103 -12
  100. package/src/data-structures/binary-tree/red-black-tree.ts +110 -20
  101. package/src/data-structures/binary-tree/tree-counter.ts +105 -14
  102. package/src/data-structures/binary-tree/tree-multi-map.ts +123 -12
  103. package/src/data-structures/graph/abstract-graph.ts +5 -5
  104. package/src/data-structures/graph/directed-graph.ts +5 -5
  105. package/src/data-structures/graph/undirected-graph.ts +5 -5
  106. package/src/data-structures/heap/heap.ts +5 -5
  107. package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
  108. package/src/interfaces/binary-tree.ts +1 -1
  109. package/tsconfig.base.json +23 -0
  110. package/tsconfig.json +8 -34
  111. package/tsconfig.test.json +8 -0
  112. package/tsconfig.types.json +15 -0
  113. package/tsup.config.js +28 -0
  114. package/tsup.node.config.js +71 -0
  115. package/dist/common/index.js +0 -28
  116. package/dist/constants/index.js +0 -8
  117. package/dist/data-structures/base/index.d.ts +0 -2
  118. package/dist/data-structures/base/index.js +0 -18
  119. package/dist/data-structures/base/iterable-element-base.js +0 -243
  120. package/dist/data-structures/base/iterable-entry-base.js +0 -183
  121. package/dist/data-structures/base/linear-base.js +0 -415
  122. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
  123. package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
  124. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
  125. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
  126. package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
  127. package/dist/data-structures/binary-tree/avl-tree.js +0 -611
  128. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
  129. package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
  130. package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
  131. package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
  132. package/dist/data-structures/binary-tree/bst.d.ts +0 -413
  133. package/dist/data-structures/binary-tree/bst.js +0 -903
  134. package/dist/data-structures/binary-tree/index.js +0 -26
  135. package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
  136. package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
  137. package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
  138. package/dist/data-structures/binary-tree/segment-tree.js +0 -297
  139. package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
  140. package/dist/data-structures/binary-tree/tree-counter.js +0 -413
  141. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
  142. package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
  143. package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
  144. package/dist/data-structures/graph/abstract-graph.js +0 -896
  145. package/dist/data-structures/graph/directed-graph.d.ts +0 -207
  146. package/dist/data-structures/graph/directed-graph.js +0 -525
  147. package/dist/data-structures/graph/index.js +0 -20
  148. package/dist/data-structures/graph/map-graph.d.ts +0 -78
  149. package/dist/data-structures/graph/map-graph.js +0 -107
  150. package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
  151. package/dist/data-structures/graph/undirected-graph.js +0 -424
  152. package/dist/data-structures/hash/hash-map.d.ts +0 -345
  153. package/dist/data-structures/hash/hash-map.js +0 -692
  154. package/dist/data-structures/hash/index.d.ts +0 -1
  155. package/dist/data-structures/hash/index.js +0 -17
  156. package/dist/data-structures/heap/heap.d.ts +0 -503
  157. package/dist/data-structures/heap/heap.js +0 -901
  158. package/dist/data-structures/heap/index.d.ts +0 -3
  159. package/dist/data-structures/heap/index.js +0 -19
  160. package/dist/data-structures/heap/max-heap.d.ts +0 -32
  161. package/dist/data-structures/heap/max-heap.js +0 -40
  162. package/dist/data-structures/heap/min-heap.d.ts +0 -33
  163. package/dist/data-structures/heap/min-heap.js +0 -31
  164. package/dist/data-structures/index.js +0 -28
  165. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
  166. package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
  167. package/dist/data-structures/linked-list/index.js +0 -19
  168. package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
  169. package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
  170. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
  171. package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
  172. package/dist/data-structures/matrix/index.js +0 -18
  173. package/dist/data-structures/matrix/matrix.d.ts +0 -168
  174. package/dist/data-structures/matrix/matrix.js +0 -448
  175. package/dist/data-structures/matrix/navigator.d.ts +0 -55
  176. package/dist/data-structures/matrix/navigator.js +0 -111
  177. package/dist/data-structures/priority-queue/index.js +0 -19
  178. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
  179. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
  180. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
  181. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
  182. package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
  183. package/dist/data-structures/priority-queue/priority-queue.js +0 -20
  184. package/dist/data-structures/queue/deque.d.ts +0 -431
  185. package/dist/data-structures/queue/deque.js +0 -879
  186. package/dist/data-structures/queue/index.js +0 -18
  187. package/dist/data-structures/queue/queue.d.ts +0 -308
  188. package/dist/data-structures/queue/queue.js +0 -473
  189. package/dist/data-structures/stack/index.js +0 -17
  190. package/dist/data-structures/stack/stack.d.ts +0 -306
  191. package/dist/data-structures/stack/stack.js +0 -401
  192. package/dist/data-structures/tree/index.js +0 -17
  193. package/dist/data-structures/tree/tree.d.ts +0 -62
  194. package/dist/data-structures/tree/tree.js +0 -107
  195. package/dist/data-structures/trie/index.js +0 -17
  196. package/dist/data-structures/trie/trie.d.ts +0 -350
  197. package/dist/data-structures/trie/trie.js +0 -610
  198. package/dist/index.d.ts +0 -12
  199. package/dist/index.js +0 -28
  200. package/dist/interfaces/binary-tree.js +0 -2
  201. package/dist/interfaces/doubly-linked-list.js +0 -2
  202. package/dist/interfaces/graph.js +0 -2
  203. package/dist/interfaces/heap.js +0 -2
  204. package/dist/interfaces/index.js +0 -24
  205. package/dist/interfaces/navigator.js +0 -2
  206. package/dist/interfaces/priority-queue.js +0 -2
  207. package/dist/interfaces/segment-tree.js +0 -2
  208. package/dist/interfaces/singly-linked-list.js +0 -2
  209. package/dist/types/common.js +0 -2
  210. package/dist/types/data-structures/base/base.js +0 -2
  211. package/dist/types/data-structures/base/index.js +0 -17
  212. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
  213. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
  214. package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
  215. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
  216. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
  217. package/dist/types/data-structures/binary-tree/bst.js +0 -2
  218. package/dist/types/data-structures/binary-tree/index.js +0 -25
  219. package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
  220. package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
  221. package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
  222. package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
  223. package/dist/types/data-structures/graph/abstract-graph.js +0 -2
  224. package/dist/types/data-structures/graph/directed-graph.js +0 -2
  225. package/dist/types/data-structures/graph/index.js +0 -19
  226. package/dist/types/data-structures/graph/map-graph.js +0 -2
  227. package/dist/types/data-structures/graph/undirected-graph.js +0 -2
  228. package/dist/types/data-structures/hash/hash-map.js +0 -2
  229. package/dist/types/data-structures/hash/index.js +0 -17
  230. package/dist/types/data-structures/heap/heap.js +0 -2
  231. package/dist/types/data-structures/heap/index.js +0 -17
  232. package/dist/types/data-structures/heap/max-heap.js +0 -2
  233. package/dist/types/data-structures/heap/min-heap.js +0 -2
  234. package/dist/types/data-structures/index.js +0 -28
  235. package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
  236. package/dist/types/data-structures/linked-list/index.js +0 -19
  237. package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
  238. package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
  239. package/dist/types/data-structures/matrix/index.js +0 -18
  240. package/dist/types/data-structures/matrix/matrix.js +0 -2
  241. package/dist/types/data-structures/matrix/navigator.js +0 -2
  242. package/dist/types/data-structures/priority-queue/index.js +0 -19
  243. package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
  244. package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
  245. package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
  246. package/dist/types/data-structures/queue/deque.js +0 -2
  247. package/dist/types/data-structures/queue/index.js +0 -18
  248. package/dist/types/data-structures/queue/queue.js +0 -2
  249. package/dist/types/data-structures/stack/index.js +0 -17
  250. package/dist/types/data-structures/stack/stack.js +0 -2
  251. package/dist/types/data-structures/tree/index.js +0 -17
  252. package/dist/types/data-structures/tree/tree.js +0 -2
  253. package/dist/types/data-structures/trie/index.js +0 -17
  254. package/dist/types/data-structures/trie/trie.js +0 -2
  255. package/dist/types/index.js +0 -19
  256. package/dist/types/utils/index.js +0 -18
  257. package/dist/types/utils/utils.js +0 -2
  258. package/dist/types/utils/validate-type.js +0 -2
  259. package/dist/utils/index.d.ts +0 -2
  260. package/dist/utils/index.js +0 -18
  261. package/dist/utils/number.js +0 -24
  262. package/dist/utils/utils.d.ts +0 -209
  263. package/dist/utils/utils.js +0 -353
  264. package/dist/{common → types/common}/index.d.ts +0 -0
  265. package/dist/{constants → types/constants}/index.d.ts +0 -0
  266. package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
  267. package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
  268. package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
  269. package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
  270. package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
  271. package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
  272. package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
  273. package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
  274. package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
  275. package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
  276. package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
  277. package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
  278. package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
  279. package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
  280. package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
  281. package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
  282. /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
  283. /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
  284. /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
  285. /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
  286. /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
  287. /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
  288. /package/dist/{utils → types/utils}/number.d.ts +0 -0
@@ -1,424 +0,0 @@
1
- "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Pablo Zeng
6
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.UndirectedGraph = exports.UndirectedEdge = exports.UndirectedVertex = void 0;
11
- const abstract_graph_1 = require("./abstract-graph");
12
- const utils_1 = require("../../utils");
13
- class UndirectedVertex extends abstract_graph_1.AbstractVertex {
14
- constructor(key, value) {
15
- super(key, value);
16
- }
17
- }
18
- exports.UndirectedVertex = UndirectedVertex;
19
- class UndirectedEdge extends abstract_graph_1.AbstractEdge {
20
- constructor(v1, v2, weight, value) {
21
- super(weight, value);
22
- this.endpoints = [v1, v2];
23
- }
24
- }
25
- exports.UndirectedEdge = UndirectedEdge;
26
- /**
27
- * Undirected graph implementation.
28
- * @template V - Vertex value type.
29
- * @template E - Edge value type.
30
- * @template VO - Concrete vertex class (extends AbstractVertex<V>).
31
- * @template EO - Concrete edge class (extends AbstractEdge<E>).
32
- * @remarks Time O(1), Space O(1)
33
- * @example examples will be generated by unit test
34
- */
35
- class UndirectedGraph extends abstract_graph_1.AbstractGraph {
36
- /**
37
- * Construct an undirected graph with runtime defaults.
38
- * @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
39
- * @remarks Time O(1), Space O(1)
40
- */
41
- constructor(options) {
42
- super(options);
43
- this._edgeMap = new Map();
44
- }
45
- get edgeMap() {
46
- return this._edgeMap;
47
- }
48
- set edgeMap(v) {
49
- this._edgeMap = v;
50
- }
51
- /**
52
- * Construct an undirected graph from keys with value initializer `v => v`.
53
- * @template K - Vertex key type.
54
- * @param keys - Iterable of vertex keys.
55
- * @returns UndirectedGraph with all keys added.
56
- * @remarks Time O(V), Space O(V)
57
- */
58
- static fromKeys(keys) {
59
- const g = new UndirectedGraph({
60
- vertexValueInitializer: (k) => k
61
- });
62
- for (const k of keys)
63
- g.addVertex(k);
64
- return g;
65
- }
66
- /**
67
- * Construct an undirected graph from `[key, value]` entries.
68
- * @template V - Vertex value type.
69
- * @param entries - Iterable of `[key, value]` pairs.
70
- * @returns UndirectedGraph with all vertices added.
71
- * @remarks Time O(V), Space O(V)
72
- */
73
- static fromEntries(entries) {
74
- const g = new UndirectedGraph();
75
- for (const [k, v] of entries)
76
- g.addVertex(k, v);
77
- return g;
78
- }
79
- /**
80
- * Create an undirected vertex instance. Does not insert into the graph.
81
- * @param key - Vertex identifier.
82
- * @param value - Optional payload.
83
- * @returns Concrete vertex instance.
84
- * @remarks Time O(1), Space O(1)
85
- */
86
- createVertex(key, value) {
87
- return new UndirectedVertex(key, value);
88
- }
89
- /**
90
- * Create an undirected edge instance. Does not insert into the graph.
91
- * @param v1 - One endpoint key.
92
- * @param v2 - The other endpoint key.
93
- * @param weight - Edge weight; defaults to `defaultEdgeWeight`.
94
- * @param value - Edge payload.
95
- * @returns Concrete edge instance.
96
- * @remarks Time O(1), Space O(1)
97
- */
98
- createEdge(v1, v2, weight, value) {
99
- var _a;
100
- return new UndirectedEdge(v1, v2, (_a = weight !== null && weight !== void 0 ? weight : this.options.defaultEdgeWeight) !== null && _a !== void 0 ? _a : 1, value);
101
- }
102
- /**
103
- * Get an undirected edge between two vertices, if present.
104
- * @param v1 - One vertex or key.
105
- * @param v2 - The other vertex or key.
106
- * @returns Edge instance or `undefined`.
107
- * @remarks Time O(1) avg, Space O(1)
108
- */
109
- getEdge(v1, v2) {
110
- var _a;
111
- let edgeMap = [];
112
- if (v1 !== undefined && v2 !== undefined) {
113
- const vertex1 = this._getVertex(v1);
114
- const vertex2 = this._getVertex(v2);
115
- if (vertex1 && vertex2) {
116
- edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.endpoints.includes(vertex2.key));
117
- }
118
- }
119
- return edgeMap ? edgeMap[0] || undefined : undefined;
120
- }
121
- /**
122
- * Delete a single undirected edge between two vertices.
123
- * @param v1 - One vertex or key.
124
- * @param v2 - The other vertex or key.
125
- * @returns Removed edge or `undefined`.
126
- * @remarks Time O(1) avg, Space O(1)
127
- */
128
- deleteEdgeBetween(v1, v2) {
129
- const vertex1 = this._getVertex(v1);
130
- const vertex2 = this._getVertex(v2);
131
- if (!vertex1 || !vertex2) {
132
- return undefined;
133
- }
134
- const v1Edges = this._edgeMap.get(vertex1);
135
- let removed = undefined;
136
- if (v1Edges) {
137
- removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.endpoints.includes(vertex2.key))[0] || undefined;
138
- }
139
- const v2Edges = this._edgeMap.get(vertex2);
140
- if (v2Edges) {
141
- (0, utils_1.arrayRemove)(v2Edges, (e) => e.endpoints.includes(vertex1.key));
142
- }
143
- return removed;
144
- }
145
- /**
146
- * Delete an edge by instance or by a pair of keys.
147
- * @param edgeOrOneSideVertexKey - Edge instance or one endpoint vertex/key.
148
- * @param otherSideVertexKey - Required second endpoint when deleting by pair.
149
- * @returns Removed edge or `undefined`.
150
- * @remarks Time O(1) avg, Space O(1)
151
- */
152
- deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
153
- let oneSide, otherSide;
154
- if (this.isVertexKey(edgeOrOneSideVertexKey)) {
155
- if (this.isVertexKey(otherSideVertexKey)) {
156
- oneSide = this._getVertex(edgeOrOneSideVertexKey);
157
- otherSide = this._getVertex(otherSideVertexKey);
158
- }
159
- else {
160
- return;
161
- }
162
- }
163
- else {
164
- oneSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[0]);
165
- otherSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[1]);
166
- }
167
- if (oneSide && otherSide) {
168
- return this.deleteEdgeBetween(oneSide, otherSide);
169
- }
170
- else {
171
- return;
172
- }
173
- }
174
- /**
175
- * Delete a vertex and remove it from all neighbor lists.
176
- * @param vertexOrKey - Vertex or key.
177
- * @returns `true` if removed; otherwise `false`.
178
- * @remarks Time O(deg), Space O(1)
179
- */
180
- deleteVertex(vertexOrKey) {
181
- let vertexKey;
182
- let vertex;
183
- if (this.isVertexKey(vertexOrKey)) {
184
- vertex = this.getVertex(vertexOrKey);
185
- vertexKey = vertexOrKey;
186
- }
187
- else {
188
- vertex = vertexOrKey;
189
- vertexKey = this._getVertexKey(vertexOrKey);
190
- }
191
- /**
192
- * All neighbors connected via undirected edges.
193
- * @param vertexOrKey - Vertex or key.
194
- * @returns Array of neighbor vertices.
195
- * @remarks Time O(deg), Space O(deg)
196
- */
197
- const neighbors = this.getNeighbors(vertexOrKey);
198
- if (vertex) {
199
- neighbors.forEach(neighbor => {
200
- const neighborEdges = this._edgeMap.get(neighbor);
201
- if (neighborEdges) {
202
- const restEdges = neighborEdges.filter(edge => {
203
- return !edge.endpoints.includes(vertexKey);
204
- });
205
- this._edgeMap.set(neighbor, restEdges);
206
- }
207
- });
208
- this._edgeMap.delete(vertex);
209
- }
210
- return this._vertexMap.delete(vertexKey);
211
- }
212
- /**
213
- * Degree of a vertex (# of incident undirected edges).
214
- * @param vertexOrKey - Vertex or key.
215
- * @returns Non-negative integer.
216
- * @remarks Time O(1) avg, Space O(1)
217
- */
218
- degreeOf(vertexOrKey) {
219
- var _a;
220
- const vertex = this._getVertex(vertexOrKey);
221
- if (vertex) {
222
- return ((_a = this._edgeMap.get(vertex)) === null || _a === void 0 ? void 0 : _a.length) || 0;
223
- }
224
- else {
225
- return 0;
226
- }
227
- }
228
- /**
229
- * Incident undirected edges of a vertex.
230
- * @param vertexOrKey - Vertex or key.
231
- * @returns Array of incident edges.
232
- * @remarks Time O(deg), Space O(deg)
233
- */
234
- edgesOf(vertexOrKey) {
235
- const vertex = this._getVertex(vertexOrKey);
236
- if (vertex) {
237
- return this._edgeMap.get(vertex) || [];
238
- }
239
- else {
240
- return [];
241
- }
242
- }
243
- /**
244
- * Unique set of undirected edges across endpoints.
245
- * @returns Array of edges.
246
- * @remarks Time O(E), Space O(E)
247
- */
248
- edgeSet() {
249
- const edgeSet = new Set();
250
- this._edgeMap.forEach(edgeMap => {
251
- edgeMap.forEach(edge => {
252
- edgeSet.add(edge);
253
- });
254
- });
255
- return [...edgeSet];
256
- }
257
- getNeighbors(vertexOrKey) {
258
- const neighbors = [];
259
- const vertex = this._getVertex(vertexOrKey);
260
- if (vertex) {
261
- const neighborEdges = this.edgesOf(vertex);
262
- for (const edge of neighborEdges) {
263
- const neighbor = this._getVertex(edge.endpoints.filter(e => e !== vertex.key)[0]);
264
- if (neighbor) {
265
- neighbors.push(neighbor);
266
- }
267
- }
268
- }
269
- return neighbors;
270
- }
271
- /**
272
- * Resolve an edge's two endpoints to vertex instances.
273
- * @param edge - Edge instance.
274
- * @returns `[v1, v2]` or `undefined` if either endpoint is missing.
275
- * @remarks Time O(1), Space O(1)
276
- */
277
- getEndsOfEdge(edge) {
278
- if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
279
- return undefined;
280
- }
281
- const v1 = this._getVertex(edge.endpoints[0]);
282
- const v2 = this._getVertex(edge.endpoints[1]);
283
- if (v1 && v2) {
284
- return [v1, v2];
285
- }
286
- else {
287
- return undefined;
288
- }
289
- }
290
- /**
291
- * Whether the graph has no vertices and no edges.
292
- * @remarks Time O(1), Space O(1)
293
- */
294
- isEmpty() {
295
- return this.vertexMap.size === 0 && this.edgeMap.size === 0;
296
- }
297
- /**
298
- * Remove all vertices and edges.
299
- * @remarks Time O(V + E), Space O(1)
300
- */
301
- clear() {
302
- this._vertexMap = new Map();
303
- this._edgeMap = new Map();
304
- }
305
- /**
306
- * Deep clone as the same concrete class.
307
- * @returns A new graph of the same concrete class (`this` type).
308
- * @remarks Time O(V + E), Space O(V + E)
309
- */
310
- clone() {
311
- return super.clone();
312
- }
313
- /**
314
- * Tarjan-based bridge and articulation point detection.
315
- * @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
316
- * @remarks Time O(V + E), Space O(V + E)
317
- */
318
- tarjan() {
319
- const dfnMap = new Map();
320
- const lowMap = new Map();
321
- const bridges = [];
322
- const cutVertices = [];
323
- let time = 0;
324
- const dfs = (vertex, parent) => {
325
- dfnMap.set(vertex, time);
326
- lowMap.set(vertex, time);
327
- time++;
328
- const neighbors = this.getNeighbors(vertex);
329
- let childCount = 0;
330
- for (const neighbor of neighbors) {
331
- if (!dfnMap.has(neighbor)) {
332
- childCount++;
333
- dfs(neighbor, vertex);
334
- lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
335
- if (lowMap.get(neighbor) > dfnMap.get(vertex)) {
336
- // Found a bridge
337
- const edge = this.getEdge(vertex, neighbor);
338
- if (edge) {
339
- bridges.push(edge);
340
- }
341
- }
342
- if (parent !== undefined && lowMap.get(neighbor) >= dfnMap.get(vertex)) {
343
- // Found an articulation point
344
- cutVertices.push(vertex);
345
- }
346
- }
347
- else if (neighbor !== parent) {
348
- lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
349
- }
350
- }
351
- if (parent === undefined && childCount > 1) {
352
- // Special case for root in DFS tree
353
- cutVertices.push(vertex);
354
- }
355
- };
356
- for (const vertex of this.vertexMap.values()) {
357
- if (!dfnMap.has(vertex)) {
358
- dfs(vertex, undefined);
359
- }
360
- }
361
- return {
362
- dfnMap,
363
- lowMap,
364
- bridges,
365
- cutVertices
366
- };
367
- }
368
- /**
369
- * Get bridges discovered by `tarjan()`.
370
- * @returns Array of edges that are bridges.
371
- * @remarks Time O(B), Space O(1)
372
- */
373
- getBridges() {
374
- return this.tarjan().bridges;
375
- }
376
- /**
377
- * Get articulation points discovered by `tarjan()`.
378
- * @returns Array of cut vertices.
379
- * @remarks Time O(C), Space O(1)
380
- */
381
- getCutVertices() {
382
- return this.tarjan().cutVertices;
383
- }
384
- /**
385
- * DFN index map computed by `tarjan()`.
386
- * @returns Map from vertex to DFN index.
387
- * @remarks Time O(V), Space O(V)
388
- */
389
- getDFNMap() {
390
- return this.tarjan().dfnMap;
391
- }
392
- /**
393
- * LOW link map computed by `tarjan()`.
394
- * @returns Map from vertex to LOW value.
395
- * @remarks Time O(V), Space O(V)
396
- */
397
- getLowMap() {
398
- return this.tarjan().lowMap;
399
- }
400
- /**
401
- * Internal hook to attach an undirected edge into adjacency maps.
402
- * @param edge - Edge instance.
403
- * @returns `true` if both endpoints exist; otherwise `false`.
404
- * @remarks Time O(1) avg, Space O(1)
405
- */
406
- _addEdge(edge) {
407
- for (const end of edge.endpoints) {
408
- const endVertex = this._getVertex(end);
409
- if (endVertex === undefined)
410
- return false;
411
- if (endVertex) {
412
- const edgeMap = this._edgeMap.get(endVertex);
413
- if (edgeMap) {
414
- edgeMap.push(edge);
415
- }
416
- else {
417
- this._edgeMap.set(endVertex, [edge]);
418
- }
419
- }
420
- }
421
- return true;
422
- }
423
- }
424
- exports.UndirectedGraph = UndirectedGraph;