@graphty/algorithms 1.3.1 → 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 (313) hide show
  1. package/README.md +358 -413
  2. package/dist/algorithms.js +564 -261
  3. package/dist/algorithms.js.map +1 -1
  4. package/dist/src/algorithms/centrality/betweenness.d.ts +10 -0
  5. package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -1
  6. package/dist/src/algorithms/centrality/betweenness.js +27 -6
  7. package/dist/src/algorithms/centrality/betweenness.js.map +1 -1
  8. package/dist/src/algorithms/centrality/closeness.d.ts +11 -2
  9. package/dist/src/algorithms/centrality/closeness.d.ts.map +1 -1
  10. package/dist/src/algorithms/centrality/closeness.js +17 -3
  11. package/dist/src/algorithms/centrality/closeness.js.map +1 -1
  12. package/dist/src/algorithms/centrality/degree.d.ts +7 -0
  13. package/dist/src/algorithms/centrality/degree.d.ts.map +1 -1
  14. package/dist/src/algorithms/centrality/degree.js +9 -6
  15. package/dist/src/algorithms/centrality/degree.js.map +1 -1
  16. package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts +21 -3
  17. package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts.map +1 -1
  18. package/dist/src/algorithms/centrality/delta-pagerank-simple.js +25 -7
  19. package/dist/src/algorithms/centrality/delta-pagerank-simple.js.map +1 -1
  20. package/dist/src/algorithms/centrality/delta-pagerank.d.ts +28 -2
  21. package/dist/src/algorithms/centrality/delta-pagerank.d.ts.map +1 -1
  22. package/dist/src/algorithms/centrality/delta-pagerank.js +30 -4
  23. package/dist/src/algorithms/centrality/delta-pagerank.js.map +1 -1
  24. package/dist/src/algorithms/centrality/eigenvector.d.ts +10 -3
  25. package/dist/src/algorithms/centrality/eigenvector.d.ts.map +1 -1
  26. package/dist/src/algorithms/centrality/eigenvector.js +11 -4
  27. package/dist/src/algorithms/centrality/eigenvector.js.map +1 -1
  28. package/dist/src/algorithms/centrality/hits.d.ts +10 -3
  29. package/dist/src/algorithms/centrality/hits.d.ts.map +1 -1
  30. package/dist/src/algorithms/centrality/hits.js +11 -4
  31. package/dist/src/algorithms/centrality/hits.js.map +1 -1
  32. package/dist/src/algorithms/centrality/index.d.ts +1 -1
  33. package/dist/src/algorithms/centrality/index.d.ts.map +1 -1
  34. package/dist/src/algorithms/centrality/index.js +1 -1
  35. package/dist/src/algorithms/centrality/index.js.map +1 -1
  36. package/dist/src/algorithms/centrality/katz.d.ts +10 -3
  37. package/dist/src/algorithms/centrality/katz.d.ts.map +1 -1
  38. package/dist/src/algorithms/centrality/katz.js +13 -8
  39. package/dist/src/algorithms/centrality/katz.js.map +1 -1
  40. package/dist/src/algorithms/centrality/pagerank.d.ts +14 -0
  41. package/dist/src/algorithms/centrality/pagerank.d.ts.map +1 -1
  42. package/dist/src/algorithms/centrality/pagerank.js +16 -1
  43. package/dist/src/algorithms/centrality/pagerank.js.map +1 -1
  44. package/dist/src/algorithms/community/girvan-newman.d.ts +2 -3
  45. package/dist/src/algorithms/community/girvan-newman.d.ts.map +1 -1
  46. package/dist/src/algorithms/community/girvan-newman.js +21 -4
  47. package/dist/src/algorithms/community/girvan-newman.js.map +1 -1
  48. package/dist/src/algorithms/community/index.d.ts.map +1 -1
  49. package/dist/src/algorithms/community/index.js.map +1 -1
  50. package/dist/src/algorithms/community/label-propagation.d.ts +0 -3
  51. package/dist/src/algorithms/community/label-propagation.d.ts.map +1 -1
  52. package/dist/src/algorithms/community/label-propagation.js +13 -6
  53. package/dist/src/algorithms/community/label-propagation.js.map +1 -1
  54. package/dist/src/algorithms/community/leiden.d.ts +0 -1
  55. package/dist/src/algorithms/community/leiden.d.ts.map +1 -1
  56. package/dist/src/algorithms/community/leiden.js +37 -6
  57. package/dist/src/algorithms/community/leiden.js.map +1 -1
  58. package/dist/src/algorithms/community/louvain-optimized.d.ts +37 -0
  59. package/dist/src/algorithms/community/louvain-optimized.d.ts.map +1 -1
  60. package/dist/src/algorithms/community/louvain-optimized.js +42 -7
  61. package/dist/src/algorithms/community/louvain-optimized.js.map +1 -1
  62. package/dist/src/algorithms/community/louvain.d.ts +2 -3
  63. package/dist/src/algorithms/community/louvain.d.ts.map +1 -1
  64. package/dist/src/algorithms/community/louvain.js +19 -6
  65. package/dist/src/algorithms/community/louvain.js.map +1 -1
  66. package/dist/src/algorithms/community/modularity-utils.d.ts +0 -4
  67. package/dist/src/algorithms/community/modularity-utils.d.ts.map +1 -1
  68. package/dist/src/algorithms/community/modularity-utils.js +1 -5
  69. package/dist/src/algorithms/community/modularity-utils.js.map +1 -1
  70. package/dist/src/algorithms/components/connected.d.ts +23 -0
  71. package/dist/src/algorithms/components/connected.d.ts.map +1 -1
  72. package/dist/src/algorithms/components/connected.js +29 -3
  73. package/dist/src/algorithms/components/connected.js.map +1 -1
  74. package/dist/src/algorithms/matching/bipartite.d.ts +10 -0
  75. package/dist/src/algorithms/matching/bipartite.d.ts.map +1 -1
  76. package/dist/src/algorithms/matching/bipartite.js +10 -0
  77. package/dist/src/algorithms/matching/bipartite.js.map +1 -1
  78. package/dist/src/algorithms/matching/index.d.ts.map +1 -1
  79. package/dist/src/algorithms/matching/index.js.map +1 -1
  80. package/dist/src/algorithms/matching/isomorphism.d.ts +8 -0
  81. package/dist/src/algorithms/matching/isomorphism.d.ts.map +1 -1
  82. package/dist/src/algorithms/matching/isomorphism.js +36 -2
  83. package/dist/src/algorithms/matching/isomorphism.js.map +1 -1
  84. package/dist/src/algorithms/mst/index.d.ts.map +1 -1
  85. package/dist/src/algorithms/mst/index.js.map +1 -1
  86. package/dist/src/algorithms/mst/kruskal.d.ts +14 -0
  87. package/dist/src/algorithms/mst/kruskal.d.ts.map +1 -1
  88. package/dist/src/algorithms/mst/kruskal.js +17 -3
  89. package/dist/src/algorithms/mst/kruskal.js.map +1 -1
  90. package/dist/src/algorithms/mst/prim.d.ts +9 -0
  91. package/dist/src/algorithms/mst/prim.d.ts.map +1 -1
  92. package/dist/src/algorithms/mst/prim.js +9 -0
  93. package/dist/src/algorithms/mst/prim.js.map +1 -1
  94. package/dist/src/algorithms/shortest-path/bellman-ford.d.ts +10 -0
  95. package/dist/src/algorithms/shortest-path/bellman-ford.d.ts.map +1 -1
  96. package/dist/src/algorithms/shortest-path/bellman-ford.js +10 -0
  97. package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -1
  98. package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts +11 -4
  99. package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts.map +1 -1
  100. package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js +18 -16
  101. package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js.map +1 -1
  102. package/dist/src/algorithms/shortest-path/dijkstra.d.ts +15 -0
  103. package/dist/src/algorithms/shortest-path/dijkstra.d.ts.map +1 -1
  104. package/dist/src/algorithms/shortest-path/dijkstra.js +15 -0
  105. package/dist/src/algorithms/shortest-path/dijkstra.js.map +1 -1
  106. package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts +17 -0
  107. package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts.map +1 -1
  108. package/dist/src/algorithms/shortest-path/floyd-warshall.js +17 -0
  109. package/dist/src/algorithms/shortest-path/floyd-warshall.js.map +1 -1
  110. package/dist/src/algorithms/shortest-path/index.d.ts.map +1 -1
  111. package/dist/src/algorithms/shortest-path/index.js.map +1 -1
  112. package/dist/src/algorithms/traversal/bfs-unified.d.ts +13 -0
  113. package/dist/src/algorithms/traversal/bfs-unified.d.ts.map +1 -1
  114. package/dist/src/algorithms/traversal/bfs-unified.js +37 -0
  115. package/dist/src/algorithms/traversal/bfs-unified.js.map +1 -1
  116. package/dist/src/algorithms/traversal/bfs-variants.d.ts +23 -0
  117. package/dist/src/algorithms/traversal/bfs-variants.d.ts.map +1 -1
  118. package/dist/src/algorithms/traversal/bfs-variants.js +34 -0
  119. package/dist/src/algorithms/traversal/bfs-variants.js.map +1 -1
  120. package/dist/src/algorithms/traversal/bfs.d.ts +1 -1
  121. package/dist/src/algorithms/traversal/bfs.d.ts.map +1 -1
  122. package/dist/src/algorithms/traversal/bfs.js +1 -1
  123. package/dist/src/algorithms/traversal/bfs.js.map +1 -1
  124. package/dist/src/algorithms/traversal/dfs.d.ts +10 -0
  125. package/dist/src/algorithms/traversal/dfs.d.ts.map +1 -1
  126. package/dist/src/algorithms/traversal/dfs.js +48 -0
  127. package/dist/src/algorithms/traversal/dfs.js.map +1 -1
  128. package/dist/src/algorithms/traversal/index.d.ts.map +1 -1
  129. package/dist/src/algorithms/traversal/index.js.map +1 -1
  130. package/dist/src/benchmark-all-algorithms.d.ts +2 -0
  131. package/dist/src/benchmark-all-algorithms.d.ts.map +1 -1
  132. package/dist/src/benchmark-all-algorithms.js +32 -3
  133. package/dist/src/benchmark-all-algorithms.js.map +1 -1
  134. package/dist/src/clustering/hierarchical.d.ts +8 -1
  135. package/dist/src/clustering/hierarchical.d.ts.map +1 -1
  136. package/dist/src/clustering/hierarchical.js +22 -6
  137. package/dist/src/clustering/hierarchical.js.map +1 -1
  138. package/dist/src/clustering/index.d.ts.map +1 -1
  139. package/dist/src/clustering/index.js.map +1 -1
  140. package/dist/src/clustering/k-core.d.ts +2 -5
  141. package/dist/src/clustering/k-core.d.ts.map +1 -1
  142. package/dist/src/clustering/k-core.js +14 -12
  143. package/dist/src/clustering/k-core.js.map +1 -1
  144. package/dist/src/clustering/mcl.d.ts +6 -0
  145. package/dist/src/clustering/mcl.d.ts.map +1 -1
  146. package/dist/src/clustering/mcl.js +34 -3
  147. package/dist/src/clustering/mcl.js.map +1 -1
  148. package/dist/src/clustering/spectral.d.ts +4 -1
  149. package/dist/src/clustering/spectral.d.ts.map +1 -1
  150. package/dist/src/clustering/spectral.js +61 -28
  151. package/dist/src/clustering/spectral.js.map +1 -1
  152. package/dist/src/core/graph.d.ts +45 -0
  153. package/dist/src/core/graph.d.ts.map +1 -1
  154. package/dist/src/core/graph.js +45 -1
  155. package/dist/src/core/graph.js.map +1 -1
  156. package/dist/src/data-structures/index.d.ts.map +1 -1
  157. package/dist/src/data-structures/index.js.map +1 -1
  158. package/dist/src/data-structures/priority-queue.d.ts +18 -1
  159. package/dist/src/data-structures/priority-queue.d.ts.map +1 -1
  160. package/dist/src/data-structures/priority-queue.js +26 -5
  161. package/dist/src/data-structures/priority-queue.js.map +1 -1
  162. package/dist/src/data-structures/union-find.d.ts +21 -0
  163. package/dist/src/data-structures/union-find.d.ts.map +1 -1
  164. package/dist/src/data-structures/union-find.js +21 -0
  165. package/dist/src/data-structures/union-find.js.map +1 -1
  166. package/dist/src/flow/ford-fulkerson.d.ts +4 -2
  167. package/dist/src/flow/ford-fulkerson.d.ts.map +1 -1
  168. package/dist/src/flow/ford-fulkerson.js +31 -2
  169. package/dist/src/flow/ford-fulkerson.js.map +1 -1
  170. package/dist/src/flow/min-cut.d.ts +0 -3
  171. package/dist/src/flow/min-cut.d.ts.map +1 -1
  172. package/dist/src/flow/min-cut.js +17 -4
  173. package/dist/src/flow/min-cut.js.map +1 -1
  174. package/dist/src/index.d.ts +2 -1
  175. package/dist/src/index.d.ts.map +1 -1
  176. package/dist/src/index.js +2 -1
  177. package/dist/src/index.js.map +1 -1
  178. package/dist/src/link-prediction/adamic-adar.d.ts +26 -0
  179. package/dist/src/link-prediction/adamic-adar.d.ts.map +1 -1
  180. package/dist/src/link-prediction/adamic-adar.js +36 -10
  181. package/dist/src/link-prediction/adamic-adar.js.map +1 -1
  182. package/dist/src/link-prediction/common-neighbors.d.ts +21 -0
  183. package/dist/src/link-prediction/common-neighbors.d.ts.map +1 -1
  184. package/dist/src/link-prediction/common-neighbors.js +27 -6
  185. package/dist/src/link-prediction/common-neighbors.js.map +1 -1
  186. package/dist/src/link-prediction/index.d.ts.map +1 -1
  187. package/dist/src/optimized/bit-packed.d.ts +103 -53
  188. package/dist/src/optimized/bit-packed.d.ts.map +1 -1
  189. package/dist/src/optimized/bit-packed.js +104 -54
  190. package/dist/src/optimized/bit-packed.js.map +1 -1
  191. package/dist/src/optimized/csr-graph.d.ts +87 -24
  192. package/dist/src/optimized/csr-graph.d.ts.map +1 -1
  193. package/dist/src/optimized/csr-graph.js +87 -24
  194. package/dist/src/optimized/csr-graph.js.map +1 -1
  195. package/dist/src/optimized/direction-optimized-bfs.d.ts +37 -16
  196. package/dist/src/optimized/direction-optimized-bfs.d.ts.map +1 -1
  197. package/dist/src/optimized/direction-optimized-bfs.js +39 -20
  198. package/dist/src/optimized/direction-optimized-bfs.js.map +1 -1
  199. package/dist/src/optimized/graph-adapter.d.ts +58 -9
  200. package/dist/src/optimized/graph-adapter.d.ts.map +1 -1
  201. package/dist/src/optimized/graph-adapter.js +58 -10
  202. package/dist/src/optimized/graph-adapter.js.map +1 -1
  203. package/dist/src/optimized/index.d.ts +1 -1
  204. package/dist/src/optimized/index.d.ts.map +1 -1
  205. package/dist/src/optimized/index.js +3 -1
  206. package/dist/src/optimized/index.js.map +1 -1
  207. package/dist/src/pathfinding/astar.d.ts +25 -9
  208. package/dist/src/pathfinding/astar.d.ts.map +1 -1
  209. package/dist/src/pathfinding/astar.js +25 -10
  210. package/dist/src/pathfinding/astar.js.map +1 -1
  211. package/dist/src/pathfinding/utils.d.ts +15 -6
  212. package/dist/src/pathfinding/utils.d.ts.map +1 -1
  213. package/dist/src/pathfinding/utils.js +19 -7
  214. package/dist/src/pathfinding/utils.js.map +1 -1
  215. package/dist/src/research/grsbm.d.ts +0 -1
  216. package/dist/src/research/grsbm.d.ts.map +1 -1
  217. package/dist/src/research/grsbm.js +22 -9
  218. package/dist/src/research/grsbm.js.map +1 -1
  219. package/dist/src/research/index.d.ts.map +1 -1
  220. package/dist/src/research/index.js.map +1 -1
  221. package/dist/src/research/sync.d.ts +0 -1
  222. package/dist/src/research/sync.d.ts.map +1 -1
  223. package/dist/src/research/sync.js +28 -7
  224. package/dist/src/research/sync.js.map +1 -1
  225. package/dist/src/research/terahac.d.ts +0 -1
  226. package/dist/src/research/terahac.d.ts.map +1 -1
  227. package/dist/src/research/terahac.js +32 -4
  228. package/dist/src/research/terahac.js.map +1 -1
  229. package/dist/src/utils/graph-converters.d.ts +31 -0
  230. package/dist/src/utils/graph-converters.d.ts.map +1 -1
  231. package/dist/src/utils/graph-converters.js +31 -0
  232. package/dist/src/utils/graph-converters.js.map +1 -1
  233. package/dist/src/utils/graph-utilities.d.ts.map +1 -1
  234. package/dist/src/utils/graph-utilities.js.map +1 -1
  235. package/dist/src/utils/math-utilities.d.ts +11 -0
  236. package/dist/src/utils/math-utilities.d.ts.map +1 -1
  237. package/dist/src/utils/math-utilities.js +14 -5
  238. package/dist/src/utils/math-utilities.js.map +1 -1
  239. package/dist/src/utils/optimization-helpers.d.ts +9 -0
  240. package/dist/src/utils/optimization-helpers.d.ts.map +1 -1
  241. package/dist/src/utils/optimization-helpers.js +12 -0
  242. package/dist/src/utils/optimization-helpers.js.map +1 -1
  243. package/dist/src/utils/priorityQueue.d.ts +24 -0
  244. package/dist/src/utils/priorityQueue.d.ts.map +1 -1
  245. package/dist/src/utils/priorityQueue.js +34 -4
  246. package/dist/src/utils/priorityQueue.js.map +1 -1
  247. package/package.json +49 -45
  248. package/src/algorithms/centrality/betweenness.ts +34 -17
  249. package/src/algorithms/centrality/closeness.ts +34 -17
  250. package/src/algorithms/centrality/degree.ts +15 -19
  251. package/src/algorithms/centrality/delta-pagerank-simple.ts +36 -28
  252. package/src/algorithms/centrality/delta-pagerank.ts +34 -11
  253. package/src/algorithms/centrality/eigenvector.ts +14 -15
  254. package/src/algorithms/centrality/hits.ts +17 -17
  255. package/src/algorithms/centrality/index.ts +20 -15
  256. package/src/algorithms/centrality/katz.ts +17 -25
  257. package/src/algorithms/centrality/pagerank.ts +26 -17
  258. package/src/algorithms/community/girvan-newman.ts +39 -28
  259. package/src/algorithms/community/index.ts +6 -7
  260. package/src/algorithms/community/label-propagation.ts +20 -28
  261. package/src/algorithms/community/leiden.ts +61 -37
  262. package/src/algorithms/community/louvain-optimized.ts +49 -20
  263. package/src/algorithms/community/louvain.ts +35 -25
  264. package/src/algorithms/community/modularity-utils.ts +5 -17
  265. package/src/algorithms/components/connected.ts +40 -17
  266. package/src/algorithms/matching/bipartite.ts +17 -10
  267. package/src/algorithms/matching/index.ts +4 -4
  268. package/src/algorithms/matching/isomorphism.ts +53 -23
  269. package/src/algorithms/mst/index.ts +3 -4
  270. package/src/algorithms/mst/kruskal.ts +21 -6
  271. package/src/algorithms/mst/prim.ts +13 -4
  272. package/src/algorithms/shortest-path/bellman-ford.ts +16 -15
  273. package/src/algorithms/shortest-path/bidirectional-dijkstra.ts +28 -28
  274. package/src/algorithms/shortest-path/dijkstra.ts +23 -17
  275. package/src/algorithms/shortest-path/floyd-warshall.ts +21 -4
  276. package/src/algorithms/shortest-path/index.ts +5 -5
  277. package/src/algorithms/traversal/bfs-unified.ts +64 -60
  278. package/src/algorithms/traversal/bfs-variants.ts +61 -37
  279. package/src/algorithms/traversal/bfs.ts +1 -7
  280. package/src/algorithms/traversal/dfs.ts +61 -42
  281. package/src/algorithms/traversal/index.ts +3 -3
  282. package/src/benchmark-all-algorithms.ts +57 -25
  283. package/src/clustering/hierarchical.ts +40 -34
  284. package/src/clustering/index.ts +8 -8
  285. package/src/clustering/k-core.ts +26 -42
  286. package/src/clustering/mcl.ts +53 -23
  287. package/src/clustering/spectral.ts +91 -57
  288. package/src/core/graph.ts +54 -14
  289. package/src/data-structures/index.ts +2 -2
  290. package/src/data-structures/priority-queue.ts +36 -11
  291. package/src/data-structures/union-find.ts +22 -1
  292. package/src/flow/ford-fulkerson.ts +62 -35
  293. package/src/flow/min-cut.ts +43 -45
  294. package/src/index.ts +3 -2
  295. package/src/link-prediction/adamic-adar.ts +65 -51
  296. package/src/link-prediction/common-neighbors.ts +45 -36
  297. package/src/link-prediction/index.ts +1 -1
  298. package/src/optimized/bit-packed.ts +106 -56
  299. package/src/optimized/csr-graph.ts +93 -30
  300. package/src/optimized/direction-optimized-bfs.ts +44 -25
  301. package/src/optimized/graph-adapter.ts +64 -16
  302. package/src/optimized/index.ts +7 -9
  303. package/src/pathfinding/astar.ts +40 -25
  304. package/src/pathfinding/utils.ts +20 -13
  305. package/src/research/grsbm.ts +30 -24
  306. package/src/research/index.ts +4 -4
  307. package/src/research/sync.ts +39 -23
  308. package/src/research/terahac.ts +54 -20
  309. package/src/utils/graph-converters.ts +42 -14
  310. package/src/utils/graph-utilities.ts +11 -37
  311. package/src/utils/math-utilities.ts +17 -8
  312. package/src/utils/optimization-helpers.ts +27 -21
  313. package/src/utils/priorityQueue.ts +40 -6
