@graphty/algorithms 2.1.1 → 2.2.0
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 +139 -50
- package/dist/algorithms.js +15260 -10390
- package/dist/algorithms.js.map +1 -1
- package/dist/algorithms.standalone.js +26675 -21542
- package/dist/algorithms.standalone.js.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.d.ts +18 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.js +40 -1
- package/dist/src/algorithms/centrality/betweenness.js.map +1 -1
- package/dist/src/algorithms/centrality/closeness.d.ts +20 -0
- package/dist/src/algorithms/centrality/closeness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/closeness.js +93 -8
- package/dist/src/algorithms/centrality/closeness.js.map +1 -1
- package/dist/src/algorithms/centrality/degree.d.ts +9 -0
- package/dist/src/algorithms/centrality/degree.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/degree.js +18 -0
- package/dist/src/algorithms/centrality/degree.js.map +1 -1
- package/dist/src/algorithms/centrality/eigenvector.d.ts +10 -0
- package/dist/src/algorithms/centrality/eigenvector.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/eigenvector.js +29 -0
- package/dist/src/algorithms/centrality/eigenvector.js.map +1 -1
- package/dist/src/algorithms/centrality/hits.d.ts +9 -0
- package/dist/src/algorithms/centrality/hits.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/hits.js +23 -0
- package/dist/src/algorithms/centrality/hits.js.map +1 -1
- package/dist/src/algorithms/centrality/katz.d.ts +9 -0
- package/dist/src/algorithms/centrality/katz.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/katz.js +31 -0
- package/dist/src/algorithms/centrality/katz.js.map +1 -1
- package/dist/src/algorithms/centrality/pagerank.d.ts +13 -13
- package/dist/src/algorithms/centrality/pagerank.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/pagerank.js +47 -10
- package/dist/src/algorithms/centrality/pagerank.js.map +1 -1
- package/dist/src/algorithms/community/girvan-newman-legacy.d.ts +27 -0
- package/dist/src/algorithms/community/girvan-newman-legacy.d.ts.map +1 -0
- package/dist/src/algorithms/community/girvan-newman-legacy.js +335 -0
- package/dist/src/algorithms/community/girvan-newman-legacy.js.map +1 -0
- package/dist/src/algorithms/community/girvan-newman.d.ts +5 -3
- package/dist/src/algorithms/community/girvan-newman.d.ts.map +1 -1
- package/dist/src/algorithms/community/girvan-newman.js +34 -308
- package/dist/src/algorithms/community/girvan-newman.js.map +1 -1
- package/dist/src/algorithms/community/label-propagation.d.ts +9 -6
- package/dist/src/algorithms/community/label-propagation.d.ts.map +1 -1
- package/dist/src/algorithms/community/label-propagation.js +24 -110
- package/dist/src/algorithms/community/label-propagation.js.map +1 -1
- package/dist/src/algorithms/components/connected.d.ts.map +1 -1
- package/dist/src/algorithms/components/connected.js +28 -77
- package/dist/src/algorithms/components/connected.js.map +1 -1
- package/dist/src/algorithms/mst/kruskal.d.ts.map +1 -1
- package/dist/src/algorithms/mst/kruskal.js +59 -3
- package/dist/src/algorithms/mst/kruskal.js.map +1 -1
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/bellman-ford.js +46 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -1
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/dijkstra.js +27 -0
- package/dist/src/algorithms/shortest-path/dijkstra.js.map +1 -1
- package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts +15 -5
- package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/floyd-warshall.js +97 -107
- package/dist/src/algorithms/shortest-path/floyd-warshall.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs-unified.d.ts +2 -12
- package/dist/src/algorithms/traversal/bfs-unified.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs-unified.js +47 -308
- package/dist/src/algorithms/traversal/bfs-unified.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs-variants.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs-variants.js +5 -25
- package/dist/src/algorithms/traversal/bfs-variants.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs.d.ts +0 -3
- package/dist/src/algorithms/traversal/bfs.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs.js +0 -4
- package/dist/src/algorithms/traversal/bfs.js.map +1 -1
- package/dist/src/algorithms/traversal/dfs.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/dfs.js +33 -154
- package/dist/src/algorithms/traversal/dfs.js.map +1 -1
- package/dist/src/clustering/hierarchical-legacy.d.ts +56 -0
- package/dist/src/clustering/hierarchical-legacy.d.ts.map +1 -0
- package/dist/src/clustering/hierarchical-legacy.js +441 -0
- package/dist/src/clustering/hierarchical-legacy.js.map +1 -0
- package/dist/src/clustering/hierarchical.d.ts +7 -39
- package/dist/src/clustering/hierarchical.d.ts.map +1 -1
- package/dist/src/clustering/hierarchical.js +56 -417
- package/dist/src/clustering/hierarchical.js.map +1 -1
- package/dist/src/clustering/k-core-legacy.d.ts +30 -0
- package/dist/src/clustering/k-core-legacy.d.ts.map +1 -0
- package/dist/src/clustering/k-core-legacy.js +191 -0
- package/dist/src/clustering/k-core-legacy.js.map +1 -0
- package/dist/src/clustering/k-core.d.ts +8 -7
- package/dist/src/clustering/k-core.d.ts.map +1 -1
- package/dist/src/clustering/k-core.js +33 -174
- package/dist/src/clustering/k-core.js.map +1 -1
- package/dist/src/clustering/mcl-legacy.d.ts +42 -0
- package/dist/src/clustering/mcl-legacy.d.ts.map +1 -0
- package/dist/src/clustering/mcl-legacy.js +386 -0
- package/dist/src/clustering/mcl-legacy.js.map +1 -0
- package/dist/src/clustering/mcl.d.ts +9 -32
- package/dist/src/clustering/mcl.d.ts.map +1 -1
- package/dist/src/clustering/mcl.js +48 -374
- package/dist/src/clustering/mcl.js.map +1 -1
- package/dist/src/index.d.ts +29 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/indexed/accelerator.d.ts +141 -11
- package/dist/src/indexed/accelerator.d.ts.map +1 -1
- package/dist/src/indexed/accelerator.js +319 -5
- package/dist/src/indexed/accelerator.js.map +1 -1
- package/dist/src/indexed/all-pairs.d.ts +59 -0
- package/dist/src/indexed/all-pairs.d.ts.map +1 -0
- package/dist/src/indexed/all-pairs.js +274 -0
- package/dist/src/indexed/all-pairs.js.map +1 -0
- package/dist/src/indexed/bellman-ford.d.ts +25 -0
- package/dist/src/indexed/bellman-ford.d.ts.map +1 -0
- package/dist/src/indexed/bellman-ford.js +59 -0
- package/dist/src/indexed/bellman-ford.js.map +1 -0
- package/dist/src/indexed/betweenness.d.ts +85 -0
- package/dist/src/indexed/betweenness.d.ts.map +1 -0
- package/dist/src/indexed/betweenness.js +188 -0
- package/dist/src/indexed/betweenness.js.map +1 -0
- package/dist/src/indexed/bfs.d.ts +69 -2
- package/dist/src/indexed/bfs.d.ts.map +1 -1
- package/dist/src/indexed/bfs.js +148 -1
- package/dist/src/indexed/bfs.js.map +1 -1
- package/dist/src/indexed/bipartite.d.ts +37 -0
- package/dist/src/indexed/bipartite.d.ts.map +1 -0
- package/dist/src/indexed/bipartite.js +54 -0
- package/dist/src/indexed/bipartite.js.map +1 -0
- package/dist/src/indexed/closeness.d.ts +49 -0
- package/dist/src/indexed/closeness.d.ts.map +1 -0
- package/dist/src/indexed/closeness.js +127 -0
- package/dist/src/indexed/closeness.js.map +1 -0
- package/dist/src/indexed/common-neighbors.d.ts +14 -1
- package/dist/src/indexed/common-neighbors.d.ts.map +1 -1
- package/dist/src/indexed/common-neighbors.js +24 -14
- package/dist/src/indexed/common-neighbors.js.map +1 -1
- package/dist/src/indexed/degree.d.ts +19 -0
- package/dist/src/indexed/degree.d.ts.map +1 -0
- package/dist/src/indexed/degree.js +45 -0
- package/dist/src/indexed/degree.js.map +1 -0
- package/dist/src/indexed/delta-pagerank.d.ts +145 -0
- package/dist/src/indexed/delta-pagerank.d.ts.map +1 -0
- package/dist/src/indexed/delta-pagerank.js +428 -0
- package/dist/src/indexed/delta-pagerank.js.map +1 -0
- package/dist/src/indexed/dfs.d.ts +55 -0
- package/dist/src/indexed/dfs.d.ts.map +1 -0
- package/dist/src/indexed/dfs.js +146 -0
- package/dist/src/indexed/dfs.js.map +1 -0
- package/dist/src/indexed/eigenvector.d.ts +48 -0
- package/dist/src/indexed/eigenvector.d.ts.map +1 -0
- package/dist/src/indexed/eigenvector.js +162 -0
- package/dist/src/indexed/eigenvector.js.map +1 -0
- package/dist/src/indexed/facade.d.ts +109 -0
- package/dist/src/indexed/facade.d.ts.map +1 -0
- package/dist/src/indexed/facade.js +204 -0
- package/dist/src/indexed/facade.js.map +1 -0
- package/dist/src/indexed/flow.d.ts +120 -0
- package/dist/src/indexed/flow.d.ts.map +1 -0
- package/dist/src/indexed/flow.js +377 -0
- package/dist/src/indexed/flow.js.map +1 -0
- package/dist/src/indexed/girvan-newman.d.ts +42 -0
- package/dist/src/indexed/girvan-newman.d.ts.map +1 -0
- package/dist/src/indexed/girvan-newman.js +90 -0
- package/dist/src/indexed/girvan-newman.js.map +1 -0
- package/dist/src/indexed/grsbm.d.ts +85 -0
- package/dist/src/indexed/grsbm.d.ts.map +1 -0
- package/dist/src/indexed/grsbm.js +287 -0
- package/dist/src/indexed/grsbm.js.map +1 -0
- package/dist/src/indexed/hierarchical.d.ts +69 -0
- package/dist/src/indexed/hierarchical.d.ts.map +1 -0
- package/dist/src/indexed/hierarchical.js +245 -0
- package/dist/src/indexed/hierarchical.js.map +1 -0
- package/dist/src/indexed/index.d.ts +29 -3
- package/dist/src/indexed/index.d.ts.map +1 -1
- package/dist/src/indexed/index.js +29 -3
- package/dist/src/indexed/index.js.map +1 -1
- package/dist/src/indexed/isomorphism.d.ts +42 -0
- package/dist/src/indexed/isomorphism.d.ts.map +1 -0
- package/dist/src/indexed/isomorphism.js +269 -0
- package/dist/src/indexed/isomorphism.js.map +1 -0
- package/dist/src/indexed/label-propagation.d.ts +134 -0
- package/dist/src/indexed/label-propagation.d.ts.map +1 -0
- package/dist/src/indexed/label-propagation.js +486 -0
- package/dist/src/indexed/label-propagation.js.map +1 -0
- package/dist/src/indexed/leiden.d.ts +52 -0
- package/dist/src/indexed/leiden.d.ts.map +1 -0
- package/dist/src/indexed/leiden.js +384 -0
- package/dist/src/indexed/leiden.js.map +1 -0
- package/dist/src/indexed/link-prediction.d.ts +147 -0
- package/dist/src/indexed/link-prediction.d.ts.map +1 -0
- package/dist/src/indexed/link-prediction.js +317 -0
- package/dist/src/indexed/link-prediction.js.map +1 -0
- package/dist/src/indexed/markov.d.ts +54 -0
- package/dist/src/indexed/markov.d.ts.map +1 -0
- package/dist/src/indexed/markov.js +284 -0
- package/dist/src/indexed/markov.js.map +1 -0
- package/dist/src/indexed/matching.d.ts +53 -0
- package/dist/src/indexed/matching.d.ts.map +1 -0
- package/dist/src/indexed/matching.js +139 -0
- package/dist/src/indexed/matching.js.map +1 -0
- package/dist/src/indexed/min-cut.d.ts +48 -0
- package/dist/src/indexed/min-cut.d.ts.map +1 -0
- package/dist/src/indexed/min-cut.js +175 -0
- package/dist/src/indexed/min-cut.js.map +1 -0
- package/dist/src/indexed/modularity.d.ts +34 -0
- package/dist/src/indexed/modularity.d.ts.map +1 -0
- package/dist/src/indexed/modularity.js +74 -0
- package/dist/src/indexed/modularity.js.map +1 -0
- package/dist/src/indexed/mst.d.ts +29 -1
- package/dist/src/indexed/mst.d.ts.map +1 -1
- package/dist/src/indexed/mst.js +62 -0
- package/dist/src/indexed/mst.js.map +1 -1
- package/dist/src/indexed/pagerank.d.ts +29 -6
- package/dist/src/indexed/pagerank.d.ts.map +1 -1
- package/dist/src/indexed/pagerank.js +107 -11
- package/dist/src/indexed/pagerank.js.map +1 -1
- package/dist/src/indexed/point-to-point.d.ts +53 -0
- package/dist/src/indexed/point-to-point.d.ts.map +1 -0
- package/dist/src/indexed/point-to-point.js +173 -0
- package/dist/src/indexed/point-to-point.js.map +1 -0
- package/dist/src/indexed/scc.d.ts +53 -0
- package/dist/src/indexed/scc.d.ts.map +1 -0
- package/dist/src/indexed/scc.js +110 -0
- package/dist/src/indexed/scc.js.map +1 -0
- package/dist/src/indexed/spectral.d.ts +61 -0
- package/dist/src/indexed/spectral.d.ts.map +1 -0
- package/dist/src/indexed/spectral.js +476 -0
- package/dist/src/indexed/spectral.js.map +1 -0
- package/dist/src/indexed/structures/bit-set.d.ts +45 -0
- package/dist/src/indexed/structures/bit-set.d.ts.map +1 -0
- package/dist/src/indexed/structures/bit-set.js +57 -0
- package/dist/src/indexed/structures/bit-set.js.map +1 -0
- package/dist/src/indexed/structures/max-heap.d.ts +52 -0
- package/dist/src/indexed/structures/max-heap.d.ts.map +1 -0
- package/dist/src/indexed/structures/max-heap.js +66 -0
- package/dist/src/indexed/structures/max-heap.js.map +1 -0
- package/dist/src/indexed/structures/min-heap.d.ts +30 -1
- package/dist/src/indexed/structures/min-heap.d.ts.map +1 -1
- package/dist/src/indexed/structures/min-heap.js +46 -6
- package/dist/src/indexed/structures/min-heap.js.map +1 -1
- package/dist/src/indexed/structures/ring-queue.d.ts +41 -0
- package/dist/src/indexed/structures/ring-queue.d.ts.map +1 -0
- package/dist/src/indexed/structures/ring-queue.js +64 -0
- package/dist/src/indexed/structures/ring-queue.js.map +1 -0
- package/dist/src/indexed/sync.d.ts +50 -0
- package/dist/src/indexed/sync.d.ts.map +1 -0
- package/dist/src/indexed/sync.js +175 -0
- package/dist/src/indexed/sync.js.map +1 -0
- package/dist/src/indexed/terahac.d.ts +58 -0
- package/dist/src/indexed/terahac.d.ts.map +1 -0
- package/dist/src/indexed/terahac.js +244 -0
- package/dist/src/indexed/terahac.js.map +1 -0
- package/dist/src/indexed/to-snapshot.d.ts +45 -2
- package/dist/src/indexed/to-snapshot.d.ts.map +1 -1
- package/dist/src/indexed/to-snapshot.js +140 -6
- package/dist/src/indexed/to-snapshot.js.map +1 -1
- package/dist/src/link-prediction/common-neighbors-legacy.d.ts +71 -0
- package/dist/src/link-prediction/common-neighbors-legacy.d.ts.map +1 -0
- package/dist/src/link-prediction/common-neighbors-legacy.js +172 -0
- package/dist/src/link-prediction/common-neighbors-legacy.js.map +1 -0
- package/dist/src/link-prediction/common-neighbors.d.ts +11 -19
- package/dist/src/link-prediction/common-neighbors.d.ts.map +1 -1
- package/dist/src/link-prediction/common-neighbors.js +56 -123
- package/dist/src/link-prediction/common-neighbors.js.map +1 -1
- package/dist/src/optimized/direction-optimized-bfs.d.ts +6 -0
- package/dist/src/optimized/direction-optimized-bfs.d.ts.map +1 -1
- package/dist/src/optimized/direction-optimized-bfs.js +6 -0
- package/dist/src/optimized/direction-optimized-bfs.js.map +1 -1
- package/dist/src/research/grsbm-legacy.d.ts +82 -0
- package/dist/src/research/grsbm-legacy.d.ts.map +1 -0
- package/dist/src/research/grsbm-legacy.js +416 -0
- package/dist/src/research/grsbm-legacy.js.map +1 -0
- package/dist/src/research/grsbm.d.ts +9 -66
- package/dist/src/research/grsbm.d.ts.map +1 -1
- package/dist/src/research/grsbm.js +67 -391
- package/dist/src/research/grsbm.js.map +1 -1
- package/dist/src/research/sync-legacy.d.ts +48 -0
- package/dist/src/research/sync-legacy.d.ts.map +1 -0
- package/dist/src/research/sync-legacy.js +337 -0
- package/dist/src/research/sync-legacy.js.map +1 -0
- package/dist/src/research/sync.d.ts +10 -32
- package/dist/src/research/sync.d.ts.map +1 -1
- package/dist/src/research/sync.js +28 -316
- package/dist/src/research/sync.js.map +1 -1
- package/package.json +4 -3
- package/src/algorithms/centrality/betweenness.ts +51 -1
- package/src/algorithms/centrality/closeness.ts +112 -12
- package/src/algorithms/centrality/degree.ts +19 -0
- package/src/algorithms/centrality/eigenvector.ts +34 -0
- package/src/algorithms/centrality/hits.ts +24 -0
- package/src/algorithms/centrality/katz.ts +32 -0
- package/src/algorithms/centrality/pagerank.ts +53 -13
- package/src/algorithms/community/girvan-newman-legacy.ts +414 -0
- package/src/algorithms/community/girvan-newman.ts +36 -386
- package/src/algorithms/community/label-propagation.ts +24 -133
- package/src/algorithms/components/connected.ts +28 -95
- package/src/algorithms/mst/kruskal.ts +65 -4
- package/src/algorithms/shortest-path/bellman-ford.ts +49 -0
- package/src/algorithms/shortest-path/dijkstra.ts +29 -0
- package/src/algorithms/shortest-path/floyd-warshall.ts +106 -136
- package/src/algorithms/traversal/bfs-unified.ts +52 -366
- package/src/algorithms/traversal/bfs-variants.ts +5 -28
- package/src/algorithms/traversal/bfs.ts +0 -4
- package/src/algorithms/traversal/dfs.ts +39 -182
- package/src/clustering/hierarchical-legacy.ts +551 -0
- package/src/clustering/hierarchical.ts +64 -520
- package/src/clustering/k-core-legacy.ts +229 -0
- package/src/clustering/k-core.ts +38 -209
- package/src/clustering/mcl-legacy.ts +498 -0
- package/src/clustering/mcl.ts +46 -479
- package/src/index.ts +90 -3
- package/src/indexed/accelerator.ts +502 -16
- package/src/indexed/all-pairs.ts +342 -0
- package/src/indexed/bellman-ford.ts +72 -0
- package/src/indexed/betweenness.ts +258 -0
- package/src/indexed/bfs.ts +189 -3
- package/src/indexed/bipartite.ts +77 -0
- package/src/indexed/closeness.ts +160 -0
- package/src/indexed/common-neighbors.ts +31 -14
- package/src/indexed/degree.ts +55 -0
- package/src/indexed/delta-pagerank.ts +513 -0
- package/src/indexed/dfs.ts +189 -0
- package/src/indexed/eigenvector.ts +202 -0
- package/src/indexed/facade.ts +229 -0
- package/src/indexed/flow.ts +500 -0
- package/src/indexed/girvan-newman.ts +120 -0
- package/src/indexed/grsbm.ts +390 -0
- package/src/indexed/hierarchical.ts +295 -0
- package/src/indexed/index.ts +94 -3
- package/src/indexed/isomorphism.ts +305 -0
- package/src/indexed/label-propagation.ts +582 -0
- package/src/indexed/leiden.ts +436 -0
- package/src/indexed/link-prediction.ts +399 -0
- package/src/indexed/markov.ts +324 -0
- package/src/indexed/matching.ts +192 -0
- package/src/indexed/min-cut.ts +201 -0
- package/src/indexed/modularity.ts +81 -0
- package/src/indexed/mst.ts +79 -1
- package/src/indexed/pagerank.ts +128 -15
- package/src/indexed/point-to-point.ts +220 -0
- package/src/indexed/scc.ts +132 -0
- package/src/indexed/spectral.ts +550 -0
- package/src/indexed/structures/bit-set.ts +65 -0
- package/src/indexed/structures/max-heap.ts +74 -0
- package/src/indexed/structures/min-heap.ts +51 -6
- package/src/indexed/structures/ring-queue.ts +70 -0
- package/src/indexed/sync.ts +229 -0
- package/src/indexed/terahac.ts +290 -0
- package/src/indexed/to-snapshot.ts +156 -6
- package/src/link-prediction/common-neighbors-legacy.ts +252 -0
- package/src/link-prediction/common-neighbors.ts +65 -168
- package/src/optimized/direction-optimized-bfs.ts +6 -1
- package/src/research/grsbm-legacy.ts +586 -0
- package/src/research/grsbm.ts +76 -555
- package/src/research/sync-legacy.ts +456 -0
- package/src/research/sync.ts +30 -431
package/dist/src/indexed/mst.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { INVALID_INDEX } from "@graphty/graph-format";
|
|
2
|
+
import { IndexedMinHeap } from "./structures/min-heap.js";
|
|
1
3
|
import { IntUnionFind } from "./structures/union-find.js";
|
|
2
4
|
/**
|
|
3
5
|
* Kruskal's minimum spanning forest over logical edges.
|
|
@@ -45,4 +47,64 @@ export function kruskalMST(s, o = {}) {
|
|
|
45
47
|
}
|
|
46
48
|
return { edges: accepted.subarray(0, taken), totalWeight };
|
|
47
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Prim's minimum spanning tree, grown from a start node by always taking the cheapest arc from the
|
|
52
|
+
* tree to a node outside it (an indexed heap keyed per node, so of several parallel edges the
|
|
53
|
+
* cheapest, and of equal ones the lowest-indexed, is the one taken). `edges` lists the logical
|
|
54
|
+
* edges in the order they joined the tree and `totalWeight` sums them in that order, as legacy
|
|
55
|
+
* `primMST` does. The start index is checked: one outside the graph throws a RangeError.
|
|
56
|
+
* @param s - An undirected snapshot
|
|
57
|
+
* @param o - Per-arc weight override, start node and the spanning-forest switch
|
|
58
|
+
* @returns The accepted edges, their total weight and the discovery arc per node
|
|
59
|
+
* @throws Error on a directed snapshot, and on a disconnected one unless `forest` is set
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export function primMST(s, o = {}) {
|
|
63
|
+
if (s.directed) {
|
|
64
|
+
throw new Error("Prim's algorithm requires an undirected graph");
|
|
65
|
+
}
|
|
66
|
+
const { nodeCount: n, rowPtr, colIdx } = s;
|
|
67
|
+
const weights = o.weights ?? s.weights;
|
|
68
|
+
const key = new Float64Array(n).fill(Infinity);
|
|
69
|
+
const predArc = new Uint32Array(n).fill(INVALID_INDEX);
|
|
70
|
+
const inTree = new Uint8Array(n);
|
|
71
|
+
const heap = new IndexedMinHeap(n);
|
|
72
|
+
const accepted = new Uint32Array(Math.max(n - 1, 0));
|
|
73
|
+
let taken = 0;
|
|
74
|
+
let totalWeight = 0;
|
|
75
|
+
const grow = (root) => {
|
|
76
|
+
heap.push(root, 0);
|
|
77
|
+
while (!heap.isEmpty()) {
|
|
78
|
+
const u = heap.pop();
|
|
79
|
+
inTree[u] = 1;
|
|
80
|
+
if (predArc[u] !== INVALID_INDEX) {
|
|
81
|
+
accepted[taken++] = s.arcToEdge[predArc[u]];
|
|
82
|
+
totalWeight += key[u];
|
|
83
|
+
}
|
|
84
|
+
for (let a = rowPtr[u]; a < rowPtr[u + 1]; a++) {
|
|
85
|
+
const v = colIdx[a];
|
|
86
|
+
const w = weights === null ? 1 : weights[a];
|
|
87
|
+
if (inTree[v] === 0 && w < key[v]) {
|
|
88
|
+
key[v] = w;
|
|
89
|
+
predArc[v] = a;
|
|
90
|
+
heap.pushOrDecrease(v, w);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
if (n > 0) {
|
|
96
|
+
grow(o.start ?? 0);
|
|
97
|
+
}
|
|
98
|
+
if (o.forest === true) {
|
|
99
|
+
for (let root = 0; root < n; root++) {
|
|
100
|
+
if (inTree[root] === 0) {
|
|
101
|
+
grow(root);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else if (taken < n - 1) {
|
|
106
|
+
throw new Error("Graph is not connected");
|
|
107
|
+
}
|
|
108
|
+
return { edges: accepted.subarray(0, taken), totalWeight, predArc };
|
|
109
|
+
}
|
|
48
110
|
//# sourceMappingURL=mst.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mst.js","sourceRoot":"","sources":["../../../src/indexed/mst.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mst.js","sourceRoot":"","sources":["../../../src/indexed/mst.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAgC,MAAM,uBAAuB,CAAC;AAExG,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAgB1D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CAAC,CAAgB,EAAE,IAAgB,EAAE;IAC3D,MAAM,EAAE,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;IACxB,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC;IACtB,MAAM,IAAI,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;IACjC,IAAI,CAAC,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,6FAA6F;QAC7F,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACzB,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACnC,CAAC;IACL,CAAC;SAAM,IAAI,EAAE,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;QAC7B,K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AG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC9C,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC;AACxE,CAAC"}
|
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
import type { F64, GraphSnapshot } from "@graphty/graph-format";
|
|
2
|
-
/** Options of the index-based PageRank (graph-format design 14.2 Port 3
|
|
1
|
+
import type { F32, F64, GraphSnapshot } from "@graphty/graph-format";
|
|
2
|
+
/** Options of the index-based PageRank (graph-format design 14.2 Port 3). @public */
|
|
3
3
|
export interface PageRankOptions {
|
|
4
4
|
/** Probability of following a link; default 0.85. */
|
|
5
5
|
readonly dampingFactor?: number | undefined;
|
|
6
6
|
/** Iteration cap; default 100. */
|
|
7
7
|
readonly maxIterations?: number | undefined;
|
|
8
|
-
/**
|
|
8
|
+
/** Convergence tolerance on the per-iteration change; default 1e-6. */
|
|
9
9
|
readonly tolerance?: number | undefined;
|
|
10
10
|
/** Use the snapshot's arc weights; default false. */
|
|
11
11
|
readonly weighted?: boolean | undefined;
|
|
12
|
+
/** Starting rank per node index, normalised to sum 1 (when the sum is positive); default uniform. */
|
|
13
|
+
readonly initialRanks?: F32 | F64 | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* How the per-iteration change is measured against `tolerance`: `"l1"` (default) sums it over
|
|
16
|
+
* all nodes, `"max"` takes the largest single-node change, the legacy `pageRank` rule.
|
|
17
|
+
*/
|
|
18
|
+
readonly convergenceNorm?: "l1" | "max" | undefined;
|
|
12
19
|
}
|
|
13
20
|
/** Result of the index-based PageRank. @public */
|
|
14
21
|
export interface PageRankResult {
|
|
@@ -16,15 +23,31 @@ export interface PageRankResult {
|
|
|
16
23
|
readonly scores: F64;
|
|
17
24
|
/** Iterations actually run. */
|
|
18
25
|
readonly iterations: number;
|
|
19
|
-
/** Whether the
|
|
26
|
+
/** Whether the change fell below the tolerance. */
|
|
20
27
|
readonly converged: boolean;
|
|
21
28
|
}
|
|
22
29
|
/**
|
|
23
|
-
* PageRank by pull over `reverse()`, with the dangling mass redistributed uniformly.
|
|
24
|
-
*
|
|
30
|
+
* PageRank by pull over `reverse()`, with the dangling mass redistributed uniformly. On an
|
|
31
|
+
* undirected snapshot every edge carries rank both ways (a self-loop once), which is PageRank on
|
|
32
|
+
* the directed graph holding both arcs of every edge.
|
|
33
|
+
* @param s - The snapshot
|
|
25
34
|
* @param o - Algorithm options
|
|
26
35
|
* @returns The scores, the iteration count and the convergence flag
|
|
27
36
|
* @public
|
|
28
37
|
*/
|
|
29
38
|
export declare function pageRank(s: GraphSnapshot, o?: PageRankOptions): PageRankResult;
|
|
39
|
+
/**
|
|
40
|
+
* Personalized PageRank, as the legacy function computes it: the personalization vector,
|
|
41
|
+
* normalised to sum 1, replaces the uniform `1 / n` in the teleport, and each node receives
|
|
42
|
+
* `d * dangling / n` of the dangling mass scaled by its personalization. That keeps only a
|
|
43
|
+
* `1 / n` share of the dangling mass, so on a graph with a dangling node the scores sum to less
|
|
44
|
+
* than 1 (networkx spreads all of it). An all-zero vector gives plain PageRank, as the legacy
|
|
45
|
+
* function does for an empty list of personal nodes.
|
|
46
|
+
* @param s - The snapshot
|
|
47
|
+
* @param personalization - One finite, non-negative mass per node index
|
|
48
|
+
* @param o - Algorithm options
|
|
49
|
+
* @returns The scores, the iteration count and the convergence flag
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export declare function personalizedPageRank(s: GraphSnapshot, personalization: F32 | F64, o?: PageRankOptions): PageRankResult;
|
|
30
53
|
//# sourceMappingURL=pagerank.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagerank.d.ts","sourceRoot":"","sources":["../../../src/indexed/pagerank.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,aAAa,EAAiB,MAAM,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
|
/**
|
|
2
|
-
* PageRank by pull over `reverse()`, with the dangling mass redistributed uniformly.
|
|
3
|
-
*
|
|
2
|
+
* PageRank by pull over `reverse()`, with the dangling mass redistributed uniformly. On an
|
|
3
|
+
* undirected snapshot every edge carries rank both ways (a self-loop once), which is PageRank on
|
|
4
|
+
* the directed graph holding both arcs of every edge.
|
|
5
|
+
* @param s - The snapshot
|
|
4
6
|
* @param o - Algorithm options
|
|
5
7
|
* @returns The scores, the iteration count and the convergence flag
|
|
6
8
|
* @public
|
|
7
9
|
*/
|
|
8
10
|
export function pageRank(s, o = {}) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
return run(s, null, o);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Personalized PageRank, as the legacy function computes it: the personalization vector,
|
|
15
|
+
* normalised to sum 1, replaces the uniform `1 / n` in the teleport, and each node receives
|
|
16
|
+
* `d * dangling / n` of the dangling mass scaled by its personalization. That keeps only a
|
|
17
|
+
* `1 / n` share of the dangling mass, so on a graph with a dangling node the scores sum to less
|
|
18
|
+
* than 1 (networkx spreads all of it). An all-zero vector gives plain PageRank, as the legacy
|
|
19
|
+
* function does for an empty list of personal nodes.
|
|
20
|
+
* @param s - The snapshot
|
|
21
|
+
* @param personalization - One finite, non-negative mass per node index
|
|
22
|
+
* @param o - Algorithm options
|
|
23
|
+
* @returns The scores, the iteration count and the convergence flag
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export function personalizedPageRank(s, personalization, o = {}) {
|
|
27
|
+
return run(s, distribution(personalization, s.nodeCount, "personalization"), o);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The shared power iteration.
|
|
31
|
+
* @param s - The snapshot
|
|
32
|
+
* @param p - The normalised personalization, or null for the uniform teleport
|
|
33
|
+
* @param o - Algorithm options
|
|
34
|
+
* @returns The scores, the iteration count and the convergence flag
|
|
35
|
+
*/
|
|
36
|
+
function run(s, p, o) {
|
|
12
37
|
const n = s.nodeCount;
|
|
13
38
|
const d = o.dampingFactor ?? 0.85;
|
|
14
39
|
const maxIter = o.maxIterations ?? 100;
|
|
15
40
|
const tol = o.tolerance ?? 1e-6;
|
|
41
|
+
const useMax = o.convergenceNorm === "max";
|
|
42
|
+
if (n === 0) {
|
|
43
|
+
return { scores: new Float64Array(0), iterations: 0, converged: true };
|
|
44
|
+
}
|
|
16
45
|
const rev = s.reverse();
|
|
17
46
|
const weighted = o.weighted === true && rev.weights !== null;
|
|
18
|
-
|
|
19
|
-
|
|
47
|
+
// The f64 shadow toSnapshot keeps when a weight is not f32-exact, else the f32 arc weights.
|
|
48
|
+
const shadow = weighted ? s.edges.byRole("weight") : null;
|
|
49
|
+
const exact = shadow?.dtype === "f64" ? shadow.data : null;
|
|
50
|
+
let revW = weighted ? rev.weights : null;
|
|
51
|
+
let outW = weighted ? s.weightedOutDegree() : s.outDegree();
|
|
52
|
+
if (exact !== null) {
|
|
53
|
+
revW = gather(exact, rev.arcToEdge);
|
|
54
|
+
outW = rowSums(s, gather(exact, s.arcToEdge));
|
|
55
|
+
}
|
|
56
|
+
let rank = (o.initialRanks === undefined ? null : distribution(o.initialRanks, n, "initialRanks")) ??
|
|
57
|
+
new Float64Array(n).fill(o.initialRanks === undefined ? 1 / n : 0);
|
|
20
58
|
let next = new Float64Array(n);
|
|
21
59
|
let it = 0;
|
|
22
60
|
let converged = false;
|
|
@@ -27,7 +65,9 @@ export function pageRank(s, o = {}) {
|
|
|
27
65
|
dangling += rank[u];
|
|
28
66
|
}
|
|
29
67
|
}
|
|
30
|
-
|
|
68
|
+
// Teleport (1 - d) spread by p or uniformly; the dangling mass d * dangling spread uniformly,
|
|
69
|
+
// and with p scaled by p[v] on top of the uniform 1 / n, as the legacy function does.
|
|
70
|
+
const spread = 1 - d + d * dangling * (p === null ? 1 : 1 / n);
|
|
31
71
|
let delta = 0;
|
|
32
72
|
for (let v = 0; v < n; v++) {
|
|
33
73
|
let acc = 0;
|
|
@@ -36,15 +76,71 @@ export function pageRank(s, o = {}) {
|
|
|
36
76
|
const u = rev.colIdx[a];
|
|
37
77
|
const ow = outW[u];
|
|
38
78
|
if (ow > 0) {
|
|
39
|
-
acc += (rank[u] * (
|
|
79
|
+
acc += (rank[u] * (revW === null ? 1 : revW[a])) / ow;
|
|
40
80
|
}
|
|
41
81
|
}
|
|
42
|
-
next[v] =
|
|
43
|
-
|
|
82
|
+
next[v] = spread * (p === null ? 1 / n : p[v]) + d * acc;
|
|
83
|
+
const change = Math.abs(next[v] - rank[v]);
|
|
84
|
+
delta = useMax ? Math.max(delta, change) : delta + change;
|
|
44
85
|
}
|
|
45
86
|
[rank, next] = [next, rank];
|
|
46
87
|
converged = delta < tol;
|
|
47
88
|
}
|
|
48
89
|
return { scores: rank, iterations: it, converged };
|
|
49
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* One value per arc, read through the arc's logical edge.
|
|
93
|
+
* @param perEdge - One value per logical edge
|
|
94
|
+
* @param arcToEdge - The logical edge of every arc
|
|
95
|
+
* @returns One value per arc
|
|
96
|
+
*/
|
|
97
|
+
function gather(perEdge, arcToEdge) {
|
|
98
|
+
const out = new Float64Array(arcToEdge.length);
|
|
99
|
+
for (let a = 0; a < out.length; a++) {
|
|
100
|
+
out[a] = perEdge[arcToEdge[a]];
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The sum of each node's out-arc values.
|
|
106
|
+
* @param s - The snapshot
|
|
107
|
+
* @param perArc - One value per forward arc
|
|
108
|
+
* @returns One sum per node
|
|
109
|
+
*/
|
|
110
|
+
function rowSums(s, perArc) {
|
|
111
|
+
const out = new Float64Array(s.nodeCount);
|
|
112
|
+
for (let u = 0; u < s.nodeCount; u++) {
|
|
113
|
+
for (let a = s.rowPtr[u]; a < s.rowPtr[u + 1]; a++) {
|
|
114
|
+
out[u] += perArc[a];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Check a per-node mass vector and normalise it to sum 1.
|
|
121
|
+
* @param v - The caller's vector
|
|
122
|
+
* @param n - The node count
|
|
123
|
+
* @param name - The option's name, for the message
|
|
124
|
+
* @returns A normalised copy, or null when every entry is 0
|
|
125
|
+
*/
|
|
126
|
+
function distribution(v, n, name) {
|
|
127
|
+
if (v.length !== n) {
|
|
128
|
+
throw new Error(`PageRank: ${name} has ${String(v.length)} entries for ${String(n)} nodes`);
|
|
129
|
+
}
|
|
130
|
+
let total = 0;
|
|
131
|
+
for (const mass of v) {
|
|
132
|
+
if (!Number.isFinite(mass) || mass < 0) {
|
|
133
|
+
throw new Error(`PageRank: ${name} must be finite and non-negative, got ${String(mass)}`);
|
|
134
|
+
}
|
|
135
|
+
total += mass;
|
|
136
|
+
}
|
|
137
|
+
if (total === 0) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
const out = new Float64Array(n);
|
|
141
|
+
for (let u = 0; u < n; u++) {
|
|
142
|
+
out[u] = v[u] / total;
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
50
146
|
//# sourceMappingURL=pagerank.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagerank.js","sourceRoot":"","sources":["../../../src/indexed/pagerank.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pagerank.js","sourceRoot":"","sources":["../../../src/indexed/pagerank.ts"],"names":[],"mappings":"AA+BA;;;;;;;;GAQG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAgB,EAAE,IAAqB,EAAE;IAC9D,OAAO,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AAC3B,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,oBAAoB,CAChC,CAAgB,EAChB,eAA0B,EAC1B,IAAqB,EAAE;IAEvB,OAAO,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC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CAAC,aAAa,IAAI,QAAQ,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,gBAAgB,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;IAChG,CAAC;IACD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,IAAI,IAAI,CAAC,EAAE,CAAC;QACnB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,aAAa,IAAI,yCAAyC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9F,CAAC;QACD,KAAK,IAAI,IAAI,CAAC;IAClB,CAAC;IACD,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;IAChC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACzB,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;IAC1B,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type AdjacencyView, type F64, type GraphSnapshot, type NumericVector, type U32 } from "@graphty/graph-format";
|
|
2
|
+
/** Options of the point-to-point searches. @public */
|
|
3
|
+
export interface PathOptions {
|
|
4
|
+
/** Per-arc weight override, arcCount long -- the facade passes `expandEdges(s, shadow.data)`. */
|
|
5
|
+
readonly weights?: NumericVector | undefined;
|
|
6
|
+
}
|
|
7
|
+
/** One shortest path between two nodes. @public */
|
|
8
|
+
export interface PathResult {
|
|
9
|
+
/** Sum of the path's weights, added from the source; Infinity when the target is unreachable. */
|
|
10
|
+
readonly distance: number;
|
|
11
|
+
/** Node indices from the source to the target inclusive; empty when unreachable. */
|
|
12
|
+
readonly path: U32;
|
|
13
|
+
/** Logical edge indices along the path, one fewer than `path`: the exact parallel edge taken. */
|
|
14
|
+
readonly edges: U32;
|
|
15
|
+
}
|
|
16
|
+
/** A* result: the path plus the search state the legacy `astarWithDetails` reports. @public */
|
|
17
|
+
export interface AstarResult extends PathResult {
|
|
18
|
+
/** Best known cost from the source per node; Infinity for a node the search never reached. */
|
|
19
|
+
readonly gScore: F64;
|
|
20
|
+
/** `gScore` plus the heuristic, per node; Infinity for a node the search never reached. */
|
|
21
|
+
readonly fScore: F64;
|
|
22
|
+
/** Nodes expanded, in expansion order; the target is not expanded, so it is not listed. */
|
|
23
|
+
readonly visited: U32;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Bidirectional Dijkstra between two nodes: one search forward from the source, one backward from
|
|
27
|
+
* the target over `reverse()`, each step taken by the side whose next key is smaller, stopping once
|
|
28
|
+
* the two next keys sum to at least the best path found. That is the point-to-point query legacy
|
|
29
|
+
* `dijkstraPath` answers. The path is recorded as ARCS, so a parallel edge on it is the exact edge.
|
|
30
|
+
* @param s - The snapshot to search
|
|
31
|
+
* @param source - The node index to start from
|
|
32
|
+
* @param target - The node index to reach
|
|
33
|
+
* @param options - Per-arc weight override
|
|
34
|
+
* @returns The distance, the node path and the logical edges on it
|
|
35
|
+
* @throws Error when any weight is negative
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export declare function bidirectionalDijkstra(s: GraphSnapshot, source: number, target: number, options?: PathOptions): PathResult;
|
|
39
|
+
/**
|
|
40
|
+
* A* between two nodes: Dijkstra keyed by cost so far plus `heuristic(node, target)`, stopping when
|
|
41
|
+
* the target is taken from the queue. An expanded node is never reopened, as in legacy `astar`, so
|
|
42
|
+
* the path is shortest when the heuristic is consistent. The predecessor is the relaxing ARC, so a
|
|
43
|
+
* parallel edge on the path is the exact edge.
|
|
44
|
+
* @param g - The adjacency to search
|
|
45
|
+
* @param source - The node index to start from
|
|
46
|
+
* @param target - The node index to reach
|
|
47
|
+
* @param heuristic - Estimated remaining cost from a node index to the target index
|
|
48
|
+
* @param options - Per-arc weight override
|
|
49
|
+
* @returns The path, plus the per-node scores and the expansion order
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export declare function astar(g: AdjacencyView, source: number, target: number, heuristic: (node: number, target: number) => number, options?: PathOptions): AstarResult;
|
|
53
|
+
//# sourceMappingURL=point-to-point.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"point-to-point.d.ts","sourceRoot":"","sources":["../../../src/indexed/point-to-point.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,aAAa,EAClB,KAAK,GAAG,EACR,KAAK,aAAa,EAElB,KAAK,aAAa,EAClB,KAAK,GAAG,EACX,MAAM,uBAAuB,CAAC;AAK/B,sDAAsD;AACtD,MAAM,WAAW,WAAW;IACxB,iGAAiG;IACjG,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;CAChD;AAED,mDAAmD;AACnD,MAAM,WAAW,UAAU;IACvB,iGAAiG;IACjG,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,oFAAoF;IACpF,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC;IACnB,iGAAiG;IACjG,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC;CACvB;AAED,+FAA+F;AAC/F,MAAM,WAAW,WAAY,SAAQ,UAAU;IAC3C,8FAA8F;IAC9F,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC;IACrB,2FAA2F;IAC3F,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC;IACrB,2FAA2F;IAC3F,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC;CACzB;AAID;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CACjC,CAAC,EAAE,aAAa,EAChB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,WAAgB,GAC1B,UAAU,CAkGZ;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CACjB,CAAC,EAAE,aAAa,EAChB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,EACnD,OAAO,GAAE,WAAgB,GAC1B,WAAW,CA0Cb"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { INVALID_INDEX, } from "@graphty/graph-format";
|
|
2
|
+
import { walkPredArcs, walkPredEdges } from "./dijkstra.js";
|
|
3
|
+
import { IndexedMinHeap } from "./structures/min-heap.js";
|
|
4
|
+
const NO_PATH = { distance: Infinity, path: new Uint32Array(0), edges: new Uint32Array(0) };
|
|
5
|
+
/**
|
|
6
|
+
* Bidirectional Dijkstra between two nodes: one search forward from the source, one backward from
|
|
7
|
+
* the target over `reverse()`, each step taken by the side whose next key is smaller, stopping once
|
|
8
|
+
* the two next keys sum to at least the best path found. That is the point-to-point query legacy
|
|
9
|
+
* `dijkstraPath` answers. The path is recorded as ARCS, so a parallel edge on it is the exact edge.
|
|
10
|
+
* @param s - The snapshot to search
|
|
11
|
+
* @param source - The node index to start from
|
|
12
|
+
* @param target - The node index to reach
|
|
13
|
+
* @param options - Per-arc weight override
|
|
14
|
+
* @returns The distance, the node path and the logical edges on it
|
|
15
|
+
* @throws Error when any weight is negative
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export function bidirectionalDijkstra(s, source, target, options = {}) {
|
|
19
|
+
if (source === target) {
|
|
20
|
+
return { distance: 0, path: Uint32Array.of(source), edges: new Uint32Array(0) };
|
|
21
|
+
}
|
|
22
|
+
const n = s.nodeCount;
|
|
23
|
+
const weights = options.weights ?? s.weights;
|
|
24
|
+
// Checked up front, not per relaxed arc: the search stops early, so a per-arc check would miss
|
|
25
|
+
// a negative edge beyond the meeting point that legacy (which runs both searches out) refuses.
|
|
26
|
+
if (weights?.some((w) => w < 0) === true) {
|
|
27
|
+
throw new Error("Bidirectional Dijkstra does not support negative edge weights");
|
|
28
|
+
}
|
|
29
|
+
const rev = s.reverse();
|
|
30
|
+
const weightOf = (arc) => (weights === null ? 1 : weights[arc]);
|
|
31
|
+
const distF = new Float64Array(n).fill(Infinity);
|
|
32
|
+
const distB = new Float64Array(n).fill(Infinity);
|
|
33
|
+
// Forward: the arc into each node. Backward: the reverse-view arc that reached each node from
|
|
34
|
+
// its successor toward the target -- on an undirected snapshot that arc points the other way,
|
|
35
|
+
// so the successor node is kept too rather than read off the arc.
|
|
36
|
+
const predArc = new Uint32Array(n).fill(INVALID_INDEX);
|
|
37
|
+
const succRevArc = new Uint32Array(n).fill(INVALID_INDEX);
|
|
38
|
+
const succNode = new Uint32Array(n).fill(INVALID_INDEX);
|
|
39
|
+
const heapF = new IndexedMinHeap(n);
|
|
40
|
+
const heapB = new IndexedMinHeap(n);
|
|
41
|
+
distF[source] = 0;
|
|
42
|
+
distB[target] = 0;
|
|
43
|
+
heapF.push(source, 0);
|
|
44
|
+
heapB.push(target, 0);
|
|
45
|
+
let best = Infinity;
|
|
46
|
+
// The step joining the two searches: meetFrom -> meetTo over logical edge meetEdge.
|
|
47
|
+
let meetFrom = INVALID_INDEX;
|
|
48
|
+
let meetTo = INVALID_INDEX;
|
|
49
|
+
let meetEdge = INVALID_INDEX;
|
|
50
|
+
let meetWeight = 0;
|
|
51
|
+
while (heapF.peekKey() + heapB.peekKey() < best) {
|
|
52
|
+
if (heapF.peekKey() <= heapB.peekKey()) {
|
|
53
|
+
const u = heapF.pop();
|
|
54
|
+
for (let a = s.rowPtr[u]; a < s.rowPtr[u + 1]; a++) {
|
|
55
|
+
const v = s.colIdx[a];
|
|
56
|
+
const w = weightOf(a);
|
|
57
|
+
const dv = distF[u] + w;
|
|
58
|
+
if (dv < distF[v]) {
|
|
59
|
+
distF[v] = dv;
|
|
60
|
+
predArc[v] = a;
|
|
61
|
+
heapF.pushOrDecrease(v, dv);
|
|
62
|
+
}
|
|
63
|
+
if (dv + distB[v] < best) {
|
|
64
|
+
best = dv + distB[v];
|
|
65
|
+
[meetFrom, meetTo, meetEdge, meetWeight] = [u, v, s.arcToEdge[a], w];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const v = heapB.pop();
|
|
71
|
+
for (let k = rev.rowPtr[v]; k < rev.rowPtr[v + 1]; k++) {
|
|
72
|
+
const u = rev.colIdx[k];
|
|
73
|
+
const w = weightOf(rev.fwdArc[k]);
|
|
74
|
+
const du = distB[v] + w;
|
|
75
|
+
if (du < distB[u]) {
|
|
76
|
+
distB[u] = du;
|
|
77
|
+
succRevArc[u] = k;
|
|
78
|
+
succNode[u] = v;
|
|
79
|
+
heapB.pushOrDecrease(u, du);
|
|
80
|
+
}
|
|
81
|
+
if (distF[u] + du < best) {
|
|
82
|
+
best = distF[u] + du;
|
|
83
|
+
[meetFrom, meetTo, meetEdge, meetWeight] = [u, v, rev.arcToEdge[k], w];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (meetFrom === INVALID_INDEX) {
|
|
89
|
+
return NO_PATH;
|
|
90
|
+
}
|
|
91
|
+
const nodes = [];
|
|
92
|
+
const edges = [];
|
|
93
|
+
const steps = []; // the weight of each edge, source end first
|
|
94
|
+
for (let node = meetFrom; node !== source; node = s.arcSource(predArc[node])) {
|
|
95
|
+
nodes.push(node);
|
|
96
|
+
edges.push(s.arcToEdge[predArc[node]]);
|
|
97
|
+
steps.push(weightOf(predArc[node]));
|
|
98
|
+
}
|
|
99
|
+
nodes.push(source);
|
|
100
|
+
nodes.reverse();
|
|
101
|
+
edges.reverse();
|
|
102
|
+
steps.reverse();
|
|
103
|
+
edges.push(meetEdge);
|
|
104
|
+
steps.push(meetWeight);
|
|
105
|
+
nodes.push(meetTo);
|
|
106
|
+
for (let node = meetTo; node !== target; node = succNode[node]) {
|
|
107
|
+
edges.push(rev.arcToEdge[succRevArc[node]]);
|
|
108
|
+
steps.push(weightOf(rev.fwdArc[succRevArc[node]]));
|
|
109
|
+
nodes.push(succNode[node]);
|
|
110
|
+
}
|
|
111
|
+
let distance = 0;
|
|
112
|
+
for (const w of steps) {
|
|
113
|
+
distance += w;
|
|
114
|
+
}
|
|
115
|
+
return { distance, path: Uint32Array.from(nodes), edges: Uint32Array.from(edges) };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* A* between two nodes: Dijkstra keyed by cost so far plus `heuristic(node, target)`, stopping when
|
|
119
|
+
* the target is taken from the queue. An expanded node is never reopened, as in legacy `astar`, so
|
|
120
|
+
* the path is shortest when the heuristic is consistent. The predecessor is the relaxing ARC, so a
|
|
121
|
+
* parallel edge on the path is the exact edge.
|
|
122
|
+
* @param g - The adjacency to search
|
|
123
|
+
* @param source - The node index to start from
|
|
124
|
+
* @param target - The node index to reach
|
|
125
|
+
* @param heuristic - Estimated remaining cost from a node index to the target index
|
|
126
|
+
* @param options - Per-arc weight override
|
|
127
|
+
* @returns The path, plus the per-node scores and the expansion order
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
export function astar(g, source, target, heuristic, options = {}) {
|
|
131
|
+
const n = g.nodeCount;
|
|
132
|
+
if (!(target >= 0 && target < n)) {
|
|
133
|
+
throw new RangeError(`Target index ${String(target)} is outside the graph's ${String(n)} nodes`);
|
|
134
|
+
}
|
|
135
|
+
const weights = options.weights ?? g.weights;
|
|
136
|
+
const gScore = new Float64Array(n).fill(Infinity);
|
|
137
|
+
const fScore = new Float64Array(n).fill(Infinity);
|
|
138
|
+
const predArc = new Uint32Array(n).fill(INVALID_INDEX);
|
|
139
|
+
const closed = new Uint8Array(n);
|
|
140
|
+
const visited = [];
|
|
141
|
+
const heap = new IndexedMinHeap(n);
|
|
142
|
+
gScore[source] = 0;
|
|
143
|
+
fScore[source] = heuristic(source, target);
|
|
144
|
+
heap.push(source, fScore[source]);
|
|
145
|
+
while (!heap.isEmpty()) {
|
|
146
|
+
const u = heap.pop();
|
|
147
|
+
if (u === target) {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
closed[u] = 1;
|
|
151
|
+
visited.push(u);
|
|
152
|
+
for (let a = g.rowPtr[u]; a < g.rowPtr[u + 1]; a++) {
|
|
153
|
+
const v = g.colIdx[a];
|
|
154
|
+
if (closed[v] === 1) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const tentative = gScore[u] + (weights === null ? 1 : weights[a]);
|
|
158
|
+
if (tentative < gScore[v]) {
|
|
159
|
+
gScore[v] = tentative;
|
|
160
|
+
fScore[v] = tentative + heuristic(v, target);
|
|
161
|
+
predArc[v] = a;
|
|
162
|
+
heap.pushOrDecrease(v, fScore[v]);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const state = { gScore, fScore, visited: Uint32Array.from(visited) };
|
|
167
|
+
if (gScore[target] === Infinity) {
|
|
168
|
+
return { ...NO_PATH, ...state };
|
|
169
|
+
}
|
|
170
|
+
const path = walkPredArcs(g, predArc, source, target);
|
|
171
|
+
return { distance: gScore[target], path, edges: walkPredEdges(g, predArc, source, target), ...state };
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=point-to-point.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"point-to-point.js","sourceRoot":"","sources":["../../../src/indexed/point-to-point.ts"],"names":[],"mappings":"AAAA,OAAO,EAIH,aAAa,GAGhB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AA4B1D,MAAM,OAAO,GAAe,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,WAAW,CAAC,CA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,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;YAClE,IAAI,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;gBACxB,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;gBACtB,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;gBAC7C,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;gBACf,IAAI,CAAC,cAAc,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;YACtC,CAAC;QACL,CAAC;IACL,CAAC;IACD,MAAM,KAAK,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;IACrE,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,EAAE,GAAG,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC;IACpC,CAAC;IACD,MAAM,IAAI,GAAG,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,KAAK,EAAE,CAAC;AAC1G,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type AdjacencyView, type DerivedGraph, type GraphSnapshot, type U32 } from "@graphty/graph-format";
|
|
2
|
+
import { type ArcOrderOption } from "./bfs.js";
|
|
3
|
+
import { type LabelResult } from "./components.js";
|
|
4
|
+
/** Result of {@link condensation}. @public */
|
|
5
|
+
export interface CondensationResult {
|
|
6
|
+
/** The strongly connected components, labelled in Tarjan completion order. */
|
|
7
|
+
readonly components: LabelResult;
|
|
8
|
+
/**
|
|
9
|
+
* The condensed DAG from `contract(components.labels)`: node `c` is component `c`, one edge per
|
|
10
|
+
* connected ordered pair of components, no self-loops. Its weights are those of the first source
|
|
11
|
+
* edge of each pair; its `nodeRemap` equals `components.labels`.
|
|
12
|
+
*/
|
|
13
|
+
readonly condensed: DerivedGraph;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Strongly connected components by an iterative Tarjan: roots in index order, neighbours in row
|
|
17
|
+
* order unless `options.arcOrder` gives another, and component `c` is the `c`-th to complete -- the numbering the legacy
|
|
18
|
+
* `stronglyConnectedComponents` gives its result array. Completion order is a reverse topological
|
|
19
|
+
* order of the condensation.
|
|
20
|
+
* @param g - A directed adjacency
|
|
21
|
+
* @param options - The neighbour order
|
|
22
|
+
* @returns The partition, labels in completion order
|
|
23
|
+
* @throws Error when the graph is undirected
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export declare function stronglyConnectedComponents(g: AdjacencyView, options?: ArcOrderOption): LabelResult;
|
|
27
|
+
/**
|
|
28
|
+
* The Tarjan walk behind {@link stronglyConnectedComponents}, also returning every node in the
|
|
29
|
+
* order it left the component stack. Each component leaves as one run, in label order, its members
|
|
30
|
+
* in reverse discovery order -- the order the legacy `stronglyConnectedComponents` lists them in.
|
|
31
|
+
* @param g - A directed adjacency
|
|
32
|
+
* @param options - The neighbour order
|
|
33
|
+
* @returns The labels, their count and the pop order
|
|
34
|
+
* @throws Error when the graph is undirected
|
|
35
|
+
*/
|
|
36
|
+
export declare function tarjan(g: AdjacencyView, options?: ArcOrderOption): {
|
|
37
|
+
labels: U32;
|
|
38
|
+
count: number;
|
|
39
|
+
popped: U32;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The condensation of a directed graph: its strongly connected components, and the DAG with one
|
|
43
|
+
* node per component that `contract()` builds from their labels. The labels already run `0..k-1`,
|
|
44
|
+
* so contract keeps them as block indices and the condensed node numbering is the component
|
|
45
|
+
* numbering.
|
|
46
|
+
* @param s - A directed snapshot
|
|
47
|
+
* @param options - The neighbour order Tarjan tries arcs in, which fixes the component numbering
|
|
48
|
+
* @returns The components and the condensed graph
|
|
49
|
+
* @throws Error when the snapshot is undirected
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export declare function condensation(s: GraphSnapshot, options?: ArcOrderOption): CondensationResult;
|
|
53
|
+
//# sourceMappingURL=scc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scc.d.ts","sourceRoot":"","sources":["../../../src/indexed/scc.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,aAAa,EAElB,KAAK,GAAG,EACX,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,KAAK,cAAc,EAAiB,MAAM,UAAU,CAAC;AAC9D,OAAO,EAAE,KAAK,WAAW,EAAc,MAAM,iBAAiB,CAAC;AAE/D,8CAA8C;AAC9C,MAAM,WAAW,kBAAkB;IAC/B,8EAA8E;IAC9E,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAC;IACjC;;;;OAIG;IACH,QAAQ,CAAC,SAAS,EAAE,YAAY,CAAC;CACpC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,2BAA2B,CAAC,CAAC,EAAE,aAAa,EAAE,OAAO,GAAE,cAAmB,GAAG,WAAW,CAGvG;AAED;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,aAAa,EAAE,OAAO,GAAE,cAAmB,GAAG;IAAE,MAAM,EAAE,GAAG,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,GAAG,CAAA;CAAE,CAkElH;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,aAAa,EAAE,OAAO,GAAE,cAAmB,GAAG,kBAAkB,CAI/F"}
|