liveBook · Manning

A-star algorithm

As explained in Grokking AI Algorithms, Second Edition

A* is a heuristic graph and tree search algorithm that prioritizes nodes using the cost already incurred plus a heuristic estimate of the remaining cost to the goal.

A* is an informed search algorithm that chooses the next node by combining the cost already paid with a heuristic estimate of the remaining distance to the goal. Its priority queue favors the node with the lowest estimated total cost. section 3.2 section 3.3

Unlike breadth-first search and depth-first search, A* follows no fixed traversal pattern. It is useful when problem knowledge can guide the search toward a promising goal, such as navigating a maze. section 3.2 section 3.3

A* repeatedly performs this search cycle:

  • Place the root or start node in a priority queue, often implemented as a min-heap. section 3.4
  • Remove the node with the lowest estimated total cost and mark it visited if it has not already been processed. section 3.4
  • If the node is the goal, reconstruct the path by following parent links back to the root. section 3.4
  • Otherwise, calculate costs for its neighbors, assign the current node as their parent, and add them to the queue. section 3.4

Maze navigation

For a maze, the path cost can combine the distance traveled so far, the cost of the next move, and a Manhattan-distance estimate to the goal. The queue then promotes the most promising frontier point, while parent links preserve the route used to reach it.

A* guarantees a shortest path only when its heuristic is admissible and consistent. Admissible heuristics never overestimate the actual remaining cost; consistency prevents estimates from dropping erratically in graphs with loops. section 3.3

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Explains cost calculation, heuristic conditions, priority-queue ordering, path reconstruction, and a maze-oriented implementation.
Examples
Demonstrates maze navigation with parent-linked nodes, a min-heap priority queue, path length, move cost, and Manhattan distance.
What you can do after
Apply it via a priority queue to explore a maze or other graph and reconstruct a shortest path under suitable heuristic conditions.
How it connects
Connects informed search with heuristics, breadth-first search, depth-first search, greedy search, priority queues, and graph traversal.

Also explained in

Compare how 5 books explain A-star algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.