liveBook · Manning

A-star algorithm

As explained in Advanced Algorithms and Data Structures

The A* algorithm is a best-first shortest-path search algorithm that prioritizes vertices using the cost already incurred plus a heuristic estimate of the remaining cost.

A* finds a shortest path by prioritizing each vertex with an f-score: the cost already incurred from the source plus a heuristic estimate of the remaining cost to the goal. This combines distance information with domain knowledge to focus the search on promising routes. section 14.5 section 14.5.2

Implement A* around these operations:

  • Accept a graph, a start vertex, a goal predicate, an edge-distance function, and a heuristic function. section 14.5
  • Initialize a priority queue, tentative distances, f-scores, and parent links; assign the start vertex its heuristic score. section 14.5
  • Repeatedly remove the highest-priority vertex, stop when it satisfies the goal predicate, and otherwise relax its outgoing edges. section 14.5
  • Use compatible units for edge distances and heuristic estimates; rescale estimates when they represent time, speed, or another related metric. section 14.5.2

Route planning with live traffic

A delivery navigator can use travel time as its distance metric while using typical route times as a heuristic. When traffic makes one route slower than usual, the heuristic helps balance live conditions against longer-term route expectations. section 14.5.2

Listing 14.7 The A* algorithm
function aStar(graph, start, isGoal, distance, heuristic)     #1
  queue ← new PriorityQueue()                                 #2
  queue.insert(start, 0)                                      #3
  distances[v] ← inf (∀ v ϵ graph | v <> start)               #4
  fScore[v] ← inf (∀ v ϵ graph | v <> start)                  #5
  parents[v] ← null ( v ϵ graph)                              #6
  distances [start] ← 0                                       #7
  fScore[start] ← heuristic(start)
  while  not queue.empty() do                                 #8
    v ← queue.top()                                           #9
    if isGoal(v) then                                         #10
      return (v, parents)
    else
      for e in graph.adjacencyList[v] do                      #11
        u ← e.dest
        if distances[u] > distances[v] + distance(e) then     #12
          distances[u] ← distance(e) + distances[v]           #13
          fScore[u] ← distances[u] + heuristic(u)             #14
          parents[u] ← v
          queue.update(u, fScore[u])
  return (null, parents)                                      #15

Inspect the pseudocode for A* initialization, priority-queue processing, goal detection, and neighbor updates. section 14.5

How Advanced Algorithms and Data Structures covers it

How deep it goes
Develops a generic implementation, compares BFS and Dijkstra’s algorithm, and discusses heuristic scaling and performance limits.
Examples
Uses delivery routes, straight-line distance, traffic-aware navigation, and graph cases where A* does or does not outperform Dijkstra’s algorithm.
What it assumes
Assumes graph representations and basic search familiarity, then contrasts A* with BFS and Dijkstra’s algorithm.
What you can do after
Build an extensible shortest-path search and tune its distance and heuristic functions for route-planning data.
How it connects
Connects graphs, shortest-path search, breadth-first search, Dijkstra’s algorithm, heuristics, edge weights, and priority queues.

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 Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.