Skip to content

bestFirstSearch

bestFirstSearch<TNode>(start, traits): GraphPath<TNode> | null

Defined in: graph/best-first-search.ts:30

Finds a path from start to a goal, always expanding whichever frontier node looks closest to one — greedy best-first search.

Ordering is by heuristicOf alone, ignoring the cost already paid. That reaches a goal far sooner than aStar on a graph where the heuristic points the right way, and returns a worse than optimal path when it does not. Use aStar whenever the cheapest path is what matters; use this when any path, found fast, is enough.

The returned cost is still the true cost of the path walked, so a caller can measure how far off it landed.

Time complexity: O((V + E) log V), and costOf must be non-negative.

TNode extends DefinedValue

TNode

The node to search from.

GraphSearchTraits<TNode>

How to expand, weight and test a node. heuristicOf is what drives this search; leaving it at its 0 default degenerates to an arbitrary-order traversal.

GraphPath<TNode> | null

The path found and its cost, or null if no goal is reachable.