GraphIteratorAStar
Guide: /guide/algorithms/graph/iterator-a-star
Signature
ts
const iterator = new GraphIteratorAStar(graph, goal, heuristic);
createGraphIterator(graph, EnumGraphTraversalType.A_STAR, { to, heuristic });
shortestPath({
graph,
from,
to,
traversalType: EnumGraphTraversalType.A_STAR,
heuristic,
});heuristic defaults to (vertex, goal) => 0. A literal factory call still requires to when that value is undefined.
Params
| Name | Type | Required | Description |
|---|---|---|---|
| graph | IGraph<T> | + | Graph copied at initIterator |
| to | T | + | Goal. The walk stops when it is extracted |
| heuristic | (vertex: T, goal: T) => number | Consistent estimate. Zero matches Dijkstra's cost when every sum is exact. 0.1 then 0.2 is a path for Dijkstra and an inexact sum for A* |
Returns
IGraphIterator<T>
When the path is ready
getPath is available for a vertex that has been extracted. The goal does not have to be the only one. An empty queue reports an unreachable goal through getPath.
Heuristic check
Init computes h once per vertex and checks every directed arc before replacing the previous run. The check is O(V + E) even if the goal is close. It rejects a bad weight, a bad heuristic, h(goal) !== 0, and a broken consistency inequality.