Skip to main content

A* Search Algorithm - Detailed Explanation with Python Example

 What is A* Search Algorithm?

A* Search (pronounced "A-star") is one of the most popular and powerful pathfinding and graph traversal algorithms. It finds the shortest path between nodes using both the cost to reach a node and a heuristic estimation of the remaining cost to the goal.

Key Concepts

  • g(n): The actual cost from the start node to the current node $n$.
  • h(n): The heuristic estimated cost from node $n$ to the goal.
  • f(n): The total estimated cost of the cheapest solution through node $n$, calculated as:$f(n) = g(n) + h(n)$

A* Algorithm Steps

  1. Initialize the open set with the start node.
  2. Initialize a map to record the lowest cost to reach each node ($g$ value).
  3. While the open set is not empty:
    • Pick the node with the lowest $f(n)$ value.
    • If the node is the goal, reconstruct and return the path.
    • Else, for each neighbor:
      • Calculate tentative $g$ score.
      • If this score is better than previously recorded, update it.
      • Set neighbor's parent to the current node.
      • If the neighbor is not in the open set, add it.

Python Example Code

import heapq

def a_star_search(graph, start, goal, heuristic):
    open_set = []
    heapq.heappush(open_set, (0, start))
    
    came_from = {}
    g_score = {node: float('inf') for node in graph}
    g_score[start] = 0

    f_score = {node: float('inf') for node in graph}
    f_score[start] = heuristic(start, goal)

    while open_set:
        current = heapq.heappop(open_set)[1]

        if current == goal:
            path = []
            while current in came_from:
                path.append(current)
                current = came_from[current]
            path.append(start)
            return path[::-1]

        for neighbor, cost in graph[current]:
            tentative_g_score = g_score[current] + cost
            if tentative_g_score < g_score[neighbor]:
                came_from[neighbor] = current
                g_score[neighbor] = tentative_g_score
                f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal)
                heapq.heappush(open_set, (f_score[neighbor], neighbor))

    return None

# Example heuristic: straight-line distance (mocked)
def heuristic(n, goal):
    h_values = {
        'A': 6, 'B': 4, 'C': 4, 'D': 2, 'E': 0
    }
    return h_values.get(n, 0)

# Example graph
graph = {
    'A': [('B', 1), ('C', 3)],
    'B': [('D', 1)],
    'C': [('D', 1), ('E', 5)],
    'D': [('E', 2)],
    'E': []
}

# Usage
path = a_star_search(graph, 'A', 'E', heuristic)
print("Path found:", path)

Output:

Path found: ['A', 'B', 'D', 'E']


Complexity Analysis

Time Complexity

  • In the worst case (when the heuristic function is poor), the time complexity can degrade to that of Dijkstra's Algorithm:$O((V + E) \log V)$ where $V$ is the number of vertices and $E$ is the number of edges.
  • If the heuristic is perfect (always estimates the real cost exactly), A* can explore only the optimal path.

Space Complexity

  • Similar to the time complexity, A* stores all generated nodes in memory, so space complexity is:$O(V)$

Best, Average, and Worst Cases

  • Best Case: When $h(n)$ exactly predicts the true cost. Only optimal nodes are expanded.
  • Average Case: A reasonably good heuristic reduces unnecessary exploration.
  • Worst Case: A poor heuristic behaves like Uniform Cost Search (no pruning).

Conclusion

A* Search is widely used in AI, robotics, and game development due to its balance between optimality and efficiency. Choosing a good heuristic function is critical to unlocking A*'s full potential.

References

Comments

Popular

Understanding SentencePiece: A Language-Independent Tokenizer for AI Engineers

In the realm of Natural Language Processing (NLP), tokenization plays a pivotal role in preparing text data for machine learning models. Traditional tokenization methods often rely on language-specific rules and pre-tokenized inputs, which can be limiting when dealing with diverse languages and scripts. Enter SentencePiece—a language-independent tokenizer and detokenizer designed to address these challenges and streamline the preprocessing pipeline for neural text processing systems. What is SentencePiece? SentencePiece is an open-source tokenizer and detokenizer developed by Google, tailored for neural-based text processing tasks such as Neural Machine Translation (NMT). Unlike conventional tokenizers that depend on whitespace and language-specific rules, SentencePiece treats the input text as a raw byte sequence, enabling it to process languages without explicit word boundaries, such as Japanese, Chinese, and Korean. This approach allows SentencePiece to train subword models di...

Mastering the Byte Pair Encoding (BPE) Tokenizer for NLP and LLMs

Byte Pair Encoding (BPE) is one of the most important and widely adopted subword tokenization algorithms in modern Natural Language Processing (NLP), especially in training Large Language Models (LLMs) like GPT. This guide provides a deep technical dive into how BPE works, compares it with other tokenizers like WordPiece and SentencePiece, and explains its practical implementation with Python code. This article is optimized for AI engineers building real-world models and systems. 1. What is Byte Pair Encoding? BPE was originally introduced as a data compression algorithm by Gage in 1994. It replaces the most frequent pair of bytes in a sequence with a single, unused byte. In 2015, Sennrich et al. adapted BPE for NLP to address the out-of-vocabulary (OOV) problem in neural machine translation. Instead of working with full words, BPE decomposes them into subword units that can be recombined to represent rare or unseen words. 2. Why Tokenization Matters in LLMs Tokenization is th...

ZeRO: Deep Memory Optimization for Training Trillion-Parameter Models

In 2020, Microsoft researchers introduced ZeRO (Zero Redundancy Optimizer) via their paper "ZeRO: Memory Optimization Towards Training Trillion Parameter Models" (arXiv:1910.02054). ZeRO is a memory optimization technique that eliminates redundancy in distributed training, enabling efficient scaling to trillion-parameter models. This provides an in-depth technical breakdown of ZeRO's partitioning strategies, memory usage analysis, and integration with DeepSpeed. 1. What is ZeRO? ZeRO eliminates redundant memory copies of model states across GPUs. Instead of replicating parameters, gradients, and optimizer states across each GPU, ZeRO partitions them across all devices. This results in near-linear memory savings as the number of GPUs increases. 2. Limitations of Traditional Data Parallelism In standard data-parallel training, every GPU maintains: Model Parameters $\theta$ Gradients $\nabla \theta$ Optimizer States $O(\theta)$ This causes memory usage ...