@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
@@ -5,9 +5,13 @@
5
5
  * Uses a min-heap by default but supports custom comparison functions.
6
6
  */
7
7
  export class PriorityQueue<T> {
8
- private heap: {item: T, priority: number}[];
8
+ private heap: { item: T; priority: number }[];
9
9
  private compareFn: (a: number, b: number) => number;
10
10
 
11
+ /**
12
+ * Creates a new PriorityQueue instance.
13
+ * @param compareFn - Custom comparison function for priorities. Returns negative if a has higher priority than b.
14
+ */
11
15
  constructor(compareFn?: (a: number, b: number) => number) {
12
16
  this.heap = [];
13
17
  // Default to min-heap (smaller priority values have higher priority)
@@ -16,15 +20,18 @@ export class PriorityQueue<T> {
16
20
 
17
21
  /**
18
22
  * Add an item with the given priority to the queue
23
+ * @param item - The item to add to the queue
24
+ * @param priority - The priority value for the item (lower values have higher priority in min-heap)
19
25
  */
20
26
  enqueue(item: T, priority: number): void {
21
- const element = {item, priority};
27
+ const element = { item, priority };
22
28
  this.heap.push(element);
23
29
  this.heapifyUp(this.heap.length - 1);
24
30
  }
25
31
 
26
32
  /**
27
33
  * Remove and return the item with the highest priority
34
+ * @returns The item with the highest priority, or undefined if the queue is empty
28
35
  */
29
36
  dequeue(): T | undefined {
30
37
  if (this.heap.length === 0) {
@@ -52,6 +59,7 @@ export class PriorityQueue<T> {
52
59
 
53
60
  /**
54
61
  * View the item with the highest priority without removing it
62
+ * @returns The item with the highest priority, or undefined if the queue is empty
55
63
  */
56
64
  peek(): T | undefined {
57
65
  const first = this.heap[0];
@@ -60,6 +68,7 @@ export class PriorityQueue<T> {
60
68
 
61
69
  /**
62
70
  * Check if the queue is empty
71
+ * @returns True if the queue has no items, false otherwise
63
72
  */
64
73
  isEmpty(): boolean {
65
74
  return this.heap.length === 0;
@@ -67,6 +76,7 @@ export class PriorityQueue<T> {
67
76
 
68
77
  /**
69
78
  * Get the number of items in the queue
79
+ * @returns The number of items currently in the queue
70
80
  */
71
81
  size(): number {
72
82
  return this.heap.length;
@@ -74,7 +84,9 @@ export class PriorityQueue<T> {
74
84
 
75
85
  /**
76
86
  * Update the priority of an item if it exists in the queue
77
- * Returns true if the item was found and updated
87
+ * @param item - The item whose priority should be updated
88
+ * @param newPriority - The new priority value for the item
89
+ * @returns True if the item was found and updated, false otherwise
78
90
  */
79
91
  updatePriority(item: T, newPriority: number): boolean {
80
92
  const index = this.heap.findIndex((element) => element.item === item);
@@ -110,13 +122,15 @@ export class PriorityQueue<T> {
110
122
 
111
123
  /**
112
124
  * Convert queue to array (for testing/debugging)
125
+ * @returns An array of all items with their priorities
113
126
  */
114
- toArray(): {item: T, priority: number}[] {
115
- return [... this.heap];
127
+ toArray(): { item: T; priority: number }[] {
128
+ return [...this.heap];
116
129
  }
117
130
 
118
131
  /**
119
132
  * Move element up the heap until heap property is satisfied
133
+ * @param index - The index of the element to heapify up
120
134
  */
121
135
  private heapifyUp(index: number): void {
122
136
  if (index === 0) {
@@ -136,26 +150,35 @@ export class PriorityQueue<T> {
136
150
 
137
151
  /**
138
152
  * Move element down the heap until heap property is satisfied
153
+ * @param index - The index of the element to heapify down
139
154
  */
140
155
  private heapifyDown(index: number): void {
141
- const leftChildIndex = (2 * index) + 1;
142
- const rightChildIndex = (2 * index) + 2;
156
+ const leftChildIndex = 2 * index + 1;
157
+ const rightChildIndex = 2 * index + 2;
143
158
  let targetIndex = index;
144
159
 
145
160
  // Find the child with highest priority (lowest value for min-heap)
146
161
  const leftChild = this.heap[leftChildIndex];
147
162
  const target = this.heap[targetIndex];
148
163
 
149
- if (leftChildIndex < this.heap.length && leftChild && target &&
150
- this.compareFn(leftChild.priority, target.priority) < 0) {
164
+ if (
165
+ leftChildIndex < this.heap.length &&
166
+ leftChild &&
167
+ target &&
168
+ this.compareFn(leftChild.priority, target.priority) < 0
169
+ ) {
151
170
  targetIndex = leftChildIndex;
152
171
  }
153
172
 
154
173
  const rightChild = this.heap[rightChildIndex];
155
174
  const newTarget = this.heap[targetIndex];
156
175
 
157
- if (rightChildIndex < this.heap.length && rightChild && newTarget &&
158
- this.compareFn(rightChild.priority, newTarget.priority) < 0) {
176
+ if (
177
+ rightChildIndex < this.heap.length &&
178
+ rightChild &&
179
+ newTarget &&
180
+ this.compareFn(rightChild.priority, newTarget.priority) < 0
181
+ ) {
159
182
  targetIndex = rightChildIndex;
160
183
  }
161
184
 
@@ -168,6 +191,8 @@ export class PriorityQueue<T> {
168
191
 
169
192
  /**
170
193
  * Swap two elements in the heap
194
+ * @param i - Index of the first element
195
+ * @param j - Index of the second element
171
196
  */
172
197
  private swap(i: number, j: number): void {
173
198
  const temp = this.heap[i];
@@ -1,4 +1,4 @@
1
- import type {NodeId} from "../types/index.js";
1
+ import type { NodeId } from "../types/index.js";
2
2
 
3
3
  /**
4
4
  * Union-Find (Disjoint Set) data structure with path compression and union by rank
@@ -11,6 +11,10 @@ export class UnionFind {
11
11
  private rank: Map<NodeId, number>;
12
12
  private componentCount: number;
13
13
 
14
+ /**
15
+ * Creates a new UnionFind data structure with the given elements.
16
+ * @param elements - Array of elements to initialize the data structure with
17
+ */
14
18
  constructor(elements: NodeId[]) {
15
19
  this.parent = new Map();
16
20
  this.rank = new Map();
@@ -25,6 +29,8 @@ export class UnionFind {
25
29
 
26
30
  /**
27
31
  * Find the root of the set containing the element with path compression
32
+ * @param element - The element to find the root for
33
+ * @returns The root element of the set containing the given element
28
34
  */
29
35
  find(element: NodeId): NodeId {
30
36
  const parent = this.parent.get(element);
@@ -48,6 +54,8 @@ export class UnionFind {
48
54
 
49
55
  /**
50
56
  * Union two sets using union by rank
57
+ * @param elementA - The first element whose set should be merged
58
+ * @param elementB - The second element whose set should be merged
51
59
  */
52
60
  union(elementA: NodeId, elementB: NodeId): void {
53
61
  const rootA = this.find(elementA);
@@ -80,6 +88,9 @@ export class UnionFind {
80
88
 
81
89
  /**
82
90
  * Check if two elements are in the same connected component
91
+ * @param elementA - The first element to check
92
+ * @param elementB - The second element to check
93
+ * @returns True if both elements are in the same component, false otherwise
83
94
  */
84
95
  connected(elementA: NodeId, elementB: NodeId): boolean {
85
96
  try {
@@ -91,6 +102,7 @@ export class UnionFind {
91
102
 
92
103
  /**
93
104
  * Get the number of connected components
105
+ * @returns The current count of disjoint components
94
106
  */
95
107
  getComponentCount(): number {
96
108
  return this.componentCount;
@@ -98,6 +110,8 @@ export class UnionFind {
98
110
 
99
111
  /**
100
112
  * Get all elements that belong to the same component as the given element
113
+ * @param element - The element whose component should be retrieved
114
+ * @returns Array of all elements in the same component
101
115
  */
102
116
  getComponent(element: NodeId): NodeId[] {
103
117
  const root = this.find(element);
@@ -114,6 +128,7 @@ export class UnionFind {
114
128
 
115
129
  /**
116
130
  * Get all connected components as separate arrays
131
+ * @returns Array of arrays, where each inner array contains elements of one component
117
132
  */
118
133
  getAllComponents(): NodeId[][] {
119
134
  const componentMap = new Map<NodeId, NodeId[]>();
@@ -136,6 +151,8 @@ export class UnionFind {
136
151
 
137
152
  /**
138
153
  * Get the size of the component containing the given element
154
+ * @param element - The element whose component size should be retrieved
155
+ * @returns The number of elements in the component containing the given element
139
156
  */
140
157
  getComponentSize(element: NodeId): number {
141
158
  return this.getComponent(element).length;
@@ -143,6 +160,7 @@ export class UnionFind {
143
160
 
144
161
  /**
145
162
  * Add a new element to the data structure
163
+ * @param element - The element to add as a new singleton set
146
164
  */
147
165
  addElement(element: NodeId): void {
148
166
  if (this.parent.has(element)) {
@@ -156,6 +174,8 @@ export class UnionFind {
156
174
 
157
175
  /**
158
176
  * Check if an element exists in the data structure
177
+ * @param element - The element to check for
178
+ * @returns True if the element exists, false otherwise
159
179
  */
160
180
  hasElement(element: NodeId): boolean {
161
181
  return this.parent.has(element);
@@ -163,6 +183,7 @@ export class UnionFind {
163
183
 
164
184
  /**
165
185
  * Get the total number of elements
186
+ * @returns The total count of all elements in the data structure
166
187
  */
167
188
  size(): number {
168
189
  return this.parent.size;
@@ -1,6 +1,6 @@
1
- import {bfsAugmentingPath} from "../algorithms/traversal/bfs-variants.js";
2
- import type {Graph} from "../core/graph.js";
3
- import {graphToMap} from "../utils/graph-converters.js";
1
+ import { bfsAugmentingPath } from "../algorithms/traversal/bfs-variants.js";
2
+ import type { Graph } from "../core/graph.js";
3
+ import { graphToMap } from "../utils/graph-converters.js";
4
4
 
5
5
  /**
6
6
  * Ford-Fulkerson Algorithm for Maximum Flow
@@ -24,15 +24,15 @@ export interface FlowNetwork {
24
24
  export interface MaxFlowResult {
25
25
  maxFlow: number;
26
26
  flowGraph: Map<string, Map<string, number>>;
27
- minCut?: {source: Set<string>, sink: Set<string>, edges: [string, string][]};
27
+ minCut?: { source: Set<string>; sink: Set<string>; edges: [string, string][] };
28
28
  }
29
29
 
30
30
  /**
31
31
  * Create residual graph from original graph
32
+ * @param graph - The original graph with capacity values
33
+ * @returns A residual graph initialized with the original capacities
32
34
  */
33
- function createResidualGraph(
34
- graph: Map<string, Map<string, number>>,
35
- ): Map<string, Map<string, number>> {
35
+ function createResidualGraph(graph: Map<string, Map<string, number>>): Map<string, Map<string, number>> {
36
36
  const residual = new Map<string, Map<string, number>>();
37
37
 
38
38
  for (const [u, neighbors] of graph) {
@@ -50,6 +50,10 @@ function createResidualGraph(
50
50
 
51
51
  /**
52
52
  * Find augmenting path using DFS
53
+ * @param residualGraph - The residual graph with remaining capacities
54
+ * @param source - The source node
55
+ * @param sink - The sink node
56
+ * @returns An array of nodes forming the path, or null if no path exists
53
57
  */
54
58
  function findAugmentingPathDFS(
55
59
  residualGraph: Map<string, Map<string, number>>,
@@ -92,6 +96,11 @@ function findAugmentingPathDFS(
92
96
 
93
97
  /**
94
98
  * Update flow along an augmenting path
99
+ * @param residualGraph - The residual graph to update
100
+ * @param flowGraph - The flow graph to update with new flow values
101
+ * @param originalGraph - The original graph for edge direction reference
102
+ * @param path - The augmenting path to update flow along
103
+ * @param pathFlow - The amount of flow to push through the path
95
104
  */
96
105
  function updateFlow(
97
106
  residualGraph: Map<string, Map<string, number>>,
@@ -146,11 +155,14 @@ function updateFlow(
146
155
 
147
156
  /**
148
157
  * Find minimum cut from source side
158
+ * @param residualGraph - The residual graph after max flow computation
159
+ * @param source - The source node
160
+ * @returns The minimum cut partition with source and sink sets and cut edges
149
161
  */
150
162
  function findMinCut(
151
163
  residualGraph: Map<string, Map<string, number>>,
152
164
  source: string,
153
- ): {source: Set<string>, sink: Set<string>, edges: [string, string][]} {
165
+ ): { source: Set<string>; sink: Set<string>; edges: [string, string][] } {
154
166
  // Find all reachable nodes from source in residual graph
155
167
  const sourceSet = new Set<string>();
156
168
  const queue = [source];
@@ -195,20 +207,29 @@ function findMinCut(
195
207
  }
196
208
  }
197
209
 
198
- return {source: sourceSet, sink: sinkSet, edges: cutEdges};
210
+ return { source: sourceSet, sink: sinkSet, edges: cutEdges };
199
211
  }
200
212
 
201
213
  /**
202
214
  * Core max flow algorithm implementation
215
+ * @param graph - The graph with capacity values
216
+ * @param source - The source node
217
+ * @param sink - The sink node
218
+ * @param findPath - Function to find an augmenting path in the residual graph
219
+ * @returns The maximum flow result including flow value, flow graph, and minimum cut
203
220
  */
204
221
  function maxFlowCore(
205
222
  graph: Map<string, Map<string, number>>,
206
223
  source: string,
207
224
  sink: string,
208
- findPath: (residual: Map<string, Map<string, number>>, src: string, snk: string) => {path: string[], pathCapacity: number} | null,
225
+ findPath: (
226
+ residual: Map<string, Map<string, number>>,
227
+ src: string,
228
+ snk: string,
229
+ ) => { path: string[]; pathCapacity: number } | null,
209
230
  ): MaxFlowResult {
210
231
  if (!graph.has(source) || !graph.has(sink)) {
211
- return {maxFlow: 0, flowGraph: new Map()};
232
+ return { maxFlow: 0, flowGraph: new Map() };
212
233
  }
213
234
 
214
235
  // Create residual graph
@@ -230,7 +251,7 @@ function maxFlowCore(
230
251
  let pathResult = findPath(residualGraph, source, sink);
231
252
 
232
253
  while (pathResult !== null) {
233
- const {path, pathCapacity} = pathResult;
254
+ const { path, pathCapacity } = pathResult;
234
255
 
235
256
  // Update flow
236
257
  updateFlow(residualGraph, flowGraph, graph, path, pathCapacity);
@@ -243,19 +264,23 @@ function maxFlowCore(
243
264
  // Find minimum cut
244
265
  const minCut = findMinCut(residualGraph, source);
245
266
 
246
- return {maxFlow, flowGraph, minCut};
267
+ return { maxFlow, flowGraph, minCut };
247
268
  }
248
269
 
249
270
  /**
250
271
  * Internal implementation of Ford-Fulkerson algorithm using DFS
272
+ * @param graph - The graph with capacity values
273
+ * @param source - The source node
274
+ * @param sink - The sink node
275
+ * @returns The maximum flow result
251
276
  */
252
- function fordFulkersonImpl(
253
- graph: Map<string, Map<string, number>>,
254
- source: string,
255
- sink: string,
256
- ): MaxFlowResult {
277
+ function fordFulkersonImpl(graph: Map<string, Map<string, number>>, source: string, sink: string): MaxFlowResult {
257
278
  // Adapter function for DFS path finding
258
- const findPathDFS = (residual: Map<string, Map<string, number>>, src: string, snk: string): {path: string[], pathCapacity: number} | null => {
279
+ const findPathDFS = (
280
+ residual: Map<string, Map<string, number>>,
281
+ src: string,
282
+ snk: string,
283
+ ): { path: string[]; pathCapacity: number } | null => {
259
284
  const path = findAugmentingPathDFS(residual, src, snk);
260
285
  if (!path) {
261
286
  return null;
@@ -274,7 +299,7 @@ function fordFulkersonImpl(
274
299
  }
275
300
  }
276
301
 
277
- return {path, pathCapacity};
302
+ return { path, pathCapacity };
278
303
  };
279
304
 
280
305
  return maxFlowCore(graph, source, sink, findPathDFS);
@@ -282,14 +307,18 @@ function fordFulkersonImpl(
282
307
 
283
308
  /**
284
309
  * Internal implementation of Edmonds-Karp algorithm
310
+ * @param graph - The graph with capacity values
311
+ * @param source - The source node
312
+ * @param sink - The sink node
313
+ * @returns The maximum flow result
285
314
  */
286
- function edmondsKarpImpl(
287
- graph: Map<string, Map<string, number>>,
288
- source: string,
289
- sink: string,
290
- ): MaxFlowResult {
315
+ function edmondsKarpImpl(graph: Map<string, Map<string, number>>, source: string, sink: string): MaxFlowResult {
291
316
  // Use BFS variant for path finding
292
- const findPathBFS = (residual: Map<string, Map<string, number>>, src: string, snk: string): {path: string[], pathCapacity: number} | null => {
317
+ const findPathBFS = (
318
+ residual: Map<string, Map<string, number>>,
319
+ src: string,
320
+ snk: string,
321
+ ): { path: string[]; pathCapacity: number } | null => {
293
322
  return bfsAugmentingPath(residual, src, snk);
294
323
  };
295
324
 
@@ -298,12 +327,16 @@ function edmondsKarpImpl(
298
327
 
299
328
  /**
300
329
  * Utility function to create a flow network for bipartite matching
330
+ * @param leftNodes - Array of left partition node identifiers
331
+ * @param rightNodes - Array of right partition node identifiers
332
+ * @param edges - Array of edges connecting left nodes to right nodes
333
+ * @returns The flow network graph with source and sink nodes
301
334
  */
302
335
  export function createBipartiteFlowNetwork(
303
336
  leftNodes: string[],
304
337
  rightNodes: string[],
305
338
  edges: [string, string][],
306
- ): {graph: Map<string, Map<string, number>>, source: string, sink: string} {
339
+ ): { graph: Map<string, Map<string, number>>; source: string; sink: string } {
307
340
  const graph = new Map<string, Map<string, number>>();
308
341
  const source = "__source__";
309
342
  const sink = "__sink__";
@@ -345,12 +378,11 @@ export function createBipartiteFlowNetwork(
345
378
 
346
379
  graph.set(sink, new Map());
347
380
 
348
- return {graph, source, sink};
381
+ return { graph, source, sink };
349
382
  }
350
383
 
351
384
  /**
352
385
  * Ford-Fulkerson algorithm using DFS for finding augmenting paths
353
- *
354
386
  * @param graph - Adjacency list representation with capacities - accepts Graph class or Map
355
387
  * @param source - Source node
356
388
  * @param sink - Sink node
@@ -359,11 +391,7 @@ export function createBipartiteFlowNetwork(
359
391
  * Time Complexity: O(E * f) where f is the maximum flow
360
392
  * Space Complexity: O(V + E)
361
393
  */
362
- export function fordFulkerson(
363
- graph: Graph,
364
- source: string,
365
- sink: string,
366
- ): MaxFlowResult {
394
+ export function fordFulkerson(graph: Graph, source: string, sink: string): MaxFlowResult {
367
395
  // Convert Graph to Map representation
368
396
  const graphMap = graphToMap(graph);
369
397
  return fordFulkersonImpl(graphMap, source, sink);
@@ -372,7 +400,6 @@ export function fordFulkerson(
372
400
  /**
373
401
  * Edmonds-Karp algorithm (Ford-Fulkerson with BFS)
374
402
  * More efficient implementation with better time complexity
375
- *
376
403
  * @param graph - Adjacency list representation with capacities - accepts Graph class or Map
377
404
  * @param source - Source node
378
405
  * @param sink - Sink node