Skip to content

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 ​

NameTypeRequiredDescription
graphIGraph<T>+Graph copied at initIterator
toT+Goal. The walk stops when it is extracted
heuristic(vertex: T, goal: T) => numberConsistent 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.

Throws ​

IsNotFoundException when the start or the goal is missing ​

IllegalArgumentException when a weight, heuristic, or sum is invalid. An inexact g + h during the walk keeps failing until a later initIterator replaces that run. An inexact weight + h is rejected before the previous run is replaced, so the previous path stays available ​

IllegalStateException when the requested vertex is not settled or cannot be reached ​

MIT Licensed