To speed up the search, you can provide a heuristic function. This function 'guesses' the distance between the current node and the target.
Important: The heuristic must not overestimate the actual distance between nodes, otherwise the algorithm cannot guarantee the shortest path.
Options:
distance(fromNode, toNode): The actual cost to move between nodes.heuristic(fromNode, toNode): The estimated cost from the current node to the target.
let pathFinder = aStar(graph, {
distance(fromNode, toNode) {
let dx = fromNode.data.x - toNode.data.x;
let dy = fromNode.data.y - toNode.data.y;
return Math.sqrt(dx * dx + dy * dy);
},
heuristic(fromNode, toNode) {
let dx = fromNode.data.x - toNode.data.x;
let dy = fromNode.data.y - toNode.data.y;
return Math.sqrt(dx * dx + dy * dy);
}
});
let path = pathFinder.find('NYC', 'Washington');