pathfinding
A-star
algorithms
AI
graph traversal

A-star algorithm

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

A* (A-star) algorithm is one of the most popular and powerful pathfinding and graph traversal algorithms. It is widely used in various applications such as gaming, robotics, and geographical information systems, where finding the shortest path efficiently is crucial.

Overview

The A* algorithm is a combination of Dijkstra's Algorithm and Greedy Best-First-Search. It aims to find the most efficient route from a start node to a target node, using a cost function `f(n) = g(n) + h(n)` where: • `g(n)` is the cost of the path from the start node to node `n`. • `h(n)` is the heuristic function that estimates the cost of the cheapest path from node `n` to the goal.

The A* algorithm maintains a priority queue of nodes to be explored, prioritizing nodes with the lowest `f(n)` value.

Technical Explanation

  1. Initialization: • Start with an open list (priority queue) containing the initial node. • Initialize the closed list as empty, which will contain nodes already evaluated.
  2. Processing Nodes: • While the open list is not empty: • Extract the node with the lowest `f(n)` from the open list. • If this node is the goal, reconstruct the path and terminate. • Move this node to the closed list and evaluate all its neighbors.
  3. Evaluating Neighbors: • For each neighbor: • Calculate tentative `g(n)` as the sum of the `g(n)` of the current node and the cost to reach the neighbor. • If the neighbor is in the closed list and has a higher `g(n)`, ignore it. • If not in open or closed lists, or if the new tentative `g(n)` is lower: • Update or set `g(n)`, `h(n)`, and `f(n)` for the neighbor. • Set the current node as the neighbor's parent. • Add the neighbor to the open list unless it's already there with a smaller `f(n)`.

Heuristic Function

The heuristic function `h(n)` substantially influences the performance of the A* algorithm. It needs to be: • Admissible – never overestimates the true cost to the goal. • Consistent (or Monotonic) – ensures that `h(n)` is less or equal to the cost from node `n` to a neighbor `m` plus `h(m)`.

Examples of heuristic functions: • Manhattan Distance: Suitable for grid-based maps with only horizontal and vertical movements. • Euclidean Distance: Applicable when diagonal movement is allowed in grids.

Example

Grid Map:

Consider a simple 3x3 grid:

012
0S
1
2G

(S = Start, G = Goal)

Movement Costs:

• Horizontal/Vertical cost = 1 • Diagonal cost = 2\sqrt{2}

Process:

  1. Start from `(0,0)` (S), add to open list.
  2. For each step, calculate `f(n) = g(n) + h(n)` for neighboring nodes.
  3. Continue until the goal `(2,2)` (G) is reached, reconstruct path.

Key Points Summary

FeatureDescription
TypePathfinding and graph traversal
ComponentsOpen List, Closed List, Priority Queue, Path Cost g(n), Heuristic h(n)
Cost Functionf(n)=g(n)+h(n)f(n) = g(n) + h(n)
Heuristic RequirementsAdmissible and Consistent
ApplicationsGames, Robotics, GIS
ComplexityTime: O(E)O(E), Space: O(V)O(V) (depends heavily on heuristic)

Applications

Game Development: Pathfinding in AI characters for strategy or simulation games where characters need to evade obstacles, find treasures, or reach destinations. • Robotics: Navigating autonomous robots through environments to reach goals while avoiding obstacles. • Navigation Systems: Finding shortest paths on maps for cars or pedestrians.

A*'s efficiency and precision make it a go-to algorithm in a range of computational fields where determining optimal paths is of essence. Its ability to integrate the benefits of both the Dijkstra's algorithm and Greedy Best-First Search lends it significant practical relevance.


Course illustration
Course illustration

All Rights Reserved.