A* Search Algorithm
The A* (A-star) search algorithm is a graph traversal and path planning algorithm that determines the shortest path between a designated start node and target node. By combining uniform-cost search (Dijkstra's algorithm) with greedy best-first search, A* achieves optimal and complete pathfinding efficiency through the use of a heuristic evaluation function, f(n) = g(n) + h(n).
HOW-TO IMPLEMENT & CONFIGURE
- Runtime: Python 3.10+ or any modern systems language with a native priority queue implementation.
- Libraries: Standard library heapq (Python) or equivalent binary/fibonacci heap structures.
- Graph representation utilizing adjacency lists or explicit grid-based coordinate mappings.
Step 1
Define the Node abstraction and initialize the open and closed sets. The open set must be backed by a priority queue (min-heap) ordered by the f-cost of each node to achieve O(log n) insertions and extractions.
import heapq
class Node:
def __init__(self, position, g=float('inf'), h=0, parent=None):
self.position = position
self.g = g
self.h = h
self.f = g + h
self.parent = parent
def __lt__(self, other):
return self.f < other.f
Step 2
Implement the core traversal loop, processing nodes from the priority queue, generating valid successors, and applying g-cost updates alongside closed-set validation.
def a_star(start_pos, goal_pos, grid):
open_set = []
start_node = Node(start_pos, g=0, h=heuristic(start_pos, goal_pos))
heapq.heappush(open_set, start_node)
open_set_hash = {start_pos: start_node}
closed_set = set()
while open_set:
current = heapq.heappop(open_set)
open_set_hash.pop(current.position, None)
if current.position == goal_pos:
return reconstruct_path(current)
closed_set.add(current.position)
for neighbor_pos in get_neighbors(current.position, grid):
if neighbor_pos in closed_set:
continue
tentative_g = current.g + cost(current.position, neighbor_pos)
neighbor_node = open_set_hash.get(neighbor_pos)
if neighbor_node is None:
neighbor_node = Node(neighbor_pos, g=tentative_g, h=heuristic(neighbor_pos, goal_pos), parent=current)
heapq.heappush(open_set, neighbor_node)
open_set_hash[neighbor_pos] = neighbor_node
elif tentative_g < neighbor_node.g:
neighbor_node.g = tentative_g
neighbor_node.f = tentative_g + neighbor_node.h
neighbor_node.parent = current
heapq.heapify(open_set)
return None
Step 3
Define mathematically valid heuristics depending on topology, such as Manhattan distance for grid-based orthogonal movement or Euclidean distance for continuous multi-directional spaces.
def heuristic(a, b):
# Manhattan distance for 4-connected grids
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def reconstruct_path(node):
path = []
current = node
while current:
path.append(current.position)
current = current.parent
return path[::-1]
Step 4
Mitigate performance degradation in unreachable scenarios by establishing maximum iteration limits and memory ceilings. Ensure floating-point precision comparisons use epsilon tolerances to avoid priority inversion.
def a_star_bounded(start_pos, goal_pos, grid, max_iterations=100000):
iterations = 0
# Implement bounded check within the main loop
# to guarantee bounded execution time under memory constraints.
pass
Mathematical Foundation & Admissibility
A* evaluates nodes using the function f(n) = g(n) + h(n), where g(n) represents the exact cost of the path from the start node to the current node n, and h(n) represents the estimated heuristic cost from n to the goal. For A* to guarantee optimality (shortest path), the heuristic function h(n) must be admissible, meaning it never overestimates the actual cost to reach the goal. Furthermore, to ensure computational efficiency and prevent redundant node expansions in dense graphs, the heuristic must satisfy consistency (monotonicity): h(n) <= c(n, n') + h(n') for every edge (n, n') with cost c(n, n').
📚 STANDARDS & ACADEMIC RFC CITATIONS
- Hart, Peter E.; Nilsson, Nils J.; Raphael, Bertram (1968). 'A Formal Basis for the Heuristic Determination of Minimum Cost Paths'. Systems Science and Cybernetics, IEEE Transactions on.
- Russell, Stuart J.; Norvig, Peter (2020). Artificial Intelligence: A Modern Approach (4th ed.). Pearson.