@graphty/algorithms 1.2.0 → 1.4.2

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 (337) hide show
  1. package/README.md +375 -276
  2. package/dist/algorithms.d.ts +42 -2
  3. package/dist/algorithms.js +11402 -2
  4. package/dist/algorithms.js.map +1 -1
  5. package/dist/src/algorithms/centrality/betweenness.d.ts +14 -0
  6. package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -1
  7. package/dist/src/algorithms/centrality/betweenness.js +144 -169
  8. package/dist/src/algorithms/centrality/betweenness.js.map +1 -1
  9. package/dist/src/algorithms/centrality/closeness.d.ts +35 -1
  10. package/dist/src/algorithms/centrality/closeness.d.ts.map +1 -1
  11. package/dist/src/algorithms/centrality/closeness.js +70 -149
  12. package/dist/src/algorithms/centrality/closeness.js.map +1 -1
  13. package/dist/src/algorithms/centrality/degree.d.ts +7 -0
  14. package/dist/src/algorithms/centrality/degree.d.ts.map +1 -1
  15. package/dist/src/algorithms/centrality/degree.js +12 -9
  16. package/dist/src/algorithms/centrality/degree.js.map +1 -1
  17. package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts +57 -0
  18. package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts.map +1 -0
  19. package/dist/src/algorithms/centrality/delta-pagerank-simple.js +304 -0
  20. package/dist/src/algorithms/centrality/delta-pagerank-simple.js.map +1 -0
  21. package/dist/src/algorithms/centrality/delta-pagerank.d.ts +106 -0
  22. package/dist/src/algorithms/centrality/delta-pagerank.d.ts.map +1 -0
  23. package/dist/src/algorithms/centrality/delta-pagerank.js +320 -0
  24. package/dist/src/algorithms/centrality/delta-pagerank.js.map +1 -0
  25. package/dist/src/algorithms/centrality/eigenvector.d.ts +10 -3
  26. package/dist/src/algorithms/centrality/eigenvector.d.ts.map +1 -1
  27. package/dist/src/algorithms/centrality/eigenvector.js +11 -4
  28. package/dist/src/algorithms/centrality/eigenvector.js.map +1 -1
  29. package/dist/src/algorithms/centrality/hits.d.ts +10 -3
  30. package/dist/src/algorithms/centrality/hits.d.ts.map +1 -1
  31. package/dist/src/algorithms/centrality/hits.js +11 -4
  32. package/dist/src/algorithms/centrality/hits.js.map +1 -1
  33. package/dist/src/algorithms/centrality/index.d.ts +3 -1
  34. package/dist/src/algorithms/centrality/index.d.ts.map +1 -1
  35. package/dist/src/algorithms/centrality/index.js +2 -1
  36. package/dist/src/algorithms/centrality/index.js.map +1 -1
  37. package/dist/src/algorithms/centrality/katz.d.ts +10 -3
  38. package/dist/src/algorithms/centrality/katz.d.ts.map +1 -1
  39. package/dist/src/algorithms/centrality/katz.js +13 -8
  40. package/dist/src/algorithms/centrality/katz.js.map +1 -1
  41. package/dist/src/algorithms/centrality/pagerank.d.ts +31 -0
  42. package/dist/src/algorithms/centrality/pagerank.d.ts.map +1 -1
  43. package/dist/src/algorithms/centrality/pagerank.js +61 -1
  44. package/dist/src/algorithms/centrality/pagerank.js.map +1 -1
  45. package/dist/src/algorithms/community/girvan-newman.d.ts +2 -3
  46. package/dist/src/algorithms/community/girvan-newman.d.ts.map +1 -1
  47. package/dist/src/algorithms/community/girvan-newman.js +67 -57
  48. package/dist/src/algorithms/community/girvan-newman.js.map +1 -1
  49. package/dist/src/algorithms/community/index.d.ts +2 -0
  50. package/dist/src/algorithms/community/index.d.ts.map +1 -1
  51. package/dist/src/algorithms/community/index.js.map +1 -1
  52. package/dist/src/algorithms/community/label-propagation.d.ts +11 -4
  53. package/dist/src/algorithms/community/label-propagation.d.ts.map +1 -1
  54. package/dist/src/algorithms/community/label-propagation.js +60 -38
  55. package/dist/src/algorithms/community/label-propagation.js.map +1 -1
  56. package/dist/src/algorithms/community/leiden.d.ts +3 -3
  57. package/dist/src/algorithms/community/leiden.d.ts.map +1 -1
  58. package/dist/src/algorithms/community/leiden.js +70 -44
  59. package/dist/src/algorithms/community/leiden.js.map +1 -1
  60. package/dist/src/algorithms/community/louvain-optimized.d.ts +149 -0
  61. package/dist/src/algorithms/community/louvain-optimized.d.ts.map +1 -0
  62. package/dist/src/algorithms/community/louvain-optimized.js +338 -0
  63. package/dist/src/algorithms/community/louvain-optimized.js.map +1 -0
  64. package/dist/src/algorithms/community/louvain.d.ts +2 -3
  65. package/dist/src/algorithms/community/louvain.d.ts.map +1 -1
  66. package/dist/src/algorithms/community/louvain.js +35 -81
  67. package/dist/src/algorithms/community/louvain.js.map +1 -1
  68. package/dist/src/algorithms/community/modularity-utils.d.ts +55 -0
  69. package/dist/src/algorithms/community/modularity-utils.d.ts.map +1 -0
  70. package/dist/src/algorithms/community/modularity-utils.js +108 -0
  71. package/dist/src/algorithms/community/modularity-utils.js.map +1 -0
  72. package/dist/src/algorithms/components/connected.d.ts +23 -0
  73. package/dist/src/algorithms/components/connected.d.ts.map +1 -1
  74. package/dist/src/algorithms/components/connected.js +29 -3
  75. package/dist/src/algorithms/components/connected.js.map +1 -1
  76. package/dist/src/algorithms/matching/bipartite.d.ts +10 -0
  77. package/dist/src/algorithms/matching/bipartite.d.ts.map +1 -1
  78. package/dist/src/algorithms/matching/bipartite.js +18 -40
  79. package/dist/src/algorithms/matching/bipartite.js.map +1 -1
  80. package/dist/src/algorithms/matching/index.d.ts.map +1 -1
  81. package/dist/src/algorithms/matching/index.js.map +1 -1
  82. package/dist/src/algorithms/matching/isomorphism.d.ts +8 -0
  83. package/dist/src/algorithms/matching/isomorphism.d.ts.map +1 -1
  84. package/dist/src/algorithms/matching/isomorphism.js +36 -2
  85. package/dist/src/algorithms/matching/isomorphism.js.map +1 -1
  86. package/dist/src/algorithms/mst/index.d.ts.map +1 -1
  87. package/dist/src/algorithms/mst/index.js.map +1 -1
  88. package/dist/src/algorithms/mst/kruskal.d.ts +14 -0
  89. package/dist/src/algorithms/mst/kruskal.d.ts.map +1 -1
  90. package/dist/src/algorithms/mst/kruskal.js +17 -3
  91. package/dist/src/algorithms/mst/kruskal.js.map +1 -1
  92. package/dist/src/algorithms/mst/prim.d.ts +9 -0
  93. package/dist/src/algorithms/mst/prim.d.ts.map +1 -1
  94. package/dist/src/algorithms/mst/prim.js +9 -0
  95. package/dist/src/algorithms/mst/prim.js.map +1 -1
  96. package/dist/src/algorithms/shortest-path/bellman-ford.d.ts +10 -0
  97. package/dist/src/algorithms/shortest-path/bellman-ford.d.ts.map +1 -1
  98. package/dist/src/algorithms/shortest-path/bellman-ford.js +11 -12
  99. package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -1
  100. package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts +44 -0
  101. package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts.map +1 -0
  102. package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js +195 -0
  103. package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js.map +1 -0
  104. package/dist/src/algorithms/shortest-path/dijkstra.d.ts +20 -2
  105. package/dist/src/algorithms/shortest-path/dijkstra.d.ts.map +1 -1
  106. package/dist/src/algorithms/shortest-path/dijkstra.js +30 -14
  107. package/dist/src/algorithms/shortest-path/dijkstra.js.map +1 -1
  108. package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts +17 -0
  109. package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts.map +1 -1
  110. package/dist/src/algorithms/shortest-path/floyd-warshall.js +17 -0
  111. package/dist/src/algorithms/shortest-path/floyd-warshall.js.map +1 -1
  112. package/dist/src/algorithms/shortest-path/index.d.ts.map +1 -1
  113. package/dist/src/algorithms/shortest-path/index.js.map +1 -1
  114. package/dist/src/algorithms/traversal/bfs-unified.d.ts +42 -0
  115. package/dist/src/algorithms/traversal/bfs-unified.d.ts.map +1 -0
  116. package/dist/src/algorithms/traversal/bfs-unified.js +370 -0
  117. package/dist/src/algorithms/traversal/bfs-unified.js.map +1 -0
  118. package/dist/src/algorithms/traversal/bfs-variants.d.ts +77 -0
  119. package/dist/src/algorithms/traversal/bfs-variants.d.ts.map +1 -0
  120. package/dist/src/algorithms/traversal/bfs-variants.js +402 -0
  121. package/dist/src/algorithms/traversal/bfs-variants.js.map +1 -0
  122. package/dist/src/algorithms/traversal/bfs.d.ts +4 -18
  123. package/dist/src/algorithms/traversal/bfs.d.ts.map +1 -1
  124. package/dist/src/algorithms/traversal/bfs.js +5 -182
  125. package/dist/src/algorithms/traversal/bfs.js.map +1 -1
  126. package/dist/src/algorithms/traversal/dfs.d.ts +10 -0
  127. package/dist/src/algorithms/traversal/dfs.d.ts.map +1 -1
  128. package/dist/src/algorithms/traversal/dfs.js +48 -0
  129. package/dist/src/algorithms/traversal/dfs.js.map +1 -1
  130. package/dist/src/algorithms/traversal/index.d.ts.map +1 -1
  131. package/dist/src/algorithms/traversal/index.js.map +1 -1
  132. package/dist/src/benchmark-all-algorithms.d.ts +25 -0
  133. package/dist/src/benchmark-all-algorithms.d.ts.map +1 -0
  134. package/dist/src/benchmark-all-algorithms.js +491 -0
  135. package/dist/src/benchmark-all-algorithms.js.map +1 -0
  136. package/dist/src/clustering/hierarchical.d.ts +23 -15
  137. package/dist/src/clustering/hierarchical.d.ts.map +1 -1
  138. package/dist/src/clustering/hierarchical.js +226 -224
  139. package/dist/src/clustering/hierarchical.js.map +1 -1
  140. package/dist/src/clustering/index.d.ts +4 -2
  141. package/dist/src/clustering/index.d.ts.map +1 -1
  142. package/dist/src/clustering/index.js +2 -2
  143. package/dist/src/clustering/index.js.map +1 -1
  144. package/dist/src/clustering/k-core.d.ts +17 -19
  145. package/dist/src/clustering/k-core.d.ts.map +1 -1
  146. package/dist/src/clustering/k-core.js +68 -30
  147. package/dist/src/clustering/k-core.js.map +1 -1
  148. package/dist/src/clustering/mcl.d.ts +6 -0
  149. package/dist/src/clustering/mcl.d.ts.map +1 -1
  150. package/dist/src/clustering/mcl.js +34 -3
  151. package/dist/src/clustering/mcl.js.map +1 -1
  152. package/dist/src/clustering/spectral.d.ts +11 -0
  153. package/dist/src/clustering/spectral.d.ts.map +1 -1
  154. package/dist/src/clustering/spectral.js +99 -47
  155. package/dist/src/clustering/spectral.js.map +1 -1
  156. package/dist/src/core/graph.d.ts +45 -0
  157. package/dist/src/core/graph.d.ts.map +1 -1
  158. package/dist/src/core/graph.js +49 -5
  159. package/dist/src/core/graph.js.map +1 -1
  160. package/dist/src/data-structures/index.d.ts.map +1 -1
  161. package/dist/src/data-structures/index.js.map +1 -1
  162. package/dist/src/data-structures/priority-queue.d.ts +18 -1
  163. package/dist/src/data-structures/priority-queue.d.ts.map +1 -1
  164. package/dist/src/data-structures/priority-queue.js +26 -5
  165. package/dist/src/data-structures/priority-queue.js.map +1 -1
  166. package/dist/src/data-structures/union-find.d.ts +21 -0
  167. package/dist/src/data-structures/union-find.d.ts.map +1 -1
  168. package/dist/src/data-structures/union-find.js +21 -0
  169. package/dist/src/data-structures/union-find.js.map +1 -1
  170. package/dist/src/flow/ford-fulkerson.d.ts +20 -12
  171. package/dist/src/flow/ford-fulkerson.d.ts.map +1 -1
  172. package/dist/src/flow/ford-fulkerson.js +170 -217
  173. package/dist/src/flow/ford-fulkerson.js.map +1 -1
  174. package/dist/src/flow/min-cut.d.ts +6 -8
  175. package/dist/src/flow/min-cut.d.ts.map +1 -1
  176. package/dist/src/flow/min-cut.js +34 -12
  177. package/dist/src/flow/min-cut.js.map +1 -1
  178. package/dist/src/index.d.ts +3 -1
  179. package/dist/src/index.d.ts.map +1 -1
  180. package/dist/src/index.js +6 -1
  181. package/dist/src/index.js.map +1 -1
  182. package/dist/src/link-prediction/adamic-adar.d.ts +26 -0
  183. package/dist/src/link-prediction/adamic-adar.d.ts.map +1 -1
  184. package/dist/src/link-prediction/adamic-adar.js +47 -30
  185. package/dist/src/link-prediction/adamic-adar.js.map +1 -1
  186. package/dist/src/link-prediction/common-neighbors.d.ts +21 -0
  187. package/dist/src/link-prediction/common-neighbors.d.ts.map +1 -1
  188. package/dist/src/link-prediction/common-neighbors.js +31 -14
  189. package/dist/src/link-prediction/common-neighbors.js.map +1 -1
  190. package/dist/src/link-prediction/index.d.ts.map +1 -1
  191. package/dist/src/optimized/bit-packed.d.ts +193 -0
  192. package/dist/src/optimized/bit-packed.d.ts.map +1 -0
  193. package/dist/src/optimized/bit-packed.js +342 -0
  194. package/dist/src/optimized/bit-packed.js.map +1 -0
  195. package/dist/src/optimized/csr-graph.d.ts +149 -0
  196. package/dist/src/optimized/csr-graph.d.ts.map +1 -0
  197. package/dist/src/optimized/csr-graph.js +404 -0
  198. package/dist/src/optimized/csr-graph.js.map +1 -0
  199. package/dist/src/optimized/direction-optimized-bfs.d.ts +95 -0
  200. package/dist/src/optimized/direction-optimized-bfs.d.ts.map +1 -0
  201. package/dist/src/optimized/direction-optimized-bfs.js +242 -0
  202. package/dist/src/optimized/direction-optimized-bfs.js.map +1 -0
  203. package/dist/src/optimized/graph-adapter.d.ts +111 -0
  204. package/dist/src/optimized/graph-adapter.d.ts.map +1 -0
  205. package/dist/src/optimized/graph-adapter.js +194 -0
  206. package/dist/src/optimized/graph-adapter.js.map +1 -0
  207. package/dist/src/optimized/index.d.ts +14 -0
  208. package/dist/src/optimized/index.d.ts.map +1 -0
  209. package/dist/src/optimized/index.js +30 -0
  210. package/dist/src/optimized/index.js.map +1 -0
  211. package/dist/src/pathfinding/astar.d.ts +25 -9
  212. package/dist/src/pathfinding/astar.d.ts.map +1 -1
  213. package/dist/src/pathfinding/astar.js +25 -10
  214. package/dist/src/pathfinding/astar.js.map +1 -1
  215. package/dist/src/pathfinding/utils.d.ts +15 -6
  216. package/dist/src/pathfinding/utils.d.ts.map +1 -1
  217. package/dist/src/pathfinding/utils.js +19 -7
  218. package/dist/src/pathfinding/utils.js.map +1 -1
  219. package/dist/src/research/grsbm.d.ts +0 -1
  220. package/dist/src/research/grsbm.d.ts.map +1 -1
  221. package/dist/src/research/grsbm.js +28 -23
  222. package/dist/src/research/grsbm.js.map +1 -1
  223. package/dist/src/research/index.d.ts.map +1 -1
  224. package/dist/src/research/index.js.map +1 -1
  225. package/dist/src/research/sync.d.ts +0 -1
  226. package/dist/src/research/sync.d.ts.map +1 -1
  227. package/dist/src/research/sync.js +31 -36
  228. package/dist/src/research/sync.js.map +1 -1
  229. package/dist/src/research/terahac.d.ts +2 -1
  230. package/dist/src/research/terahac.d.ts.map +1 -1
  231. package/dist/src/research/terahac.js +39 -7
  232. package/dist/src/research/terahac.js.map +1 -1
  233. package/dist/src/types/index.d.ts +8 -0
  234. package/dist/src/types/index.d.ts.map +1 -1
  235. package/dist/src/utils/algorithm-utilities.d.ts +2 -0
  236. package/dist/src/utils/algorithm-utilities.d.ts.map +1 -0
  237. package/dist/src/utils/algorithm-utilities.js +2 -0
  238. package/dist/src/utils/algorithm-utilities.js.map +1 -0
  239. package/dist/src/utils/graph-converters.d.ts +110 -0
  240. package/dist/src/utils/graph-converters.d.ts.map +1 -0
  241. package/dist/src/utils/graph-converters.js +227 -0
  242. package/dist/src/utils/graph-converters.js.map +1 -0
  243. package/dist/src/utils/graph-utilities.d.ts +62 -0
  244. package/dist/src/utils/graph-utilities.d.ts.map +1 -0
  245. package/dist/src/utils/graph-utilities.js +148 -0
  246. package/dist/src/utils/graph-utilities.js.map +1 -0
  247. package/dist/src/utils/index.d.ts +5 -0
  248. package/dist/src/utils/index.d.ts.map +1 -0
  249. package/dist/src/utils/index.js +5 -0
  250. package/dist/src/utils/index.js.map +1 -0
  251. package/dist/src/utils/math-utilities.d.ts +63 -0
  252. package/dist/src/utils/math-utilities.d.ts.map +1 -0
  253. package/dist/src/utils/math-utilities.js +132 -0
  254. package/dist/src/utils/math-utilities.js.map +1 -0
  255. package/dist/src/utils/matrix-utilities.d.ts +2 -0
  256. package/dist/src/utils/matrix-utilities.d.ts.map +1 -0
  257. package/dist/src/utils/matrix-utilities.js +2 -0
  258. package/dist/src/utils/matrix-utilities.js.map +1 -0
  259. package/dist/src/utils/optimization-helpers.d.ts +42 -0
  260. package/dist/src/utils/optimization-helpers.d.ts.map +1 -0
  261. package/dist/src/utils/optimization-helpers.js +59 -0
  262. package/dist/src/utils/optimization-helpers.js.map +1 -0
  263. package/dist/src/utils/priorityQueue.d.ts +24 -0
  264. package/dist/src/utils/priorityQueue.d.ts.map +1 -1
  265. package/dist/src/utils/priorityQueue.js +34 -4
  266. package/dist/src/utils/priorityQueue.js.map +1 -1
  267. package/package.json +58 -39
  268. package/src/algorithms/centrality/betweenness.ts +185 -200
  269. package/src/algorithms/centrality/closeness.ts +93 -194
  270. package/src/algorithms/centrality/degree.ts +18 -22
  271. package/src/algorithms/centrality/delta-pagerank-simple.ts +374 -0
  272. package/src/algorithms/centrality/delta-pagerank.ts +458 -0
  273. package/src/algorithms/centrality/eigenvector.ts +14 -15
  274. package/src/algorithms/centrality/hits.ts +17 -17
  275. package/src/algorithms/centrality/index.ts +20 -13
  276. package/src/algorithms/centrality/katz.ts +17 -25
  277. package/src/algorithms/centrality/pagerank.ts +91 -16
  278. package/src/algorithms/community/girvan-newman.ts +90 -86
  279. package/src/algorithms/community/index.ts +6 -5
  280. package/src/algorithms/community/label-propagation.ts +69 -48
  281. package/src/algorithms/community/leiden.ts +88 -65
  282. package/src/algorithms/community/louvain-optimized.ts +474 -0
  283. package/src/algorithms/community/louvain.ts +52 -122
  284. package/src/algorithms/community/modularity-utils.ts +134 -0
  285. package/src/algorithms/components/connected.ts +40 -17
  286. package/src/algorithms/matching/bipartite.ts +24 -53
  287. package/src/algorithms/matching/index.ts +4 -4
  288. package/src/algorithms/matching/isomorphism.ts +53 -23
  289. package/src/algorithms/mst/index.ts +3 -4
  290. package/src/algorithms/mst/kruskal.ts +21 -6
  291. package/src/algorithms/mst/prim.ts +13 -4
  292. package/src/algorithms/shortest-path/bellman-ford.ts +16 -28
  293. package/src/algorithms/shortest-path/bidirectional-dijkstra.ts +248 -0
  294. package/src/algorithms/shortest-path/dijkstra.ts +38 -30
  295. package/src/algorithms/shortest-path/floyd-warshall.ts +21 -4
  296. package/src/algorithms/shortest-path/index.ts +5 -5
  297. package/src/algorithms/traversal/bfs-unified.ts +437 -0
  298. package/src/algorithms/traversal/bfs-variants.ts +520 -0
  299. package/src/algorithms/traversal/bfs.ts +5 -229
  300. package/src/algorithms/traversal/dfs.ts +61 -42
  301. package/src/algorithms/traversal/index.ts +3 -3
  302. package/src/benchmark-all-algorithms.ts +580 -0
  303. package/src/clustering/hierarchical.ts +268 -272
  304. package/src/clustering/index.ts +8 -6
  305. package/src/clustering/k-core.ts +93 -61
  306. package/src/clustering/mcl.ts +53 -23
  307. package/src/clustering/spectral.ts +130 -72
  308. package/src/core/graph.ts +58 -18
  309. package/src/data-structures/index.ts +2 -2
  310. package/src/data-structures/priority-queue.ts +36 -11
  311. package/src/data-structures/union-find.ts +22 -1
  312. package/src/flow/ford-fulkerson.ts +221 -262
  313. package/src/flow/min-cut.ts +59 -51
  314. package/src/index.ts +9 -2
  315. package/src/link-prediction/adamic-adar.ts +75 -74
  316. package/src/link-prediction/common-neighbors.ts +49 -49
  317. package/src/link-prediction/index.ts +1 -1
  318. package/src/optimized/bit-packed.ts +390 -0
  319. package/src/optimized/csr-graph.ts +518 -0
  320. package/src/optimized/direction-optimized-bfs.ts +310 -0
  321. package/src/optimized/graph-adapter.ts +248 -0
  322. package/src/optimized/index.ts +34 -0
  323. package/src/pathfinding/astar.ts +40 -25
  324. package/src/pathfinding/utils.ts +20 -13
  325. package/src/research/grsbm.ts +36 -39
  326. package/src/research/index.ts +4 -4
  327. package/src/research/sync.ts +41 -54
  328. package/src/research/terahac.ts +63 -21
  329. package/src/types/index.ts +8 -0
  330. package/src/utils/algorithm-utilities.ts +2 -0
  331. package/src/utils/graph-converters.ts +269 -0
  332. package/src/utils/graph-utilities.ts +173 -0
  333. package/src/utils/index.ts +4 -0
  334. package/src/utils/math-utilities.ts +149 -0
  335. package/src/utils/matrix-utilities.ts +2 -0
  336. package/src/utils/optimization-helpers.ts +87 -0
  337. package/src/utils/priorityQueue.ts +40 -6
