liveBook · Manning

heuristicFn

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

heuristicFn is a book-specific function parameter that supplies a heuristic score for a search state.

heuristicFn is a function parameter for A* search. It assigns each search state a heuristic score, which combines with path cost to prioritize frontier nodes. section 2.2

A maze-solving implementation uses heuristicFn to:

  • Accept a state and return a Float heuristic score. section 2.2
  • Initialize and prioritize nodes in a priority queue using cost plus heuristic. section 2.2
  • Guide A* toward the goal while retaining optimal paths when the heuristic is good, consistent, and admissible. section 2.2

Distance heuristics for a maze

euclideanDistance computes straight-line distance to the goal. manhattanDistance adds row and column differences, matching movement without diagonals. Either function can be passed as heuristicFn to astar(). section 2.2

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Explains the parameter signature, node scoring, heuristic quality, and two distance implementations without formal proofs.
Examples
Uses Euclidean distance and Manhattan distance to score maze locations relative to a goal.
What you can do after
Build an A* call by supplying a state-scoring function such as manhattanDistance.
How it connects
Connects heuristic functions with A* search, priority queues, path cost, admissible heuristics, and breadth-first search.

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.