@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,8 +1,8 @@
1
- export type {ClusterNode, HierarchicalClusteringResult, LinkageMethod} from "./hierarchical.js";
2
- export {hierarchicalClustering} from "./hierarchical.js";
3
- export type {KCoreResult} from "./k-core.js";
4
- export {getKCore, kCoreDecomposition} from "./k-core.js";
5
- export type {MCLOptions, MCLResult} from "./mcl.js";
6
- export {calculateMCLModularity, markovClustering} from "./mcl.js";
7
- export type {SpectralClusteringOptions, SpectralClusteringResult} from "./spectral.js";
8
- export {spectralClustering} from "./spectral.js";
1
+ export type { ClusterNode, HierarchicalClusteringResult, LinkageMethod } from "./hierarchical.js";
2
+ export { hierarchicalClustering } from "./hierarchical.js";
3
+ export type { KCoreResult } from "./k-core.js";
4
+ export { getKCore, kCoreDecomposition } from "./k-core.js";
5
+ export type { MCLOptions, MCLResult } from "./mcl.js";
6
+ export { calculateMCLModularity, markovClustering } from "./mcl.js";
7
+ export type { SpectralClusteringOptions, SpectralClusteringResult } from "./spectral.js";
8
+ export { spectralClustering } from "./spectral.js";
@@ -6,10 +6,12 @@
6
6
  * understanding graph structure.
7
7
  */
8
8
 
9
- import type {Graph} from "../core/graph.js";
9
+ import type { Graph } from "../core/graph.js";
10
10
 
11
11
  /**
12
12
  * Convert Graph to adjacency set representation
13
+ * @param graph - The input graph to convert
14
+ * @returns Map of node IDs to sets of neighbor node IDs
13
15
  */