@@ -1,9 +1,11 @@
1
- import {Graph} from "../core/graph.js";
2
- import type {NodeId} from "../types/index.js";
1
+ import { Graph } from "../core/graph.js";
2
+ import type { NodeId } from "../types/index.js";
3
3
 
4
4
  /**
5
5
  * Convert Graph class to Map representation
6
6
  * Used by community detection and flow algorithms
7
+ * @param graph - The Graph instance to convert
8
+ * @returns A nested Map where outer keys are source nodes and inner maps contain target nodes with edge weights
7
9
  */
8
10
  export function graphToMap(graph: Graph): Map<string, Map<string, number>> {
9
11
  const map = new Map<string, Map<string, number>>();
@@ -38,12 +40,12 @@ export function graphToMap(graph: Graph): Map<string, Map<string, number>> {
38
40
 
39
41
  /**
40
42
  * Convert Map representation to Graph class
43
+ * @param map - The nested Map representation of the graph
44
+ * @param isDirected - Whether the resulting graph should be directed (defaults to true)
45
+ * @returns A new Graph instance with nodes and edges from the map
41
46
  */
42
- export function mapToGraph(
43
- map: Map<string, Map<string, number>>,
44
- isDirected = true,
45
- ): Graph {
46
- const graph = new Graph({directed: isDirected});
47
+ export function mapToGraph(map: Map<string, Map<string, number>>, isDirected = true): Graph {
48
+ const graph = new Graph({ directed: isDirected });
47
49
 
48
50
  // Add all nodes first
49
51
  for (const node of map.keys()) {
@@ -78,6 +80,11 @@ export class GraphAdapter {
78
80
  private map: Map<string, Map<string, number>>;
79
81
  public readonly isDirected: boolean;
80
82
 
83
+ /**
84
+ * Creates a new GraphAdapter instance wrapping a Map representation.
85
+ * @param map - The nested Map representation of the graph
86
+ * @param isDirected - Whether the graph should be treated as directed (defaults to true)
87
+ */
81
88
  constructor(map: Map<string, Map<string, number>>, isDirected = true) {
82
89
  this.map = map;
83
90
  this.isDirected = isDirected;
@@ -85,13 +92,15 @@ export class GraphAdapter {
85
92
 
86
93
  /**
87
94
  * Get all nodes
95
+ * @returns Array of node objects with their ids
88
96
  */
89
- nodes(): {id: NodeId}[] {
90
- return Array.from(this.map.keys()).map((id) => ({id}));
97
+ nodes(): { id: NodeId }[] {
98
+ return Array.from(this.map.keys()).map((id) => ({ id }));
91
99
  }
92
100
 
93
101
  /**
94
102
  * Get node count
103
+ * @returns The total number of nodes in the graph
95
104
  */
96
105
  get nodeCount(): number {
97
106
  return this.map.size;
@@ -99,6 +108,8 @@ export class GraphAdapter {
99
108
 
100
109
  /**
101
110
  * Check if node exists
111
+ * @param nodeId - The id of the node to check
112
+ * @returns True if the node exists, false otherwise
102
113
  */
103
114
  hasNode(nodeId: NodeId): boolean {
104
115
  return this.map.has(String(nodeId));
@@ -106,6 +117,8 @@ export class GraphAdapter {
106
117
 
107
118
  /**
108
119
  * Get neighbors of a node
120
+ * @param nodeId - The id of the node to get neighbors for
121
+ * @returns Array of neighbor node ids
109
122
  */
110
123
  neighbors(nodeId: NodeId): NodeId[] {
111
124
  const neighbors = this.map.get(String(nodeId));
@@ -118,8 +131,11 @@ export class GraphAdapter {
118
131
 
119
132
  /**
120
133
  * Get edge between two nodes
134
+ * @param source - The source node id
135
+ * @param target - The target node id
136
+ * @returns The edge object with source, target and weight, or null if no edge exists
121
137
  */
122
- getEdge(source: NodeId, target: NodeId): {source: NodeId, target: NodeId, weight?: number} | null {
138
+ getEdge(source: NodeId, target: NodeId): { source: NodeId; target: NodeId; weight?: number } | null {
123
139
  const sourceNeighbors = this.map.get(String(source));
124
140
  if (!sourceNeighbors) {
125
141
  return null;
@@ -130,11 +146,14 @@ export class GraphAdapter {
130
146
  return null;
131
147
  }
132
148
 
133
- return {source, target, weight};
149
+ return { source, target, weight };
134
150
  }
135
151
 
136
152
  /**
137
153
  * Check if edge exists
154
+ * @param source - The source node id
155
+ * @param target - The target node id
156
+ * @returns True if an edge exists between source and target, false otherwise
138
157
  */
139
158
  hasEdge(source: NodeId, target: NodeId): boolean {
140
159
  const sourceNeighbors = this.map.get(String(source));
@@ -147,9 +166,10 @@ export class GraphAdapter {
147
166
 
148
167
  /**
149
168
  * Get all edges
169
+ * @returns Array of edge objects with source, target and optional weight
150
170
  */
151
- edges(): {source: NodeId, target: NodeId, weight?: number}[] {
152
- const edges: {source: NodeId, target: NodeId, weight?: number}[] = [];
171
+ edges(): { source: NodeId; target: NodeId; weight?: number }[] {
172
+ const edges: { source: NodeId; target: NodeId; weight?: number }[] = [];
153
173
  const processedEdges = new Set<string>();
154
174
 
155
175
  for (const [source, neighbors] of this.map) {
@@ -161,7 +181,7 @@ export class GraphAdapter {
161
181
  continue;
162
182
  }
163
183
 
164
- edges.push({source, target, weight});
184
+ edges.push({ source, target, weight });
165
185
  processedEdges.add(edgeKey);
166
186
  }
167
187
  }
@@ -171,6 +191,7 @@ export class GraphAdapter {
171
191
 
172
192
  /**
173
193
  * Get edge count
194
+ * @returns The total number of edges in the graph
174
195
  */
175
196
  get edgeCount(): number {
176
197
  if (this.isDirected) {
@@ -199,6 +220,8 @@ export class GraphAdapter {
199
220
 
200
221
  /**
201
222
  * Get node degree
223
+ * @param nodeId - The id of the node to get degree for
224
+ * @returns The number of edges connected to the node
202
225
  */
203
226
  degree(nodeId: NodeId): number {
204
227
  const neighbors = this.map.get(String(nodeId));
@@ -207,6 +230,8 @@ export class GraphAdapter {
207
230
 
208
231
  /**
209
232
  * Get in-degree (for directed graphs)
233
+ * @param nodeId - The id of the node to get in-degree for
234
+ * @returns The number of incoming edges to the node
210
235
  */
211
236
  inDegree(nodeId: NodeId): number {
212
237
  if (!this.isDirected) {
@@ -227,6 +252,8 @@ export class GraphAdapter {
227
252
 
228
253
  /**
229
254
  * Get out-degree (for directed graphs)
255
+ * @param nodeId - The id of the node to get out-degree for
256
+ * @returns The number of outgoing edges from the node
230
257
  */
231
258
  outDegree(nodeId: NodeId): number {
232
259
  return this.degree(nodeId);
@@ -234,6 +261,7 @@ export class GraphAdapter {
234
261
 
235
262
  /**
236
263
  * Get underlying map representation
264
+ * @returns The internal Map representation of the graph
237
265
  */
238
266
  getMap(): Map<string, Map<string, number>> {
239
267
  return this.map;
@@ -1,5 +1,5 @@
1
- import {Graph} from "../core/graph.js";
2
- import type {NodeId} from "../types/index.js";
1
+ import { Graph } from "../core/graph.js";
2
+ import type { NodeId } from "../types/index.js";
3
3
 
4
4
  /**
5
5
  * Reconstructs a path from source to target using a predecessor map
@@ -7,10 +7,7 @@ import type {NodeId} from "../types/index.js";
7
7
  * @param predecessor - Map of node to its predecessor in the path
8
8
  * @returns Array of nodes from source to target, or empty array if no path exists
9
9
  */
10
- export function reconstructPath<T>(
11
- target: T,
12
- predecessor: Map<T, T | null>,
13
- ): T[] {
10
+ export function reconstructPath<T>(target: T, predecessor: Map<T, T | null>): T[] {
14
11
  const path: T[] = [];
15
12
  let current: T | null = target;
16
13
 
@@ -37,18 +34,9 @@ export function reconstructPath<T>(
37
34
  * @param directed - Whether to consider edge direction
38
35
  * @returns Set of common neighbor node IDs
39
36
  */
40
- export function getCommonNeighbors(
41
- graph: Graph,
42
- source: NodeId,
43
- target: NodeId,
44
- directed = false,
45
- ): Set<NodeId> {
46
- const sourceNeighbors = new Set(
47
- directed ? graph.outNeighbors(source) : graph.neighbors(source),
48
- );
49
- const targetNeighbors = new Set(
50
- directed ? graph.outNeighbors(target) : graph.neighbors(target),
51
- );
37
+ export function getCommonNeighbors(graph: Graph, source: NodeId, target: NodeId, directed = false): Set<NodeId> {
38
+ const sourceNeighbors = new Set(directed ? graph.outNeighbors(source) : graph.neighbors(source));
39
+ const targetNeighbors = new Set(directed ? graph.outNeighbors(target) : graph.neighbors(target));
52
40
 
53
41
  const common = new Set<NodeId>();
54
42
  for (const neighbor of sourceNeighbors) {
@@ -68,11 +56,7 @@ export function getCommonNeighbors(
68
56
  * @param target - Second node
69
57
  * @returns Set of intermediate node IDs
70
58
  */
71
- export function getIntermediateNodes(
72
- graph: Graph,
73
- source: NodeId,
74
- target: NodeId,
75
- ): Set<NodeId> {
59
+ export function getIntermediateNodes(graph: Graph, source: NodeId, target: NodeId): Set<NodeId> {
76
60
  const sourceOutNeighbors = new Set(graph.outNeighbors(source));
77
61
  const targetInNeighbors = new Set(graph.inNeighbors(target));
78
62
 
@@ -93,11 +77,7 @@ export function getIntermediateNodes(
93
77
  * @param isDirected - Whether the graph is directed
94
78
  * @returns A consistent edge identifier
95
79
  */
96
- export function getEdgeKey(
97
- source: NodeId,
98
- target: NodeId,
99
- isDirected: boolean,
100
- ): string {
80
+ export function getEdgeKey(source: NodeId, target: NodeId, isDirected: boolean): string {
101
81
  if (isDirected) {
102
82
  return `${String(source)}->${String(target)}`;
103
83
  }
@@ -127,11 +107,7 @@ export function getTotalEdgeWeight(graph: Graph): number {
127
107
  * @param mode - Degree mode for directed graphs
128
108
  * @returns Node degree
129
109
  */
130
- export function getNodeDegree(
131
- graph: Graph,
132
- nodeId: NodeId,
133
- mode: "in" | "out" | "total" = "total",
134
- ): number {
110
+ export function getNodeDegree(graph: Graph, nodeId: NodeId, mode: "in" | "out" | "total" = "total"): number {
135
111
  if (!graph.isDirected || mode === "total") {
136
112
  return graph.degree(nodeId);
137
113
  }
@@ -153,7 +129,7 @@ export function makeUndirected(graph: Graph): Graph {
153
129
  return graph;
154
130
  }
155
131
 
156
- const undirected = new Graph({directed: false});
132
+ const undirected = new Graph({ directed: false });
157
133
 
158
134
  // Add all nodes
159
135
  for (const node of graph.nodes()) {
@@ -176,9 +152,7 @@ export function makeUndirected(graph: Graph): Graph {
176
152
  * @param communities - Map of node to community ID
177
153
  * @returns Map with renumbered community IDs
178
154
  */
179
- export function renumberCommunities<T>(
180
- communities: Map<T, number>,
181
- ): Map<T, number> {
155
+ export function renumberCommunities<T>(communities: Map<T, number>): Map<T, number> {
182
156
  const uniqueCommunities = new Set(communities.values());
183
157
  const remapping = new Map<number, number>();
184
158
  let newId = 0;
@@ -7,6 +7,10 @@ export class SeededRandom {
7
7
  private readonly a = 1103515245;
8
8
  private readonly c = 12345;
9
9
 
10
+ /**
11
+ * Creates a new SeededRandom instance with the given seed.
12
+ * @param seed - The seed value for reproducible random number generation
13
+ */
10
14
  constructor(seed: number) {
11
15
  // Handle negative seeds correctly
12
16
  this.seed = ((seed % this.m) + this.m) % this.m;
@@ -14,14 +18,17 @@ export class SeededRandom {
14
18
 
15
19
  /**
16
20
  * Generate next random number between 0 and 1
21
+ * @returns A pseudo-random number in the range [0, 1]
17
22
  */
18
23
  next(): number {
19
- this.seed = ((this.a * this.seed) + this.c) % this.m;
24
+ this.seed = (this.a * this.seed + this.c) % this.m;
20
25
  return this.seed / (this.m - 1);
21
26
  }
22
27
 
23
28
  /**
24
29
  * Create a generator function for backward compatibility
30
+ * @param seed - The seed value for reproducible random number generation
31
+ * @returns A function that returns the next random number when called
25
32
  */
26
33
  static createGenerator(seed: number): () => number {
27
34
  const rng = new SeededRandom(seed);
@@ -75,11 +82,12 @@ export function euclideanDistance(a: number[], b: number[]): number {
75
82
  export const normalize = {
76
83
  /**
77
84
  * Min-max normalization to [0, 1] range
85
+ * @param values - Map of values to normalize in place
78
86
  */
79
87
  minMax(values: Map<string, number>): void {
80
88
  const vals = Array.from(values.values());
81
- const min = Math.min(... vals);
82
- const max = Math.max(... vals);
89
+ const min = Math.min(...vals);
90
+ const max = Math.max(...vals);
83
91
  const range = max - min;
84
92
 
85
93
  if (range === 0) {
@@ -93,9 +101,10 @@ export const normalize = {
93
101
 
94
102
  /**
95
103
  * Normalize by maximum value
104
+ * @param values - Map of values to normalize in place
96
105
  */
97
106
  byMax(values: Map<string, number>): void {
98
- const max = Math.max(... values.values());
107
+ const max = Math.max(...values.values());
99
108
  if (max === 0) {
100
109
  return;
101
110
  }
@@ -107,10 +116,10 @@ export const normalize = {
107
116
 
108
117
  /**
109
118
  * L2 (Euclidean) normalization
119
+ * @param values - Map of values to normalize in place
110
120
  */
111
121
  l2Norm(values: Map<string, number>): void {
112
- const sumSquares = Array.from(values.values())
113
- .reduce((sum, val) => sum + (val * val), 0);
122
+ const sumSquares = Array.from(values.values()).reduce((sum, val) => sum + val * val, 0);
114
123
  const norm = Math.sqrt(sumSquares);
115
124
 
116
125
  if (norm === 0) {
@@ -124,10 +133,10 @@ export const normalize = {
124
133
 
125
134
  /**
126
135
  * Normalize to sum to 1
136
+ * @param values - Map of values to normalize in place
127
137
  */
128
138
  sumToOne(values: Map<string, number>): void {
129
- const sum = Array.from(values.values())
130
- .reduce((acc, val) => acc + val, 0);
139
+ const sum = Array.from(values.values()).reduce((acc, val) => acc + val, 0);
131
140
 
132
141
  if (sum === 0) {
133
142
  return;
@@ -1,4 +1,4 @@
1
- import type {Graph} from "../core/graph.js";
1
+ import type { Graph } from "../core/graph.js";
2
2
 
3
3
  /**
4
4
  * Common interface for algorithm options with optimization support
@@ -15,6 +15,9 @@ export interface OptimizableOptions {
15
15
 
16
16
  /**
17
17
  * Check if a graph should use optimizations based on its size
18
+ * @param nodeCount - The number of nodes in the graph
19
+ * @param edgeCount - The number of edges in the graph
20
+ * @returns True if the graph is large enough to benefit from optimizations
18
21
  */
19
22
  function shouldUseOptimizations(nodeCount: number, edgeCount: number): boolean {
20
23
  // Use optimizations for graphs with more than 10k nodes or 100k edges
@@ -23,12 +26,12 @@ function shouldUseOptimizations(nodeCount: number, edgeCount: number): boolean {
23
26
 
24
27
  /**
25
28
  * Determine if optimizations should be used for a given graph
29
+ * @param graph - The graph to check for optimization eligibility
30
+ * @param options - Options containing the optimization preference
31
+ * @returns True if optimizations should be used for this graph
26
32
  */
27
- export function shouldOptimize(
28
- graph: Graph,
29
- options: OptimizableOptions = {},
30
- ): boolean {
31
- const {optimized = "auto"} = options;
33
+ export function shouldOptimize(graph: Graph, options: OptimizableOptions = {}): boolean {
34
+ const { optimized = "auto" } = options;
32
35
 
33
36
  if (optimized === "auto") {
34
37
  // Check graph size
@@ -40,42 +43,45 @@ export function shouldOptimize(
40
43
 
41
44
  /**
42
45
  * Merge optimization options with algorithm-specific options
46
+ * @param graph - The graph to determine optimization settings for
47
+ * @param options - Algorithm options that may include optimization settings
48
+ * @returns The options with resolved optimization boolean flag
43
49
  */
44
50
  export function mergeOptimizationOptions<T extends Record<string, unknown>>(
45
51
  graph: Graph,
46
52
  options: T & OptimizableOptions,
47
- ): T & {optimized?: boolean} {
48
- const {optimized: userOptimized, ... rest} = options;
53
+ ): T & { optimized?: boolean } {
54
+ const { optimized: userOptimized, ...rest } = options;
49
55
 
50
56
  return {
51
- ... rest,
52
- optimized: shouldOptimize(graph, userOptimized !== undefined ? {optimized: userOptimized} : {}),
53
- } as T & {optimized?: boolean};
57
+ ...rest,
58
+ optimized: shouldOptimize(graph, userOptimized !== undefined ? { optimized: userOptimized } : {}),
59
+ } as T & { optimized?: boolean };
54
60
  }
55
61
 
56
62
  /**
57
63
  * Create an optimized version of an algorithm
64
+ * @param algorithm - The algorithm function to wrap with optimization support
65
+ * @param optionsTransformer - Optional function to transform options before passing to the algorithm
66
+ * @returns A wrapped version of the algorithm that applies optimization transformations
58
67
  * @example
59
68
  * const optimizedBetweenness = createOptimizedAlgorithm(
60
69
  * betweennessCentrality,
61
70
  * (graph, options) => ({...options, optimized: true})
62
71
  * );
63
72
  */
64
- export function createOptimizedAlgorithm<
65
- TArgs extends [Graph, ...unknown[]],
66
- TResult,
67
- >(
68
- algorithm: (... args: TArgs) => TResult,
73
+ export function createOptimizedAlgorithm<TArgs extends [Graph, ...unknown[]], TResult>(
74
+ algorithm: (...args: TArgs) => TResult,
69
75
  optionsTransformer?: (graph: Graph, options: unknown) => unknown,
70
- ): (... args: TArgs) => TResult {
71
- return (... args: TArgs): TResult => {
72
- const [graph, options, ... rest] = args;
76
+ ): (...args: TArgs) => TResult {
77
+ return (...args: TArgs): TResult => {
78
+ const [graph, options, ...rest] = args;
73
79
 
74
80
  if (optionsTransformer && options !== undefined) {
75
81
  const transformedOptions = optionsTransformer(graph, options);
76
- return algorithm(... ([graph, transformedOptions, ... rest] as unknown as TArgs));
82
+ return algorithm(...([graph, transformedOptions, ...rest] as unknown as TArgs));
77
83
  }
78
84
 
79
- return algorithm(... args);
85
+ return algorithm(...args);
80
86
  };
81
87
  }
@@ -5,13 +5,17 @@ export class MinPriorityQueue<T> {
5
5
  private heap: T[] = [];
6
6
  private compare: (a: T, b: T) => number;
7
7
 
8
+ /**
9
+ * Creates a new MinPriorityQueue instance.
10
+ * @param compareFunction - Custom comparison function. Returns negative if a has higher priority than b.
11
+ */
8
12
  constructor(compareFunction?: (a: T, b: T) => number) {
9
13
  this.compare = compareFunction ?? ((a: T, b: T) => this.defaultCompare(a, b));
10
14
  }
11
15
 
12
16
  private defaultCompare(a: T, b: T): number {
13
- const aWithDistance = a as T & {distance?: number};
14
- const bWithDistance = b as T & {distance?: number};
17
+ const aWithDistance = a as T & { distance?: number };
18
+ const bWithDistance = b as T & { distance?: number };
15
19
 
16
20
  if (aWithDistance.distance !== undefined && bWithDistance.distance !== undefined) {
17
21
  return aWithDistance.distance - bWithDistance.distance;
@@ -25,11 +29,11 @@ export class MinPriorityQueue<T> {
25
29
  }
26
30
 
27
31
  private leftChild(i: number): number {
28
- return (2 * i) + 1;
32
+ return 2 * i + 1;
29
33
  }
30
34
 
31
35
  private rightChild(i: number): number {
32
- return (2 * i) + 2;
36
+ return 2 * i + 2;
33
37
  }
34
38
 
35
39
  private swap(i: number, j: number): void {
@@ -63,13 +67,23 @@ export class MinPriorityQueue<T> {
63
67
 
64
68
  const leftItem = this.heap[left];
65
69
  const minItem = this.heap[minIndex];
66
- if (left < this.heap.length && leftItem !== undefined && minItem !== undefined && this.compare(leftItem, minItem) < 0) {
70
+ if (
71
+ left < this.heap.length &&
72
+ leftItem !== undefined &&
73
+ minItem !== undefined &&
74
+ this.compare(leftItem, minItem) < 0
75
+ ) {
67
76
  minIndex = left;
68
77
  }
69
78
 
70
79
  const rightItem = this.heap[right];
71
80
  const newMinItem = this.heap[minIndex];
72
- if (right < this.heap.length && rightItem !== undefined && newMinItem !== undefined && this.compare(rightItem, newMinItem) < 0) {
81
+ if (
82
+ right < this.heap.length &&
83
+ rightItem !== undefined &&
84
+ newMinItem !== undefined &&
85
+ this.compare(rightItem, newMinItem) < 0
86
+ ) {
73
87
  minIndex = right;
74
88
  }
75
89
 
@@ -82,11 +96,19 @@ export class MinPriorityQueue<T> {
82
96
  }
83
97
  }
84
98
 
99
+ /**
100
+ * Insert a value into the priority queue.
101
+ * @param value - The value to insert
102
+ */
85
103
  insert(value: T): void {
86
104
  this.heap.push(value);
87
105
  this.heapifyUp(this.heap.length - 1);
88
106
  }
89
107
 
108
+ /**
109
+ * Remove and return the minimum value from the priority queue.
110
+ * @returns The minimum value, or undefined if the queue is empty
111
+ */
90
112
  extractMin(): T | undefined {
91
113
  if (this.heap.length === 0) {
92
114
  return undefined;
@@ -106,14 +128,26 @@ export class MinPriorityQueue<T> {
106
128
  return min;
107
129
  }
108
130
 
131
+ /**
132
+ * View the minimum value without removing it.
133
+ * @returns The minimum value, or undefined if the queue is empty
134
+ */
109
135
  peek(): T | undefined {
110
136
  return this.heap[0];
111
137
  }
112
138
 
139
+ /**
140
+ * Check if the priority queue is empty.
141
+ * @returns True if the queue has no items, false otherwise
142
+ */
113
143
  isEmpty(): boolean {
114
144
  return this.heap.length === 0;
115
145
  }
116
146
 
147
+ /**
148
+ * Get the number of items in the priority queue.
149
+ * @returns The number of items currently in the queue
150
+ */
117
151
  size(): number {
118
152
  return this.heap.length;
119
153
  }