@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
|
@@ -1,40 +1,35 @@
|
|
|
1
|
+
import { bfsDistancesOnly, bfsWeightedDistances } from "../traversal/bfs-variants.js";
|
|
1
2
|
/**
|
|
2
|
-
* Calculate closeness centrality
|
|
3
|
+
* Calculate closeness centrality from distances
|
|
4
|
+
* @param distances - Map of node IDs to their distances from source
|
|
5
|
+
* @param sourceNode - The source node for distance calculations
|
|
6
|
+
* @param totalNodes - Total number of nodes in the graph
|
|
7
|
+
* @param options - Algorithm configuration options
|
|
8
|
+
* @returns The closeness centrality score for the source node
|
|
3
9
|
*/
|
|
4
|
-
|
|
5
|
-
const nodes = Array.from(graph.nodes()).map((node) => node.id);
|
|
6
|
-
const centrality = {};
|
|
7
|
-
for (const sourceNode of nodes) {
|
|
8
|
-
centrality[String(sourceNode)] = nodeClosenessCentrality(graph, sourceNode, options);
|
|
9
|
-
}
|
|
10
|
-
return centrality;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Calculate closeness centrality for a specific node
|
|
14
|
-
*/
|
|
15
|
-
export function nodeClosenessCentrality(graph, node, options = {}) {
|
|
16
|
-
if (!graph.hasNode(node)) {
|
|
17
|
-
throw new Error(`Node ${String(node)} not found in graph`);
|
|
18
|
-
}
|
|
19
|
-
const distances = singleSourceShortestPathLengths(graph, node, options.cutoff);
|
|
10
|
+
function calculateClosenessFromDistances(distances, sourceNode, totalNodes, options) {
|
|
20
11
|
if (distances.size <= 1) {
|
|
21
12
|
return 0; // No other nodes reachable
|
|
22
13
|
}
|
|
23
14
|
let centrality = 0;
|
|
24
15
|
if (options.harmonic) {
|
|
25
16
|
// Harmonic centrality: sum of reciprocals of distances
|
|
26
|
-
for (const [targetNode, distance] of
|
|
27
|
-
if (targetNode !==
|
|
17
|
+
for (const [targetNode, distance] of distances) {
|
|
18
|
+
if (targetNode !== sourceNode && distance > 0 && distance < Infinity) {
|
|
28
19
|
centrality += 1 / distance;
|
|
29
20
|
}
|
|
30
21
|
}
|
|
22
|
+
// Normalization for harmonic centrality
|
|
23
|
+
if (options.normalized && totalNodes > 1) {
|
|
24
|
+
centrality = centrality / (totalNodes - 1);
|
|
25
|
+
}
|
|
31
26
|
}
|
|
32
27
|
else {
|
|
33
28
|
// Standard closeness: reciprocal of sum of distances
|
|
34
29
|
let totalDistance = 0;
|
|
35
30
|
let reachableNodes = 0;
|
|
36
|
-
for (const [targetNode, distance] of
|
|
37
|
-
if (targetNode !==
|
|
31
|
+
for (const [targetNode, distance] of distances) {
|
|
32
|
+
if (targetNode !== sourceNode && distance < Infinity) {
|
|
38
33
|
totalDistance += distance;
|
|
39
34
|
reachableNodes++;
|
|
40
35
|
}
|
|
@@ -42,59 +37,64 @@ export function nodeClosenessCentrality(graph, node, options = {}) {
|
|
|
42
37
|
if (totalDistance > 0) {
|
|
43
38
|
centrality = 1 / totalDistance;
|
|
44
39
|
// Wasserman and Faust normalization for disconnected graphs
|
|
45
|
-
if (options.normalized) {
|
|
46
|
-
|
|
47
|
-
centrality = centrality * reachableNodes / (n - 1);
|
|
40
|
+
if (options.normalized && totalNodes > 1) {
|
|
41
|
+
centrality = (centrality * reachableNodes) / (totalNodes - 1);
|
|
48
42
|
}
|
|
49
43
|
}
|
|
50
44
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
45
|
+
return centrality;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Calculate closeness centrality for all nodes in the graph
|
|
49
|
+
*
|
|
50
|
+
* Closeness centrality measures how close a node is to all other nodes
|
|
51
|
+
* in the graph. It is the reciprocal of the sum of the shortest path
|
|
52
|
+
* distances to all other reachable nodes.
|
|
53
|
+
* @param graph - The input graph
|
|
54
|
+
* @param options - Algorithm options
|
|
55
|
+
* @returns Centrality scores for each node
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const graph = new Graph();
|
|
59
|
+
* graph.addEdge("A", "B");
|
|
60
|
+
* graph.addEdge("B", "C");
|
|
61
|
+
*
|
|
62
|
+
* const centrality = closenessCentrality(graph);
|
|
63
|
+
* // { A: 0.5, B: 1.0, C: 0.5 }
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* Time Complexity: O(V * (V + E)) for unweighted graphs
|
|
67
|
+
* Space Complexity: O(V)
|
|
68
|
+
*/
|
|
69
|
+
export function closenessCentrality(graph, options = {}) {
|
|
70
|
+
const nodes = Array.from(graph.nodes()).map((node) => node.id);
|
|
71
|
+
const centrality = {};
|
|
72
|
+
for (const sourceNode of nodes) {
|
|
73
|
+
centrality[String(sourceNode)] = nodeClosenessCentrality(graph, sourceNode, options);
|
|
57
74
|
}
|
|
58
75
|
return centrality;
|
|
59
76
|
}
|
|
60
77
|
/**
|
|
61
|
-
*
|
|
78
|
+
* Calculate closeness centrality for a specific node
|
|
79
|
+
* @param graph - The input graph to analyze
|
|
80
|
+
* @param node - The node to calculate centrality for
|
|
81
|
+
* @param options - Algorithm configuration options
|
|
82
|
+
* @returns The closeness centrality score for the node
|
|
62
83
|
*/
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const queue = [];
|
|
67
|
-
// Initialize
|
|
68
|
-
queue.push({ node: source, distance: 0 });
|
|
69
|
-
visited.add(source);
|
|
70
|
-
distances.set(source, 0);
|
|
71
|
-
while (queue.length > 0) {
|
|
72
|
-
const current = queue.shift();
|
|
73
|
-
if (!current) {
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
// Skip if beyond cutoff
|
|
77
|
-
if (cutoff !== undefined && current.distance >= cutoff) {
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
// Explore neighbors
|
|
81
|
-
for (const neighbor of Array.from(graph.neighbors(current.node))) {
|
|
82
|
-
if (!visited.has(neighbor)) {
|
|
83
|
-
const newDistance = current.distance + 1;
|
|
84
|
-
// Skip if beyond cutoff
|
|
85
|
-
if (cutoff !== undefined && newDistance > cutoff) {
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
visited.add(neighbor);
|
|
89
|
-
distances.set(neighbor, newDistance);
|
|
90
|
-
queue.push({ node: neighbor, distance: newDistance });
|
|
91
|
-
}
|
|
92
|
-
}
|
|
84
|
+
export function nodeClosenessCentrality(graph, node, options = {}) {
|
|
85
|
+
if (!graph.hasNode(node)) {
|
|
86
|
+
throw new Error(`Node ${String(node)} not found in graph`);
|
|
93
87
|
}
|
|
94
|
-
|
|
88
|
+
// Use optimized BFS variant for unweighted graphs
|
|
89
|
+
const distances = bfsDistancesOnly(graph, node, options.cutoff, options.optimized !== undefined ? { optimized: options.optimized } : {});
|
|
90
|
+
const totalNodes = graph.nodeCount;
|
|
91
|
+
return calculateClosenessFromDistances(distances, node, totalNodes, options);
|
|
95
92
|
}
|
|
96
93
|
/**
|
|
97
94
|
* Calculate weighted closeness centrality using Dijkstra's algorithm
|
|
95
|
+
* @param graph - The input graph to analyze
|
|
96
|
+
* @param options - Algorithm configuration options
|
|
97
|
+
* @returns Centrality scores for each node keyed by node ID
|
|
98
98
|
*/
|
|
99
99
|
export function weightedClosenessCentrality(graph, options = {}) {
|
|
100
100
|
const nodes = Array.from(graph.nodes()).map((node) => node.id);
|
|
@@ -106,97 +106,18 @@ export function weightedClosenessCentrality(graph, options = {}) {
|
|
|
106
106
|
}
|
|
107
107
|
/**
|
|
108
108
|
* Calculate weighted closeness centrality for a specific node using Dijkstra
|
|
109
|
+
* @param graph - The input graph to analyze
|
|
110
|
+
* @param node - The node to calculate centrality for
|
|
111
|
+
* @param options - Algorithm configuration options
|
|
112
|
+
* @returns The weighted closeness centrality score for the node
|
|
109
113
|
*/
|
|
110
114
|
export function nodeWeightedClosenessCentrality(graph, node, options = {}) {
|
|
111
115
|
if (!graph.hasNode(node)) {
|
|
112
116
|
throw new Error(`Node ${String(node)} not found in graph`);
|
|
113
117
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
let centrality = 0;
|
|
119
|
-
if (options.harmonic) {
|
|
120
|
-
// Harmonic centrality: sum of reciprocals of distances
|
|
121
|
-
for (const [targetNode, distance] of Array.from(distances)) {
|
|
122
|
-
if (targetNode !== node && distance > 0 && distance < Infinity) {
|
|
123
|
-
centrality += 1 / distance;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
// Standard closeness: reciprocal of sum of distances
|
|
129
|
-
let totalDistance = 0;
|
|
130
|
-
let reachableNodes = 0;
|
|
131
|
-
for (const [targetNode, distance] of Array.from(distances)) {
|
|
132
|
-
if (targetNode !== node && distance < Infinity) {
|
|
133
|
-
totalDistance += distance;
|
|
134
|
-
reachableNodes++;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (totalDistance > 0) {
|
|
138
|
-
centrality = 1 / totalDistance;
|
|
139
|
-
// Wasserman and Faust normalization for disconnected graphs
|
|
140
|
-
if (options.normalized) {
|
|
141
|
-
const n = Array.from(graph.nodes()).length;
|
|
142
|
-
centrality = centrality * reachableNodes / (n - 1);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
// Normalization for harmonic centrality
|
|
147
|
-
if (options.harmonic && options.normalized) {
|
|
148
|
-
const n = Array.from(graph.nodes()).length;
|
|
149
|
-
if (n > 1) {
|
|
150
|
-
centrality = centrality / (n - 1);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return centrality;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Single-source shortest path distances using Dijkstra's algorithm
|
|
157
|
-
*/
|
|
158
|
-
function dijkstraDistances(graph, source, cutoff) {
|
|
159
|
-
const distances = new Map();
|
|
160
|
-
const visited = new Set();
|
|
161
|
-
const pq = [];
|
|
162
|
-
// Initialize distances
|
|
163
|
-
for (const node of Array.from(graph.nodes())) {
|
|
164
|
-
distances.set(node.id, node.id === source ? 0 : Infinity);
|
|
165
|
-
}
|
|
166
|
-
pq.push({ node: source, distance: 0 });
|
|
167
|
-
while (pq.length > 0) {
|
|
168
|
-
// Simple priority queue (could be optimized with a proper heap)
|
|
169
|
-
pq.sort((a, b) => a.distance - b.distance);
|
|
170
|
-
const current = pq.shift();
|
|
171
|
-
if (!current || visited.has(current.node)) {
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
visited.add(current.node);
|
|
175
|
-
// Skip if beyond cutoff
|
|
176
|
-
if (cutoff !== undefined && current.distance > cutoff) {
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
// Explore neighbors
|
|
180
|
-
for (const neighbor of Array.from(graph.neighbors(current.node))) {
|
|
181
|
-
if (visited.has(neighbor)) {
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
const edge = graph.getEdge(current.node, neighbor);
|
|
185
|
-
if (!edge) {
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
const edgeWeight = edge.weight ?? 1;
|
|
189
|
-
const tentativeDistance = current.distance + edgeWeight;
|
|
190
|
-
const currentDistance = distances.get(neighbor) ?? Infinity;
|
|
191
|
-
if (tentativeDistance < currentDistance) {
|
|
192
|
-
distances.set(neighbor, tentativeDistance);
|
|
193
|
-
// Skip if beyond cutoff
|
|
194
|
-
if (cutoff === undefined || tentativeDistance <= cutoff) {
|
|
195
|
-
pq.push({ node: neighbor, distance: tentativeDistance });
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return distances;
|
|
118
|
+
// Use optimized weighted BFS variant (simplified Dijkstra)
|
|
119
|
+
const distances = bfsWeightedDistances(graph, node, options.cutoff, options.optimized !== undefined ? { optimized: options.optimized } : {});
|
|
120
|
+
const totalNodes = graph.nodeCount;
|
|
121
|
+
return calculateClosenessFromDistances(distances, node, totalNodes, options);
|
|
201
122
|
}
|
|
202
123
|
//# sourceMappingURL=closeness.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"closeness.js","sourceRoot":"","sources":["../../../../src/algorithms/centrality/closeness.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"closeness.js","sourceRoot":"","sources":["../../../../src/algorithms/centrality/closeness.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAgCtF;;;;;;;GAOG;AACH,SAAS,+BAA+B,CACpC,SAA8B,EAC9B,UAAkB,EAClB,UAAkB,EAClB,OAAmC;IAEnC,IAAI,SAAS,CAAC,IAAI,IAAI,CAAC,EAAE,CAAC;QACtB,OAAO,CAAC,CAAC,CAAC,2BAA2B;IACzC,CAAC;IAED,IAAI,UAAU,GAAG,CAAC,CAAC;IAEnB,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACnB,uDAAuD;QACvD,KAAK,MAAM,CAAC,UAAU,EAAE,QAAQ,CAAC,IAAI,SAAS,EAAE,CAAC;YAC7C,IAAI,UAAU,KAAK,UAAU,IAAI,QAAQ,GAAG,CAAC,IAAI,QAAQ,GAAG,QAAQ,EAAE,CAAC;gBACnE,UAAU,IAAI,CAAC,GAAG,QAAQ,CAAC;YAC/B,CAAC;QACL,CAAC;QAED,wCAAwC;QACxC,IAAI,OAAO,CAAC,UAAU,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;YACvC,UAAU,GAAG,UAAU,GAAG,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;QAC/C,CAAC;IACL,CAAC;SAAM,CAAC;QACJ,qDAAqD;QACrD,IAAI,aAAa,GAAG,CAAC,CAAC;QAC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,CAClC,KAAK,EACL,IAAI,EACJ,OAAO,CAAC,MAAM,EACd,OAAO,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAC1E,CAAC;IACF,MAAM,UAAU,GAAG,KAAK,CAAC,SAAS,CAAC;IAEnC,OAAO,+BAA+B,CAAC,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;AACjF,CAAC"}
|
|
@@ -8,10 +8,17 @@ import type { CentralityOptions, CentralityResult } from "../../types/index.js";
|
|
|
8
8
|
*/
|
|
9
9
|
/**
|
|
10
10
|
* Calculate degree centrality for all nodes in the graph
|
|
11
|
+
* @param graph - The input graph to analyze
|
|
12
|
+
* @param options - Algorithm configuration options
|
|
13
|
+
* @returns Centrality scores for each node keyed by node ID
|
|
11
14
|
*/
|
|
12
15
|
export declare function degreeCentrality(graph: Graph, options?: CentralityOptions): CentralityResult;
|
|
13
16
|
/**
|
|
14
17
|
* Calculate degree centrality for a specific node
|
|
18
|
+
* @param graph - The input graph to analyze
|
|
19
|
+
* @param nodeId - The node to calculate centrality for
|
|
20
|
+
* @param options - Algorithm configuration options
|
|
21
|
+
* @returns The degree centrality score for the node
|
|
15
22
|
*/
|
|
16
23
|
export declare function nodeDegreeCentrality(graph: Graph, nodeId: string | number, options?: CentralityOptions): number;
|
|
17
24
|
//# sourceMappingURL=degree.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"degree.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/centrality/degree.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"degree.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/centrality/degree.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAEhF;;;;;GAKG;AAEH;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,GAAE,iBAAsB,GAAG,gBAAgB,CAyChG;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,MAAM,CAkCnH"}
|
|
@@ -6,13 +6,16 @@
|
|
|
6
6
|
*/
|
|
7
7
|
/**
|
|
8
8
|
* Calculate degree centrality for all nodes in the graph
|
|
9
|
+
* @param graph - The input graph to analyze
|
|
10
|
+
* @param options - Algorithm configuration options
|
|
11
|
+
* @returns Centrality scores for each node keyed by node ID
|
|
9
12
|
*/
|
|
10
13
|
export function degreeCentrality(graph, options = {}) {
|
|
11
14
|
const centrality = {};
|
|
12
15
|
const { nodeCount } = graph;
|
|
13
|
-
// Calculate normalization factor
|
|
14
|
-
const maxPossibleDegree =
|
|
15
|
-
for (const node of
|
|
16
|
+
// Calculate normalization factor (same for both directed and undirected graphs)
|
|
17
|
+
const maxPossibleDegree = nodeCount - 1;
|
|
18
|
+
for (const node of graph.nodes()) {
|
|
16
19
|
let degree;
|
|
17
20
|
if (graph.isDirected && options.mode) {
|
|
18
21
|
// For directed graphs, calculate specific degree type
|
|
@@ -39,15 +42,17 @@ export function degreeCentrality(graph, options = {}) {
|
|
|
39
42
|
degree = graph.degree(node.id);
|
|
40
43
|
}
|
|
41
44
|
// Apply normalization if requested
|
|
42
|
-
const normalizedDegree = options.normalized && maxPossibleDegree > 0 ?
|
|
43
|
-
degree / maxPossibleDegree :
|
|
44
|
-
degree;
|
|
45
|
+
const normalizedDegree = options.normalized && maxPossibleDegree > 0 ? degree / maxPossibleDegree : degree;
|
|
45
46
|
centrality[node.id.toString()] = normalizedDegree;
|
|
46
47
|
}
|
|
47
48
|
return centrality;
|
|
48
49
|
}
|
|
49
50
|
/**
|
|
50
51
|
* Calculate degree centrality for a specific node
|
|
52
|
+
* @param graph - The input graph to analyze
|
|
53
|
+
* @param nodeId - The node to calculate centrality for
|
|
54
|
+
* @param options - Algorithm configuration options
|
|
55
|
+
* @returns The degree centrality score for the node
|
|
51
56
|
*/
|
|
52
57
|
export function nodeDegreeCentrality(graph, nodeId, options = {}) {
|
|
53
58
|
if (!graph.hasNode(nodeId)) {
|
|
@@ -79,8 +84,6 @@ export function nodeDegreeCentrality(graph, nodeId, options = {}) {
|
|
|
79
84
|
}
|
|
80
85
|
// Apply normalization if requested
|
|
81
86
|
const maxPossibleDegree = nodeCount - 1;
|
|
82
|
-
return options.normalized && maxPossibleDegree > 0 ?
|
|
83
|
-
degree / maxPossibleDegree :
|
|
84
|
-
degree;
|
|
87
|
+
return options.normalized && maxPossibleDegree > 0 ? degree / maxPossibleDegree : degree;
|
|
85
88
|
}
|
|
86
89
|
//# sourceMappingURL=degree.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"degree.js","sourceRoot":"","sources":["../../../../src/algorithms/centrality/degree.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH
|
|
1
|
+
{"version":3,"file":"degree.js","sourceRoot":"","sources":["../../../../src/algorithms/centrality/degree.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAY,EAAE,UAA6B,EAAE;IAC1E,MAAM,UAAU,GAAqB,EAAE,CAAC;IACxC,MAAM,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC;IAE5B,gFAAgF;IAChF,MAAM,iBAAiB,GAAG,SAAS,GAAG,CAAC,CAAC;IAExC,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;QAC/B,IAAI,MAAc,CAAC;QAEnB,IAAI,KAAK,CAAC,UAAU,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;YACnC,sDAAs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MAAM,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC;IAC5B,IAAI,MAAc,CAAC;IAEnB,IAAI,KAAK,CAAC,UAAU,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QACnC,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;YACnB,KAAK,IAAI,CAAC,CAAC,CAAC;gBACR,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;gBAChC,MAAM;YACV,CAAC;YACD,KAAK,KAAK,CAAC,CAAC,CAAC;gBACT,MAAM,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;gBACjC,MAAM;YACV,CAAC;YACD,KAAK,OAAO,CAAC,CAAC,CAAC;gBACX,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAC9B,MAAM;YACV,CAAC;YACD,OAAO,CAAC,CAAC,CAAC;gBACN,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAClC,CAAC;QACL,CAAC;IACL,CAAC;SAAM,CAAC;QACJ,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;IAED,mCAAmC;IACnC,MAAM,iBAAiB,GAAG,SAAS,GAAG,CAAC,CAAC;IAExC,OAAO,OAAO,CAAC,UAAU,IAAI,iBAAiB,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,MAAM,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Graph } from "../../core/graph.js";
|
|
2
|
+
import type { NodeId } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Simplified Delta-based PageRank that matches the standard algorithm
|
|
5
|
+
* but only processes active nodes for efficiency.
|
|
6
|
+
*
|
|
7
|
+
* Key optimization: Supports incremental updates when graph structure changes,
|
|
8
|
+
* avoiding full recomputation from scratch.
|
|
9
|
+
*/
|
|
10
|
+
export declare class SimpleDeltaPageRank {
|
|
11
|
+
private graph;
|
|
12
|
+
private nodeCount;
|
|
13
|
+
private previousScores;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new SimpleDeltaPageRank instance for the given graph.
|
|
16
|
+
* @param graph - The directed graph to compute PageRank on
|
|
17
|
+
*/
|
|
18
|
+
constructor(graph: Graph);
|
|
19
|
+
/**
|
|
20
|
+
* Computes PageRank scores for all nodes in the graph using power iteration.
|
|
21
|
+
* @param options - Configuration options for PageRank computation
|
|
22
|
+
* @param options.dampingFactor - Probability of following a link (default: 0.85)
|
|
23
|
+
* @param options.tolerance - Convergence tolerance threshold (default: 1e-6)
|
|
24
|
+
* @param options.maxIterations - Maximum number of iterations (default: 100)
|
|
25
|
+
* @param options.personalization - Personalization vector for Personalized PageRank
|
|
26
|
+
* @param options.weight - Edge attribute name for weighted PageRank
|
|
27
|
+
* @returns Map of node IDs to their PageRank scores
|
|
28
|
+
*/
|
|
29
|
+
compute(options?: {
|
|
30
|
+
dampingFactor?: number;
|
|
31
|
+
tolerance?: number;
|
|
32
|
+
maxIterations?: number;
|
|
33
|
+
personalization?: Map<NodeId, number>;
|
|
34
|
+
weight?: string;
|
|
35
|
+
}): Map<NodeId, number>;
|
|
36
|
+
/**
|
|
37
|
+
* Perform incremental update after graph modification.
|
|
38
|
+
* This is where delta-based approach provides significant speedup.
|
|
39
|
+
* @param modifiedNodes - Set of nodes that were modified (edges added/removed)
|
|
40
|
+
* @param options - Computation options
|
|
41
|
+
* @param options.dampingFactor - Probability of following a link (default: 0.85)
|
|
42
|
+
* @param options.tolerance - Convergence tolerance threshold (default: 1e-6)
|
|
43
|
+
* @param options.maxIterations - Maximum number of iterations (default: 100)
|
|
44
|
+
* @param options.personalization - Personalization vector for Personalized PageRank
|
|
45
|
+
* @param options.weight - Edge attribute name for weighted PageRank
|
|
46
|
+
* @returns Updated PageRank scores
|
|
47
|
+
*/
|
|
48
|
+
update(modifiedNodes: Set<NodeId>, options?: {
|
|
49
|
+
dampingFactor?: number;
|
|
50
|
+
tolerance?: number;
|
|
51
|
+
maxIterations?: number;
|
|
52
|
+
personalization?: Map<NodeId, number>;
|
|
53
|
+
weight?: string;
|
|
54
|
+
}): Map<NodeId, number>;
|
|
55
|
+
private normalizeMap;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=delta-pagerank-simple.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delta-pagerank-simple.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/centrality/delta-pagerank-simple.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAEnD;;;;;;GAMG;AACH,qBAAa,mBAAmB;IAC5B,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,cAAc,CAAoC;IAE1D;;;OAGG;gBACS,KAAK,EAAE,KAAK;IASxB;;;;;;;;;OASG;IACI,OAAO,CACV,OAAO,GAAE;QACL,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,eAAe,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACtC,MAAM,CAAC,EAAE,MAAM,CAAC;KACd,GACP,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;IA0ItB;;;;;;;;;;;OAWG;IACI,MAAM,CACT,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,EAC1B,OAAO,GAAE;QACL,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,eAAe,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACtC,MAAM,CAAC,EAAE,MAAM,CAAC;KACd,GACP,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;IA4JtB,OAAO,CAAC,YAAY;CAYvB"}
|