Pathfinding
AStar Algorithm
Algorithms
Computer Science
Artificial Intelligence

AStar - explanation of name

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

The A* algorithm, often pronounced "A-Star", is a cornerstone in the realm of pathfinding and graph traversal algorithms. It is renowned for its performance and accuracy, facilitating applications ranging from artificial intelligence in games to robotics and network routing. To fully grasp the significance of A*, it's essential to delve into its etymology, technical aspects, and practical applications.

Origin of the Name

The "A" in A* stands for an algorithm type (`algorithm A`), which specifically signifies that it is a general search algorithm. The asterisk (``) denotes its optimal, heuristic nature. This combination indicates that A is an optimal, refined successor to the basic algorithm, designed to find the least cost path in a graph.

Technical Explanation

A* merges aspects of Dijkstra’s algorithm and Greedy Best-First Search. It computes the shortest path by considering both the cost of the path from the start node (`g(n)`) and the estimated cost to the goal (`h(n)`), also known as the heuristic. The sum of these two components is what influences the choice of path, captured by the function:

f(n)=g(n)+h(n)f(n) = g(n) + h(n)

Where:

  • g(n)g(n) = Cost from the start node to node nn.
  • h(n)h(n) = Estimated cost from node nn to the goal.

The choice of heuristic function h(n)h(n) is critical to the efficiency of A*. The function must be admissible, meaning it never overestimates the actual cost to reach the goal, ensuring the algorithm’s optimality.

Example

Consider a grid-like graph where a robot explores a maze. Here, g(n)g(n) could represent the number of steps from the start to the current point, while h(n)h(n) might utilize the Manhattan distance to estimate the remaining steps to the goal.

Steps in A* Algorithm

  1. Initialize the open list with the start node.
  2. Keep a closed list of nodes already evaluated.
  3. Loop, processing each node:
    • Select the node with the lowest f(n)f(n) from the open list.
    • If it is the goal, reconstruct and return the path.
    • Otherwise, generate its neighbors.
    • For each neighbor:
      • If it is not in the open list or offers a lower g(n)g(n), update its f(n)f(n).
      • Add it to the open list.
  4. Repeat until the goal is reached or the open list is empty.

The Heuristic Function’s Role

  • Admissible: Never overestimates the cost to reach the goal. Ensures paths found are optimal.
  • Consistent (Monotonic): For every node nn and successor nn', the estimated cost should satisfy: h(n)c(n,n)+h(n)h(n) \leq c(n, n') + h(n') Where c(n,n)c(n, n') is the step cost to the neighbor. A* uses this property to enhance efficiency by not revisiting nodes, enabling the open list order.

Applications of A*

  1. Game Development: A* is widely used in games for AI pathfinding, determining optimal movements or decisions for character navigation in complex environments.
  2. Robotics: Robots utilize A* for mapping out routes in unstructured environments, balancing between speed and path optimality.
  3. Network Routing: Finds the shortest path for data packets between nodes in a network.
  4. Geographic Information Systems (GIS): Helps in terrain navigation by assimilating elevation data into path cost calculations.

Example Implementation

Here is a pseudo-code outline for A*:


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.