@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,149 @@
1
+ import type { NodeId } from "../types/index.js";
2
+ /**
3
+ * Interface for read-only graph operations
4
+ */
5
+ export interface ReadonlyGraph<TNodeId = NodeId> {
6
+ nodeCount(): number;
7
+ edgeCount(): number;
8
+ hasNode(nodeId: TNodeId): boolean;
9
+ hasEdge(source: TNodeId, target: TNodeId): boolean;
10
+ neighbors(nodeId: TNodeId): IterableIterator<TNodeId>;
11
+ outDegree(nodeId: TNodeId): number;
12
+ nodes(): IterableIterator<TNodeId>;
13
+ }
14
+ /**
15
+ * Compressed Sparse Row (CSR) graph representation
16
+ *
17
+ * Provides cache-efficient graph storage with sequential memory access patterns.
18
+ * Optimized for traversal operations and sparse graphs.
19
+ */
20
+ export declare class CSRGraph<TNodeId = NodeId> implements ReadonlyGraph<TNodeId> {
21
+ private data;
22
+ /**
23
+ * Creates a new CSRGraph from an adjacency list representation.
24
+ * @param adjacencyList - Map of node IDs to arrays of neighbor node IDs
25
+ * @param weights - Optional map of edge weights keyed by "source-target" strings
26
+ * @param buildReverse - Whether to build reverse edges for bottom-up BFS (default true)
27
+ */
28
+ constructor(adjacencyList: Map<TNodeId, TNodeId[]>, weights?: Map<string, number>, buildReverse?: boolean);
29
+ /**
30
+ * Build CSR structure from adjacency list.
31
+ * @param adjacencyList - Map of node IDs to arrays of neighbor node IDs
32
+ * @param weights - Optional map of edge weights keyed by "source-target" strings
33
+ * @param buildReverse - Whether to build reverse edges for bottom-up BFS
34
+ * @returns The constructed CSR graph data structure
35
+ */
36
+ private buildCSR;
37
+ /**
38
+ * Get the total number of nodes in the graph.
39
+ * @returns The number of nodes
40
+ */
41
+ nodeCount(): number;
42
+ /**
43
+ * Get the total number of edges in the graph.
44
+ * @returns The number of edges
45
+ */
46
+ edgeCount(): number;
47
+ /**
48
+ * Check if a node exists in the graph.
49
+ * @param nodeId - The node ID to check
50
+ * @returns True if the node exists, false otherwise
51
+ */
52
+ hasNode(nodeId: TNodeId): boolean;
53
+ /**
54
+ * Check if an edge exists between two nodes.
55
+ * @param source - The source node ID
56
+ * @param target - The target node ID
57
+ * @returns True if the edge exists, false otherwise
58
+ */
59
+ hasEdge(source: TNodeId, target: TNodeId): boolean;
60
+ /**
61
+ * Get neighbors of a node as node IDs.
62
+ * @param nodeId - The node ID to get neighbors for
63
+ * @returns An iterator over neighbor node IDs
64
+ */
65
+ neighbors(nodeId: TNodeId): IterableIterator<TNodeId>;
66
+ /**
67
+ * Get all nodes in the graph.
68
+ * @returns An iterator over all node IDs
69
+ */
70
+ nodes(): IterableIterator<TNodeId>;
71
+ /**
72
+ * Get neighbors as indices (internal use).
73
+ * @param nodeIndex - The internal index of the node
74
+ * @returns Array of neighbor indices
75
+ */
76
+ getNeighborIndices(nodeIndex: number): number[];
77
+ /**
78
+ * Get the out-degree (number of outgoing edges) of a node.
79
+ * @param nodeId - The node ID to get the out-degree for
80
+ * @returns The number of outgoing edges from the node
81
+ */
82
+ outDegree(nodeId: TNodeId): number;
83
+ /**
84
+ * Get out-degree by index (internal use).
85
+ * @param nodeIndex - The internal index of the node
86
+ * @returns The number of outgoing edges from the node
87
+ */
88
+ outDegreeByIndex(nodeIndex: number): number;
89
+ /**
90
+ * Iterator support for neighbor indices.
91
+ * @param nodeIndex - The internal index of the node
92
+ * @yields The indices of neighboring nodes
93
+ */
94
+ iterateNeighborIndices(nodeIndex: number): Generator<number>;
95
+ /**
96
+ * Iterator support for incoming neighbor indices (for bottom-up BFS).
97
+ * @param nodeIndex - The internal index of the node
98
+ * @yields The indices of nodes with edges pointing to this node
99
+ */
100
+ iterateIncomingNeighborIndices(nodeIndex: number): Generator<number>;
101
+ /**
102
+ * Convert node ID to internal index.
103
+ * @param nodeId - The node ID to convert
104
+ * @returns The internal index for the node
105
+ */
106
+ nodeToIndex(nodeId: TNodeId): number;
107
+ /**
108
+ * Convert internal index to node ID.
109
+ * @param index - The internal index to convert
110
+ * @returns The node ID for the given index
111
+ */
112
+ indexToNodeId(index: number): TNodeId;
113
+ /**
114
+ * Get edge weight between two nodes.
115
+ * @param source - The source node ID
116
+ * @param target - The target node ID
117
+ * @returns The edge weight, or undefined if the edge doesn't exist or has no weight
118
+ */
119
+ getEdgeWeight(source: TNodeId, target: TNodeId): number | undefined;
120
+ /**
121
+ * Binary search for target in sorted array.
122
+ * @param arr - The typed array to search in
123
+ * @param target - The value to search for
124
+ * @param start - The starting index (inclusive)
125
+ * @param end - The ending index (exclusive)
126
+ * @returns The index of the target, or -1 if not found
127
+ */
128
+ private binarySearch;
129
+ /**
130
+ * Create CSR graph from standard Graph interface.
131
+ * @param graph - The graph object to convert, must implement nodes, neighbors, hasNode, and optionally getEdge
132
+ * @param graph.nodes - Function that returns an iterator over node objects with id property
133
+ * @param graph.neighbors - Function that returns an iterator over neighbor node IDs
134
+ * @param graph.hasNode - Function that checks if a node exists in the graph
135
+ * @param graph.getEdge - Optional function that returns edge data including weight
136
+ * @returns A new CSRGraph instance
137
+ */
138
+ static fromGraph<TNodeId = NodeId>(graph: {
139
+ nodes(): IterableIterator<{
140
+ id: TNodeId;
141
+ }>;
142
+ neighbors(nodeId: TNodeId): IterableIterator<TNodeId>;
143
+ hasNode(nodeId: TNodeId): boolean;
144
+ getEdge?(source: TNodeId, target: TNodeId): {
145
+ weight?: number;
146
+ } | undefined;
147
+ }): CSRGraph<TNodeId>;
148
+ }
149
+ //# sourceMappingURL=csr-graph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"csr-graph.d.ts","sourceRoot":"","sources":["../../../src/optimized/csr-graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,OAAO,GAAG,MAAM;IAC3C,SAAS,IAAI,MAAM,CAAC;IACpB,SAAS,IAAI,MAAM,CAAC;IACpB,OAAO,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC;IAClC,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC;IACnD,SAAS,CAAC,MAAM,EAAE,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACtD,SAAS,CAAC,MAAM,EAAE,OAAO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aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IASrC;;;;;OAKG;IACH,aAAa,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IA2BnE;;;;;;;OAOG;IACH,OAAO,CAAC,YAAY;IA0BpB;;;;;;;;OAQG;IACH,MAAM,CAAC,SAAS,CAAC,OAAO,GAAG,MAAM,EAAE,KAAK,EAAE;QACtC,KAAK,IAAI,gBAAgB,CAAC;YAAE,EAAE,EAAE,OAAO,CAAA;SAAE,CAAC,CAAC;QAC3C,SAAS,CAAC,MAAM,EAAE,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;QACtD,OAAO,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC;QAClC,OAAO,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG;YAAE,MAAM,CAAC,EAAE,MAAM,CAAA;SAAE,GAAG,SAAS,CAAC;KAC/E,GAAG,QAAQ,CAAC,OAAO,CAAC;CAuBxB"}
@@ -0,0 +1,404 @@
1
+ /**
2
+ * Compressed Sparse Row (CSR) graph representation
3
+ *
4
+ * Provides cache-efficient graph storage with sequential memory access patterns.
5
+ * Optimized for traversal operations and sparse graphs.
6
+ */
7
+ export class CSRGraph {
8
+ /**
9
+ * Creates a new CSRGraph from an adjacency list representation.
10
+ * @param adjacencyList - Map of node IDs to arrays of neighbor node IDs
11
+ * @param weights - Optional map of edge weights keyed by "source-target" strings
12
+ * @param buildReverse - Whether to build reverse edges for bottom-up BFS (default true)
13
+ */
14
+ constructor(adjacencyList, weights, buildReverse = true) {
15
+ this.data = this.buildCSR(adjacencyList, weights, buildReverse);
16
+ }
17
+ /**
18
+ * Build CSR structure from adjacency list.
19
+ * @param adjacencyList - Map of node IDs to arrays of neighbor node IDs
20
+ * @param weights - Optional map of edge weights keyed by "source-target" strings
21
+ * @param buildReverse - Whether to build reverse edges for bottom-up BFS
22
+ * @returns The constructed CSR graph data structure
23
+ */
24
+ buildCSR(adjacencyList, weights, buildReverse = true) {
25
+ // Collect all unique nodes (both sources and targets)
26
+ const allNodes = new Set();
27
+ for (const [source, neighbors] of adjacencyList) {
28
+ allNodes.add(source);
29
+ for (const neighbor of neighbors) {
30
+ allNodes.add(neighbor);
31
+ }
32
+ }
33
+ // Sort nodes for consistent ordering. Numeric IDs sort numerically,
34
+ // string IDs sort lexicographically for predictable iteration order.
35
+ const nodes = Array.from(allNodes).sort((a, b) => {
36
+ if (typeof a === "number" && typeof b === "number") {
37
+ return a - b;
38
+ }
39
+ return String(a).localeCompare(String(b));
40
+ });
41
+ const nodeCount = nodes.length;
42
+ // Build node mappings
43
+ const nodeIdToIndex = new Map();
44
+ const indexToNodeId = [];
45
+ nodes.forEach((nodeId, index) => {
46
+ nodeIdToIndex.set(nodeId, index);
47
+ indexToNodeId[index] = nodeId;
48
+ });
49
+ // Count total edges
50
+ let edgeCount = 0;
51
+ for (const neighbors of adjacencyList.values()) {
52
+ edgeCount += neighbors.length;
53
+ }
54
+ // Allocate arrays
55
+ const rowPointers = new Uint32Array(nodeCount + 1);
56
+ const columnIndices = new Uint32Array(edgeCount);
57
+ const edgeWeights = weights ? new Float32Array(edgeCount) : undefined;
58
+ // Build CSR structure
59
+ let currentEdge = 0;
60
+ for (let i = 0; i < nodeCount; i++) {
61
+ rowPointers[i] = currentEdge;
62
+ const nodeId = indexToNodeId[i];
63
+ if (nodeId === undefined) {
64
+ throw new Error(`Invalid node index ${String(i)}`);
65
+ }
66
+ const neighbors = adjacencyList.get(nodeId) ?? [];
67
+ // Sort neighbors for better cache locality and binary search
68
+ const sortedNeighbors = neighbors
69
+ .map((n) => {
70
+ const index = nodeIdToIndex.get(n);
71
+ if (index === undefined) {
72
+ throw new Error(`Node ${String(n)} not found in nodeIdToIndex map`);
73
+ }
74
+ return { id: n, index };
75
+ })
76
+ .sort((a, b) => a.index - b.index);
77
+ for (const neighbor of sortedNeighbors) {
78
+ columnIndices[currentEdge] = neighbor.index;
79
+ if (edgeWeights && weights) {
80
+ const edgeKey = `${String(nodeId)}-${String(neighbor.id)}`;
81
+ const weight = weights.get(edgeKey);
82
+ if (weight !== undefined) {
83
+ edgeWeights[currentEdge] = weight;
84
+ }
85
+ else {
86
+ edgeWeights[currentEdge] = 1;
87
+ }
88
+ }
89
+ currentEdge++;
90
+ }
91
+ }
92
+ rowPointers[nodeCount] = currentEdge;
93
+ const result = {
94
+ rowPointers,
95
+ columnIndices,
96
+ nodeIdToIndex,
97
+ indexToNodeId,
98
+ };
99
+ if (edgeWeights) {
100
+ result.edgeWeights = edgeWeights;
101
+ }
102
+ // Build reverse edges for bottom-up BFS
103
+ if (buildReverse) {
104
+ const reverseAdjacency = new Map();
105
+ for (let i = 0; i < nodeCount; i++) {
106
+ reverseAdjacency.set(i, []);
107
+ }
108
+ // Build reverse adjacency from forward edges
109
+ for (let i = 0; i < nodeCount; i++) {
110
+ const start = rowPointers[i];
111
+ const end = rowPointers[i + 1];
112
+ if (start !== undefined && end !== undefined) {
113
+ for (let j = start; j < end; j++) {
114
+ const target = columnIndices[j];
115
+ if (target !== undefined) {
116
+ reverseAdjacency.get(target)?.push(i);
117
+ }
118
+ }
119
+ }
120
+ }
121
+ // Build reverse CSR
122
+ const reverseRowPointers = new Uint32Array(nodeCount + 1);
123
+ const reverseColumnIndices = new Uint32Array(edgeCount);
124
+ let reverseEdge = 0;
125
+ for (let i = 0; i < nodeCount; i++) {
126
+ reverseRowPointers[i] = reverseEdge;
127
+ const incoming = reverseAdjacency.get(i) ?? [];
128
+ incoming.sort((a, b) => a - b);
129
+ for (const source of incoming) {
130
+ reverseColumnIndices[reverseEdge++] = source;
131
+ }
132
+ }
133
+ reverseRowPointers[nodeCount] = reverseEdge;
134
+ result.reverseRowPointers = reverseRowPointers;
135
+ result.reverseColumnIndices = reverseColumnIndices;
136
+ }
137
+ return result;
138
+ }
139
+ // Core API methods
140
+ /**
141
+ * Get the total number of nodes in the graph.
142
+ * @returns The number of nodes
143
+ */
144
+ nodeCount() {
145
+ return this.data.indexToNodeId.length;
146
+ }
147
+ /**
148
+ * Get the total number of edges in the graph.
149
+ * @returns The number of edges
150
+ */
151
+ edgeCount() {
152
+ return this.data.columnIndices.length;
153
+ }
154
+ /**
155
+ * Check if a node exists in the graph.
156
+ * @param nodeId - The node ID to check
157
+ * @returns True if the node exists, false otherwise
158
+ */
159
+ hasNode(nodeId) {
160
+ return this.data.nodeIdToIndex.has(nodeId);
161
+ }
162
+ /**
163
+ * Check if an edge exists between two nodes.
164
+ * @param source - The source node ID
165
+ * @param target - The target node ID
166
+ * @returns True if the edge exists, false otherwise
167
+ */
168
+ hasEdge(source, target) {
169
+ const sourceIndex = this.data.nodeIdToIndex.get(source);
170
+ const targetIndex = this.data.nodeIdToIndex.get(target);
171
+ if (sourceIndex === undefined || targetIndex === undefined) {
172
+ return false;
173
+ }
174
+ const start = this.data.rowPointers[sourceIndex];
175
+ const end = this.data.rowPointers[sourceIndex + 1];
176
+ // Binary search for target in sorted neighbors
177
+ if (start === undefined || end === undefined) {
178
+ return false;
179
+ }
180
+ return this.binarySearch(this.data.columnIndices, targetIndex, start, end) !== -1;
181
+ }
182
+ /**
183
+ * Get neighbors of a node as node IDs.
184
+ * @param nodeId - The node ID to get neighbors for
185
+ * @returns An iterator over neighbor node IDs
186
+ */
187
+ neighbors(nodeId) {
188
+ const nodeIndex = this.data.nodeIdToIndex.get(nodeId);
189
+ if (nodeIndex === undefined) {
190
+ return new Set().values();
191
+ }
192
+ const start = this.data.rowPointers[nodeIndex];
193
+ const end = this.data.rowPointers[nodeIndex + 1];
194
+ const { columnIndices, indexToNodeId } = this.data;
195
+ function* generateNeighbors() {
196
+ if (start !== undefined && end !== undefined) {
197
+ for (let i = start; i < end; i++) {
198
+ const idx = columnIndices[i];
199
+ if (idx !== undefined) {
200
+ const nodeId = indexToNodeId[idx];
201
+ if (nodeId !== undefined) {
202
+ yield nodeId;
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+ return generateNeighbors();
209
+ }
210
+ /**
211
+ * Get all nodes in the graph.
212
+ * @returns An iterator over all node IDs
213
+ */
214
+ nodes() {
215
+ return this.data.indexToNodeId.values();
216
+ }
217
+ /**
218
+ * Get neighbors as indices (internal use).
219
+ * @param nodeIndex - The internal index of the node
220
+ * @returns Array of neighbor indices
221
+ */
222
+ getNeighborIndices(nodeIndex) {
223
+ if (nodeIndex < 0 || nodeIndex >= this.data.indexToNodeId.length) {
224
+ return [];
225
+ }
226
+ const start = this.data.rowPointers[nodeIndex];
227
+ const end = this.data.rowPointers[nodeIndex + 1];
228
+ if (start === undefined || end === undefined) {
229
+ return [];
230
+ }
231
+ return Array.from(this.data.columnIndices.subarray(start, end));
232
+ }
233
+ /**
234
+ * Get the out-degree (number of outgoing edges) of a node.
235
+ * @param nodeId - The node ID to get the out-degree for
236
+ * @returns The number of outgoing edges from the node
237
+ */
238
+ outDegree(nodeId) {
239
+ const nodeIndex = this.data.nodeIdToIndex.get(nodeId);
240
+ if (nodeIndex === undefined) {
241
+ return 0;
242
+ }
243
+ return this.outDegreeByIndex(nodeIndex);
244
+ }
245
+ /**
246
+ * Get out-degree by index (internal use).
247
+ * @param nodeIndex - The internal index of the node
248
+ * @returns The number of outgoing edges from the node
249
+ */
250
+ outDegreeByIndex(nodeIndex) {
251
+ const start = this.data.rowPointers[nodeIndex];
252
+ const end = this.data.rowPointers[nodeIndex + 1];
253
+ if (start !== undefined && end !== undefined) {
254
+ return end - start;
255
+ }
256
+ return 0;
257
+ }
258
+ /**
259
+ * Iterator support for neighbor indices.
260
+ * @param nodeIndex - The internal index of the node
261
+ * @yields The indices of neighboring nodes
262
+ */
263
+ *iterateNeighborIndices(nodeIndex) {
264
+ const start = this.data.rowPointers[nodeIndex];
265
+ const end = this.data.rowPointers[nodeIndex + 1];
266
+ if (start !== undefined && end !== undefined) {
267
+ for (let i = start; i < end; i++) {
268
+ const value = this.data.columnIndices[i];
269
+ if (value !== undefined) {
270
+ yield value;
271
+ }
272
+ }
273
+ }
274
+ }
275
+ /**
276
+ * Iterator support for incoming neighbor indices (for bottom-up BFS).
277
+ * @param nodeIndex - The internal index of the node
278
+ * @yields The indices of nodes with edges pointing to this node
279
+ */
280
+ *iterateIncomingNeighborIndices(nodeIndex) {
281
+ if (!this.data.reverseRowPointers || !this.data.reverseColumnIndices) {
282
+ return;
283
+ }
284
+ const start = this.data.reverseRowPointers[nodeIndex];
285
+ const end = this.data.reverseRowPointers[nodeIndex + 1];
286
+ if (start !== undefined && end !== undefined) {
287
+ for (let i = start; i < end; i++) {
288
+ const value = this.data.reverseColumnIndices[i];
289
+ if (value !== undefined) {
290
+ yield value;
291
+ }
292
+ }
293
+ }
294
+ }
295
+ /**
296
+ * Convert node ID to internal index.
297
+ * @param nodeId - The node ID to convert
298
+ * @returns The internal index for the node
299
+ */
300
+ nodeToIndex(nodeId) {
301
+ const index = this.data.nodeIdToIndex.get(nodeId);
302
+ if (index === undefined) {
303
+ throw new Error(`Node ${String(nodeId)} not found in graph`);
304
+ }
305
+ return index;
306
+ }
307
+ /**
308
+ * Convert internal index to node ID.
309
+ * @param index - The internal index to convert
310
+ * @returns The node ID for the given index
311
+ */
312
+ indexToNodeId(index) {
313
+ const nodeId = this.data.indexToNodeId[index];
314
+ if (nodeId === undefined) {
315
+ throw new Error(`Index ${String(index)} out of bounds`);
316
+ }
317
+ return nodeId;
318
+ }
319
+ /**
320
+ * Get edge weight between two nodes.
321
+ * @param source - The source node ID
322
+ * @param target - The target node ID
323
+ * @returns The edge weight, or undefined if the edge doesn't exist or has no weight
324
+ */
325
+ getEdgeWeight(source, target) {
326
+ if (!this.data.edgeWeights) {
327
+ return undefined;
328
+ }
329
+ const sourceIndex = this.data.nodeIdToIndex.get(source);
330
+ const targetIndex = this.data.nodeIdToIndex.get(target);
331
+ if (sourceIndex === undefined || targetIndex === undefined) {
332
+ return undefined;
333
+ }
334
+ const start = this.data.rowPointers[sourceIndex];
335
+ const end = this.data.rowPointers[sourceIndex + 1];
336
+ if (start === undefined || end === undefined) {
337
+ return undefined;
338
+ }
339
+ const edgeIndex = this.binarySearch(this.data.columnIndices, targetIndex, start, end);
340
+ if (edgeIndex === -1) {
341
+ return undefined;
342
+ }
343
+ return this.data.edgeWeights[edgeIndex];
344
+ }
345
+ /**
346
+ * Binary search for target in sorted array.
347
+ * @param arr - The typed array to search in
348
+ * @param target - The value to search for
349
+ * @param start - The starting index (inclusive)
350
+ * @param end - The ending index (exclusive)
351
+ * @returns The index of the target, or -1 if not found
352
+ */
353
+ binarySearch(arr, target, start, end) {
354
+ let left = start;
355
+ let right = end - 1;
356
+ while (left <= right) {
357
+ const mid = (left + right) >>> 1;
358
+ const value = arr[mid];
359
+ if (value === undefined) {
360
+ return -1;
361
+ }
362
+ if (value === target) {
363
+ return mid;
364
+ }
365
+ if (value < target) {
366
+ left = mid + 1;
367
+ }
368
+ else {
369
+ right = mid - 1;
370
+ }
371
+ }
372
+ return -1;
373
+ }
374
+ /**
375
+ * Create CSR graph from standard Graph interface.
376
+ * @param graph - The graph object to convert, must implement nodes, neighbors, hasNode, and optionally getEdge
377
+ * @param graph.nodes - Function that returns an iterator over node objects with id property
378
+ * @param graph.neighbors - Function that returns an iterator over neighbor node IDs
379
+ * @param graph.hasNode - Function that checks if a node exists in the graph
380
+ * @param graph.getEdge - Optional function that returns edge data including weight
381
+ * @returns A new CSRGraph instance
382
+ */
383
+ static fromGraph(graph) {
384
+ const adjacencyList = new Map();
385
+ const weights = new Map();
386
+ // Build adjacency list
387
+ for (const node of graph.nodes()) {
388
+ const neighbors = [];
389
+ for (const neighbor of graph.neighbors(node.id)) {
390
+ neighbors.push(neighbor);
391
+ // Get edge weight if available
392
+ if (graph.getEdge) {
393
+ const edge = graph.getEdge(node.id, neighbor);
394
+ if (edge?.weight !== undefined) {
395
+ weights.set(`${String(node.id)}-${String(neighbor)}`, edge.weight);
396
+ }
397
+ }
398
+ }
399
+ adjacencyList.set(node.id, neighbors);
400
+ }
401
+ return new CSRGraph(adjacencyList, weights.size > 0 ? weights : undefined);
402
+ }
403
+ }
404
+ //# sourceMappingURL=csr-graph.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"csr-graph.js","sourceRoot":"","sources":["../../../src/optimized/csr-graph.ts"],"names":[],"mappings":"AAqCA;;;;;GAKG;AACH,MAAM,OAAO,QAAQ;IAGjB;;;;;OAKG;IACH,YAAY,aAAsC,EAAE,OAA6B,EAAE,YAAY,GAAG,IAAI;QAClG,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;IACpE,CAAC;IAED;;;;;;OAMG;IACK,QAAQ,CACZ,aAAsC,EACtC,OAA6B,EAC7B,YAAY,GAAG,IAAI;QAEnB,sDAAsD;QACtD,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAW,CAAC;QACpC,KAAK,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI,aAAa,EAAE,CAAC;YAC9C,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACrB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;gBAC/B,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YAC3B,CAAC;QACL,CAAC;QAED,oEAAoE;QACpE,qEAAqE;QACrE,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;YAC7C,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;gBACjD,OAAO,CAAC,GAAG,CAAC,CAAC;YACjB,CAAC;YAED,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;QACH,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAA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ction-Optimized BFS
5
+ */
6
+ export interface DirectionOptimizedBFSOptions {
7
+ alpha?: number;
8
+ beta?: number;
9
+ }
10
+ /**
11
+ * Result of BFS traversal
12
+ */
13
+ export interface BFSResult<TNodeId> {
14
+ distances: Map<TNodeId, number>;
15
+ parents: Map<TNodeId, TNodeId | null>;
16
+ visitedCount: number;
17
+ }
18
+ /**
19
+ * Direction-Optimized Breadth-First Search
20
+ *
21
+ * Dynamically switches between top-down and bottom-up search strategies
22
+ * based on the size of the frontier. Particularly effective for low-diameter
23
+ * graphs like social networks.
24
+ *
25
+ * Based on: Beamer, S., Asanović, K., & Patterson, D. (2012).
26
+ * "Direction-optimizing breadth-first search." SC'12.
27
+ */
28
+ export declare class DirectionOptimizedBFS<TNodeId = NodeId> {
29
+ private graph;
30
+ private alpha;
31
+ private beta;
32
+ private parent;
33
+ private frontier;
34
+ private nextFrontier;
35
+ private distances;
36
+ /**
37
+ * Creates a new DirectionOptimizedBFS instance.
38
+ * @param graph - The CSR graph to perform BFS on
39
+ * @param options - Optional configuration for switching thresholds
40
+ */
41
+ constructor(graph: CSRGraph<TNodeId>, options?: DirectionOptimizedBFSOptions);
42
+ /**
43
+ * Perform BFS from a single source node.
44
+ * @param source - The source node ID to start the BFS from
45
+ * @returns The BFS result containing distances and parent pointers
46
+ */
47
+ search(source: TNodeId): BFSResult<TNodeId>;
48
+ /**
49
+ * Top-down BFS step - explore from frontier.
50
+ * @returns The scout count (total degree of newly discovered nodes)
51
+ */
52
+ private topDownStep;
53
+ /**
54
+ * Bottom-up BFS step - check unvisited nodes for frontier neighbors.
55
+ * @returns The number of newly discovered nodes
56
+ */
57
+ private bottomUpStep;
58
+ /**
59
+ * Calculate edges to check for switching heuristic.
60
+ * @returns The total number of outgoing edges from frontier nodes
61
+ */
62
+ private calculateEdgesToCheck;
63
+ /**
64
+ * Build result map from internal data structures.
65
+ * @returns The BFS result containing distances and parent pointers
66
+ */
67
+ private buildResult;
68
+ /**
69
+ * Perform multi-source BFS from multiple source nodes.
70
+ * @param sources - Array of source node IDs to start the BFS from
71
+ * @returns The BFS result containing distances and parent pointers
72
+ */
73
+ searchMultiple(sources: TNodeId[]): BFSResult<TNodeId>;
74
+ /**
75
+ * Reset internal state for reuse
76
+ */
77
+ reset(): void;
78
+ }
79
+ /**
80
+ * Convenience function for single-source BFS.
81
+ * @param graph - The CSR graph to perform BFS on
82
+ * @param source - The source node ID to start the BFS from
83
+ * @param options - Optional configuration for switching thresholds
84
+ * @returns The BFS result containing distances and parent pointers
85
+ */
86
+ export declare function directionOptimizedBFS<TNodeId = NodeId>(graph: CSRGraph<TNodeId>, source: TNodeId, options?: DirectionOptimizedBFSOptions): BFSResult<TNodeId>;
87
+ /**
88
+ * Convenience function for multi-source BFS.
89
+ * @param graph - The CSR graph to perform BFS on
90
+ * @param sources - Array of source node IDs to start the BFS from
91
+ * @param options - Optional configuration for switching thresholds
92
+ * @returns The BFS result containing distances and parent pointers
93
+ */
94
+ export declare function directionOptimizedBFSMultiple<TNodeId = NodeId>(graph: CSRGraph<TNodeId>, sources: TNodeId[], options?: DirectionOptimizedBFSOptions): BFSResult<TNodeId>;
95
+ //# sourceMappingURL=direction-optimized-bfs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"direction-optimized-bfs.d.ts","sourceRoot":"","sources":["../../../src/optimized/direction-optimized-bfs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;GAEG;AACH,MAAM,WAAW,4BAA4B;IACzC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,SAAS,CAAC,OAAO;IAC9B,SAAS,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,O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