@graphty/algorithms 1.3.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +358 -413
- package/dist/algorithms.js +564 -261
- package/dist/algorithms.js.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.d.ts +10 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.js +27 -6
- package/dist/src/algorithms/centrality/betweenness.js.map +1 -1
- package/dist/src/algorithms/centrality/closeness.d.ts +11 -2
- package/dist/src/algorithms/centrality/closeness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/closeness.js +17 -3
- 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 +9 -6
- package/dist/src/algorithms/centrality/degree.js.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts +21 -3
- package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank-simple.js +25 -7
- package/dist/src/algorithms/centrality/delta-pagerank-simple.js.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank.d.ts +28 -2
- package/dist/src/algorithms/centrality/delta-pagerank.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank.js +30 -4
- package/dist/src/algorithms/centrality/delta-pagerank.js.map +1 -1
- 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 +1 -1
- package/dist/src/algorithms/centrality/index.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/index.js +1 -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 +14 -0
- package/dist/src/algorithms/centrality/pagerank.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/pagerank.js +16 -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 +21 -4
- package/dist/src/algorithms/community/girvan-newman.js.map +1 -1
- 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 +0 -3
- package/dist/src/algorithms/community/label-propagation.d.ts.map +1 -1
- package/dist/src/algorithms/community/label-propagation.js +13 -6
- package/dist/src/algorithms/community/label-propagation.js.map +1 -1
- package/dist/src/algorithms/community/leiden.d.ts +0 -1
- package/dist/src/algorithms/community/leiden.d.ts.map +1 -1
- package/dist/src/algorithms/community/leiden.js +37 -6
- package/dist/src/algorithms/community/leiden.js.map +1 -1
- package/dist/src/algorithms/community/louvain-optimized.d.ts +37 -0
- package/dist/src/algorithms/community/louvain-optimized.d.ts.map +1 -1
- package/dist/src/algorithms/community/louvain-optimized.js +42 -7
- package/dist/src/algorithms/community/louvain-optimized.js.map +1 -1
- 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 +19 -6
- package/dist/src/algorithms/community/louvain.js.map +1 -1
- package/dist/src/algorithms/community/modularity-utils.d.ts +0 -4
- package/dist/src/algorithms/community/modularity-utils.d.ts.map +1 -1
- package/dist/src/algorithms/community/modularity-utils.js +1 -5
- package/dist/src/algorithms/community/modularity-utils.js.map +1 -1
- 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 +10 -0
- 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 +10 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -1
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts +11 -4
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js +18 -16
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js.map +1 -1
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts +15 -0
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/dijkstra.js +15 -0
- 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 +13 -0
- package/dist/src/algorithms/traversal/bfs-unified.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs-unified.js +37 -0
- package/dist/src/algorithms/traversal/bfs-unified.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs-variants.d.ts +23 -0
- package/dist/src/algorithms/traversal/bfs-variants.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs-variants.js +34 -0
- package/dist/src/algorithms/traversal/bfs-variants.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs.d.ts +1 -1
- package/dist/src/algorithms/traversal/bfs.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs.js +1 -1
- 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 +2 -0
- package/dist/src/benchmark-all-algorithms.d.ts.map +1 -1
- package/dist/src/benchmark-all-algorithms.js +32 -3
- package/dist/src/benchmark-all-algorithms.js.map +1 -1
- package/dist/src/clustering/hierarchical.d.ts +8 -1
- package/dist/src/clustering/hierarchical.d.ts.map +1 -1
- package/dist/src/clustering/hierarchical.js +22 -6
- package/dist/src/clustering/hierarchical.js.map +1 -1
- package/dist/src/clustering/index.d.ts.map +1 -1
- package/dist/src/clustering/index.js.map +1 -1
- package/dist/src/clustering/k-core.d.ts +2 -5
- package/dist/src/clustering/k-core.d.ts.map +1 -1
- package/dist/src/clustering/k-core.js +14 -12
- 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 +4 -1
- package/dist/src/clustering/spectral.d.ts.map +1 -1
- package/dist/src/clustering/spectral.js +61 -28
- 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 +45 -1
- 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 +4 -2
- package/dist/src/flow/ford-fulkerson.d.ts.map +1 -1
- package/dist/src/flow/ford-fulkerson.js +31 -2
- package/dist/src/flow/ford-fulkerson.js.map +1 -1
- package/dist/src/flow/min-cut.d.ts +0 -3
- package/dist/src/flow/min-cut.d.ts.map +1 -1
- package/dist/src/flow/min-cut.js +17 -4
- package/dist/src/flow/min-cut.js.map +1 -1
- package/dist/src/index.d.ts +2 -1
- 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/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 +36 -10
- 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 +27 -6
- 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 +103 -53
- package/dist/src/optimized/bit-packed.d.ts.map +1 -1
- package/dist/src/optimized/bit-packed.js +104 -54
- package/dist/src/optimized/bit-packed.js.map +1 -1
- package/dist/src/optimized/csr-graph.d.ts +87 -24
- package/dist/src/optimized/csr-graph.d.ts.map +1 -1
- package/dist/src/optimized/csr-graph.js +87 -24
- package/dist/src/optimized/csr-graph.js.map +1 -1
- package/dist/src/optimized/direction-optimized-bfs.d.ts +37 -16
- package/dist/src/optimized/direction-optimized-bfs.d.ts.map +1 -1
- package/dist/src/optimized/direction-optimized-bfs.js +39 -20
- package/dist/src/optimized/direction-optimized-bfs.js.map +1 -1
- package/dist/src/optimized/graph-adapter.d.ts +58 -9
- package/dist/src/optimized/graph-adapter.d.ts.map +1 -1
- package/dist/src/optimized/graph-adapter.js +58 -10
- package/dist/src/optimized/graph-adapter.js.map +1 -1
- package/dist/src/optimized/index.d.ts +1 -1
- package/dist/src/optimized/index.d.ts.map +1 -1
- package/dist/src/optimized/index.js +3 -1
- package/dist/src/optimized/index.js.map +1 -1
- 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 +22 -9
- 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 +28 -7
- package/dist/src/research/sync.js.map +1 -1
- package/dist/src/research/terahac.d.ts +0 -1
- package/dist/src/research/terahac.d.ts.map +1 -1
- package/dist/src/research/terahac.js +32 -4
- package/dist/src/research/terahac.js.map +1 -1
- package/dist/src/utils/graph-converters.d.ts +31 -0
- package/dist/src/utils/graph-converters.d.ts.map +1 -1
- package/dist/src/utils/graph-converters.js +31 -0
- package/dist/src/utils/graph-converters.js.map +1 -1
- package/dist/src/utils/graph-utilities.d.ts.map +1 -1
- package/dist/src/utils/graph-utilities.js.map +1 -1
- package/dist/src/utils/math-utilities.d.ts +11 -0
- package/dist/src/utils/math-utilities.d.ts.map +1 -1
- package/dist/src/utils/math-utilities.js +14 -5
- package/dist/src/utils/math-utilities.js.map +1 -1
- package/dist/src/utils/optimization-helpers.d.ts +9 -0
- package/dist/src/utils/optimization-helpers.d.ts.map +1 -1
- package/dist/src/utils/optimization-helpers.js +12 -0
- package/dist/src/utils/optimization-helpers.js.map +1 -1
- 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 +49 -45
- package/src/algorithms/centrality/betweenness.ts +34 -17
- package/src/algorithms/centrality/closeness.ts +34 -17
- package/src/algorithms/centrality/degree.ts +15 -19
- package/src/algorithms/centrality/delta-pagerank-simple.ts +36 -28
- package/src/algorithms/centrality/delta-pagerank.ts +34 -11
- package/src/algorithms/centrality/eigenvector.ts +14 -15
- package/src/algorithms/centrality/hits.ts +17 -17
- package/src/algorithms/centrality/index.ts +20 -15
- package/src/algorithms/centrality/katz.ts +17 -25
- package/src/algorithms/centrality/pagerank.ts +26 -17
- package/src/algorithms/community/girvan-newman.ts +39 -28
- package/src/algorithms/community/index.ts +6 -7
- package/src/algorithms/community/label-propagation.ts +20 -28
- package/src/algorithms/community/leiden.ts +61 -37
- package/src/algorithms/community/louvain-optimized.ts +49 -20
- package/src/algorithms/community/louvain.ts +35 -25
- package/src/algorithms/community/modularity-utils.ts +5 -17
- package/src/algorithms/components/connected.ts +40 -17
- package/src/algorithms/matching/bipartite.ts +17 -10
- 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 -15
- package/src/algorithms/shortest-path/bidirectional-dijkstra.ts +28 -28
- package/src/algorithms/shortest-path/dijkstra.ts +23 -17
- 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 +64 -60
- package/src/algorithms/traversal/bfs-variants.ts +61 -37
- package/src/algorithms/traversal/bfs.ts +1 -7
- package/src/algorithms/traversal/dfs.ts +61 -42
- package/src/algorithms/traversal/index.ts +3 -3
- package/src/benchmark-all-algorithms.ts +57 -25
- package/src/clustering/hierarchical.ts +40 -34
- package/src/clustering/index.ts +8 -8
- package/src/clustering/k-core.ts +26 -42
- package/src/clustering/mcl.ts +53 -23
- package/src/clustering/spectral.ts +91 -57
- package/src/core/graph.ts +54 -14
- 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 +62 -35
- package/src/flow/min-cut.ts +43 -45
- package/src/index.ts +3 -2
- package/src/link-prediction/adamic-adar.ts +65 -51
- package/src/link-prediction/common-neighbors.ts +45 -36
- package/src/link-prediction/index.ts +1 -1
- package/src/optimized/bit-packed.ts +106 -56
- package/src/optimized/csr-graph.ts +93 -30
- package/src/optimized/direction-optimized-bfs.ts +44 -25
- package/src/optimized/graph-adapter.ts +64 -16
- package/src/optimized/index.ts +7 -9
- package/src/pathfinding/astar.ts +40 -25
- package/src/pathfinding/utils.ts +20 -13
- package/src/research/grsbm.ts +30 -24
- package/src/research/index.ts +4 -4
- package/src/research/sync.ts +39 -23
- package/src/research/terahac.ts +54 -20
- package/src/utils/graph-converters.ts +42 -14
- package/src/utils/graph-utilities.ts +11 -37
- package/src/utils/math-utilities.ts +17 -8
- package/src/utils/optimization-helpers.ts +27 -21
- package/src/utils/priorityQueue.ts +40 -6
|
@@ -5,6 +5,10 @@ import { CSRGraph } from "./csr-graph.js";
|
|
|
5
5
|
* Provides transparent conversion while maintaining API compatibility
|
|
6
6
|
*/
|
|
7
7
|
export class GraphAdapter {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new GraphAdapter that wraps or converts a graph to CSR format.
|
|
10
|
+
* @param graph - The graph to adapt (either standard Graph or ReadonlyGraph)
|
|
11
|
+
*/
|
|
8
12
|
constructor(graph) {
|
|
9
13
|
// Check if already a CSRGraph
|
|
10
14
|
if (graph instanceof CSRGraph) {
|
|
@@ -16,8 +20,10 @@ export class GraphAdapter {
|
|
|
16
20
|
}
|
|
17
21
|
}
|
|
18
22
|
/**
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
* Convert standard graph to CSR format.
|
|
24
|
+
* @param graph - The graph to convert
|
|
25
|
+
* @returns A new CSRGraph instance
|
|
26
|
+
*/
|
|
21
27
|
convertToCSR(graph) {
|
|
22
28
|
const adjacencyList = new Map();
|
|
23
29
|
const weights = new Map();
|
|
@@ -62,57 +68,96 @@ export class GraphAdapter {
|
|
|
62
68
|
return new CSRGraph(adjacencyList, weights.size > 0 ? weights : undefined);
|
|
63
69
|
}
|
|
64
70
|
// Delegate all methods to CSR implementation
|
|
71
|
+
/**
|
|
72
|
+
* Get the total number of nodes in the graph.
|
|
73
|
+
* @returns The number of nodes
|
|
74
|
+
*/
|
|
65
75
|
nodeCount() {
|
|
66
76
|
return this.csrGraph.nodeCount();
|
|
67
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Get the total number of edges in the graph.
|
|
80
|
+
* @returns The number of edges
|
|
81
|
+
*/
|
|
68
82
|
edgeCount() {
|
|
69
83
|
return this.csrGraph.edgeCount();
|
|
70
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Check if a node exists in the graph.
|
|
87
|
+
* @param nodeId - The node ID to check
|
|
88
|
+
* @returns True if the node exists, false otherwise
|
|
89
|
+
*/
|
|
71
90
|
hasNode(nodeId) {
|
|
72
91
|
return this.csrGraph.hasNode(nodeId);
|
|
73
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Check if an edge exists between two nodes.
|
|
95
|
+
* @param source - The source node ID
|
|
96
|
+
* @param target - The target node ID
|
|
97
|
+
* @returns True if the edge exists, false otherwise
|
|
98
|
+
*/
|
|
74
99
|
hasEdge(source, target) {
|
|
75
100
|
return this.csrGraph.hasEdge(source, target);
|
|
76
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Get neighbors of a node as node IDs.
|
|
104
|
+
* @param nodeId - The node ID to get neighbors for
|
|
105
|
+
* @returns An iterator over neighbor node IDs
|
|
106
|
+
*/
|
|
77
107
|
neighbors(nodeId) {
|
|
78
108
|
return this.csrGraph.neighbors(nodeId);
|
|
79
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Get the out-degree (number of outgoing edges) of a node.
|
|
112
|
+
* @param nodeId - The node ID to get the out-degree for
|
|
113
|
+
* @returns The number of outgoing edges from the node
|
|
114
|
+
*/
|
|
80
115
|
outDegree(nodeId) {
|
|
81
116
|
return this.csrGraph.outDegree(nodeId);
|
|
82
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Get all nodes in the graph.
|
|
120
|
+
* @returns An iterator over all node IDs
|
|
121
|
+
*/
|
|
83
122
|
nodes() {
|
|
84
123
|
return this.csrGraph.nodes();
|
|
85
124
|
}
|
|
86
125
|
/**
|
|
87
|
-
|
|
88
|
-
|
|
126
|
+
* Get the underlying CSR graph.
|
|
127
|
+
* @returns The CSRGraph instance used internally
|
|
128
|
+
*/
|
|
89
129
|
getCSRGraph() {
|
|
90
130
|
return this.csrGraph;
|
|
91
131
|
}
|
|
92
132
|
}
|
|
93
133
|
/**
|
|
94
|
-
* Configure optimizations globally
|
|
134
|
+
* Configure optimizations globally.
|
|
95
135
|
* @deprecated This function is a no-op and will be removed in a future version
|
|
136
|
+
* @param _config - The configuration object (ignored)
|
|
96
137
|
*/
|
|
97
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
98
138
|
export function configureOptimizations(_config) {
|
|
99
139
|
// No-op for backward compatibility
|
|
100
140
|
}
|
|
101
141
|
/**
|
|
102
|
-
* Get current configuration
|
|
142
|
+
* Get current configuration.
|
|
103
143
|
* @deprecated This function returns empty config and will be removed in a future version
|
|
144
|
+
* @returns An empty configuration object
|
|
104
145
|
*/
|
|
105
146
|
export function getOptimizationConfig() {
|
|
106
147
|
return {};
|
|
107
148
|
}
|
|
108
149
|
/**
|
|
109
|
-
* Check if a graph is already in CSR format
|
|
150
|
+
* Check if a graph is already in CSR format.
|
|
151
|
+
* @param graph - The graph to check
|
|
152
|
+
* @returns True if the graph is a CSRGraph instance
|
|
110
153
|
*/
|
|
111
154
|
export function isCSRGraph(graph) {
|
|
112
155
|
return graph instanceof CSRGraph;
|
|
113
156
|
}
|
|
114
157
|
/**
|
|
115
|
-
* Convert any graph to CSR format
|
|
158
|
+
* Convert any graph to CSR format.
|
|
159
|
+
* @param graph - The graph to convert
|
|
160
|
+
* @returns A CSRGraph instance (either the original if already CSR, or a new converted one)
|
|
116
161
|
*/
|
|
117
162
|
export function toCSRGraph(graph) {
|
|
118
163
|
if (isCSRGraph(graph)) {
|
|
@@ -122,7 +167,10 @@ export function toCSRGraph(graph) {
|
|
|
122
167
|
return adapter.getCSRGraph();
|
|
123
168
|
}
|
|
124
169
|
/**
|
|
125
|
-
* Create an optimized graph from nodes and edges
|
|
170
|
+
* Create an optimized graph from nodes and edges.
|
|
171
|
+
* @param nodes - Array of node IDs
|
|
172
|
+
* @param edges - Array of edge tuples [source, target, weight?]
|
|
173
|
+
* @returns A new CSRGraph instance
|
|
126
174
|
*/
|
|
127
175
|
export function createOptimizedGraph(nodes, edges) {
|
|
128
176
|
const adjacencyList = new Map();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph-adapter.js","sourceRoot":"","sources":["../../../src/optimized/graph-adapter.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"graph-adapter.js","sourceRoot":"","sources":["../../../src/optimized/graph-adapter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAsB,MAAM,gBAAgB,CAAC;AAE9D;;;;GAIG;AACH,MAAM,OAAO,YAAY;IAGrB;;;OAGG;IACH,YAAY,KAAqC;QAC7C,8BAA8B;QAC9B,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;YAC5B,IAAI,CAAC,QAAQ,GAAG,KAA0B,CAAC;QAC/C,CAAC;aAAM,CAAC;YACJ,wBAAwB;YACxB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QAC7C,CAAC;IACL,CAAC;IAED;;;;OAI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AAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAChC,CAAC;IAED,YAAY;IACZ,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;QAC3C,MAAM,SAAS,GAAG,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC5C,IAAI,SAAS,EAAE,CAAC;YACZ,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACvB,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACvB,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YAC/D,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,IAAI,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC/E,CAAC"}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* - Compressed Sparse Row (CSR) format
|
|
7
7
|
* - Bit-packed data structures
|
|
8
8
|
*/
|
|
9
|
-
export { configureOptimizations, getOptimizationConfig, type GraphAlgorithmConfig
|
|
9
|
+
export { configureOptimizations, getOptimizationConfig, type GraphAlgorithmConfig } from "./graph-adapter.js";
|
|
10
10
|
export { CompactDistanceArray, GraphBitSet, VisitedBitArray } from "./bit-packed.js";
|
|
11
11
|
export { CSRGraph } from "./csr-graph.js";
|
|
12
12
|
export { DirectionOptimizedBFS } from "./direction-optimized-bfs.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/optimized/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/optimized/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,KAAK,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAgB9G,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrF,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAGrE,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
* - Bit-packed data structures
|
|
8
8
|
*/
|
|
9
9
|
// Configuration and optimization control
|
|
10
|
-
|
|
10
|
+
// These deprecated functions are intentionally re-exported for backward compatibility
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated
|
|
12
|
+
export { configureOptimizations, getOptimizationConfig } from "./graph-adapter.js";
|
|
11
13
|
// Note: The main BFS functions in algorithms/traversal/bfs.ts automatically
|
|
12
14
|
// use these optimizations for large graphs (>10k nodes) when optimizations are enabled.
|
|
13
15
|
// No manual configuration needed - just use breadthFirstSearch, shortestPathBFS, etc.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/optimized/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,yCAAyC;AACzC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/optimized/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,yCAAyC;AACzC,sFAAsF;AACtF,4DAA4D;AAC5D,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAA6B,MAAM,oBAAoB,CAAC;AAE9G,4EAA4E;AAC5E,wFAAwF;AACxF,sFAAsF;AACtF,EAAE;AACF,sEAAsE;AACtE,8EAA8E;AAC9E,gEAAgE;AAEhE,uEAAuE;AACvE,qEAAqE;AACrE,qEAAqE;AACrE,2DAA2D;AAE3D,4CAA4C;AAC5C,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrF,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAErE,mBAAmB;AACnB,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A* pathfinding algorithm
|
|
3
3
|
* Finds the shortest path from start to goal using a heuristic function
|
|
4
|
-
*
|
|
5
4
|
* @param graph - The graph represented as an adjacency list
|
|
6
5
|
* @param start - The starting node
|
|
7
6
|
* @param goal - The goal node
|
|
@@ -18,6 +17,11 @@ export declare function astar<T>(graph: Map<T, Map<T, number>>, start: T, goal:
|
|
|
18
17
|
/**
|
|
19
18
|
* A* pathfinding with path reconstruction details
|
|
20
19
|
* Returns detailed information about the search process
|
|
20
|
+
* @param graph - The graph represented as an adjacency list
|
|
21
|
+
* @param start - The starting node
|
|
22
|
+
* @param goal - The goal node
|
|
23
|
+
* @param heuristic - Heuristic function that estimates distance from node to goal
|
|
24
|
+
* @returns Detailed result including path, cost, visited nodes, and score maps
|
|
21
25
|
*/
|
|
22
26
|
export declare function astarWithDetails<T>(graph: Map<T, Map<T, number>>, start: T, goal: T, heuristic: (node: T, goal: T) => number): {
|
|
23
27
|
path: T[] | null;
|
|
@@ -31,20 +35,32 @@ export declare function astarWithDetails<T>(graph: Map<T, Map<T, number>>, start
|
|
|
31
35
|
*/
|
|
32
36
|
export declare const heuristics: {
|
|
33
37
|
/**
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
* Manhattan distance heuristic for grid-based graphs
|
|
39
|
+
* @param a - First coordinate as [x, y] tuple
|
|
40
|
+
* @param b - Second coordinate as [x, y] tuple
|
|
41
|
+
* @returns The Manhattan distance between the two points
|
|
42
|
+
*/
|
|
36
43
|
manhattan: (a: [number, number], b: [number, number]) => number;
|
|
37
44
|
/**
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
* Euclidean distance heuristic
|
|
46
|
+
* @param a - First coordinate as [x, y] tuple
|
|
47
|
+
* @param b - Second coordinate as [x, y] tuple
|
|
48
|
+
* @returns The Euclidean distance between the two points
|
|
49
|
+
*/
|
|
40
50
|
euclidean: (a: [number, number], b: [number, number]) => number;
|
|
41
51
|
/**
|
|
42
|
-
|
|
43
|
-
|
|
52
|
+
* Chebyshev distance heuristic (diagonal movement allowed)
|
|
53
|
+
* @param a - First coordinate as [x, y] tuple
|
|
54
|
+
* @param b - Second coordinate as [x, y] tuple
|
|
55
|
+
* @returns The Chebyshev distance between the two points
|
|
56
|
+
*/
|
|
44
57
|
chebyshev: (a: [number, number], b: [number, number]) => number;
|
|
45
58
|
/**
|
|
46
|
-
|
|
47
|
-
|
|
59
|
+
* Zero heuristic (makes A* behave like Dijkstra)
|
|
60
|
+
* @param _a - First node (unused)
|
|
61
|
+
* @param _b - Second node (unused)
|
|
62
|
+
* @returns Always returns 0
|
|
63
|
+
*/
|
|
48
64
|
zero: <T>(_a: T, _b: T) => number;
|
|
49
65
|
};
|
|
50
66
|
//# sourceMappingURL=astar.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"astar.d.ts","sourceRoot":"","sources":["../../../src/pathfinding/astar.ts"],"names":[],"mappings":"AAIA
|
|
1
|
+
{"version":3,"file":"astar.d.ts","sourceRoot":"","sources":["../../../src/pathfinding/astar.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;GAWG;AACH,wBAAgB,KAAK,CAAC,CAAC,EACnB,KAAK,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAC7B,KAAK,EAAE,CAAC,EACR,IAAI,EAAE,CAAC,EACP,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ortest path from start to goal using a heuristic function
|
|
6
|
-
*
|
|
7
6
|
* @param graph - The graph represented as an adjacency list
|
|
8
7
|
* @param start - The starting node
|
|
9
8
|
* @param goal - The goal node
|
|
@@ -79,6 +78,11 @@ export function astar(graph, start, goal, heuristic) {
|
|
|
79
78
|
/**
|
|
80
79
|
* A* pathfinding with path reconstruction details
|
|
81
80
|
* Returns detailed information about the search process
|
|
81
|
+
* @param graph - The graph represented as an adjacency list
|
|
82
|
+
* @param start - The starting node
|
|
83
|
+
* @param goal - The goal node
|
|
84
|
+
* @param heuristic - Heuristic function that estimates distance from node to goal
|
|
85
|
+
* @returns Detailed result including path, cost, visited nodes, and score maps
|
|
82
86
|
*/
|
|
83
87
|
export function astarWithDetails(graph, start, goal, heuristic) {
|
|
84
88
|
const openSet = new MinPriorityQueue();
|
|
@@ -154,27 +158,38 @@ export function astarWithDetails(graph, start, goal, heuristic) {
|
|
|
154
158
|
*/
|
|
155
159
|
export const heuristics = {
|
|
156
160
|
/**
|
|
157
|
-
|
|
158
|
-
|
|
161
|
+
* Manhattan distance heuristic for grid-based graphs
|
|
162
|
+
* @param a - First coordinate as [x, y] tuple
|
|
163
|
+
* @param b - Second coordinate as [x, y] tuple
|
|
164
|
+
* @returns The Manhattan distance between the two points
|
|
165
|
+
*/
|
|
159
166
|
manhattan: (a, b) => {
|
|
160
167
|
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
|
|
161
168
|
},
|
|
162
169
|
/**
|
|
163
|
-
|
|
164
|
-
|
|
170
|
+
* Euclidean distance heuristic
|
|
171
|
+
* @param a - First coordinate as [x, y] tuple
|
|
172
|
+
* @param b - Second coordinate as [x, y] tuple
|
|
173
|
+
* @returns The Euclidean distance between the two points
|
|
174
|
+
*/
|
|
165
175
|
euclidean: (a, b) => {
|
|
166
176
|
return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2));
|
|
167
177
|
},
|
|
168
178
|
/**
|
|
169
|
-
|
|
170
|
-
|
|
179
|
+
* Chebyshev distance heuristic (diagonal movement allowed)
|
|
180
|
+
* @param a - First coordinate as [x, y] tuple
|
|
181
|
+
* @param b - Second coordinate as [x, y] tuple
|
|
182
|
+
* @returns The Chebyshev distance between the two points
|
|
183
|
+
*/
|
|
171
184
|
chebyshev: (a, b) => {
|
|
172
185
|
return Math.max(Math.abs(a[0] - b[0]), Math.abs(a[1] - b[1]));
|
|
173
186
|
},
|
|
174
187
|
/**
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
188
|
+
* Zero heuristic (makes A* behave like Dijkstra)
|
|
189
|
+
* @param _a - First node (unused)
|
|
190
|
+
* @param _b - Second node (unused)
|
|
191
|
+
* @returns Always returns 0
|
|
192
|
+
*/
|
|
178
193
|
zero: (_a, _b) => 0,
|
|
179
194
|
};
|
|
180
195
|
//# sourceMappingURL=astar.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"astar.js","sourceRoot":"","sources":["../../../src/pathfinding/astar.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"astar.js","sourceRoot":"","sources":["../../../src/pathfinding/astar.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,KAAK,CACjB,KAA6B,EAC7B,KAAQ,EACR,IAAO,EACP,SAAuC;IAEvC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QACxC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,+CAA+C;IAC/C,MAAM,OAAO,GAAG,IAAI,gBAAgB,EAAgB,CAAC;IACrD,MAAM,MAAM,GAAG,IAAI,GAAG,EAAa,CAAC,CAAC,0BAA0B;IAC/D,MAAM,MAA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
export declare const pathfindingUtils: {
|
|
5
5
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
* Reconstructs a path from start to goal using the cameFrom map
|
|
7
|
+
* @param cameFrom - Map of each node to its predecessor on the path
|
|
8
|
+
* @param goal - The goal node to reconstruct the path to
|
|
9
|
+
* @returns An array of nodes representing the path from start to goal
|
|
10
|
+
*/
|
|
8
11
|
reconstructPath<T>(cameFrom: Map<T, T>, goal: T): T[];
|
|
9
12
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
* Creates a grid graph for testing pathfinding algorithms
|
|
14
|
+
* @param width - The width of the grid
|
|
15
|
+
* @param height - The height of the grid
|
|
16
|
+
* @param obstacles - Set of obstacle coordinates as "x,y" strings
|
|
17
|
+
* @returns A grid graph with 4-directional movement
|
|
18
|
+
*/
|
|
12
19
|
createGridGraph(width: number, height: number, obstacles?: Set<string>): Map<string, Map<string, number>>;
|
|
13
20
|
/**
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
* Parses a grid coordinate string
|
|
22
|
+
* @param coord - The coordinate string in "x,y" format
|
|
23
|
+
* @returns A tuple of [x, y] coordinates
|
|
24
|
+
*/
|
|
16
25
|
parseGridCoordinate(coord: string): [number, number];
|
|
17
26
|
};
|
|
18
27
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/pathfinding/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,gBAAgB;IACzB
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/pathfinding/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,gBAAgB;IACzB;;;;;OAKG;oBACa,CAAC,YAAY,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;IAiBrD;;;;;;OAMG;2BACoB,MAAM,UAAU,MAAM,4BAAkC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IA2C/G;;;;OAIG;+BACwB,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;CAUvD,CAAC"}
|
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const pathfindingUtils = {
|
|
5
5
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
* Reconstructs a path from start to goal using the cameFrom map
|
|
7
|
+
* @param cameFrom - Map of each node to its predecessor on the path
|
|
8
|
+
* @param goal - The goal node to reconstruct the path to
|
|
9
|
+
* @returns An array of nodes representing the path from start to goal
|
|
10
|
+
*/
|
|
8
11
|
reconstructPath(cameFrom, goal) {
|
|
9
12
|
const path = [goal];
|
|
10
13
|
let current = goal;
|
|
@@ -19,8 +22,12 @@ export const pathfindingUtils = {
|
|
|
19
22
|
return path;
|
|
20
23
|
},
|
|
21
24
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
* Creates a grid graph for testing pathfinding algorithms
|
|
26
|
+
* @param width - The width of the grid
|
|
27
|
+
* @param height - The height of the grid
|
|
28
|
+
* @param obstacles - Set of obstacle coordinates as "x,y" strings
|
|
29
|
+
* @returns A grid graph with 4-directional movement
|
|
30
|
+
*/
|
|
24
31
|
createGridGraph(width, height, obstacles = new Set()) {
|
|
25
32
|
const graph = new Map();
|
|
26
33
|
for (let x = 0; x < width; x++) {
|
|
@@ -32,7 +39,10 @@ export const pathfindingUtils = {
|
|
|
32
39
|
const neighbors = new Map();
|
|
33
40
|
// Add 4-directional neighbors
|
|
34
41
|
const directions = [
|
|
35
|
-
[0, 1],
|
|
42
|
+
[0, 1],
|
|
43
|
+
[1, 0],
|
|
44
|
+
[0, -1],
|
|
45
|
+
[-1, 0],
|
|
36
46
|
];
|
|
37
47
|
for (const dir of directions) {
|
|
38
48
|
const dx = dir[0];
|
|
@@ -53,8 +63,10 @@ export const pathfindingUtils = {
|
|
|
53
63
|
return graph;
|
|
54
64
|
},
|
|
55
65
|
/**
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
* Parses a grid coordinate string
|
|
67
|
+
* @param coord - The coordinate string in "x,y" format
|
|
68
|
+
* @returns A tuple of [x, y] coordinates
|
|
69
|
+
*/
|
|
58
70
|
parseGridCoordinate(coord) {
|
|
59
71
|
const parts = coord.split(",").map(Number);
|
|
60
72
|
const x = parts[0];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/pathfinding/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC5B
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/pathfinding/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC5B;;;;;OAKG;IACH,eAAe,CAAI,QAAmB,EAAE,IAAO;QAC3C,MAAM,IAAI,GAAQ,CAAC,IAAI,CAAC,CAAC;QACzB,IAAI,OAAO,GAAG,IAAI,CAAC;QAEnB,OAAO,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YACnC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACrB,MAAM;YACV,CAAC;YAED,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC1B,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;;;;;OAMG;IACH,eAAe,CAAC,KAAa,EAAE,MAAc,EAAE,YAAY,IAAI,GAAG,EAAU;QACxE,MAAM,KAAK,GAAG,IAAI,GAAG,EAA+B,CAAC;QAErD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,E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C,CAAC,8BAA8B;oBAC9D,CAAC;gBACL,CAAC;gBAED,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;YAC/B,CAAC;QACL,CAAC;QAED,OAAO,KAAK,CAAC;IACjB,CAAC;IAED;;;;OAIG;IACH,mBAAmB,CAAC,KAAa;QAC7B,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC3C,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,uBAAuB,KAAK,EAAE,CAAC,CAAC;QACpD,CAAC;QAED,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAClB,CAAC;CACJ,CAAC"}
|
|
@@ -74,7 +74,6 @@ export interface ClusterExplanation {
|
|
|
74
74
|
* community structure by tracking the reasoning behind each split.
|
|
75
75
|
*
|
|
76
76
|
* Based on: "Explainable Community Detection via Hierarchical Spectral Clustering" (2024)
|
|
77
|
-
*
|
|
78
77
|
* @param graph - Input graph to cluster
|
|
79
78
|
* @param config - Configuration options
|
|
80
79
|
* @returns Hierarchical clustering result with explanations
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grsbm.d.ts","sourceRoot":"","sources":["../../../src/research/grsbm.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"grsbm.d.ts","sourceRoot":"","sources":["../../../src/research/grsbm.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAGhD;;GAEG;AACH,MAAM,WAAW,WA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M,EAAE,CAAC;IACnB,gCAAgC;IAChC,cAAc,EAAE,MAAM,EAAE,CAAC;CAC5B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,GAAE,WAAgB,GAAG,WAAW,CAsJzE"}
|
|
@@ -7,7 +7,6 @@ import { SeededRandom } from "../utils/math-utilities.js";
|
|
|
7
7
|
* community structure by tracking the reasoning behind each split.
|
|
8
8
|
*
|
|
9
9
|
* Based on: "Explainable Community Detection via Hierarchical Spectral Clustering" (2024)
|
|
10
|
-
*
|
|
11
10
|
* @param graph - Input graph to cluster
|
|
12
11
|
* @param config - Configuration options
|
|
13
12
|
* @returns Hierarchical clustering result with explanations
|
|
@@ -45,8 +44,7 @@ export function grsbm(graph, config = {}) {
|
|
|
45
44
|
continue;
|
|
46
45
|
}
|
|
47
46
|
// Check stopping criteria
|
|
48
|
-
if (currentCluster.depth >= maxDepth ||
|
|
49
|
-
currentCluster.members.size < minClusterSize * 2) {
|
|
47
|
+
if (currentCluster.depth >= maxDepth || currentCluster.members.size < minClusterSize * 2) {
|
|
50
48
|
continue;
|
|
51
49
|
}
|
|
52
50
|
// Attempt spectral bisection
|
|
@@ -81,7 +79,8 @@ export function grsbm(graph, config = {}) {
|
|
|
81
79
|
const newModularity = calculateModularity(graph, newAssignment);
|
|
82
80
|
const modularityImprovement = newModularity - currentCluster.modularity;
|
|
83
81
|
// Only split if modularity improves or is neutral (allow small splits)
|
|
84
|
-
if (modularityImprovement >= -0.01) {
|
|
82
|
+
if (modularityImprovement >= -0.01) {
|
|
83
|
+
// Small tolerance for neutral splits
|
|
85
84
|
leftCluster.modularity = newModularity;
|
|
86
85
|
rightCluster.modularity = newModularity;
|
|
87
86
|
currentCluster.left = leftCluster;
|
|
@@ -133,6 +132,12 @@ export function grsbm(graph, config = {}) {
|
|
|
133
132
|
}
|
|
134
133
|
/**
|
|
135
134
|
* Perform spectral bisection on a cluster
|
|
135
|
+
* @param graph - The input graph
|
|
136
|
+
* @param cluster - The cluster to bisect
|
|
137
|
+
* @param numEigenvectors - Number of eigenvectors to compute
|
|
138
|
+
* @param tolerance - Convergence tolerance
|
|
139
|
+
* @param maxIterations - Maximum number of iterations
|
|
140
|
+
* @returns Bisection result with left/right members, scores, and explanation, or null if bisection failed
|
|
136
141
|
*/
|
|
137
142
|
function spectralBisection(graph, cluster, numEigenvectors, tolerance, maxIterations) {
|
|
138
143
|
const members = Array.from(cluster.members);
|
|
@@ -148,9 +153,7 @@ function spectralBisection(graph, cluster, numEigenvectors, tolerance, maxIterat
|
|
|
148
153
|
return null; // Could not compute eigenvector
|
|
149
154
|
}
|
|
150
155
|
// Find optimal split point
|
|
151
|
-
const sortedIndices = eigenvector
|
|
152
|
-
.map((value, index) => ({ value, index }))
|
|
153
|
-
.sort((a, b) => a.value - b.value);
|
|
156
|
+
const sortedIndices = eigenvector.map((value, index) => ({ value, index })).sort((a, b) => a.value - b.value);
|
|
154
157
|
let bestSplitIndex = Math.floor(n / 2);
|
|
155
158
|
let bestModularity = -Infinity;
|
|
156
159
|
// Try different split points
|
|
@@ -224,6 +227,9 @@ function spectralBisection(graph, cluster, numEigenvectors, tolerance, maxIterat
|
|
|
224
227
|
}
|
|
225
228
|
/**
|
|
226
229
|
* Create Laplacian matrix for a subgraph
|
|
230
|
+
* @param graph - The input graph
|
|
231
|
+
* @param nodes - The nodes to include in the subgraph
|
|
232
|
+
* @returns The Laplacian matrix as a 2D array
|
|
227
233
|
*/
|
|
228
234
|
function createLaplacianMatrix(graph, nodes) {
|
|
229
235
|
const n = nodes.length;
|
|
@@ -267,6 +273,10 @@ function createLaplacianMatrix(graph, nodes) {
|
|
|
267
273
|
}
|
|
268
274
|
/**
|
|
269
275
|
* Compute the Fiedler vector (second smallest eigenvector) using power iteration
|
|
276
|
+
* @param laplacian - The Laplacian matrix
|
|
277
|
+
* @param tolerance - Convergence tolerance
|
|
278
|
+
* @param maxIterations - Maximum number of iterations
|
|
279
|
+
* @returns The Fiedler vector, or null if computation failed
|
|
270
280
|
*/
|
|
271
281
|
function computeFiedlerVector(laplacian, tolerance, maxIterations) {
|
|
272
282
|
const n = laplacian.length;
|
|
@@ -287,7 +297,7 @@ function computeFiedlerVector(laplacian, tolerance, maxIterations) {
|
|
|
287
297
|
}
|
|
288
298
|
}
|
|
289
299
|
// Normalize
|
|
290
|
-
const norm = Math.sqrt(vector.reduce((sum, val) => sum +
|
|
300
|
+
const norm = Math.sqrt(vector.reduce((sum, val) => sum + val * val, 0));
|
|
291
301
|
if (norm > 0) {
|
|
292
302
|
for (let i = 0; i < n; i++) {
|
|
293
303
|
const val = vector[i];
|
|
@@ -332,7 +342,7 @@ function computeFiedlerVector(laplacian, tolerance, maxIterations) {
|
|
|
332
342
|
}
|
|
333
343
|
}
|
|
334
344
|
// Normalize
|
|
335
|
-
const newNorm = Math.sqrt(newVector.reduce((sum, val) => sum +
|
|
345
|
+
const newNorm = Math.sqrt(newVector.reduce((sum, val) => sum + val * val, 0));
|
|
336
346
|
if (newNorm < tolerance) {
|
|
337
347
|
break; // Converged to zero vector
|
|
338
348
|
}
|
|
@@ -360,6 +370,9 @@ function computeFiedlerVector(laplacian, tolerance, maxIterations) {
|
|
|
360
370
|
}
|
|
361
371
|
/**
|
|
362
372
|
* Calculate modularity of a graph partitioning
|
|
373
|
+
* @param graph - The input graph
|
|
374
|
+
* @param assignment - Map from node ID to community ID
|
|
375
|
+
* @returns The modularity score
|
|
363
376
|
*/
|
|
364
377
|
function calculateModularity(graph, assignment) {
|
|
365
378
|
const totalEdges = graph.uniqueEdgeCount;
|