@graphty/algorithms 1.0.1 → 1.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 +1259 -61
- package/dist/algorithms.d.ts +2 -0
- package/dist/algorithms.d.ts.map +1 -0
- package/dist/algorithms.js +2 -0
- package/dist/algorithms.js.map +1 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.js +22 -7
- package/dist/src/algorithms/centrality/betweenness.js.map +1 -1
- package/dist/src/algorithms/centrality/closeness.js +7 -7
- package/dist/src/algorithms/centrality/closeness.js.map +1 -1
- package/dist/src/algorithms/centrality/degree.js +1 -1
- package/dist/src/algorithms/centrality/degree.js.map +1 -1
- package/dist/src/algorithms/centrality/eigenvector.d.ts +27 -0
- package/dist/src/algorithms/centrality/eigenvector.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/eigenvector.js +113 -0
- package/dist/src/algorithms/centrality/eigenvector.js.map +1 -0
- package/dist/src/algorithms/centrality/hits.d.ts +35 -0
- package/dist/src/algorithms/centrality/hits.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/hits.js +146 -0
- package/dist/src/algorithms/centrality/hits.js.map +1 -0
- package/dist/src/algorithms/centrality/index.d.ts +6 -0
- package/dist/src/algorithms/centrality/index.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/index.js +3 -0
- package/dist/src/algorithms/centrality/index.js.map +1 -1
- package/dist/src/algorithms/centrality/katz.d.ts +30 -0
- package/dist/src/algorithms/centrality/katz.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/katz.js +85 -0
- package/dist/src/algorithms/centrality/katz.js.map +1 -0
- package/dist/src/algorithms/centrality/pagerank.js +3 -3
- package/dist/src/algorithms/centrality/pagerank.js.map +1 -1
- package/dist/src/algorithms/community/girvan-newman.d.ts +23 -0
- package/dist/src/algorithms/community/girvan-newman.d.ts.map +1 -0
- package/dist/src/algorithms/community/girvan-newman.js +297 -0
- package/dist/src/algorithms/community/girvan-newman.js.map +1 -0
- package/dist/src/algorithms/community/index.d.ts +12 -0
- package/dist/src/algorithms/community/index.d.ts.map +1 -0
- package/dist/src/algorithms/community/index.js +12 -0
- package/dist/src/algorithms/community/index.js.map +1 -0
- package/dist/src/algorithms/community/label-propagation.d.ts +42 -0
- package/dist/src/algorithms/community/label-propagation.d.ts.map +1 -0
- package/dist/src/algorithms/community/label-propagation.js +307 -0
- package/dist/src/algorithms/community/label-propagation.js.map +1 -0
- package/dist/src/algorithms/community/leiden.d.ts +33 -0
- package/dist/src/algorithms/community/leiden.d.ts.map +1 -0
- package/dist/src/algorithms/community/leiden.js +434 -0
- package/dist/src/algorithms/community/leiden.js.map +1 -0
- package/dist/src/algorithms/community/louvain.d.ts +19 -0
- package/dist/src/algorithms/community/louvain.d.ts.map +1 -0
- package/dist/src/algorithms/community/louvain.js +219 -0
- package/dist/src/algorithms/community/louvain.js.map +1 -0
- package/dist/src/algorithms/components/connected.js +7 -7
- package/dist/src/algorithms/components/connected.js.map +1 -1
- package/dist/src/algorithms/index.d.ts +7 -0
- package/dist/src/algorithms/index.d.ts.map +1 -1
- package/dist/src/algorithms/index.js +14 -0
- package/dist/src/algorithms/index.js.map +1 -1
- package/dist/src/algorithms/matching/bipartite.d.ts +37 -0
- package/dist/src/algorithms/matching/bipartite.d.ts.map +1 -0
- package/dist/src/algorithms/matching/bipartite.js +127 -0
- package/dist/src/algorithms/matching/bipartite.js.map +1 -0
- package/dist/src/algorithms/matching/index.d.ts +8 -0
- package/dist/src/algorithms/matching/index.d.ts.map +1 -0
- package/dist/src/algorithms/matching/index.js +6 -0
- package/dist/src/algorithms/matching/index.js.map +1 -0
- package/dist/src/algorithms/matching/isomorphism.d.ts +32 -0
- package/dist/src/algorithms/matching/isomorphism.d.ts.map +1 -0
- package/dist/src/algorithms/matching/isomorphism.js +291 -0
- package/dist/src/algorithms/matching/isomorphism.js.map +1 -0
- package/dist/src/algorithms/mst/index.d.ts +4 -0
- package/dist/src/algorithms/mst/index.d.ts.map +1 -0
- package/dist/src/algorithms/mst/index.js +3 -0
- package/dist/src/algorithms/mst/index.js.map +1 -0
- package/dist/src/algorithms/mst/kruskal.d.ts +9 -0
- package/dist/src/algorithms/mst/kruskal.d.ts.map +1 -0
- package/dist/src/algorithms/mst/kruskal.js +43 -0
- package/dist/src/algorithms/mst/kruskal.js.map +1 -0
- package/dist/src/algorithms/mst/prim.d.ts +5 -0
- package/dist/src/algorithms/mst/prim.d.ts.map +1 -0
- package/dist/src/algorithms/mst/prim.js +63 -0
- package/dist/src/algorithms/mst/prim.js.map +1 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.js +2 -2
- package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -1
- package/dist/src/algorithms/shortest-path/dijkstra.js +5 -5
- package/dist/src/algorithms/shortest-path/dijkstra.js.map +1 -1
- package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts +14 -0
- package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path/floyd-warshall.js +129 -0
- package/dist/src/algorithms/shortest-path/floyd-warshall.js.map +1 -0
- package/dist/src/algorithms/shortest-path/index.d.ts +2 -0
- package/dist/src/algorithms/shortest-path/index.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/index.js +1 -0
- package/dist/src/algorithms/shortest-path/index.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs.js +2 -2
- package/dist/src/algorithms/traversal/bfs.js.map +1 -1
- package/dist/src/algorithms/traversal/dfs.js +11 -11
- package/dist/src/algorithms/traversal/dfs.js.map +1 -1
- package/dist/src/clustering/hierarchical.d.ts +48 -0
- package/dist/src/clustering/hierarchical.d.ts.map +1 -0
- package/dist/src/clustering/hierarchical.js +439 -0
- package/dist/src/clustering/hierarchical.js.map +1 -0
- package/dist/src/clustering/index.d.ts +7 -0
- package/dist/src/clustering/index.d.ts.map +1 -0
- package/dist/src/clustering/index.js +5 -0
- package/dist/src/clustering/index.js.map +1 -0
- package/dist/src/clustering/k-core.d.ts +63 -0
- package/dist/src/clustering/k-core.d.ts.map +1 -0
- package/dist/src/clustering/k-core.js +377 -0
- package/dist/src/clustering/k-core.js.map +1 -0
- package/dist/src/clustering/mcl.d.ts +36 -0
- package/dist/src/clustering/mcl.d.ts.map +1 -0
- package/dist/src/clustering/mcl.js +343 -0
- package/dist/src/clustering/mcl.js.map +1 -0
- package/dist/src/clustering/spectral.d.ts +28 -0
- package/dist/src/clustering/spectral.d.ts.map +1 -0
- package/dist/src/clustering/spectral.js +551 -0
- package/dist/src/clustering/spectral.js.map +1 -0
- package/dist/src/core/graph.d.ts +9 -0
- package/dist/src/core/graph.d.ts.map +1 -1
- package/dist/src/core/graph.js +31 -11
- package/dist/src/core/graph.js.map +1 -1
- package/dist/src/data-structures/priority-queue.js +0 -2
- package/dist/src/data-structures/priority-queue.js.map +1 -1
- package/dist/src/data-structures/union-find.js +2 -5
- package/dist/src/data-structures/union-find.js.map +1 -1
- package/dist/src/flow/ford-fulkerson.d.ts +53 -0
- package/dist/src/flow/ford-fulkerson.d.ts.map +1 -0
- package/dist/src/flow/ford-fulkerson.js +355 -0
- package/dist/src/flow/ford-fulkerson.js.map +1 -0
- package/dist/src/flow/index.d.ts +3 -0
- package/dist/src/flow/index.d.ts.map +1 -0
- package/dist/src/flow/index.js +3 -0
- package/dist/src/flow/index.js.map +1 -0
- package/dist/src/flow/min-cut.d.ts +49 -0
- package/dist/src/flow/min-cut.d.ts.map +1 -0
- package/dist/src/flow/min-cut.js +383 -0
- package/dist/src/flow/min-cut.js.map +1 -0
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/link-prediction/adamic-adar.d.ts +56 -0
- package/dist/src/link-prediction/adamic-adar.d.ts.map +1 -0
- package/dist/src/link-prediction/adamic-adar.js +256 -0
- package/dist/src/link-prediction/adamic-adar.js.map +1 -0
- package/dist/src/link-prediction/common-neighbors.d.ts +50 -0
- package/dist/src/link-prediction/common-neighbors.d.ts.map +1 -0
- package/dist/src/link-prediction/common-neighbors.js +155 -0
- package/dist/src/link-prediction/common-neighbors.js.map +1 -0
- package/dist/src/link-prediction/index.d.ts +7 -0
- package/dist/src/link-prediction/index.d.ts.map +1 -0
- package/dist/src/link-prediction/index.js +6 -0
- package/dist/src/link-prediction/index.js.map +1 -0
- package/dist/src/pathfinding/astar.d.ts +50 -0
- package/dist/src/pathfinding/astar.d.ts.map +1 -0
- package/dist/src/pathfinding/astar.js +180 -0
- package/dist/src/pathfinding/astar.js.map +1 -0
- package/dist/src/pathfinding/index.d.ts +3 -0
- package/dist/src/pathfinding/index.d.ts.map +1 -0
- package/dist/src/pathfinding/index.js +3 -0
- package/dist/src/pathfinding/index.js.map +1 -0
- package/dist/src/pathfinding/utils.d.ts +18 -0
- package/dist/src/pathfinding/utils.d.ts.map +1 -0
- package/dist/src/pathfinding/utils.js +68 -0
- package/dist/src/pathfinding/utils.js.map +1 -0
- package/dist/src/research/grsbm.d.ts +83 -0
- package/dist/src/research/grsbm.d.ts.map +1 -0
- package/dist/src/research/grsbm.js +411 -0
- package/dist/src/research/grsbm.js.map +1 -0
- package/dist/src/research/index.d.ts +12 -0
- package/dist/src/research/index.d.ts.map +1 -0
- package/dist/src/research/index.js +14 -0
- package/dist/src/research/index.js.map +1 -0
- package/dist/src/research/sync.d.ts +49 -0
- package/dist/src/research/sync.d.ts.map +1 -0
- package/dist/src/research/sync.js +342 -0
- package/dist/src/research/sync.js.map +1 -0
- package/dist/src/research/terahac.d.ts +63 -0
- package/dist/src/research/terahac.d.ts.map +1 -0
- package/dist/src/research/terahac.js +375 -0
- package/dist/src/research/terahac.js.map +1 -0
- package/dist/src/types/index.d.ts +9 -0
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/utils/graphNode.d.ts +8 -0
- package/dist/src/utils/graphNode.d.ts.map +1 -0
- package/dist/src/utils/graphNode.js +2 -0
- package/dist/src/utils/graphNode.js.map +1 -0
- package/dist/src/utils/priorityQueue.d.ts +21 -0
- package/dist/src/utils/priorityQueue.d.ts.map +1 -0
- package/dist/src/utils/priorityQueue.js +101 -0
- package/dist/src/utils/priorityQueue.js.map +1 -0
- package/package.json +13 -2
- package/src/algorithms/centrality/betweenness.ts +25 -7
- package/src/algorithms/centrality/closeness.ts +7 -7
- package/src/algorithms/centrality/degree.ts +1 -1
- package/src/algorithms/centrality/eigenvector.ts +162 -0
- package/src/algorithms/centrality/hits.ts +210 -0
- package/src/algorithms/centrality/index.ts +6 -0
- package/src/algorithms/centrality/katz.ts +139 -0
- package/src/algorithms/centrality/pagerank.ts +3 -3
- package/src/algorithms/community/girvan-newman.ts +372 -0
- package/src/algorithms/community/index.ts +13 -0
- package/src/algorithms/community/label-propagation.ts +392 -0
- package/src/algorithms/community/leiden.ts +572 -0
- package/src/algorithms/community/louvain.ts +298 -0
- package/src/algorithms/components/connected.ts +7 -7
- package/src/algorithms/index.ts +21 -0
- package/src/algorithms/matching/bipartite.ts +179 -0
- package/src/algorithms/matching/index.ts +8 -0
- package/src/algorithms/matching/isomorphism.ts +392 -0
- package/src/algorithms/mst/index.ts +4 -0
- package/src/algorithms/mst/kruskal.ts +61 -0
- package/src/algorithms/mst/prim.ts +80 -0
- package/src/algorithms/shortest-path/bellman-ford.ts +2 -2
- package/src/algorithms/shortest-path/dijkstra.ts +5 -5
- package/src/algorithms/shortest-path/floyd-warshall.ts +170 -0
- package/src/algorithms/shortest-path/index.ts +2 -0
- package/src/algorithms/traversal/bfs.ts +2 -2
- package/src/algorithms/traversal/dfs.ts +11 -11
- package/src/clustering/hierarchical.ts +555 -0
- package/src/clustering/index.ts +6 -0
- package/src/clustering/k-core.ts +460 -0
- package/src/clustering/mcl.ts +454 -0
- package/src/clustering/spectral.ts +672 -0
- package/src/core/graph.ts +34 -6
- package/src/data-structures/union-find.ts +2 -2
- package/src/flow/ford-fulkerson.ts +459 -0
- package/src/flow/index.ts +2 -0
- package/src/flow/min-cut.ts +482 -0
- package/src/index.ts +5 -0
- package/src/link-prediction/adamic-adar.ts +353 -0
- package/src/link-prediction/common-neighbors.ts +252 -0
- package/src/link-prediction/index.ts +20 -0
- package/src/pathfinding/astar.ts +229 -0
- package/src/pathfinding/index.ts +2 -0
- package/src/pathfinding/utils.ts +87 -0
- package/src/research/grsbm.ts +589 -0
- package/src/research/index.ts +17 -0
- package/src/research/sync.ts +469 -0
- package/src/research/terahac.ts +506 -0
- package/src/types/index.ts +11 -0
- package/src/utils/graphNode.ts +7 -0
- package/src/utils/priorityQueue.ts +120 -0
- package/dist/test/browser/basic.test.d.ts +0 -2
- package/dist/test/browser/basic.test.d.ts.map +0 -1
- package/dist/test/browser/basic.test.js +0 -70
- package/dist/test/browser/basic.test.js.map +0 -1
- package/dist/test/unit/bellman-ford.test.d.ts +0 -2
- package/dist/test/unit/bellman-ford.test.d.ts.map +0 -1
- package/dist/test/unit/bellman-ford.test.js +0 -231
- package/dist/test/unit/bellman-ford.test.js.map +0 -1
- package/dist/test/unit/betweenness-centrality.test.d.ts +0 -2
- package/dist/test/unit/betweenness-centrality.test.d.ts.map +0 -1
- package/dist/test/unit/betweenness-centrality.test.js +0 -311
- package/dist/test/unit/betweenness-centrality.test.js.map +0 -1
- package/dist/test/unit/bfs.test.d.ts +0 -2
- package/dist/test/unit/bfs.test.d.ts.map +0 -1
- package/dist/test/unit/bfs.test.js +0 -277
- package/dist/test/unit/bfs.test.js.map +0 -1
- package/dist/test/unit/closeness.test.d.ts +0 -2
- package/dist/test/unit/closeness.test.d.ts.map +0 -1
- package/dist/test/unit/closeness.test.js +0 -266
- package/dist/test/unit/closeness.test.js.map +0 -1
- package/dist/test/unit/connected-components.test.d.ts +0 -2
- package/dist/test/unit/connected-components.test.d.ts.map +0 -1
- package/dist/test/unit/connected-components.test.js +0 -355
- package/dist/test/unit/connected-components.test.js.map +0 -1
- package/dist/test/unit/degree-centrality.test.d.ts +0 -2
- package/dist/test/unit/degree-centrality.test.d.ts.map +0 -1
- package/dist/test/unit/degree-centrality.test.js +0 -260
- package/dist/test/unit/degree-centrality.test.js.map +0 -1
- package/dist/test/unit/dfs.test.d.ts +0 -2
- package/dist/test/unit/dfs.test.d.ts.map +0 -1
- package/dist/test/unit/dfs.test.js +0 -350
- package/dist/test/unit/dfs.test.js.map +0 -1
- package/dist/test/unit/dijkstra.test.d.ts +0 -2
- package/dist/test/unit/dijkstra.test.d.ts.map +0 -1
- package/dist/test/unit/dijkstra.test.js +0 -311
- package/dist/test/unit/dijkstra.test.js.map +0 -1
- package/dist/test/unit/graph.test.d.ts +0 -2
- package/dist/test/unit/graph.test.d.ts.map +0 -1
- package/dist/test/unit/graph.test.js +0 -175
- package/dist/test/unit/graph.test.js.map +0 -1
- package/dist/test/unit/pagerank.test.d.ts +0 -2
- package/dist/test/unit/pagerank.test.d.ts.map +0 -1
- package/dist/test/unit/pagerank.test.js +0 -281
- package/dist/test/unit/pagerank.test.js.map +0 -1
- package/dist/test/unit/priority-queue.test.d.ts +0 -2
- package/dist/test/unit/priority-queue.test.d.ts.map +0 -1
- package/dist/test/unit/priority-queue.test.js +0 -189
- package/dist/test/unit/priority-queue.test.js.map +0 -1
- package/dist/test/unit/union-find.test.d.ts +0 -2
- package/dist/test/unit/union-find.test.d.ts.map +0 -1
- package/dist/test/unit/union-find.test.js +0 -245
- package/dist/test/unit/union-find.test.js.map +0 -1
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { MinPriorityQueue } from "../utils/priorityQueue.js";
|
|
2
|
+
import { pathfindingUtils } from "./utils.js";
|
|
3
|
+
/**
|
|
4
|
+
* A* pathfinding algorithm
|
|
5
|
+
* Finds the shortest path from start to goal using a heuristic function
|
|
6
|
+
*
|
|
7
|
+
* @param graph - The graph represented as an adjacency list
|
|
8
|
+
* @param start - The starting node
|
|
9
|
+
* @param goal - The goal node
|
|
10
|
+
* @param heuristic - Heuristic function that estimates distance from node to goal
|
|
11
|
+
* @returns Object containing the shortest path and its cost, or null if no path exists
|
|
12
|
+
*
|
|
13
|
+
* Time Complexity: O(E) with good heuristic, O(b^d) worst case
|
|
14
|
+
* Space Complexity: O(V)
|
|
15
|
+
*/
|
|
16
|
+
export function astar(graph, start, goal, heuristic) {
|
|
17
|
+
if (!graph.has(start) || !graph.has(goal)) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
// Priority queue ordered by f(n) = g(n) + h(n)
|
|
21
|
+
const openSet = new MinPriorityQueue();
|
|
22
|
+
const gScore = new Map(); // Cost from start to node
|
|
23
|
+
const fScore = new Map(); // Estimated total cost
|
|
24
|
+
const cameFrom = new Map();
|
|
25
|
+
const closedSet = new Set();
|
|
26
|
+
// Initialize start node
|
|
27
|
+
gScore.set(start, 0);
|
|
28
|
+
fScore.set(start, heuristic(start, goal));
|
|
29
|
+
const startFScore = fScore.get(start);
|
|
30
|
+
if (startFScore === undefined) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
openSet.insert({ node: start, distance: startFScore });
|
|
34
|
+
while (!openSet.isEmpty()) {
|
|
35
|
+
const current = openSet.extractMin();
|
|
36
|
+
if (!current) {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
const currentNode = current.node;
|
|
40
|
+
if (currentNode === goal) {
|
|
41
|
+
// Reconstruct path
|
|
42
|
+
const path = pathfindingUtils.reconstructPath(cameFrom, goal);
|
|
43
|
+
const goalCost = gScore.get(goal);
|
|
44
|
+
if (goalCost === undefined) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
return { path, cost: goalCost };
|
|
48
|
+
}
|
|
49
|
+
closedSet.add(currentNode);
|
|
50
|
+
const neighbors = graph.get(currentNode);
|
|
51
|
+
if (!neighbors) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
for (const [neighbor, weight] of neighbors) {
|
|
55
|
+
if (closedSet.has(neighbor)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const currentNodeGScore = gScore.get(currentNode);
|
|
59
|
+
if (currentNodeGScore === undefined) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const tentativeGScore = currentNodeGScore + weight;
|
|
63
|
+
const currentGScore = gScore.get(neighbor) ?? Infinity;
|
|
64
|
+
if (tentativeGScore < currentGScore) {
|
|
65
|
+
// This path to neighbor is better
|
|
66
|
+
cameFrom.set(neighbor, currentNode);
|
|
67
|
+
gScore.set(neighbor, tentativeGScore);
|
|
68
|
+
fScore.set(neighbor, tentativeGScore + heuristic(neighbor, goal));
|
|
69
|
+
// Add to open set if not already there
|
|
70
|
+
const neighborFScore = fScore.get(neighbor);
|
|
71
|
+
if (neighborFScore !== undefined) {
|
|
72
|
+
openSet.insert({ node: neighbor, distance: neighborFScore });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return null; // No path found
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* A* pathfinding with path reconstruction details
|
|
81
|
+
* Returns detailed information about the search process
|
|
82
|
+
*/
|
|
83
|
+
export function astarWithDetails(graph, start, goal, heuristic) {
|
|
84
|
+
const openSet = new MinPriorityQueue();
|
|
85
|
+
const gScore = new Map();
|
|
86
|
+
const fScore = new Map();
|
|
87
|
+
const cameFrom = new Map();
|
|
88
|
+
const closedSet = new Set();
|
|
89
|
+
gScore.set(start, 0);
|
|
90
|
+
fScore.set(start, heuristic(start, goal));
|
|
91
|
+
const startFScore = fScore.get(start);
|
|
92
|
+
if (startFScore === undefined) {
|
|
93
|
+
return {
|
|
94
|
+
path: null,
|
|
95
|
+
cost: Infinity,
|
|
96
|
+
visited: new Set(),
|
|
97
|
+
gScores: new Map(),
|
|
98
|
+
fScores: new Map(),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
openSet.insert({ node: start, distance: startFScore });
|
|
102
|
+
while (!openSet.isEmpty()) {
|
|
103
|
+
const current = openSet.extractMin();
|
|
104
|
+
if (!current) {
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
const currentNode = current.node;
|
|
108
|
+
if (currentNode === goal) {
|
|
109
|
+
const path = pathfindingUtils.reconstructPath(cameFrom, goal);
|
|
110
|
+
return {
|
|
111
|
+
path,
|
|
112
|
+
cost: gScore.get(goal) ?? Infinity,
|
|
113
|
+
visited: closedSet,
|
|
114
|
+
gScores: gScore,
|
|
115
|
+
fScores: fScore,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
closedSet.add(currentNode);
|
|
119
|
+
const neighbors = graph.get(currentNode);
|
|
120
|
+
if (!neighbors) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
for (const [neighbor, weight] of neighbors) {
|
|
124
|
+
if (closedSet.has(neighbor)) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const currentNodeGScore = gScore.get(currentNode);
|
|
128
|
+
if (currentNodeGScore === undefined) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const tentativeGScore = currentNodeGScore + weight;
|
|
132
|
+
const currentGScore = gScore.get(neighbor) ?? Infinity;
|
|
133
|
+
if (tentativeGScore < currentGScore) {
|
|
134
|
+
cameFrom.set(neighbor, currentNode);
|
|
135
|
+
gScore.set(neighbor, tentativeGScore);
|
|
136
|
+
fScore.set(neighbor, tentativeGScore + heuristic(neighbor, goal));
|
|
137
|
+
const neighborFScore = fScore.get(neighbor);
|
|
138
|
+
if (neighborFScore !== undefined) {
|
|
139
|
+
openSet.insert({ node: neighbor, distance: neighborFScore });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
path: null,
|
|
146
|
+
cost: Infinity,
|
|
147
|
+
visited: closedSet,
|
|
148
|
+
gScores: gScore,
|
|
149
|
+
fScores: fScore,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Common heuristic functions for A*
|
|
154
|
+
*/
|
|
155
|
+
export const heuristics = {
|
|
156
|
+
/**
|
|
157
|
+
* Manhattan distance heuristic for grid-based graphs
|
|
158
|
+
*/
|
|
159
|
+
manhattan: (a, b) => {
|
|
160
|
+
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
|
|
161
|
+
},
|
|
162
|
+
/**
|
|
163
|
+
* Euclidean distance heuristic
|
|
164
|
+
*/
|
|
165
|
+
euclidean: (a, b) => {
|
|
166
|
+
return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2));
|
|
167
|
+
},
|
|
168
|
+
/**
|
|
169
|
+
* Chebyshev distance heuristic (diagonal movement allowed)
|
|
170
|
+
*/
|
|
171
|
+
chebyshev: (a, b) => {
|
|
172
|
+
return Math.max(Math.abs(a[0] - b[0]), Math.abs(a[1] - b[1]));
|
|
173
|
+
},
|
|
174
|
+
/**
|
|
175
|
+
* Zero heuristic (makes A* behave like Dijkstra)
|
|
176
|
+
*/
|
|
177
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
178
|
+
zero: (_a, _b) => 0,
|
|
179
|
+
};
|
|
180
|
+
//# sourceMappingURL=astar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"astar.js","sourceRoot":"","sources":["../../../src/pathfinding/astar.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAC,gBAAgB,EAAC,MAAM,YAAY,CAAC;AAE5C;;;;;;;;;;;;GAYG;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,MAA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,CAAI,EAAK,EAAE,EAAK,EAAU,EAAE,CAAC,CAAC;CACvC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/pathfinding/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/pathfinding/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for pathfinding algorithms
|
|
3
|
+
*/
|
|
4
|
+
export declare const pathfindingUtils: {
|
|
5
|
+
/**
|
|
6
|
+
* Reconstructs a path from start to goal using the cameFrom map
|
|
7
|
+
*/
|
|
8
|
+
reconstructPath<T>(cameFrom: Map<T, T>, goal: T): T[];
|
|
9
|
+
/**
|
|
10
|
+
* Creates a grid graph for testing pathfinding algorithms
|
|
11
|
+
*/
|
|
12
|
+
createGridGraph(width: number, height: number, obstacles?: Set<string>): Map<string, Map<string, number>>;
|
|
13
|
+
/**
|
|
14
|
+
* Parses a grid coordinate string
|
|
15
|
+
*/
|
|
16
|
+
parseGridCoordinate(coord: string): [number, number];
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/pathfinding/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,gBAAgB;IACzB;;KAEC;oBACe,CAAC,YAAY,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;IAiBrD;;KAEC;2BAEU,MAAM,UACL,MAAM,4BAEf,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAwCnC;;KAEC;+BAC0B,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;CAUvD,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for pathfinding algorithms
|
|
3
|
+
*/
|
|
4
|
+
export const pathfindingUtils = {
|
|
5
|
+
/**
|
|
6
|
+
* Reconstructs a path from start to goal using the cameFrom map
|
|
7
|
+
*/
|
|
8
|
+
reconstructPath(cameFrom, goal) {
|
|
9
|
+
const path = [goal];
|
|
10
|
+
let current = goal;
|
|
11
|
+
while (cameFrom.has(current)) {
|
|
12
|
+
const next = cameFrom.get(current);
|
|
13
|
+
if (next === undefined) {
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
current = next;
|
|
17
|
+
path.unshift(current);
|
|
18
|
+
}
|
|
19
|
+
return path;
|
|
20
|
+
},
|
|
21
|
+
/**
|
|
22
|
+
* Creates a grid graph for testing pathfinding algorithms
|
|
23
|
+
*/
|
|
24
|
+
createGridGraph(width, height, obstacles = new Set()) {
|
|
25
|
+
const graph = new Map();
|
|
26
|
+
for (let x = 0; x < width; x++) {
|
|
27
|
+
for (let y = 0; y < height; y++) {
|
|
28
|
+
const node = `${String(x)},${String(y)}`;
|
|
29
|
+
if (obstacles.has(node)) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const neighbors = new Map();
|
|
33
|
+
// Add 4-directional neighbors
|
|
34
|
+
const directions = [
|
|
35
|
+
[0, 1], [1, 0], [0, -1], [-1, 0],
|
|
36
|
+
];
|
|
37
|
+
for (const dir of directions) {
|
|
38
|
+
const dx = dir[0];
|
|
39
|
+
const dy = dir[1];
|
|
40
|
+
if (dx === undefined || dy === undefined) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const nx = x + dx;
|
|
44
|
+
const ny = y + dy;
|
|
45
|
+
const neighbor = `${String(nx)},${String(ny)}`;
|
|
46
|
+
if (nx >= 0 && nx < width && ny >= 0 && ny < height && !obstacles.has(neighbor)) {
|
|
47
|
+
neighbors.set(neighbor, 1); // Unit cost for grid movement
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
graph.set(node, neighbors);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return graph;
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Parses a grid coordinate string
|
|
57
|
+
*/
|
|
58
|
+
parseGridCoordinate(coord) {
|
|
59
|
+
const parts = coord.split(",").map(Number);
|
|
60
|
+
const x = parts[0];
|
|
61
|
+
const y = parts[1];
|
|
62
|
+
if (x === undefined || y === undefined) {
|
|
63
|
+
throw new Error(`Invalid coordinate: ${coord}`);
|
|
64
|
+
}
|
|
65
|
+
return [x, y];
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/pathfinding/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC5B;;KAEC;IACD,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;;KAEC;IACD,eAAe,CACX,KAAa,EACb,MAAc,EACd,YAAY,IAAI,G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,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;wBAC9E,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,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;;KAEC;IACD,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"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Graph } from "../core/graph.js";
|
|
2
|
+
import type { NodeId } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the GRSBM (Greedy Recursive Spectral Bisection) algorithm
|
|
5
|
+
*/
|
|
6
|
+
export interface GRSBMConfig {
|
|
7
|
+
/** Maximum depth of recursive bisection */
|
|
8
|
+
maxDepth?: number;
|
|
9
|
+
/** Minimum cluster size to continue splitting */
|
|
10
|
+
minClusterSize?: number;
|
|
11
|
+
/** Number of eigenvectors to use for spectral embedding */
|
|
12
|
+
numEigenvectors?: number;
|
|
13
|
+
/** Convergence tolerance for eigenvalue computation */
|
|
14
|
+
tolerance?: number;
|
|
15
|
+
/** Maximum number of power iterations for eigenvalue computation */
|
|
16
|
+
maxIterations?: number;
|
|
17
|
+
/** Random seed for reproducibility */
|
|
18
|
+
seed?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Represents a cluster in the hierarchical structure
|
|
22
|
+
*/
|
|
23
|
+
export interface GRSBMCluster {
|
|
24
|
+
/** Unique identifier for this cluster */
|
|
25
|
+
id: string;
|
|
26
|
+
/** Node IDs in this cluster */
|
|
27
|
+
members: Set<NodeId>;
|
|
28
|
+
/** Left child cluster (if split) */
|
|
29
|
+
left?: GRSBMCluster;
|
|
30
|
+
/** Right child cluster (if split) */
|
|
31
|
+
right?: GRSBMCluster;
|
|
32
|
+
/** Modularity score of this cluster */
|
|
33
|
+
modularity: number;
|
|
34
|
+
/** Depth in the hierarchy */
|
|
35
|
+
depth: number;
|
|
36
|
+
/** Spectral embedding quality score */
|
|
37
|
+
spectralScore: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Result of the GRSBM clustering algorithm
|
|
41
|
+
*/
|
|
42
|
+
export interface GRSBMResult {
|
|
43
|
+
/** Root of the cluster hierarchy */
|
|
44
|
+
root: GRSBMCluster;
|
|
45
|
+
/** Flat clustering at leaf level */
|
|
46
|
+
clusters: Map<NodeId, number>;
|
|
47
|
+
/** Number of final clusters */
|
|
48
|
+
numClusters: number;
|
|
49
|
+
/** Modularity scores for each level */
|
|
50
|
+
modularityScores: number[];
|
|
51
|
+
/** Explanation of the clustering structure */
|
|
52
|
+
explanation: ClusterExplanation[];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Explanation for a cluster split decision
|
|
56
|
+
*/
|
|
57
|
+
export interface ClusterExplanation {
|
|
58
|
+
/** Cluster that was split */
|
|
59
|
+
clusterId: string;
|
|
60
|
+
/** Reason for the split */
|
|
61
|
+
reason: string;
|
|
62
|
+
/** Modularity improvement from the split */
|
|
63
|
+
modularityImprovement: number;
|
|
64
|
+
/** Key nodes that influenced the split */
|
|
65
|
+
keyNodes: NodeId[];
|
|
66
|
+
/** Spectral embedding values */
|
|
67
|
+
spectralValues: number[];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* GRSBM - Greedy Recursive Spectral Bisection with Modularity
|
|
71
|
+
*
|
|
72
|
+
* This algorithm performs hierarchical community detection using spectral
|
|
73
|
+
* bisection guided by modularity optimization. It provides explainable
|
|
74
|
+
* community structure by tracking the reasoning behind each split.
|
|
75
|
+
*
|
|
76
|
+
* Based on: "Explainable Community Detection via Hierarchical Spectral Clustering" (2024)
|
|
77
|
+
*
|
|
78
|
+
* @param graph - Input graph to cluster
|
|
79
|
+
* @param config - Configuration options
|
|
80
|
+
* @returns Hierarchical clustering result with explanations
|
|
81
|
+
*/
|
|
82
|
+
export declare function grsbm(graph: Graph, config?: GRSBMConfig): GRSBMResult;
|
|
83
|
+
//# sourceMappingURL=grsbm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grsbm.d.ts","sourceRoot":"","sources":["../../../src/research/grsbm.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,kBAAkB,CAAC;AAC5C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mBAAmB,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iDAAiD;IACjD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,2DAA2D;IAC3D,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,oEAAoE;IACpE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,sCAAsC;IACtC,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IACzB,yCAAyC;IACzC,EAAE,EAAE,MAAM,CAAC;IACX,+BAA+B;IAC/B,OAAO,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