@@ -0,0 +1,227 @@
1
+ import { Graph } from "../core/graph.js";
2
+ /**
3
+ * Convert Graph class to Map representation
4
+ * Used by community detection and flow algorithms
5
+ * @param graph - The Graph instance to convert
6
+ * @returns A nested Map where outer keys are source nodes and inner maps contain target nodes with edge weights
7
+ */
8
+ export function graphToMap(graph) {
9
+ const map = new Map();
10
+ // Initialize all nodes
11
+ for (const node of graph.nodes()) {
12
+ map.set(String(node.id), new Map());
13
+ }
14
+ // Add edges with weights
15
+ for (const edge of graph.edges()) {
16
+ const source = String(edge.source);
17
+ const target = String(edge.target);
18
+ const weight = edge.weight ?? 1;
19
+ const sourceMap = map.get(source);
20
+ if (sourceMap) {
21
+ sourceMap.set(target, weight);
22
+ }
23
+ // For undirected graphs, add reverse edge
24
+ if (!graph.isDirected) {
25
+ const targetMap = map.get(target);
26
+ if (targetMap) {
27
+ targetMap.set(source, weight);
28
+ }
29
+ }
30
+ }
31
+ return map;
32
+ }
33
+ /**
34
+ * Convert Map representation to Graph class
35
+ * @param map - The nested Map representation of the graph
36
+ * @param isDirected - Whether the resulting graph should be directed (defaults to true)
37
+ * @returns A new Graph instance with nodes and edges from the map
38
+ */
39
+ export function mapToGraph(map, isDirected = true) {
40
+ const graph = new Graph({ directed: isDirected });
41
+ // Add all nodes first
42
+ for (const node of map.keys()) {
43
+ graph.addNode(node);
44
+ }
45
+ // Add edges
46
+ const processedEdges = new Set();
47
+ for (const [source, neighbors] of map) {
48
+ for (const [target, weight] of neighbors) {
49
+ const edgeKey = isDirected ? `${source}->${target}` : [source, target].sort().join("-");
50
+ // Skip if we've already processed this edge (for undirected graphs)
51
+ if (!isDirected && processedEdges.has(edgeKey)) {
52
+ continue;
53
+ }
54
+ graph.addEdge(source, target, weight);
55
+ processedEdges.add(edgeKey);
56
+ }
57
+ }
58
+ return graph;
59
+ }
60
+ /**
61
+ * Adapter for using Graph algorithms with Map representation
62
+ * Implements a subset of Graph interface methods needed by algorithms
63
+ */
64
+ export class GraphAdapter {
65
+ /**
66
+ * Creates a new GraphAdapter instance wrapping a Map representation.
67
+ * @param map - The nested Map representation of the graph
68
+ * @param isDirected - Whether the graph should be treated as directed (defaults to true)
69
+ */
70
+ constructor(map, isDirected = true) {
71
+ this.map = map;
72
+ this.isDirected = isDirected;
73
+ }
74
+ /**
75
+ * Get all nodes
76
+ * @returns Array of node objects with their ids
77
+ */
78
+ nodes() {
79
+ return Array.from(this.map.keys()).map((id) => ({ id }));
80
+ }
81
+ /**
82
+ * Get node count
83
+ * @returns The total number of nodes in the graph
84
+ */
85
+ get nodeCount() {
86
+ return this.map.size;
87
+ }
88
+ /**
89
+ * Check if node exists
90
+ * @param nodeId - The id of the node to check
91
+ * @returns True if the node exists, false otherwise
92
+ */
93
+ hasNode(nodeId) {
94
+ return this.map.has(String(nodeId));
95
+ }
96
+ /**
97
+ * Get neighbors of a node
98
+ * @param nodeId - The id of the node to get neighbors for
99
+ * @returns Array of neighbor node ids
100
+ */
101
+ neighbors(nodeId) {
102
+ const neighbors = this.map.get(String(nodeId));
103
+ if (!neighbors) {
104
+ return [];
105
+ }
106
+ return Array.from(neighbors.keys());
107
+ }
108
+ /**
109
+ * Get edge between two nodes
110
+ * @param source - The source node id
111
+ * @param target - The target node id
112
+ * @returns The edge object with source, target and weight, or null if no edge exists
113
+ */
114
+ getEdge(source, target) {
115
+ const sourceNeighbors = this.map.get(String(source));
116
+ if (!sourceNeighbors) {
117
+ return null;
118
+ }
119
+ const weight = sourceNeighbors.get(String(target));
120
+ if (weight === undefined) {
121
+ return null;
122
+ }
123
+ return { source, target, weight };
124
+ }
125
+ /**
126
+ * Check if edge exists
127
+ * @param source - The source node id
128
+ * @param target - The target node id
129
+ * @returns True if an edge exists between source and target, false otherwise
130
+ */
131
+ hasEdge(source, target) {
132
+ const sourceNeighbors = this.map.get(String(source));
133
+ if (!sourceNeighbors) {
134
+ return false;
135
+ }
136
+ return sourceNeighbors.has(String(target));
137
+ }
138
+ /**
139
+ * Get all edges
140
+ * @returns Array of edge objects with source, target and optional weight
141
+ */
142
+ edges() {
143
+ const edges = [];
144
+ const processedEdges = new Set();
145
+ for (const [source, neighbors] of this.map) {
146
+ for (const [target, weight] of neighbors) {
147
+ const edgeKey = this.isDirected ? `${source}->${target}` : [source, target].sort().join("-");
148
+ // Skip if we've already processed this edge (for undirected graphs)
149
+ if (!this.isDirected && processedEdges.has(edgeKey)) {
150
+ continue;
151
+ }
152
+ edges.push({ source, target, weight });
153
+ processedEdges.add(edgeKey);
154
+ }
155
+ }
156
+ return edges;
157
+ }
158
+ /**
159
+ * Get edge count
160
+ * @returns The total number of edges in the graph
161
+ */
162
+ get edgeCount() {
163
+ if (this.isDirected) {
164
+ let count = 0;
165
+ for (const neighbors of this.map.values()) {
166
+ count += neighbors.size;
167
+ }
168
+ return count;
169
+ }
170
+ // For undirected graphs, count each edge once
171
+ let count = 0;
172
+ const counted = new Set();
173
+ for (const [source, neighbors] of this.map) {
174
+ for (const target of neighbors.keys()) {
175
+ const edgeKey = [source, target].sort().join("-");
176
+ if (!counted.has(edgeKey)) {
177
+ counted.add(edgeKey);
178
+ count++;
179
+ }
180
+ }
181
+ }
182
+ return count;
183
+ }
184
+ /**
185
+ * Get node degree
186
+ * @param nodeId - The id of the node to get degree for
187
+ * @returns The number of edges connected to the node
188
+ */
189
+ degree(nodeId) {
190
+ const neighbors = this.map.get(String(nodeId));
191
+ return neighbors ? neighbors.size : 0;
192
+ }
193
+ /**
194
+ * Get in-degree (for directed graphs)
195
+ * @param nodeId - The id of the node to get in-degree for
196
+ * @returns The number of incoming edges to the node
197
+ */
198
+ inDegree(nodeId) {
199
+ if (!this.isDirected) {
200
+ return this.degree(nodeId);
201
+ }
202
+ let count = 0;
203
+ const targetId = String(nodeId);
204
+ for (const neighbors of this.map.values()) {
205
+ if (neighbors.has(targetId)) {
206
+ count++;
207
+ }
208
+ }
209
+ return count;
210
+ }
211
+ /**
212
+ * Get out-degree (for directed graphs)
213
+ * @param nodeId - The id of the node to get out-degree for
214
+ * @returns The number of outgoing edges from the node
215
+ */
216
+ outDegree(nodeId) {
217
+ return this.degree(nodeId);
218
+ }
219
+ /**
220
+ * Get underlying map representation
221
+ * @returns The internal Map representation of the graph
222
+ */
223
+ getMap() {
224
+ return this.map;
225
+ }
226
+ }
227
+ //# sourceMappingURL=graph-converters.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-converters.js","sourceRoot":"","sources":["../../../src/utils/graph-converters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAGzC;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,KAAY;IACnC,MAAM,GAAG,GAAG,IAAI,GAAG,EAA+B,CAAC;IAEnD,uBAAuB;IACvB,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;QAC/B,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;IACxC,CAAC;IAED,yBAAyB;IACzB,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;QAC/B,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;QAEhC,MAAM,SAAS,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAClC,IAAI,SAAS,EAAE,CAAC;YACZ,SAAS,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAClC,CAAC;QAED,0CAA0C;QAC1C,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;YACpB,MAAM,SAAS,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YAClC,IAAI,SAAS,EAAE,CAAC;gBACZ,SAAS,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YAClC,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,GAAG,CAAC;AACf,CAAC;AAED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CAAC;QAED,OAAO,KAAK,CAAC;IACjB,CAAC;IAED;;;;OAIG;IACH,SAAS,CAAC,MAAc;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;IAED;;;OAGG;IACH,MAAM;QACF,OAAO,IAAI,CAAC,GAAG,CAAC;IACpB,CAAC;CACJ"}
@@ -0,0 +1,62 @@
1
+ import { Graph } from "../core/graph.js";
2
+ import type { NodeId } from "../types/index.js";
3
+ /**
4
+ * Reconstructs a path from source to target using a predecessor map
5
+ * @param target - The target node
6
+ * @param predecessor - Map of node to its predecessor in the path
7
+ * @returns Array of nodes from source to target, or empty array if no path exists
8
+ */
9
+ export declare function reconstructPath<T>(target: T, predecessor: Map<T, T | null>): T[];
10
+ /**
11
+ * Find common neighbors between two nodes
12
+ * @param graph - The graph
13
+ * @param source - First node
14
+ * @param target - Second node
15
+ * @param directed - Whether to consider edge direction
16
+ * @returns Set of common neighbor node IDs
17
+ */
18
+ export declare function getCommonNeighbors(graph: Graph, source: NodeId, target: NodeId, directed?: boolean): Set<NodeId>;
19
+ /**
20
+ * Find common neighbors for link prediction in directed graphs
21
+ * This finds nodes that form a path from source to target (source->X->target)
22
+ * @param graph - The graph
23
+ * @param source - First node
24
+ * @param target - Second node
25
+ * @returns Set of intermediate node IDs
26
+ */
27
+ export declare function getIntermediateNodes(graph: Graph, source: NodeId, target: NodeId): Set<NodeId>;
28
+ /**
29
+ * Generate a consistent edge key for undirected graphs
30
+ * @param source - Source node
31
+ * @param target - Target node
32
+ * @param isDirected - Whether the graph is directed
33
+ * @returns A consistent edge identifier
34
+ */
35
+ export declare function getEdgeKey(source: NodeId, target: NodeId, isDirected: boolean): string;
36
+ /**
37
+ * Calculate total edge weight in a graph
38
+ * @param graph - The graph
39
+ * @returns Total weight of all edges
40
+ */
41
+ export declare function getTotalEdgeWeight(graph: Graph): number;
42
+ /**
43
+ * Get node degree with optional mode for directed graphs
44
+ * @param graph - The graph
45
+ * @param nodeId - The node ID
46
+ * @param mode - Degree mode for directed graphs
47
+ * @returns Node degree
48
+ */
49
+ export declare function getNodeDegree(graph: Graph, nodeId: NodeId, mode?: "in" | "out" | "total"): number;
50
+ /**
51
+ * Convert directed graph to undirected
52
+ * @param graph - The directed graph
53
+ * @returns A new undirected graph
54
+ */
55
+ export declare function makeUndirected(graph: Graph): Graph;
56
+ /**
57
+ * Renumber communities consecutively starting from 0
58
+ * @param communities - Map of node to community ID
59
+ * @returns Map with renumbered community IDs
60
+ */
61
+ export declare function renumberCommunities<T>(communities: Map<T, number>): Map<T, number>;
62
+ //# sourceMappingURL=graph-utilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-utilities.d.ts","sourceRoot":"","sources":["../../../src/utils/graph-utilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,WAAW,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAiBhF;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,UAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,CAY9G;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAY9F;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,GAAG,MAAM,CAQtF;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,CAMvD;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAE,IAAI,GAAG,KAAK,GAAG,OAAiB,GAAG,MAAM,CAU1G;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAqBlD;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAkBlF"}
@@ -0,0 +1,148 @@
1
+ import { Graph } from "../core/graph.js";
2
+ /**
3
+ * Reconstructs a path from source to target using a predecessor map
4
+ * @param target - The target node
5
+ * @param predecessor - Map of node to its predecessor in the path
6
+ * @returns Array of nodes from source to target, or empty array if no path exists
7
+ */
8
+ export function reconstructPath(target, predecessor) {
9
+ const path = [];
10
+ let current = target;
11
+ // Build path backwards from target to source
12
+ while (current !== null) {
13
+ path.unshift(current);
14
+ const pred = predecessor.get(current);
15
+ if (pred === undefined) {
16
+ // No path exists
17
+ return [];
18
+ }
19
+ current = pred;
20
+ }
21
+ return path;
22
+ }
23
+ /**
24
+ * Find common neighbors between two nodes
25
+ * @param graph - The graph
26
+ * @param source - First node
27
+ * @param target - Second node
28
+ * @param directed - Whether to consider edge direction
29
+ * @returns Set of common neighbor node IDs
30
+ */
31
+ export function getCommonNeighbors(graph, source, target, directed = false) {
32
+ const sourceNeighbors = new Set(directed ? graph.outNeighbors(source) : graph.neighbors(source));
33
+ const targetNeighbors = new Set(directed ? graph.outNeighbors(target) : graph.neighbors(target));
34
+ const common = new Set();
35
+ for (const neighbor of sourceNeighbors) {
36
+ if (targetNeighbors.has(neighbor)) {
37
+ common.add(neighbor);
38
+ }
39
+ }
40
+ return common;
41
+ }
42
+ /**
43
+ * Find common neighbors for link prediction in directed graphs
44
+ * This finds nodes that form a path from source to target (source->X->target)
45
+ * @param graph - The graph
46
+ * @param source - First node
47
+ * @param target - Second node
48
+ * @returns Set of intermediate node IDs
49
+ */
50
+ export function getIntermediateNodes(graph, source, target) {
51
+ const sourceOutNeighbors = new Set(graph.outNeighbors(source));
52
+ const targetInNeighbors = new Set(graph.inNeighbors(target));
53
+ const common = new Set();
54
+ for (const neighbor of sourceOutNeighbors) {
55
+ if (targetInNeighbors.has(neighbor)) {
56
+ common.add(neighbor);
57
+ }
58
+ }
59
+ return common;
60
+ }
61
+ /**
62
+ * Generate a consistent edge key for undirected graphs
63
+ * @param source - Source node
64
+ * @param target - Target node
65
+ * @param isDirected - Whether the graph is directed
66
+ * @returns A consistent edge identifier
67
+ */
68
+ export function getEdgeKey(source, target, isDirected) {
69
+ if (isDirected) {
70
+ return `${String(source)}->${String(target)}`;
71
+ }
72
+ // For undirected graphs, sort to ensure consistency
73
+ const [first, second] = source < target ? [source, target] : [target, source];
74
+ return `${String(first)}-${String(second)}`;
75
+ }
76
+ /**
77
+ * Calculate total edge weight in a graph
78
+ * @param graph - The graph
79
+ * @returns Total weight of all edges
80
+ */
81
+ export function getTotalEdgeWeight(graph) {
82
+ let totalWeight = 0;
83
+ for (const edge of graph.edges()) {
84
+ totalWeight += edge.weight ?? 1;
85
+ }
86
+ return graph.isDirected ? totalWeight : totalWeight / 2;
87
+ }
88
+ /**
89
+ * Get node degree with optional mode for directed graphs
90
+ * @param graph - The graph
91
+ * @param nodeId - The node ID
92
+ * @param mode - Degree mode for directed graphs
93
+ * @returns Node degree
94
+ */
95
+ export function getNodeDegree(graph, nodeId, mode = "total") {
96
+ if (!graph.isDirected || mode === "total") {
97
+ return graph.degree(nodeId);
98
+ }
99
+ if (mode === "in") {
100
+ return graph.inDegree(nodeId);
101
+ }
102
+ return graph.outDegree(nodeId);
103
+ }
104
+ /**
105
+ * Convert directed graph to undirected
106
+ * @param graph - The directed graph
107
+ * @returns A new undirected graph
108
+ */
109
+ export function makeUndirected(graph) {
110
+ if (!graph.isDirected) {
111
+ return graph;
112
+ }
113
+ const undirected = new Graph({ directed: false });
114
+ // Add all nodes
115
+ for (const node of graph.nodes()) {
116
+ undirected.addNode(node.id, node.data);
117
+ }
118
+ // Add edges (will automatically be made bidirectional)
119
+ for (const edge of graph.edges()) {
120
+ // For undirected, we only add each edge once
121
+ if (!undirected.hasEdge(edge.source, edge.target)) {
122
+ undirected.addEdge(edge.source, edge.target, edge.weight, edge.data);
123
+ }
124
+ }
125
+ return undirected;
126
+ }
127
+ /**
128
+ * Renumber communities consecutively starting from 0
129
+ * @param communities - Map of node to community ID
130
+ * @returns Map with renumbered community IDs
131
+ */
132
+ export function renumberCommunities(communities) {
133
+ const uniqueCommunities = new Set(communities.values());
134
+ const remapping = new Map();
135
+ let newId = 0;
136
+ for (const community of uniqueCommunities) {
137
+ remapping.set(community, newId++);
138
+ }
139
+ const renumbered = new Map();
140
+ for (const [node, community] of communities) {
141
+ const newCommunity = remapping.get(community);
142
+ if (newCommunity !== undefined) {
143
+ renumbered.set(node, newCommunity);
144
+ }
145
+ }
146
+ return renumbered;
147
+ }
148
+ //# sourceMappingURL=graph-utilities.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-utilities.js","sourceRoot":"","sources":["../../../src/utils/graph-utilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAGzC;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAI,MAAS,EAAE,WAA6B;IACvE,MAAM,IAAI,GAAQ,EAAE,CAAC;IACrB,IAAI,OAAO,GAAa,MAAM,CAAC;IAE/B,6CAA6C;IAC7C,OAAO,OAAO,KAAK,IAAI,EAAE,CAAC;QACtB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACtB,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACtC,IAA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AG,CAAC,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;IACtC,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,GAAG,EAAa,CAAC;IACxC,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,WAAW,EAAE,CAAC;QAC1C,MAAM,YAAY,GAAG,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC9C,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;YAC7B,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;QACvC,CAAC;IACL,CAAC;IAED,OAAO,UAAU,CAAC;AACtB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from "./algorithm-utilities.js";
2
+ export * from "./graph-utilities.js";
3
+ export * from "./math-utilities.js";
4
+ export * from "./matrix-utilities.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from "./algorithm-utilities.js";
2
+ export * from "./graph-utilities.js";
3
+ export * from "./math-utilities.js";
4
+ export * from "./matrix-utilities.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Seeded random number generator for reproducible results
3
+ */
4
+ export declare class SeededRandom {
5
+ private seed;
6
+ private readonly m;
7
+ private readonly a;
8
+ private readonly c;
9
+ /**
10
+ * Creates a new SeededRandom instance with the given seed.
11
+ * @param seed - The seed value for reproducible random number generation
12
+ */
13
+ constructor(seed: number);
14
+ /**
15
+ * Generate next random number between 0 and 1
16
+ * @returns A pseudo-random number in the range [0, 1]
17
+ */
18
+ next(): number;
19
+ /**
20
+ * Create a generator function for backward compatibility
21
+ * @param seed - The seed value for reproducible random number generation
22
+ * @returns A function that returns the next random number when called
23
+ */
24
+ static createGenerator(seed: number): () => number;
25
+ }
26
+ /**
27
+ * Fisher-Yates shuffle algorithm
28
+ * @param array - Array to shuffle (will be modified in place)
29
+ * @param rng - Optional random number generator (defaults to Math.random)
30
+ * @returns The shuffled array (same reference as input)
31
+ */
32
+ export declare function shuffle<T>(array: T[], rng?: () => number): T[];
33
+ /**
34
+ * Calculate Euclidean distance between two vectors
35
+ * @param a - First vector
36
+ * @param b - Second vector
37
+ * @returns Euclidean distance
38
+ * @throws Error if vectors have different lengths
39
+ */
40
+ export declare function euclideanDistance(a: number[], b: number[]): number;
41
+ export declare const normalize: {
42
+ /**
43
+ * Min-max normalization to [0, 1] range
44
+ * @param values - Map of values to normalize in place
45
+ */
46
+ minMax(values: Map<string, number>): void;
47
+ /**
48
+ * Normalize by maximum value
49
+ * @param values - Map of values to normalize in place
50
+ */
51
+ byMax(values: Map<string, number>): void;
52
+ /**
53
+ * L2 (Euclidean) normalization
54
+ * @param values - Map of values to normalize in place
55
+ */
56
+ l2Norm(values: Map<string, number>): void;
57
+ /**
58
+ * Normalize to sum to 1
59
+ * @param values - Map of values to normalize in place
60
+ */
61
+ sumToOne(values: Map<string, number>): void;
62
+ };
63
+ //# sourceMappingURL=math-utilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math-utilities.d.ts","sourceRoot":"","sources":["../../../src/utils/math-utilities.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,YAAY;IACrB,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAc;IAChC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAc;IAChC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAS;IAE3B;;;OAGG;gBACS,IAAI,EAAE,MAAM;IAKxB;;;OAGG;IACH,IAAI,IAAI,MAAM;IAKd;;;;OAIG;IACH,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,MAAM;CAIrD;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,GAAG,GAAE,MAAM,MAAoB,GAAG,CAAC,EAAE,CAW3E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAelE;AAED,eAAO,MAAM,SAAS;IAClB;;;OAGG;mBACY,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAezC;;;OAGG;kBACW,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAWxC;;;OAGG;mBACY,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAazC;;;OAGG;qBACc,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;CAW9C,CAAC"}
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Seeded random number generator for reproducible results
3
+ */
4
+ export class SeededRandom {
5
+ /**
6
+ * Creates a new SeededRandom instance with the given seed.
7
+ * @param seed - The seed value for reproducible random number generation
8
+ */
9
+ constructor(seed) {
10
+ this.m = 0x80000000; // 2**31
11
+ this.a = 1103515245;
12
+ this.c = 12345;
13
+ // Handle negative seeds correctly
14
+ this.seed = ((seed % this.m) + this.m) % this.m;
15
+ }
16
+ /**
17
+ * Generate next random number between 0 and 1
18
+ * @returns A pseudo-random number in the range [0, 1]
19
+ */
20
+ next() {
21
+ this.seed = (this.a * this.seed + this.c) % this.m;
22
+ return this.seed / (this.m - 1);
23
+ }
24
+ /**
25
+ * Create a generator function for backward compatibility
26
+ * @param seed - The seed value for reproducible random number generation
27
+ * @returns A function that returns the next random number when called
28
+ */
29
+ static createGenerator(seed) {
30
+ const rng = new SeededRandom(seed);
31
+ return () => rng.next();
32
+ }
33
+ }
34
+ /**
35
+ * Fisher-Yates shuffle algorithm
36
+ * @param array - Array to shuffle (will be modified in place)
37
+ * @param rng - Optional random number generator (defaults to Math.random)
38
+ * @returns The shuffled array (same reference as input)
39
+ */
40
+ export function shuffle(array, rng = Math.random) {
41
+ for (let i = array.length - 1; i > 0; i--) {
42
+ const j = Math.floor(rng() * (i + 1));
43
+ const temp = array[i];
44
+ const tempJ = array[j];
45
+ if (temp !== undefined && tempJ !== undefined) {
46
+ array[i] = tempJ;
47
+ array[j] = temp;
48
+ }
49
+ }
50
+ return array;
51
+ }
52
+ /**
53
+ * Calculate Euclidean distance between two vectors
54
+ * @param a - First vector
55
+ * @param b - Second vector
56
+ * @returns Euclidean distance
57
+ * @throws Error if vectors have different lengths
58
+ */
59
+ export function euclideanDistance(a, b) {
60
+ if (a.length !== b.length) {
61
+ throw new Error("Vectors must have same length");
62
+ }
63
+ let sum = 0;
64
+ for (let i = 0; i < a.length; i++) {
65
+ const aVal = a[i];
66
+ const bVal = b[i];
67
+ if (aVal !== undefined && bVal !== undefined) {
68
+ const diff = aVal - bVal;
69
+ sum += diff * diff;
70
+ }
71
+ }
72
+ return Math.sqrt(sum);
73
+ }
74
+ export const normalize = {
75
+ /**
76
+ * Min-max normalization to [0, 1] range
77
+ * @param values - Map of values to normalize in place
78
+ */
79
+ minMax(values) {
80
+ const vals = Array.from(values.values());
81
+ const min = Math.min(...vals);
82
+ const max = Math.max(...vals);
83
+ const range = max - min;
84
+ if (range === 0) {
85
+ return;
86
+ }
87
+ for (const [key, value] of values) {
88
+ values.set(key, (value - min) / range);
89
+ }
90
+ },
91
+ /**
92
+ * Normalize by maximum value
93
+ * @param values - Map of values to normalize in place
94
+ */
95
+ byMax(values) {
96
+ const max = Math.max(...values.values());
97
+ if (max === 0) {
98
+ return;
99
+ }
100
+ for (const [key, value] of values) {
101
+ values.set(key, value / max);
102
+ }
103
+ },
104
+ /**
105
+ * L2 (Euclidean) normalization
106
+ * @param values - Map of values to normalize in place
107
+ */
108
+ l2Norm(values) {
109
+ const sumSquares = Array.from(values.values()).reduce((sum, val) => sum + val * val, 0);
110
+ const norm = Math.sqrt(sumSquares);
111
+ if (norm === 0) {
112
+ return;
113
+ }
114
+ for (const [key, value] of values) {
115
+ values.set(key, value / norm);
116
+ }
117
+ },
118
+ /**
119
+ * Normalize to sum to 1
120
+ * @param values - Map of values to normalize in place
121
+ */
122
+ sumToOne(values) {
123
+ const sum = Array.from(values.values()).reduce((acc, val) => acc + val, 0);
124
+ if (sum === 0) {
125
+ return;
126
+ }
127
+ for (const [key, value] of values) {
128
+ values.set(key, value / sum);
129
+ }
130
+ },
131
+ };
132
+ //# sourceMappingURL=math-utilities.js.map