Sitemap
Biswamit Biswal

Biswamit Biswal

Computer Scientist, Economist and Author

A* Search Algorithm: A complete Guide: Formulas, Intuition and Comparisons

6 min readMar 29, 2026

--

  1. What is A* Search?

Imagine you are using Google Maps to find the fastest route from home to a restaurant. The app does not check every possible road, it is smart enough to focus on roads that are likely to get you there faster. This exactly what the A* search algorithm does.

A* is a pathfinding and graph traversal algorithm that finds the shortest or lowest cost path between a starting point and a goal. It was first described by Peter Hart, Nils Nilsson, and Bertram Raphael at Stanford Research Institute in 1968 and remains one of the most widely used algorithms in computer science today.

Simple idea: A* is like a smart navigator, instead of exploring every possible road, it always picks the road that looks most promising — based on how far it has already travelled and how far it still needs to go.

2. The Core Formula

Every decision A* makes is guided by one simple formula:

The A* Evaluation function: f(n)= g(n)+ h(n)

this formula gives every node or location a score. A* always expands the node with the lowest f(n) score first.

Breaking down the formula,

  1. g(n) — The cost so far

It is the actual cost of the path from the start node to the current node n. It is the real measured distance or cost you have already paid to reach this point. For example, on a road map it might be the total kilometers driven so far.

h(n) — The Estimated Cost to the Goal (Heuristic)

h(n) is a guess (called a heuristic) of the cost from the current node to the goal node. It is an estimate — we do not know the exact remaining distance until we actually travel it. The quality of this guess determines how efficient A* is.

f(n) — The Total Estimated Cost

f(n) combines both: the real cost already paid plus the estimated cost still to be paid. A* picks the node with the smallest f(n) at each step, because that node looks like the best bet for the cheapest total path.

Think of it like travelling by train. g(n) = the money you have already spent on tickets. h(n) = your best guess at the remaining ticket costs. f(n) = your total estimated trip cost. You always choose the route with the lowest total estimated cost.

3. Heuristic Functions: h(n) in Detail

The heuristic h(n) is the heart of A*. Choosing the right one is critical. A good heuristic makes A* fast; a bad one slows it down or leads to wrong answers.

3.1 Admissibility — The Golden Rule

For A* to guarantee the shortest path, the heuristic must be admissible: it must never overestimate the true cost to the goal.

Admissibility Condition

h(n) <= h*(n) for all nodes n

Where h*(n) is the true (optimal) cost from n to the goal. The heuristic must always be optimistic — it can underestimate but never overestimate.

An admissible heuristic is like a hopeful friend: they might think the journey is shorter than it is, but they will never think it is longer. This ensures A* never gives up on the optimal path too early.

3.2 Consistency (Monotonicity)

A stronger property is consistency (also called monotonicity). A heuristic is consistent if the estimated cost from node n to the goal is never greater than the actual step cost to a neighbour plus the estimate from that neighbour.

Consistency Condition h(n) <= c(n, n’) + h(n’)

Where c(n, n’) is the actual cost of moving from node n to its neighbour n’. Every consistent heuristic is also admissible.

Path Finding

Common Heuristic Functions

Here are the most widely used heuristics, explained simply:

Manhattan Distance (L1 Distance)

Used in grid maps where you can only move up, down, left, or right (like city blocks). Count how many steps horizontally + how many steps vertically.

Manhattan Distance

h(n) = |x1 — x2| + |y1 — y2|

x1, y1 = current position coordinates. x2, y2 = goal coordinates. | | means absolute value (always positive).

Example: If you are at position (2, 3) and the goal is at (5, 7), the Manhattan distance = |2–5| + |3–7| = 3 + 4 = 7 steps.

Euclidean Distance (Straight-Line Distance)

Used when movement is allowed in any direction (not just grid moves). It is the actual straight-line distance between two points — like measuring with a ruler on a map.

Euclidean Distance

h(n) = sqrt( (x1-x2)² + (y1-y2)² )

This is simply the Pythagorean theorem. sqrt = square root.

Example: At (2, 3) to goal (5, 7): h = sqrt((2–5)² + (3–7)²) = sqrt(9 + 16) = sqrt(25) = 5. A straight-line distance of 5 units.

