@graphty/algorithms 1.2.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +375 -276
- package/dist/algorithms.d.ts +42 -2
- package/dist/algorithms.js +11402 -2
- package/dist/algorithms.js.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.d.ts +14 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.js +144 -169
- package/dist/src/algorithms/centrality/betweenness.js.map +1 -1
- package/dist/src/algorithms/centrality/closeness.d.ts +35 -1
- package/dist/src/algorithms/centrality/closeness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/closeness.js +70 -149
- package/dist/src/algorithms/centrality/closeness.js.map +1 -1
- package/dist/src/algorithms/centrality/degree.d.ts +7 -0
- package/dist/src/algorithms/centrality/degree.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/degree.js +12 -9
- package/dist/src/algorithms/centrality/degree.js.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts +57 -0
- package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/delta-pagerank-simple.js +304 -0
- package/dist/src/algorithms/centrality/delta-pagerank-simple.js.map +1 -0
- package/dist/src/algorithms/centrality/delta-pagerank.d.ts +106 -0
- package/dist/src/algorithms/centrality/delta-pagerank.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/delta-pagerank.js +320 -0
- package/dist/src/algorithms/centrality/delta-pagerank.js.map +1 -0
- package/dist/src/algorithms/centrality/eigenvector.d.ts +10 -3
- package/dist/src/algorithms/centrality/eigenvector.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/eigenvector.js +11 -4
- package/dist/src/algorithms/centrality/eigenvector.js.map +1 -1
- package/dist/src/algorithms/centrality/hits.d.ts +10 -3
- package/dist/src/algorithms/centrality/hits.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/hits.js +11 -4
- package/dist/src/algorithms/centrality/hits.js.map +1 -1
- package/dist/src/algorithms/centrality/index.d.ts +3 -1
- package/dist/src/algorithms/centrality/index.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/index.js +2 -1
- package/dist/src/algorithms/centrality/index.js.map +1 -1
- package/dist/src/algorithms/centrality/katz.d.ts +10 -3
- package/dist/src/algorithms/centrality/katz.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/katz.js +13 -8
- package/dist/src/algorithms/centrality/katz.js.map +1 -1
- package/dist/src/algorithms/centrality/pagerank.d.ts +31 -0
- package/dist/src/algorithms/centrality/pagerank.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/pagerank.js +61 -1
- package/dist/src/algorithms/centrality/pagerank.js.map +1 -1
- package/dist/src/algorithms/community/girvan-newman.d.ts +2 -3
- package/dist/src/algorithms/community/girvan-newman.d.ts.map +1 -1
- package/dist/src/algorithms/community/girvan-newman.js +67 -57
- package/dist/src/algorithms/community/girvan-newman.js.map +1 -1
- package/dist/src/algorithms/community/index.d.ts +2 -0
- package/dist/src/algorithms/community/index.d.ts.map +1 -1
- package/dist/src/algorithms/community/index.js.map +1 -1
- package/dist/src/algorithms/community/label-propagation.d.ts +11 -4
- package/dist/src/algorithms/community/label-propagation.d.ts.map +1 -1
- package/dist/src/algorithms/community/label-propagation.js +60 -38
- package/dist/src/algorithms/community/label-propagation.js.map +1 -1
- package/dist/src/algorithms/community/leiden.d.ts +3 -3
- package/dist/src/algorithms/community/leiden.d.ts.map +1 -1
- package/dist/src/algorithms/community/leiden.js +70 -44
- package/dist/src/algorithms/community/leiden.js.map +1 -1
- package/dist/src/algorithms/community/louvain-optimized.d.ts +149 -0
- package/dist/src/algorithms/community/louvain-optimized.d.ts.map +1 -0
- package/dist/src/algorithms/community/louvain-optimized.js +338 -0
- package/dist/src/algorithms/community/louvain-optimized.js.map +1 -0
- package/dist/src/algorithms/community/louvain.d.ts +2 -3
- package/dist/src/algorithms/community/louvain.d.ts.map +1 -1
- package/dist/src/algorithms/community/louvain.js +35 -81
- package/dist/src/algorithms/community/louvain.js.map +1 -1
- package/dist/src/algorithms/community/modularity-utils.d.ts +55 -0
- package/dist/src/algorithms/community/modularity-utils.d.ts.map +1 -0
- package/dist/src/algorithms/community/modularity-utils.js +108 -0
- package/dist/src/algorithms/community/modularity-utils.js.map +1 -0
- package/dist/src/algorithms/components/connected.d.ts +23 -0
- package/dist/src/algorithms/components/connected.d.ts.map +1 -1
- package/dist/src/algorithms/components/connected.js +29 -3
- package/dist/src/algorithms/components/connected.js.map +1 -1
- package/dist/src/algorithms/matching/bipartite.d.ts +10 -0
- package/dist/src/algorithms/matching/bipartite.d.ts.map +1 -1
- package/dist/src/algorithms/matching/bipartite.js +18 -40
- package/dist/src/algorithms/matching/bipartite.js.map +1 -1
- package/dist/src/algorithms/matching/index.d.ts.map +1 -1
- package/dist/src/algorithms/matching/index.js.map +1 -1
- package/dist/src/algorithms/matching/isomorphism.d.ts +8 -0
- package/dist/src/algorithms/matching/isomorphism.d.ts.map +1 -1
- package/dist/src/algorithms/matching/isomorphism.js +36 -2
- package/dist/src/algorithms/matching/isomorphism.js.map +1 -1
- package/dist/src/algorithms/mst/index.d.ts.map +1 -1
- package/dist/src/algorithms/mst/index.js.map +1 -1
- package/dist/src/algorithms/mst/kruskal.d.ts +14 -0
- package/dist/src/algorithms/mst/kruskal.d.ts.map +1 -1
- package/dist/src/algorithms/mst/kruskal.js +17 -3
- package/dist/src/algorithms/mst/kruskal.js.map +1 -1
- package/dist/src/algorithms/mst/prim.d.ts +9 -0
- package/dist/src/algorithms/mst/prim.d.ts.map +1 -1
- package/dist/src/algorithms/mst/prim.js +9 -0
- package/dist/src/algorithms/mst/prim.js.map +1 -1
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts +10 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/bellman-ford.js +11 -12
- package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -1
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts +44 -0
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js +195 -0
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js.map +1 -0
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts +20 -2
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/dijkstra.js +30 -14
- package/dist/src/algorithms/shortest-path/dijkstra.js.map +1 -1
- package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts +17 -0
- package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/floyd-warshall.js +17 -0
- package/dist/src/algorithms/shortest-path/floyd-warshall.js.map +1 -1
- package/dist/src/algorithms/shortest-path/index.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/index.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs-unified.d.ts +42 -0
- package/dist/src/algorithms/traversal/bfs-unified.d.ts.map +1 -0
- package/dist/src/algorithms/traversal/bfs-unified.js +370 -0
- package/dist/src/algorithms/traversal/bfs-unified.js.map +1 -0
- package/dist/src/algorithms/traversal/bfs-variants.d.ts +77 -0
- package/dist/src/algorithms/traversal/bfs-variants.d.ts.map +1 -0
- package/dist/src/algorithms/traversal/bfs-variants.js +402 -0
- package/dist/src/algorithms/traversal/bfs-variants.js.map +1 -0
- package/dist/src/algorithms/traversal/bfs.d.ts +4 -18
- package/dist/src/algorithms/traversal/bfs.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs.js +5 -182
- package/dist/src/algorithms/traversal/bfs.js.map +1 -1
- package/dist/src/algorithms/traversal/dfs.d.ts +10 -0
- package/dist/src/algorithms/traversal/dfs.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/dfs.js +48 -0
- package/dist/src/algorithms/traversal/dfs.js.map +1 -1
- package/dist/src/algorithms/traversal/index.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/index.js.map +1 -1
- package/dist/src/benchmark-all-algorithms.d.ts +25 -0
- package/dist/src/benchmark-all-algorithms.d.ts.map +1 -0
- package/dist/src/benchmark-all-algorithms.js +491 -0
- package/dist/src/benchmark-all-algorithms.js.map +1 -0
- package/dist/src/clustering/hierarchical.d.ts +23 -15
- package/dist/src/clustering/hierarchical.d.ts.map +1 -1
- package/dist/src/clustering/hierarchical.js +226 -224
- package/dist/src/clustering/hierarchical.js.map +1 -1
- package/dist/src/clustering/index.d.ts +4 -2
- package/dist/src/clustering/index.d.ts.map +1 -1
- package/dist/src/clustering/index.js +2 -2
- package/dist/src/clustering/index.js.map +1 -1
- package/dist/src/clustering/k-core.d.ts +17 -19
- package/dist/src/clustering/k-core.d.ts.map +1 -1
- package/dist/src/clustering/k-core.js +68 -30
- package/dist/src/clustering/k-core.js.map +1 -1
- package/dist/src/clustering/mcl.d.ts +6 -0
- package/dist/src/clustering/mcl.d.ts.map +1 -1
- package/dist/src/clustering/mcl.js +34 -3
- package/dist/src/clustering/mcl.js.map +1 -1
- package/dist/src/clustering/spectral.d.ts +11 -0
- package/dist/src/clustering/spectral.d.ts.map +1 -1
- package/dist/src/clustering/spectral.js +99 -47
- package/dist/src/clustering/spectral.js.map +1 -1
- package/dist/src/core/graph.d.ts +45 -0
- package/dist/src/core/graph.d.ts.map +1 -1
- package/dist/src/core/graph.js +49 -5
- package/dist/src/core/graph.js.map +1 -1
- package/dist/src/data-structures/index.d.ts.map +1 -1
- package/dist/src/data-structures/index.js.map +1 -1
- package/dist/src/data-structures/priority-queue.d.ts +18 -1
- package/dist/src/data-structures/priority-queue.d.ts.map +1 -1
- package/dist/src/data-structures/priority-queue.js +26 -5
- package/dist/src/data-structures/priority-queue.js.map +1 -1
- package/dist/src/data-structures/union-find.d.ts +21 -0
- package/dist/src/data-structures/union-find.d.ts.map +1 -1
- package/dist/src/data-structures/union-find.js +21 -0
- package/dist/src/data-structures/union-find.js.map +1 -1
- package/dist/src/flow/ford-fulkerson.d.ts +20 -12
- package/dist/src/flow/ford-fulkerson.d.ts.map +1 -1
- package/dist/src/flow/ford-fulkerson.js +170 -217
- package/dist/src/flow/ford-fulkerson.js.map +1 -1
- package/dist/src/flow/min-cut.d.ts +6 -8
- package/dist/src/flow/min-cut.d.ts.map +1 -1
- package/dist/src/flow/min-cut.js +34 -12
- package/dist/src/flow/min-cut.js.map +1 -1
- package/dist/src/index.d.ts +3 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +6 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/link-prediction/adamic-adar.d.ts +26 -0
- package/dist/src/link-prediction/adamic-adar.d.ts.map +1 -1
- package/dist/src/link-prediction/adamic-adar.js +47 -30
- package/dist/src/link-prediction/adamic-adar.js.map +1 -1
- package/dist/src/link-prediction/common-neighbors.d.ts +21 -0
- package/dist/src/link-prediction/common-neighbors.d.ts.map +1 -1
- package/dist/src/link-prediction/common-neighbors.js +31 -14
- package/dist/src/link-prediction/common-neighbors.js.map +1 -1
- package/dist/src/link-prediction/index.d.ts.map +1 -1
- package/dist/src/optimized/bit-packed.d.ts +193 -0
- package/dist/src/optimized/bit-packed.d.ts.map +1 -0
- package/dist/src/optimized/bit-packed.js +342 -0
- package/dist/src/optimized/bit-packed.js.map +1 -0
- package/dist/src/optimized/csr-graph.d.ts +149 -0
- package/dist/src/optimized/csr-graph.d.ts.map +1 -0
- package/dist/src/optimized/csr-graph.js +404 -0
- package/dist/src/optimized/csr-graph.js.map +1 -0
- package/dist/src/optimized/direction-optimized-bfs.d.ts +95 -0
- package/dist/src/optimized/direction-optimized-bfs.d.ts.map +1 -0
- package/dist/src/optimized/direction-optimized-bfs.js +242 -0
- package/dist/src/optimized/direction-optimized-bfs.js.map +1 -0
- package/dist/src/optimized/graph-adapter.d.ts +111 -0
- package/dist/src/optimized/graph-adapter.d.ts.map +1 -0
- package/dist/src/optimized/graph-adapter.js +194 -0
- package/dist/src/optimized/graph-adapter.js.map +1 -0
- package/dist/src/optimized/index.d.ts +14 -0
- package/dist/src/optimized/index.d.ts.map +1 -0
- package/dist/src/optimized/index.js +30 -0
- package/dist/src/optimized/index.js.map +1 -0
- package/dist/src/pathfinding/astar.d.ts +25 -9
- package/dist/src/pathfinding/astar.d.ts.map +1 -1
- package/dist/src/pathfinding/astar.js +25 -10
- package/dist/src/pathfinding/astar.js.map +1 -1
- package/dist/src/pathfinding/utils.d.ts +15 -6
- package/dist/src/pathfinding/utils.d.ts.map +1 -1
- package/dist/src/pathfinding/utils.js +19 -7
- package/dist/src/pathfinding/utils.js.map +1 -1
- package/dist/src/research/grsbm.d.ts +0 -1
- package/dist/src/research/grsbm.d.ts.map +1 -1
- package/dist/src/research/grsbm.js +28 -23
- package/dist/src/research/grsbm.js.map +1 -1
- package/dist/src/research/index.d.ts.map +1 -1
- package/dist/src/research/index.js.map +1 -1
- package/dist/src/research/sync.d.ts +0 -1
- package/dist/src/research/sync.d.ts.map +1 -1
- package/dist/src/research/sync.js +31 -36
- package/dist/src/research/sync.js.map +1 -1
- package/dist/src/research/terahac.d.ts +2 -1
- package/dist/src/research/terahac.d.ts.map +1 -1
- package/dist/src/research/terahac.js +39 -7
- package/dist/src/research/terahac.js.map +1 -1
- package/dist/src/types/index.d.ts +8 -0
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/utils/algorithm-utilities.d.ts +2 -0
- package/dist/src/utils/algorithm-utilities.d.ts.map +1 -0
- package/dist/src/utils/algorithm-utilities.js +2 -0
- package/dist/src/utils/algorithm-utilities.js.map +1 -0
- package/dist/src/utils/graph-converters.d.ts +110 -0
- package/dist/src/utils/graph-converters.d.ts.map +1 -0
- package/dist/src/utils/graph-converters.js +227 -0
- package/dist/src/utils/graph-converters.js.map +1 -0
- package/dist/src/utils/graph-utilities.d.ts +62 -0
- package/dist/src/utils/graph-utilities.d.ts.map +1 -0
- package/dist/src/utils/graph-utilities.js +148 -0
- package/dist/src/utils/graph-utilities.js.map +1 -0
- package/dist/src/utils/index.d.ts +5 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/index.js +5 -0
- package/dist/src/utils/index.js.map +1 -0
- package/dist/src/utils/math-utilities.d.ts +63 -0
- package/dist/src/utils/math-utilities.d.ts.map +1 -0
- package/dist/src/utils/math-utilities.js +132 -0
- package/dist/src/utils/math-utilities.js.map +1 -0
- package/dist/src/utils/matrix-utilities.d.ts +2 -0
- package/dist/src/utils/matrix-utilities.d.ts.map +1 -0
- package/dist/src/utils/matrix-utilities.js +2 -0
- package/dist/src/utils/matrix-utilities.js.map +1 -0
- package/dist/src/utils/optimization-helpers.d.ts +42 -0
- package/dist/src/utils/optimization-helpers.d.ts.map +1 -0
- package/dist/src/utils/optimization-helpers.js +59 -0
- package/dist/src/utils/optimization-helpers.js.map +1 -0
- package/dist/src/utils/priorityQueue.d.ts +24 -0
- package/dist/src/utils/priorityQueue.d.ts.map +1 -1
- package/dist/src/utils/priorityQueue.js +34 -4
- package/dist/src/utils/priorityQueue.js.map +1 -1
- package/package.json +58 -39
- package/src/algorithms/centrality/betweenness.ts +185 -200
- package/src/algorithms/centrality/closeness.ts +93 -194
- package/src/algorithms/centrality/degree.ts +18 -22
- package/src/algorithms/centrality/delta-pagerank-simple.ts +374 -0
- package/src/algorithms/centrality/delta-pagerank.ts +458 -0
- package/src/algorithms/centrality/eigenvector.ts +14 -15
- package/src/algorithms/centrality/hits.ts +17 -17
- package/src/algorithms/centrality/index.ts +20 -13
- package/src/algorithms/centrality/katz.ts +17 -25
- package/src/algorithms/centrality/pagerank.ts +91 -16
- package/src/algorithms/community/girvan-newman.ts +90 -86
- package/src/algorithms/community/index.ts +6 -5
- package/src/algorithms/community/label-propagation.ts +69 -48
- package/src/algorithms/community/leiden.ts +88 -65
- package/src/algorithms/community/louvain-optimized.ts +474 -0
- package/src/algorithms/community/louvain.ts +52 -122
- package/src/algorithms/community/modularity-utils.ts +134 -0
- package/src/algorithms/components/connected.ts +40 -17
- package/src/algorithms/matching/bipartite.ts +24 -53
- package/src/algorithms/matching/index.ts +4 -4
- package/src/algorithms/matching/isomorphism.ts +53 -23
- package/src/algorithms/mst/index.ts +3 -4
- package/src/algorithms/mst/kruskal.ts +21 -6
- package/src/algorithms/mst/prim.ts +13 -4
- package/src/algorithms/shortest-path/bellman-ford.ts +16 -28
- package/src/algorithms/shortest-path/bidirectional-dijkstra.ts +248 -0
- package/src/algorithms/shortest-path/dijkstra.ts +38 -30
- package/src/algorithms/shortest-path/floyd-warshall.ts +21 -4
- package/src/algorithms/shortest-path/index.ts +5 -5
- package/src/algorithms/traversal/bfs-unified.ts +437 -0
- package/src/algorithms/traversal/bfs-variants.ts +520 -0
- package/src/algorithms/traversal/bfs.ts +5 -229
- package/src/algorithms/traversal/dfs.ts +61 -42
- package/src/algorithms/traversal/index.ts +3 -3
- package/src/benchmark-all-algorithms.ts +580 -0
- package/src/clustering/hierarchical.ts +268 -272
- package/src/clustering/index.ts +8 -6
- package/src/clustering/k-core.ts +93 -61
- package/src/clustering/mcl.ts +53 -23
- package/src/clustering/spectral.ts +130 -72
- package/src/core/graph.ts +58 -18
- package/src/data-structures/index.ts +2 -2
- package/src/data-structures/priority-queue.ts +36 -11
- package/src/data-structures/union-find.ts +22 -1
- package/src/flow/ford-fulkerson.ts +221 -262
- package/src/flow/min-cut.ts +59 -51
- package/src/index.ts +9 -2
- package/src/link-prediction/adamic-adar.ts +75 -74
- package/src/link-prediction/common-neighbors.ts +49 -49
- package/src/link-prediction/index.ts +1 -1
- package/src/optimized/bit-packed.ts +390 -0
- package/src/optimized/csr-graph.ts +518 -0
- package/src/optimized/direction-optimized-bfs.ts +310 -0
- package/src/optimized/graph-adapter.ts +248 -0
- package/src/optimized/index.ts +34 -0
- package/src/pathfinding/astar.ts +40 -25
- package/src/pathfinding/utils.ts +20 -13
- package/src/research/grsbm.ts +36 -39
- package/src/research/index.ts +4 -4
- package/src/research/sync.ts +41 -54
- package/src/research/terahac.ts +63 -21
- package/src/types/index.ts +8 -0
- package/src/utils/algorithm-utilities.ts +2 -0
- package/src/utils/graph-converters.ts +269 -0
- package/src/utils/graph-utilities.ts +173 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/math-utilities.ts +149 -0
- package/src/utils/matrix-utilities.ts +2 -0
- package/src/utils/optimization-helpers.ts +87 -0
- package/src/utils/priorityQueue.ts +40 -6
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { DirectionOptimizedBFS } from "../../optimized/direction-optimized-bfs.js";
|
|
2
|
+
import { toCSRGraph } from "../../optimized/graph-adapter.js";
|
|
3
|
+
import { reconstructPath } from "../../utils/graph-utilities.js";
|
|
4
|
+
/**
|
|
5
|
+
* Unified BFS implementation that automatically optimizes for large graphs
|
|
6
|
+
*
|
|
7
|
+
* This module provides a single, user-friendly BFS implementation that:
|
|
8
|
+
* - Automatically uses Direction-Optimized BFS for large graphs (>10k nodes)
|
|
9
|
+
* - Maintains backward compatibility with existing APIs
|
|
10
|
+
* - Requires no configuration or understanding of optimization thresholds
|
|
11
|
+
* - Provides the best performance by default
|
|
12
|
+
*/
|
|
13
|
+
// Cache for CSR conversions to avoid repeated conversions
|
|
14
|
+
const csrCache = new WeakMap();
|
|
15
|
+
/**
|
|
16
|
+
* Get or create CSR representation of a graph
|
|
17
|
+
* @param graph - The input graph to convert
|
|
18
|
+
* @returns CSR graph representation (cached for efficiency)
|
|
19
|
+
*/
|
|
20
|
+
function getCSRGraph(graph) {
|
|
21
|
+
let csrGraph = csrCache.get(graph);
|
|
22
|
+
if (!csrGraph) {
|
|
23
|
+
csrGraph = toCSRGraph(graph);
|
|
24
|
+
csrCache.set(graph, csrGraph);
|
|
25
|
+
}
|
|
26
|
+
return csrGraph;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Perform breadth-first search starting from a given node
|
|
30
|
+
*
|
|
31
|
+
* Automatically uses the most optimized implementation based on graph size.
|
|
32
|
+
* No configuration needed - just call this function for the best performance.
|
|
33
|
+
* @param graph - The input graph to traverse
|
|
34
|
+
* @param startNode - The node to start the BFS from
|
|
35
|
+
* @param options - Traversal options (callbacks, target node, etc.)
|
|
36
|
+
* @returns Traversal result with visited nodes, order, and tree structure
|
|
37
|
+
*/
|
|
38
|
+
export function breadthFirstSearch(graph, startNode, options = {}) {
|
|
39
|
+
if (!graph.hasNode(startNode)) {
|
|
40
|
+
throw new Error(`Start node ${String(startNode)} not found in graph`);
|
|
41
|
+
}
|
|
42
|
+
// Automatically use optimized implementation for large graphs
|
|
43
|
+
if (graph.nodeCount > 10000) {
|
|
44
|
+
return breadthFirstSearchOptimized(graph, startNode, options);
|
|
45
|
+
}
|
|
46
|
+
// Standard implementation for smaller graphs
|
|
47
|
+
return breadthFirstSearchStandard(graph, startNode, options);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Standard BFS implementation for smaller graphs
|
|
51
|
+
* @param graph - The input graph to traverse
|
|
52
|
+
* @param startNode - The node to start the BFS from
|
|
53
|
+
* @param options - Traversal options (callbacks, target node, etc.)
|
|
54
|
+
* @returns Traversal result with visited nodes, order, and tree structure
|
|
55
|
+
*/
|
|
56
|
+
function breadthFirstSearchStandard(graph, startNode, options = {}) {
|
|
57
|
+
const visited = new Set();
|
|
58
|
+
const queue = [];
|
|
59
|
+
const order = [];
|
|
60
|
+
const tree = new Map();
|
|
61
|
+
// Initialize with start node
|
|
62
|
+
queue.push({ node: startNode, level: 0 });
|
|
63
|
+
visited.add(startNode);
|
|
64
|
+
tree.set(startNode, null);
|
|
65
|
+
while (queue.length > 0) {
|
|
66
|
+
const current = queue.shift();
|
|
67
|
+
if (!current) {
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
order.push(current.node);
|
|
71
|
+
// Call visitor callback if provided
|
|
72
|
+
if (options.visitCallback) {
|
|
73
|
+
options.visitCallback(current.node, current.level);
|
|
74
|
+
}
|
|
75
|
+
// Early termination if target found
|
|
76
|
+
if (options.targetNode && current.node === options.targetNode) {
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
// Explore neighbors
|
|
80
|
+
for (const neighbor of graph.neighbors(current.node)) {
|
|
81
|
+
if (!visited.has(neighbor)) {
|
|
82
|
+
visited.add(neighbor);
|
|
83
|
+
tree.set(neighbor, current.node);
|
|
84
|
+
queue.push({ node: neighbor, level: current.level + 1 });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return { visited, order, tree };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Optimized BFS implementation using Direction-Optimized BFS
|
|
92
|
+
* @param graph - The input graph to traverse
|
|
93
|
+
* @param startNode - The node to start the BFS from
|
|
94
|
+
* @param options - Traversal options (callbacks, target node, etc.)
|
|
95
|
+
* @returns Traversal result with visited nodes, order, and tree structure
|
|
96
|
+
*/
|
|
97
|
+
function breadthFirstSearchOptimized(graph, startNode, options = {}) {
|
|
98
|
+
const csrGraph = getCSRGraph(graph);
|
|
99
|
+
const dobfs = new DirectionOptimizedBFS(csrGraph, {
|
|
100
|
+
alpha: 15.0,
|
|
101
|
+
beta: 20.0,
|
|
102
|
+
});
|
|
103
|
+
const result = dobfs.search(startNode);
|
|
104
|
+
// Convert result to TraversalResult format
|
|
105
|
+
const visited = new Set();
|
|
106
|
+
const order = [];
|
|
107
|
+
const tree = new Map();
|
|
108
|
+
// Build traversal order using BFS from distances
|
|
109
|
+
const nodesByDistance = new Map();
|
|
110
|
+
let maxDistance = 0;
|
|
111
|
+
for (const [nodeId, distance] of result.distances) {
|
|
112
|
+
visited.add(nodeId);
|
|
113
|
+
tree.set(nodeId, result.parents.get(nodeId) ?? null);
|
|
114
|
+
if (!nodesByDistance.has(distance)) {
|
|
115
|
+
nodesByDistance.set(distance, []);
|
|
116
|
+
}
|
|
117
|
+
nodesByDistance.get(distance)?.push(nodeId);
|
|
118
|
+
maxDistance = Math.max(maxDistance, distance);
|
|
119
|
+
}
|
|
120
|
+
// Reconstruct BFS order
|
|
121
|
+
for (let d = 0; d <= maxDistance; d++) {
|
|
122
|
+
const nodes = nodesByDistance.get(d);
|
|
123
|
+
if (nodes) {
|
|
124
|
+
order.push(...nodes);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Handle visit callback if provided
|
|
128
|
+
if (options.visitCallback) {
|
|
129
|
+
for (const [nodeId, distance] of result.distances) {
|
|
130
|
+
options.visitCallback(nodeId, distance);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return { visited, order, tree };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Find shortest path between two nodes using BFS
|
|
137
|
+
*
|
|
138
|
+
* Automatically optimized for large graphs. Returns null if no path exists.
|
|
139
|
+
* @param graph - The input graph to search
|
|
140
|
+
* @param source - The source node ID
|
|
141
|
+
* @param target - The target node ID
|
|
142
|
+
* @returns Shortest path result or null if no path exists
|
|
143
|
+
*/
|
|
144
|
+
export function shortestPathBFS(graph, source, target) {
|
|
145
|
+
if (!graph.hasNode(source)) {
|
|
146
|
+
throw new Error(`Source node ${String(source)} not found in graph`);
|
|
147
|
+
}
|
|
148
|
+
if (!graph.hasNode(target)) {
|
|
149
|
+
throw new Error(`Target node ${String(target)} not found in graph`);
|
|
150
|
+
}
|
|
151
|
+
// Special case: source equals target
|
|
152
|
+
if (source === target) {
|
|
153
|
+
return {
|
|
154
|
+
distance: 0,
|
|
155
|
+
path: [source],
|
|
156
|
+
predecessor: new Map([[source, null]]),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
// Use optimized implementation for large graphs
|
|
160
|
+
if (graph.nodeCount > 10000) {
|
|
161
|
+
return shortestPathBFSOptimized(graph, source, target);
|
|
162
|
+
}
|
|
163
|
+
// Standard implementation
|
|
164
|
+
return shortestPathBFSStandard(graph, source, target);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Standard shortest path BFS
|
|
168
|
+
* @param graph - The input graph to search
|
|
169
|
+
* @param source - The source node ID
|
|
170
|
+
* @param target - The target node ID
|
|
171
|
+
* @returns Shortest path result or null if no path exists
|
|
172
|
+
*/
|
|
173
|
+
function shortestPathBFSStandard(graph, source, target) {
|
|
174
|
+
const visited = new Set();
|
|
175
|
+
const queue = [];
|
|
176
|
+
const predecessor = new Map();
|
|
177
|
+
// Initialize BFS
|
|
178
|
+
queue.push({ node: source, distance: 0 });
|
|
179
|
+
visited.add(source);
|
|
180
|
+
predecessor.set(source, null);
|
|
181
|
+
while (queue.length > 0) {
|
|
182
|
+
const current = queue.shift();
|
|
183
|
+
if (!current) {
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
// Target found
|
|
187
|
+
if (current.node === target) {
|
|
188
|
+
const path = reconstructPath(target, predecessor);
|
|
189
|
+
return {
|
|
190
|
+
distance: current.distance,
|
|
191
|
+
path,
|
|
192
|
+
predecessor,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
// Explore neighbors
|
|
196
|
+
for (const neighbor of graph.neighbors(current.node)) {
|
|
197
|
+
if (!visited.has(neighbor)) {
|
|
198
|
+
visited.add(neighbor);
|
|
199
|
+
predecessor.set(neighbor, current.node);
|
|
200
|
+
queue.push({ node: neighbor, distance: current.distance + 1 });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// No path found
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Optimized shortest path using Direction-Optimized BFS
|
|
209
|
+
* @param graph - The input graph to search
|
|
210
|
+
* @param source - The source node ID
|
|
211
|
+
* @param target - The target node ID
|
|
212
|
+
* @returns Shortest path result or null if no path exists
|
|
213
|
+
*/
|
|
214
|
+
function shortestPathBFSOptimized(graph, source, target) {
|
|
215
|
+
const csrGraph = getCSRGraph(graph);
|
|
216
|
+
const dobfs = new DirectionOptimizedBFS(csrGraph, {
|
|
217
|
+
alpha: 15.0,
|
|
218
|
+
beta: 20.0,
|
|
219
|
+
});
|
|
220
|
+
// Perform BFS
|
|
221
|
+
const result = dobfs.search(source);
|
|
222
|
+
// Check if target was reached
|
|
223
|
+
const distance = result.distances.get(target);
|
|
224
|
+
if (distance === undefined) {
|
|
225
|
+
return null; // No path found
|
|
226
|
+
}
|
|
227
|
+
// Reconstruct path
|
|
228
|
+
const path = reconstructPath(target, result.parents);
|
|
229
|
+
return {
|
|
230
|
+
distance,
|
|
231
|
+
path,
|
|
232
|
+
predecessor: result.parents,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Find shortest paths from source to all reachable nodes
|
|
237
|
+
*
|
|
238
|
+
* Automatically optimized for large graphs.
|
|
239
|
+
* @param graph - The input graph to search
|
|
240
|
+
* @param source - The source node ID to compute shortest paths from
|
|
241
|
+
* @returns Map of node IDs to their shortest path results
|
|
242
|
+
*/
|
|
243
|
+
export function singleSourceShortestPathBFS(graph, source) {
|
|
244
|
+
if (!graph.hasNode(source)) {
|
|
245
|
+
throw new Error(`Source node ${String(source)} not found in graph`);
|
|
246
|
+
}
|
|
247
|
+
// Use optimized implementation for large graphs
|
|
248
|
+
if (graph.nodeCount > 10000) {
|
|
249
|
+
return singleSourceShortestPathBFSOptimized(graph, source);
|
|
250
|
+
}
|
|
251
|
+
// Standard implementation
|
|
252
|
+
return singleSourceShortestPathBFSStandard(graph, source);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Standard single-source shortest paths
|
|
256
|
+
* @param graph - The input graph to search
|
|
257
|
+
* @param source - The source node ID to compute shortest paths from
|
|
258
|
+
* @returns Map of node IDs to their shortest path results
|
|
259
|
+
*/
|
|
260
|
+
function singleSourceShortestPathBFSStandard(graph, source) {
|
|
261
|
+
const results = new Map();
|
|
262
|
+
const visited = new Set();
|
|
263
|
+
const queue = [];
|
|
264
|
+
const predecessor = new Map();
|
|
265
|
+
const distances = new Map();
|
|
266
|
+
// Initialize BFS
|
|
267
|
+
queue.push({ node: source, distance: 0 });
|
|
268
|
+
visited.add(source);
|
|
269
|
+
predecessor.set(source, null);
|
|
270
|
+
distances.set(source, 0);
|
|
271
|
+
while (queue.length > 0) {
|
|
272
|
+
const current = queue.shift();
|
|
273
|
+
if (!current) {
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
// Explore neighbors
|
|
277
|
+
for (const neighbor of graph.neighbors(current.node)) {
|
|
278
|
+
if (!visited.has(neighbor)) {
|
|
279
|
+
visited.add(neighbor);
|
|
280
|
+
predecessor.set(neighbor, current.node);
|
|
281
|
+
distances.set(neighbor, current.distance + 1);
|
|
282
|
+
queue.push({ node: neighbor, distance: current.distance + 1 });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// Build results after BFS completes
|
|
287
|
+
// This avoids copying the predecessor map for each node
|
|
288
|
+
for (const [node, distance] of distances) {
|
|
289
|
+
const path = reconstructPath(node, predecessor);
|
|
290
|
+
results.set(node, {
|
|
291
|
+
distance,
|
|
292
|
+
path,
|
|
293
|
+
predecessor, // Share the same predecessor map
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
return results;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Optimized single-source shortest paths
|
|
300
|
+
* @param graph - The input graph to search
|
|
301
|
+
* @param source - The source node ID to compute shortest paths from
|
|
302
|
+
* @returns Map of node IDs to their shortest path results
|
|
303
|
+
*/
|
|
304
|
+
function singleSourceShortestPathBFSOptimized(graph, source) {
|
|
305
|
+
const csrGraph = getCSRGraph(graph);
|
|
306
|
+
const dobfs = new DirectionOptimizedBFS(csrGraph, {
|
|
307
|
+
alpha: 15.0,
|
|
308
|
+
beta: 20.0,
|
|
309
|
+
});
|
|
310
|
+
const bfsResult = dobfs.search(source);
|
|
311
|
+
// Convert to expected format
|
|
312
|
+
const results = new Map();
|
|
313
|
+
for (const [nodeId, distance] of bfsResult.distances) {
|
|
314
|
+
// Reconstruct path for each node
|
|
315
|
+
const path = reconstructPath(nodeId, bfsResult.parents);
|
|
316
|
+
results.set(nodeId, {
|
|
317
|
+
distance,
|
|
318
|
+
path,
|
|
319
|
+
predecessor: bfsResult.parents, // Share the same predecessor map
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
return results;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Check if the graph is bipartite using BFS coloring
|
|
326
|
+
*
|
|
327
|
+
* Note: This function does not use Direction-Optimized BFS as the
|
|
328
|
+
* coloring logic is specific and doesn't benefit from the optimization.
|
|
329
|
+
* @param graph - The undirected graph to check
|
|
330
|
+
* @returns True if the graph is bipartite, false otherwise
|
|
331
|
+
*/
|
|
332
|
+
export function isBipartite(graph) {
|
|
333
|
+
if (graph.isDirected) {
|
|
334
|
+
throw new Error("Bipartite test requires an undirected graph");
|
|
335
|
+
}
|
|
336
|
+
const color = new Map();
|
|
337
|
+
const visited = new Set();
|
|
338
|
+
// Check each connected component
|
|
339
|
+
for (const node of Array.from(graph.nodes())) {
|
|
340
|
+
if (!visited.has(node.id)) {
|
|
341
|
+
const queue = [node.id];
|
|
342
|
+
color.set(node.id, 0);
|
|
343
|
+
visited.add(node.id);
|
|
344
|
+
while (queue.length > 0) {
|
|
345
|
+
const current = queue.shift();
|
|
346
|
+
if (!current) {
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
const currentColor = color.get(current);
|
|
350
|
+
if (currentColor === undefined) {
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
for (const neighbor of Array.from(graph.neighbors(current))) {
|
|
354
|
+
if (!visited.has(neighbor)) {
|
|
355
|
+
// Color with opposite color
|
|
356
|
+
color.set(neighbor, currentColor === 0 ? 1 : 0);
|
|
357
|
+
visited.add(neighbor);
|
|
358
|
+
queue.push(neighbor);
|
|
359
|
+
}
|
|
360
|
+
else if (color.get(neighbor) === currentColor) {
|
|
361
|
+
// Same color as current node - not bipartite
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
//# sourceMappingURL=bfs-unified.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bfs-unified.js","sourceRoot":"","sources":["../../../../src/algorithms/traversal/bfs-unified.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4CAA4C,CAAC;AACnF,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAE9D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAEjE;;;;;;;;GAQG;AAEH,0DAA0D;AAC1D,MAAM,QAAQ,GAAG,IAAI,OAAO,EAAmB,CAAC;AAEhD;;;;GAIG;AACH,SAAS,WAAW,CAAC,KAAY;IAC7B,IAAI,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACnC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACZ,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;QAC7B,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAClC,CAAC;IAED,OAAO,QAAQ,CAAC;AACpB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAY,EAAE,SAAiB,EAAE,UAA4B,EAAE;IAC9F,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CAAC,cAAc,MAAM,CAAC,SAAS,CAAC,qBAAqB,CAAC,CAAC;IAC1E,CAAC;IAED,8DAA8D;IAC9D,IAAI,KAAK,CAAC,SAAS,GAAG,KAAK,EAAE,CAAC;QAC1B,OAAO,2BAA2B,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;IAClE,CAAC;IAED,6CAA6C;IAC7C,OAAO,0BAA0B,CAAC,KAAK,EAAE,SAAS,EAA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AAS,EAAE,CAAC;oBAC7B,SAAS;gBACb,CAAC;gBAED,KAAK,MAAM,QAAQ,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;oBAC1D,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;wBACzB,4BAA4B;wBAC5B,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;wBAChD,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;wBACtB,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;oBACzB,CAAC;yBAAM,IAAI,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,YAAY,EAAE,CAAC;wBAC9C,6CAA6C;wBAC7C,OAAO,KAAK,CAAC;oBACjB,CAAC;gBACL,CAAC;YACL,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Graph } from "../../core/graph.js";
|
|
2
|
+
import type { NodeId } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* BFS that tracks shortest path counts (for betweenness centrality)
|
|
5
|
+
*
|
|
6
|
+
* This variant of BFS computes:
|
|
7
|
+
* - Shortest distances from source to all reachable nodes
|
|
8
|
+
* - All predecessors on shortest paths
|
|
9
|
+
* - Number of shortest paths (sigma)
|
|
10
|
+
* - Stack of nodes in reverse BFS order
|
|
11
|
+
* @param graph - The input graph to traverse
|
|
12
|
+
* @param source - The source node to start BFS from
|
|
13
|
+
* @param options - Configuration options
|
|
14
|
+
* @param options.optimized - Whether to use CSR-optimized implementation for large graphs
|
|
15
|
+
* @returns Object containing distances, predecessors, path counts (sigma), and traversal stack
|
|
16
|
+
*/
|
|
17
|
+
export declare function bfsWithPathCounting(graph: Graph, source: NodeId, options?: {
|
|
18
|
+
optimized?: boolean;
|
|
19
|
+
}): {
|
|
20
|
+
distances: Map<NodeId, number>;
|
|
21
|
+
predecessors: Map<NodeId, NodeId[]>;
|
|
22
|
+
sigma: Map<NodeId, number>;
|
|
23
|
+
stack: NodeId[];
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* BFS that only returns distances (for closeness centrality)
|
|
27
|
+
*
|
|
28
|
+
* Optimized variant that skips predecessor tracking
|
|
29
|
+
* @param graph - The input graph to traverse
|
|
30
|
+
* @param source - The source node to start BFS from
|
|
31
|
+
* @param cutoff - Maximum distance to explore (optional)
|
|
32
|
+
* @param options - Configuration options
|
|
33
|
+
* @param options.optimized - Whether to use CSR-optimized implementation for large graphs
|
|
34
|
+
* @returns Map of node IDs to their shortest distances from the source
|
|
35
|
+
*/
|
|
36
|
+
export declare function bfsDistancesOnly(graph: Graph, source: NodeId, cutoff?: number, options?: {
|
|
37
|
+
optimized?: boolean;
|
|
38
|
+
}): Map<NodeId, number>;
|
|
39
|
+
/**
|
|
40
|
+
* BFS for bipartite checking with partition sets
|
|
41
|
+
*
|
|
42
|
+
* Returns whether the graph is bipartite and the two partitions if it is
|
|
43
|
+
* @param graph - The input graph to check for bipartiteness
|
|
44
|
+
* @returns Object with bipartite status and optional partition sets
|
|
45
|
+
*/
|
|
46
|
+
export declare function bfsColoringWithPartitions(graph: Graph): {
|
|
47
|
+
isBipartite: boolean;
|
|
48
|
+
partitions?: [Set<NodeId>, Set<NodeId>];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* BFS for finding augmenting paths (for flow algorithms)
|
|
52
|
+
*
|
|
53
|
+
* Finds a path from source to sink in a residual graph with positive capacity
|
|
54
|
+
* @param residualGraph - Map representing the residual graph with capacities
|
|
55
|
+
* @param source - The source node ID
|
|
56
|
+
* @param sink - The sink node ID
|
|
57
|
+
* @returns Object with path and capacity, or null if no path exists
|
|
58
|
+
*/
|
|
59
|
+
export declare function bfsAugmentingPath(residualGraph: Map<string, Map<string, number>>, source: string, sink: string): {
|
|
60
|
+
path: string[];
|
|
61
|
+
pathCapacity: number;
|
|
62
|
+
} | null;
|
|
63
|
+
/**
|
|
64
|
+
* BFS for weighted graphs using priority queue (simplified Dijkstra)
|
|
65
|
+
*
|
|
66
|
+
* Returns distances from source using edge weights
|
|
67
|
+
* @param graph - The input weighted graph to traverse
|
|
68
|
+
* @param source - The source node to start BFS from
|
|
69
|
+
* @param cutoff - Maximum distance to explore (optional)
|
|
70
|
+
* @param options - Configuration options
|
|
71
|
+
* @param options.optimized - Whether to use CSR-optimized implementation for large graphs
|
|
72
|
+
* @returns Map of node IDs to their shortest weighted distances from the source
|
|
73
|
+
*/
|
|
74
|
+
export declare function bfsWeightedDistances(graph: Graph, source: NodeId, cutoff?: number, options?: {
|
|
75
|
+
optimized?: boolean;
|
|
76
|
+
}): Map<NodeId, number>;
|
|
77
|
+
//# sourceMappingURL=bfs-variants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bfs-variants.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/traversal/bfs-variants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAKjD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AASnD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAC/B,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAO,GACtC;IACC,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IACpC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3B,KAAK,EAAE,MAAM,EA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