Game Dev & AI

A* Search Algorithm: Pathfinding in Games and AI

How do NPCs navigate complex mazes in video games? Why is Google Maps so fast at finding your route? The answer is almost always the A* (A-Star) search algorithm. Learn the magic of heuristics that makes A* the king of pathfinding.

15 Min Read Updated: June 2026 Intermediate Level
LGT
Learn Graph Theory Team
Expert Operations Research Engineers

1. What is the A* Algorithm?

The A* (pronounced "A-Star") algorithm is a graph traversal and path search algorithm that is widely used in computer science due to its completeness, optimality, and optimal efficiency. Invented in 1968 by Peter Hart, Nils Nilsson, and Bertram Raphael of the Stanford Research Institute, it was originally designed to help the Shakey robot navigate through rooms.

Today, A* is the absolute industry standard for routing and pathfinding. Whether a character in a strategy game is walking across a grid, or a GPS is calculating the fastest drive across a country, A* (or a variant of it) is likely doing the heavy lifting.

2. The Problem with Dijkstra's Algorithm

To understand why A* is brilliant, we must first understand what it improves upon. Before A*, the gold standard for finding the shortest path was Dijkstra's Algorithm.

Dijkstra's algorithm is guaranteed to find the shortest path. However, it is fundamentally "blind." When you ask Dijkstra to find a path from New York to Los Angeles, it will explore roads leading to Boston, Miami, and Chicago just as eagerly as it explores roads heading west. It searches outward in a perfect circle (or sphere) in all directions equally until it accidentally bumps into the target.

In a large map, exploring every possible direction is incredibly slow and wastes massive amounts of processing power. We need an algorithm that is "smart" enough to know which direction the target is in, so it can prioritize exploring paths that head towards the goal.

3. The Secret Sauce: Heuristics (f = g + h)

A* solves the "blindness" problem by introducing a Heuristic. A heuristic is an educated guess. In pathfinding, it's a function that estimates how far a node is from the final destination.

A* assigns a score to every node it discovers. The node with the lowest score is the one it explores next. The core equation of A* is:

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

By adding the h(n) component, A* is pulled towards the goal like a magnet. It will ignore paths that go in the opposite direction of the goal, drastically reducing the search space compared to Dijkstra's algorithm.

4. Choosing the Right Heuristic

The magic of A* relies entirely on the accuracy of the heuristic function h(n). If your heuristic overestimates the distance to the goal, A* is no longer guaranteed to find the shortest path. If it underestimates, it becomes slower. Therefore, choosing the right heuristic for your specific game or map is critical.

Manhattan Distance (For Grid Worlds without Diagonals)

If your game uses a grid (like Pac-Man or a traditional roguelike) and characters can only move Up, Down, Left, or Right, you should use the Manhattan Distance. It calculates the total number of blocks horizontally and vertically between the current node and the target.

function heuristic(node, goal) {
    return abs(node.x - goal.x) + abs(node.y - goal.y)
}

Euclidean Distance (For Open Worlds or Any-Angle Movement)

If characters can move in any direction (or if you are routing on a continuous map), you should use Euclidean Distance. This is the straight-line "as the crow flies" distance between two points, calculated using the Pythagorean theorem.

function heuristic(node, goal) {
    return sqrt((node.x - goal.x)^2 + (node.y - goal.y)^2)
}

Chebyshev Distance (For Grid Worlds with Diagonals)

If your game uses a grid but allows diagonal movement (where moving diagonally costs the same as moving straight), you use the Chebyshev distance. It takes the maximum of the horizontal or vertical differences.

function heuristic(node, goal) {
    return max(abs(node.x - goal.x), abs(node.y - goal.y))
}

Visualize the Difference

Watch Dijkstra and A* race to solve the same maze. Notice how Dijkstra floods the entire map, while A* creates a laser-focused path directly toward the exit.

Launch A* Visualizer

5. Step-by-Step Execution of A*

Here is exactly how the algorithm maintains its state and processes nodes during execution:

  1. Initialization: Create two sets: an OPEN set (nodes to be evaluated) and a CLOSED set (nodes already evaluated). Add the starting node to the OPEN set.
  2. Loop Start: Find the node in the OPEN set with the lowest f cost. Let's call this the Current node.
  3. Check Goal: If the Current node is the target goal, you are done! Follow the parent pointers backward to reconstruct the path.
  4. Move to Closed: Remove the Current node from the OPEN set and add it to the CLOSED set.
  5. Expand Neighbors: Look at all adjacent neighbors of the Current node. For each neighbor:
    • If the neighbor is an obstacle (wall) or is in the CLOSED set, ignore it.
    • Calculate the neighbor's g cost (Current g + distance to neighbor).
    • If the neighbor is not in the OPEN set, calculate its h and f costs, set its parent to Current, and add it to the OPEN set.
    • If the neighbor is already in the OPEN set, check if this new path to the neighbor is better (has a lower g cost). If it is, update its parent and g cost.
  6. Repeat: Loop back to Step 2. If the OPEN set becomes empty and the goal hasn't been reached, there is no valid path.

6. Real-World Applications in Game Dev & AI

A* is the backbone of movement in digital spaces.

Frequently Asked Questions

How does the A* algorithm differ from Dijkstra's?

A* uses a heuristic function (estimating the distance to the goal) to guide its path search, whereas Dijkstra's algorithm is blind and explores in all directions equally. This makes A* significantly faster and more targeted.

What is an admissible heuristic in A*?

An admissible heuristic is one that never overestimates the actual cost to reach the goal. Admissibility guarantees that A* will find the mathematically shortest path.

When should I choose Manhattan vs. Euclidean distance?

Use Manhattan distance for grids where movement is restricted to 4 directions (up, down, left, right). Use Euclidean distance for continuous environments allowing movement in any angle.

Further Exploration

Master Pathfinding

Reading about A* is good, but building mazes and watching the algorithm solve them is better. Draw walls, set start and end points, and watch how different heuristics change the search pattern in real-time.

Launch A* Visualizer