14
16
  function graphToAdjacencySet(graph: Graph): Map<string, Set<string>> {
15
17
  const adjacency = new Map<string, Set<string>>();
@@ -49,12 +51,12 @@ export interface KCoreResult<T> {
49
51
 
50
52
  /**
51
53
  * Internal implementation of K-Core decomposition algorithm
54
+ * @param graph - Adjacency map representation of the graph
55
+ * @returns K-core decomposition result with cores, coreness values, and maximum core number
52
56
  */
53
- function kCoreDecompositionImpl<T>(
54
- graph: Map<T, Set<T>>,
55
- ): KCoreResult<T> {
57
+ function kCoreDecompositionImpl<T>(graph: Map<T, Set<T>>): KCoreResult<T> {
56
58
  if (graph.size === 0) {
57
- return {cores: new Map(), coreness: new Map(), maxCore: 0};
59
+ return { cores: new Map(), coreness: new Map(), maxCore: 0 };
58
60
  }
59
61
 
60
62
  const n = graph.size;
@@ -208,22 +210,18 @@ function kCoreDecompositionImpl<T>(
208
210
  maxCore = Math.max(maxCore, core);
209
211
  }
210
212
 
211
- return {cores, coreness, maxCore};
213
+ return { cores, coreness, maxCore };
212
214
  }
213
215
 
214
216
  /**
215
217
  * Extract the k-core subgraph
216
218
  * Returns nodes that belong to k-core or higher
217
- *
218
219
  * @param graph - Undirected graph
219
220
  * @param k - Core number
220
221
  * @returns Set of nodes in k-core or higher
221
222
  */
222
- export function getKCore(
223
- graph: Graph,
224
- k: number,
225
- ): Set<string> {
226
- const {coreness} = kCoreDecomposition(graph);
223
+ export function getKCore(graph: Graph, k: number): Set<string> {
224
+ const { coreness } = kCoreDecomposition(graph);
227
225
  const kCore = new Set<string>();
228
226
 
229
227
  for (const [node, core] of coreness) {
@@ -238,15 +236,11 @@ export function getKCore(
238
236
  /**
239
237
  * Get the induced subgraph for k-core
240
238
  * Returns the actual subgraph containing only k-core nodes
241
- *
242
239
  * @param graph - Original graph
243
240
  * @param k - Core number
244
241
  * @returns K-core subgraph
245
242
  */
246
- export function getKCoreSubgraph(
247
- graph: Graph,
248
- k: number,
249
- ): Map<string, Set<string>> {
243
+ export function getKCoreSubgraph(graph: Graph, k: number): Map<string, Set<string>> {
250
244
  const kCoreNodes = getKCore(graph, k);
251
245
  const adjacencySet = graphToAdjacencySet(graph);
252
246
  const subgraph = new Map<string, Set<string>>();
@@ -270,13 +264,10 @@ export function getKCoreSubgraph(
270
264
  /**
271
265
  * Degeneracy ordering of the graph
272
266
  * Orders nodes by their coreness values
273
- *
274
267
  * @param graph - Undirected graph
275
268
  * @returns Array of nodes ordered by degeneracy
276
269
  */
277
- export function degeneracyOrdering(
278
- graph: Graph,
279
- ): string[] {
270
+ export function degeneracyOrdering(graph: Graph): string[] {
280
271
  const adjacencySet = graphToAdjacencySet(graph);
281
272
  const degree = new Map<string, number>();
282
273
  const remaining = new Map<string, Set<string>>();
@@ -290,7 +281,7 @@ export function degeneracyOrdering(
290
281
 
291
282
  // Build ordering
292
283
  while (ordering.length < adjacencySet.size) {
293
- // Find minimum degree node
284
+ // Find minimum degree node
294
285
  let minDegree = Infinity;
295
286
  let minNode: string | undefined;
296
287
 
@@ -327,15 +318,11 @@ export function degeneracyOrdering(
327
318
  /**
328
319
  * Find k-truss subgraph (triangular k-cores)
329
320
  * Each edge must be part of at least k-2 triangles
330
- *
331
321
  * @param graph - Undirected graph
332
322
  * @param k - Truss number (k >= 2)
333
323
  * @returns Edges in k-truss
334
324
  */
335
- export function kTruss(
336
- graph: Graph,
337
- k: number,
338
- ): Set<string> {
325
+ export function kTruss(graph: Graph, k: number): Set<string> {
339
326
  if (k < 2) {
340
327
  throw new Error("k must be at least 2 for k-truss");
341
328
  }
@@ -349,8 +336,9 @@ export function kTruss(
349
336
  // Initialize edges
350
337
  for (const [u, neighbors] of adjacencySet) {
351
338
  for (const v of neighbors) {
352
- if (u < v) { // Avoid duplicates
353
- const edge = `${String(u)},${String(v)}`;
339
+ if (u < v) {
340
+ // Avoid duplicates
341
+ const edge = `${u},${v}`;
354
342
  edges.add(edge);
355
343
  edgeTriangles.set(edge, 0);
356
344
  }
@@ -367,9 +355,9 @@ export function kTruss(
367
355
  for (const w of uNeighbors) {
368
356
  if (v < w && vNeighbors.has(w)) {
369
357
  // Triangle found: u-v-w
370
- const edge1 = `${String(u)},${String(v)}`;
371
- const edge2 = `${String(u)},${String(w)}`;
372
- const edge3 = `${String(v)},${String(w)}`;
358
+ const edge1 = `${u},${v}`;
359
+ const edge2 = `${u},${w}`;
360
+ const edge3 = `${v},${w}`;
373
361
 
374
362
  const edge1Count = edgeTriangles.get(edge1);
375
363
  const edge2Count = edgeTriangles.get(edge2);
@@ -428,8 +416,8 @@ export function kTruss(
428
416
  if (uNeighbors && vNeighbors) {
429
417
  for (const w of uNeighbors) {
430
418
  if (vNeighbors.has(w)) {
431
- const edge1 = u < w ? `${String(u)},${String(w)}` : `${String(w)},${String(u)}`;
432
- const edge2 = v < w ? `${String(v)},${String(w)}` : `${String(w)},${String(v)}`;
419
+ const edge1 = u < w ? `${u},${w}` : `${w},${u}`;
420
+ const edge2 = v < w ? `${v},${w}` : `${w},${v}`;
433
421
 
434
422
  if (kTrussEdges.has(edge1)) {
435
423
  const edge1Count = edgeTriangles.get(edge1);
@@ -455,10 +443,10 @@ export function kTruss(
455
443
 
456
444
  /**
457
445
  * Convert directed graph to undirected for k-core analysis
446
+ * @param directedGraph - Directed graph represented as adjacency map with edge weights
447
+ * @returns Undirected graph represented as adjacency map with neighbor sets
458
448
  */
459
- export function toUndirected<T>(
460
- directedGraph: Map<T, Map<T, number>>,
461
- ): Map<T, Set<T>> {
449
+ export function toUndirected<T>(directedGraph: Map<T, Map<T, number>>): Map<T, Set<T>> {
462
450
  const undirected = new Map<T, Set<T>>();
463
451
 
464
452
  // Initialize all nodes
@@ -491,18 +479,14 @@ export function toUndirected<T>(
491
479
  /**
492
480
  * K-Core decomposition algorithm
493
481
  * Finds all k-cores in the graph and assigns coreness values to nodes
494
- *
495
482
  * @param graph - Undirected graph - accepts Graph class or Map<T, Set<T>>
496
483
  * @returns K-core decomposition results
497
484
  *
498
485
  * Time Complexity: O(V + E)
499
486
  * Space Complexity: O(V)
500
487
  */
501
- export function kCoreDecomposition(
502
- graph: Graph,
503
- ): KCoreResult<string> {
488
+ export function kCoreDecomposition(graph: Graph): KCoreResult<string> {
504
489
  // Convert Graph to adjacency set representation
505
490
  const adjacencySet = graphToAdjacencySet(graph);
506
491
  return kCoreDecompositionImpl(adjacencySet);
507
492
  }
508
-
@@ -1,5 +1,5 @@
1
- import type {Graph} from "../core/graph.js";
2
- import type {NodeId} from "../types/index.js";
1
+ import type { Graph } from "../core/graph.js";
2
+ import type { NodeId } from "../types/index.js";
3
3
 
4
4
  /**
5
5
  * Markov Clustering (MCL) algorithm implementation
@@ -31,11 +31,11 @@ export interface MCLResult {
31
31
 
32
32
  /**
33
33
  * Perform Markov Clustering on a graph
34
+ * @param graph - The input graph to cluster
35
+ * @param options - MCL algorithm options (expansion, inflation, iterations, etc.)
36
+ * @returns MCL clustering result with communities, attractors, and convergence info
34
37
  */
35
- export function markovClustering(
36
- graph: Graph,
37
- options: MCLOptions = {},
38
- ): MCLResult {
38
+ export function markovClustering(graph: Graph, options: MCLOptions = {}): MCLResult {
39
39
  const {
40
40
  expansion = 2,
41
41
  inflation = 2,
@@ -65,7 +65,7 @@ export function markovClustering(
65
65
  let iteration = 0;
66
66
 
67
67
  for (iteration = 0; iteration < maxIterations; iteration++) {
68
- const oldMatrix = matrix.map((row) => [... row]);
68
+ const oldMatrix = matrix.map((row) => [...row]);
69
69
 
70
70
  // Expansion step (matrix multiplication)
71
71
  matrix = matrixPower(matrix, expansion);
@@ -84,7 +84,7 @@ export function markovClustering(
84
84
  }
85
85
 
86
86
  // Extract clusters from final matrix
87
- const {communities, attractors} = extractClusters(matrix, nodeIds);
87
+ const { communities, attractors } = extractClusters(matrix, nodeIds);
88
88
 
89
89
  return {
90
90
  communities,
@@ -96,10 +96,14 @@ export function markovClustering(
96
96
 
97
97
  /**
98
98
  * Build initial transition matrix from graph
99
+ * @param graph - The input graph
100
+ * @param nodeIds - Array of node IDs in the graph
101
+ * @param selfLoops - Whether to add self-loops to the matrix
102
+ * @returns Column-normalized transition matrix
99
103
  */
100
104
  function buildTransitionMatrix(graph: Graph, nodeIds: NodeId[], selfLoops: boolean): number[][] {
101
105
  const n = nodeIds.length;
102
- const matrix = Array.from({length: n}, (): number[] => Array(n).fill(0) as number[]);
106
+ const matrix = Array.from({ length: n }, (): number[] => Array(n).fill(0) as number[]);
103
107
  const nodeToIndex = new Map<NodeId, number>();
104
108
 
105
109
  nodeIds.forEach((id, index) => nodeToIndex.set(id, index));
@@ -168,6 +172,9 @@ function buildTransitionMatrix(graph: Graph, nodeIds: NodeId[], selfLoops: boole
168
172
 
169
173
  /**
170
174
  * Raise matrix to a power (for expansion step)
175
+ * @param matrix - Input square matrix
176
+ * @param power - Power to raise the matrix to
177
+ * @returns Matrix raised to the specified power
171
178
  */
172
179
  function matrixPower(matrix: number[][], power: number): number[][] {
173
180
  if (power === 1) {
@@ -187,13 +194,16 @@ function matrixPower(matrix: number[][], power: number): number[][] {
187
194
 
188
195
  /**
189
196
  * Multiply two matrices
197
+ * @param a - First matrix
198
+ * @param b - Second matrix
199
+ * @returns Product matrix a * b
190
200
  */
191
201
  function matrixMultiply(a: number[][], b: number[][]): number[][] {
192
202
  const n = a.length;
193
203
  const m = b[0]?.length ?? 0;
194
204
  const p = b.length;
195
205
 
196
- const result: number[][] = Array.from({length: n}, () => Array(m).fill(0) as number[]);
206
+ const result: number[][] = Array.from({ length: n }, () => Array(m).fill(0) as number[]);
197
207
 
198
208
  for (let i = 0; i < n; i++) {
199
209
  for (let j = 0; j < m; j++) {
@@ -207,7 +217,7 @@ function matrixMultiply(a: number[][], b: number[][]): number[][] {
207
217
 
208
218
  const prevVal = resultRow[j];
209
219
  if (prevVal !== undefined) {
210
- resultRow[j] = prevVal + (aVal * bVal);
220
+ resultRow[j] = prevVal + aVal * bVal;
211
221
  }
212
222
  }
213
223
  }
@@ -218,10 +228,13 @@ function matrixMultiply(a: number[][], b: number[][]): number[][] {
218
228
 
219
229
  /**
220
230
  * Inflation step: element-wise powering and column normalization
231
+ * @param matrix - Input matrix to inflate
232
+ * @param inflation - Inflation parameter for element-wise powering
233
+ * @returns Inflated and column-normalized matrix
221
234
  */
222
235
  function inflate(matrix: number[][], inflation: number): number[][] {
223
236
  const n = matrix.length;
224
- const result: number[][] = Array.from({length: n}, () => Array(n).fill(0) as number[]);
237
+ const result: number[][] = Array.from({ length: n }, () => Array(n).fill(0) as number[]);
225
238
 
226
239
  // Element-wise powering
227
240
  for (let i = 0; i < n; i++) {
@@ -266,10 +279,13 @@ function inflate(matrix: number[][], inflation: number): number[][] {
266
279
 
267
280
  /**
268
281
  * Pruning step: remove small values
282
+ * @param matrix - Input matrix to prune
283
+ * @param threshold - Values below this threshold are set to zero
284
+ * @returns Pruned and re-normalized matrix
269
285
  */
270
286
  function prune(matrix: number[][], threshold: number): number[][] {
271
287
  const n = matrix.length;
272
- const result: number[][] = Array.from({length: n}, () => Array(n).fill(0) as number[]);
288
+ const result: number[][] = Array.from({ length: n }, () => Array(n).fill(0) as number[]);
273
289
 
274
290
  for (let i = 0; i < n; i++) {
275
291
  for (let j = 0; j < n; j++) {
@@ -323,6 +339,10 @@ function prune(matrix: number[][], threshold: number): number[][] {
323
339
 
324
340
  /**
325
341
  * Check if the algorithm has converged
342
+ * @param oldMatrix - Matrix from previous iteration
343
+ * @param newMatrix - Matrix from current iteration
344
+ * @param tolerance - Convergence tolerance threshold
345
+ * @returns True if the maximum difference between matrices is below tolerance
326
346
  */
327
347
  function hasConverged(oldMatrix: number[][], newMatrix: number[][], tolerance: number): boolean {
328
348
  const n = oldMatrix.length;
@@ -348,8 +368,14 @@ function hasConverged(oldMatrix: number[][], newMatrix: number[][], tolerance: n
348
368
 
349
369
  /**
350
370
  * Extract clusters from the final matrix
371
+ * @param matrix - Final converged MCL matrix
372
+ * @param nodeIds - Array of node IDs corresponding to matrix indices
373
+ * @returns Object containing communities and attractor nodes
351
374
  */
352
- function extractClusters(matrix: number[][], nodeIds: NodeId[]): {
375
+ function extractClusters(
376
+ matrix: number[][],
377
+ nodeIds: NodeId[],
378
+ ): {
353
379
  communities: NodeId[][];
354
380
  attractors: Set<NodeId>;
355
381
  } {
@@ -391,10 +417,14 @@ function extractClusters(matrix: number[][], nodeIds: NodeId[]): {
391
417
  }
392
418
 
393
419
  if (clusterNodes.length > 0) {
394
- communities.push(clusterNodes.map((idx) => {
395
- const nodeId = nodeIds[idx];
396
- return nodeId;
397
- }).filter((node): node is NodeId => node !== undefined));
420
+ communities.push(
421
+ clusterNodes
422
+ .map((idx) => {
423
+ const nodeId = nodeIds[idx];
424
+ return nodeId;
425
+ })
426
+ .filter((node): node is NodeId => node !== undefined),
427
+ );
398
428
  clusterIndex++;
399
429
  }
400
430
  }
@@ -411,16 +441,16 @@ function extractClusters(matrix: number[][], nodeIds: NodeId[]): {
411
441
  }
412
442
  }
413
443
 
414
- return {communities, attractors};
444
+ return { communities, attractors };
415
445
  }
416
446
 
417
447
  /**
418
448
  * Calculate modularity of MCL clustering result
449
+ * @param graph - The original graph that was clustered
450
+ * @param communities - Array of communities (each community is an array of node IDs)
451
+ * @returns Modularity score between -0.5 and 1.0
419
452
  */
420
- export function calculateMCLModularity(
421
- graph: Graph,
422
- communities: NodeId[][],
423
- ): number {
453
+ export function calculateMCLModularity(graph: Graph, communities: NodeId[][]): number {
424
454
  const m = graph.totalEdgeCount;
425
455
  if (m === 0) {
426
456
  return 0;