liveBook · Manning

A* evaluation function

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

The A* evaluation function is the sum of the accumulated path cost and the heuristic estimate for a state.

The A* evaluation function ranks a search state by combining the cost already paid with an estimate of the remaining cost: f(n) = g(n) + h(n). A* explores the frontier state with the lowest f(n), distinguishing it from breadth-first and depth-first search. section 2.2

Use the three values this way:

  • g(n) records the number of previous steps required to reach the state. section 2.2
  • h(n) estimates the cost from the current state to the goal; an admissible heuristic never overestimates that cost. section 2.2
  • A priority queue keeps frontier nodes ordered by lowest f(n), allowing the most promising state to be selected next. section 2.2

Maze search in Swift

For a grid maze without diagonal movement, Manhattan distance supplies h(n). A Node compares itself using cost + heuristic, and astar receives manhattanDistance as its heuristic function. section 2.2

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

How deep it goes
Defines g(n), h(n), and f(n), then connects them to admissibility, priority queues, and Swift node comparison.
Examples
Uses grid mazes with Euclidean and Manhattan distance heuristics and a Swift Node comparison.
What it assumes
Assumes familiarity with search frontiers, paths, goals, and heuristic estimates.
What you can do after
Apply it via SwiftPriorityQueue to implement A* maze search with a chosen heuristic.
How it connects
Connects path cost, heuristic functions, admissibility, priority queues, breadth-first search, and depth-first search.

Related concepts

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.