Chebyshev Distance (Chessboard Distance)

Used in grids where you can move in 8 directions (including diagonals), like a king on a chessboard. It is the maximum of the horizontal and vertical differences.

Chebyshev Distance

Get Biswamit Biswal’s stories in your inbox

Join Medium for free to get updates from this writer.

h(n) = max( |x1 — x2| , |y1 — y2| )

Take the larger of the two differences: horizontal or vertical.

Example: At (2, 3) to goal (5, 7): h = max(|2–5|, |3–7|) = max(3, 4) = 4. Because diagonal moves handle the horizontal distance simultaneously.

4. How A* Works — Step by Step

A* uses two lists to keep track of nodes:

• Open List: Nodes we know about but haven’t fully explored yet (candidates to visit).

• Closed List: Nodes we have already fully explored.

4.1 The Algorithm Steps

Step 1: Add the start node to the Open List with f(start) = 0 + h(start).

Step 2: Pick the node with the lowest f(n) from the Open List. Call it the current node.

Step 3: If the current node is the GOAL, stop. Trace back the path — done!

Step 4: Move the current node from the Open List to the Closed List.

Step 5: For each neighbour of the current node:

◦ If the neighbour is in the Closed List, skip it.

◦ Calculate g(neighbour) = g(current) + cost_to_move_to_neighbour.

◦ Calculate h(neighbour) using the chosen heuristic.

◦ Calculate f(neighbour) = g(neighbour) + h(neighbour).

◦ If the neighbour is NOT in the Open List, add it. If it IS already there with a higher f, update it.

Step 6: Go back to Step 2.

6. Time and Space Complexity

How fast is A*? And how much memory does it use? These are measured using Big-O notation, which describes how performance scales as the problem gets bigger. Here b = branching factor (average number of neighbours per node) and d = depth of the optimal solution.

A* Time Complexity O(b^d)

In the worst case, A* explores exponentially many nodes. However, a good heuristic dramatically reduces the actual nodes explored.

A* Space Complexity

O(b^d)

A* keeps all generated nodes in memory (open + closed lists), which can be expensive for very large problems.

Key insight: When h(n) = h*(n) (a perfect heuristic), A* explores only nodes on the optimal path — O(d) time. The better the heuristic, the closer A* gets to this ideal.

Quick-Reference Formula Sheet

  1. Core Formula f(n) = g(n) + h(n), Total cost = actual cost so far + estimated remaining cost

2. Admissibility Condition

h(n) <= h*(n) for all n, Heuristic must never overestimate

3. Consistency Condition

h(n) <= c(n,n’) + h(n’), Triangle inequality for the heuristic

4. Manhattan Distance

h = |x1 — x2| + |y1 — y2|, 4-directional grid movement

5. Euclidean Distance

h = sqrt((x1-x2)² + (y1-y2)²), Any-angle movement (Pythagorean theorem)

6. Chebyshev Distance

h = max(|x1 — x2|, |y1 — y2|), 8-directional grid (diagonal allowed)

9. Weighted A* Heuristic

f(n) = g(n) + W * h(n) [W > 1], W > 1 trades optimality for speed

10. Dijkstra (special case of A*)

f(n) = g(n) [h = 0], No heuristic — explores all directions

11. Greedy Best-First (special case)

f(n) = h(n) [g = 0], Ignores past cost — fast but not optimal

Conclusion

The A* search algorithm is one of the most elegant ideas in computer science. Its power comes from a simple insight: combine what you know (the actual cost so far) with a smart guess (the estimated remaining cost), and always follow the path that looks cheapest in total.

When the heuristic is well-chosen and admissible, A* is guaranteed to find the shortest path, and it does so far more efficiently than blind search methods like BFS or Dijkstra’s alone.

The formula f(n) = g(n) + h(n) is deceptively simple. Yet it underpins GPS navigation, video game AI, robotic motion planning, and much more. Mastering A* means understanding one of the most impactful algorithms ever designed.

--

--

Biswamit Biswal
Biswamit Biswal

Written by Biswamit Biswal

0 followers

Computer Scientist, Economist and Author