A* (A-Star) Search Algorithm in Artificial Intelligence
A* is one of the most important informed (heuristic) search algorithms in Artificial Intelligence. It combines the actual cost of reaching a state with an estimate of the remaining cost to reach the goal.
The central idea is:
A* chooses the node with the lowest estimated total solution cost.
Its evaluation function is:\[ \boxed{f(n)=g(n)+h(n)} \]
where:
- \(g(n)\) = actual cost from the initial state to node \(n\)
- \(h(n)\) = estimated cost from node \(n\) to the goal
- \(f(n)\) = estimated total cost of a solution through node \(n\)
1. Definition
Exam Definition
A* Search is an informed search algorithm that selects the node with the minimum value of \(f(n)=g(n)+h(n)\), where \(g(n)\) is the actual cost from the initial state to node \(n\), and \(h(n)\) is the estimated cost from node \(n\) to the goal. With an admissible heuristic, A* is guaranteed to find an optimal solution under the standard search conditions.
2. Why A* Is Important
Before A*, we discussed:
Uniform Cost Search
It considers only the cost already spent:\[ f(n)=g(n) \]
Greedy Best-First Search
It considers only the estimated remaining cost:\[ f(n)=h(n) \]
A*
It considers both:\[ \boxed{f(n)=g(n)+h(n)} \]
So:
A* Search
|
┌──────────┴──────────┐
↓ ↓
Actual Cost Estimated Cost
g(n) h(n)
\ /
\ /
└───────┬─────────┘
↓
f(n) = g(n) + h(n)
↓
Choose lowest f(n)
This is the most important concept to remember.
3. Understanding \(g(n)\), \(h(n)\), and \(f(n)\)
Suppose we have:
Start → A → B → Goal
If the actual cost from Start to B is:\[ g(B)=8 \]
and our heuristic estimates that B is:\[ h(B)=5 \]
away from the goal, then:\[ f(B)=g(B)+h(B) \]\[ f(B)=8+5=13 \]
Therefore, A* considers B’s estimated total cost to be:\[ \boxed{13} \]
4. Meaning of the Three Functions
| Function | Meaning | Question |
|---|---|---|
| \(g(n)\) | Actual cost so far | “How much have I already spent?” |
| \(h(n)\) | Estimated remaining cost | “How much might remain?” |
| \(f(n)\) | Estimated total cost | “What might the complete solution cost?” |
Easy memory trick
G = Gone cost
H = How much remains
F = Full estimated cost
5. Basic Working of A*
Suppose there are several nodes in the frontier:
| Node | \(g(n)\) | \(h(n)\) | \(f(n)=g+h\) |
|---|---|---|---|
| A | 4 | 8 | 12 |
| B | 7 | 3 | 10 |
| C | 5 | 6 | 11 |
| D | 8 | 5 | 13 |
A* selects:\[ \boxed{B} \]
because:\[ f(B)=10 \]
is the smallest.
Notice that B does not have the smallest \(g\) or \(h\) individually.
It has the smallest combined estimated cost.
6. A Simple Example
Consider:
B
/ \
/ \
4 / \ 5
/ \
Start -------A--------- Goal
2 8
Let’s assume the nodes have the following values:
| Node | \(g(n)\) | \(h(n)\) | \(f(n)\) |
|---|---|---|---|
| A | 2 | 7 | 9 |
| B | 6 | 4 | 10 |
A* compares:\[ f(A)=9 \]
and:\[ f(B)=10 \]
So it selects A.
The important point is that A* doesn’t simply select the node that appears closest to the goal.
It considers:\[ \boxed{\text{cost already spent + estimated remaining cost}} \]
7. A* Search Algorithm — Step by Step
Step 1: Start with the initial node
Put the initial node into the OPEN list or priority queue.
Step 2: Calculate its \(f(n)\)
\[ f(n)=g(n)+h(n) \]
Step 3: Select the node with minimum \(f(n)\)
Step 4: Check whether it is the goal
If yes:
Return the solution path.
Step 5: Expand the node
Generate its successor nodes.
Step 6: Calculate values
For every successor:\[ g(n), h(n), f(n) \]
Step 7: Add successors to the priority queue
Step 8: Repeat
Continue selecting the node with the smallest \(f(n)\).
8. A* Flowchart
START
|
↓
Initial State
|
↓
Calculate g,h,f
|
↓
Put in OPEN List
|
↓
Select minimum f(n)
|
↓
Goal?
/ \
YES NO
| |
↓ ↓
Return Expand Node
Solution |
↓
Generate Children
|
↓
Calculate g,h,f
|
↓
Update OPEN/CLOSED
|
└──────→ Repeat
9. OPEN and CLOSED Lists
A* implementations commonly use two sets/lists.
OPEN List
Contains nodes that:
- have been generated,
- but have not yet been fully expanded.
It is essentially the frontier.
CLOSED List
Contains nodes that have already been expanded/processed.
A*
|
┌──────┴──────┐
↓ ↓
OPEN CLOSED
| |
unexplored expanded
nodes nodes
The node with the lowest \(f(n)\) is removed from OPEN and expanded.
10. Complete Numerical Example
Consider the following graph:
B
2 / \
/ \ 5
/ \
Start Goal
\ /
4 \ / 2
\ /
C
|
3
|
D
Suppose heuristic values are:
| Node | \(h(n)\) |
|---|---|
| Start | 7 |
| B | 5 |
| C | 4 |
| D | 2 |
| Goal | 0 |
From Start:
Node B
\[ g(B)=2 \]\[ h(B)=5 \]
Therefore:\[ f(B)=2+5=7 \]
Node C
\[ g(C)=4 \]\[ h(C)=4 \]
Therefore:\[ f(C)=4+4=8 \]
A* selects B because:\[ 7<8 \]
Now suppose B generates Goal:\[ g(Goal)=2+5=7 \]
and:\[ h(Goal)=0 \]
Therefore:\[ f(Goal)=7 \]
The goal can then be selected.
The resulting path is:\[ \boxed{Start\rightarrow B\rightarrow Goal} \]
with total cost:\[ \boxed{7} \]
11. What Is a Heuristic?
A heuristic function is a problem-specific estimate of the cost from the current state to the goal.
It is represented as:\[ h(n) \]
Examples:
Route Finding
\[ h(n)=\text{straight-line distance to destination} \]
8-Puzzle
\[ h(n)=\text{number of misplaced tiles} \]
or:\[ h(n)=\text{Manhattan distance} \]
8-Queens
A possible evaluation:\[ h(n)=\text{number of attacking queen pairs} \]
12. Admissible Heuristic
This is extremely important for A*.
A heuristic is called admissible if it never overestimates the actual minimum remaining cost to the goal.
Mathematically:\[ \boxed{h(n)\leq h^*(n)} \]
where:
- \(h(n)\) = estimated cost
- \(h^*(n)\) = actual minimum cost remaining
Example
Actual remaining cost:\[ 10 \]
Possible heuristic:\[ h(n)=7 \]
This is admissible because:\[ 7\leq10 \]
But:\[ h(n)=12 \]
would be an overestimate and therefore not admissible.
13. Why Is Admissibility Important?
If the heuristic never overestimates the remaining cost, A* can safely use it to guide the search without eliminating the possibility of finding an optimal solution.
Therefore:
A with an admissible heuristic is optimal under the standard conditions.*
For graph-search A*, consistency is also important in the usual optimality guarantee.
14. Consistent Heuristic
A heuristic is consistent (or monotone) if for every node \(n\) and successor \(n’\):\[ \boxed{h(n)\leq c(n,n’)+h(n’)} \]
where:
- \(c(n,n’)\) = cost of moving from \(n\) to \(n’\).
Also:\[ h(Goal)=0 \]
A consistent heuristic is always admissible.
For an MCA syllabus, the main point to remember is:
Admissible heuristic → A can guarantee optimality; consistent heuristics provide the standard stronger graph-search guarantee.*
15. A* and Optimality
A* is one of the most important search algorithms because it can be optimal.
If:
- the heuristic is admissible,
- step/action costs satisfy the standard positive-cost conditions,
then A* can find a least-cost solution.
For graph search, a consistent heuristic gives the standard optimality guarantee without needing to reopen already expanded nodes.
16. A* vs Greedy Best-First Search
This is a very important exam comparison.
| Feature | Greedy Best-First | A* |
|---|---|---|
| Evaluation | \(f(n)=h(n)\) | \(f(n)=g(n)+h(n)\) |
| Actual cost | Ignored | Considered |
| Heuristic | Used | Used |
| Goal direction | Strong | Strong |
| Optimal | Not guaranteed | Yes, under suitable conditions |
| Main concern | “Looks closest” | “Best total estimated path” |
Example
Suppose:
| Node | \(g(n)\) | \(h(n)\) |
|---|---|---|
| A | 20 | 2 |
| B | 5 | 8 |
Greedy:\[ f(A)=2 \]\[ f(B)=8 \]
So Greedy chooses A.
A*:\[ f(A)=20+2=22 \]\[ f(B)=5+8=13 \]
So A* chooses B.
This demonstrates why A* can make a better overall decision.
17. A* vs Uniform Cost Search
Another important comparison:
| Feature | Uniform Cost Search | A* |
|---|---|---|
| Evaluation | \(g(n)\) | \(g(n)+h(n)\) |
| Uses heuristic | No | Yes |
| Uses path cost | Yes | Yes |
| Goal guidance | No | Yes |
| Optimal | Yes under standard positive-cost conditions | Yes with suitable heuristic/conditions |
| Search efficiency | Can explore many nodes | Usually better with a good heuristic |
Notice:\[ A^* = UCS + \text{heuristic guidance} \]
Conceptually, if:\[ h(n)=0 \]
for every node, then:\[ f(n)=g(n) \]
and A* behaves like Uniform Cost Search.
18. A* vs Hill Climbing
| Feature | Hill Climbing | A* |
|---|---|---|
| Type | Local search | Best-first search |
| Frontier | Usually no | Yes |
| \(g(n)\) | Usually not used | Yes |
| \(h(n)\) | Yes | Yes |
| Backtracking | Limited | Yes |
| Complete | No | Yes under suitable conditions |
| Optimal | No | Yes under suitable conditions |
| Memory | Very low | Can be high |
Simple difference
Hill Climbing:
“Which neighbor looks better?”
A*:
“Which frontier node has the best estimated total cost?”
19. A* and 8-Puzzle
A* is particularly famous for solving the 8-Puzzle.
Suppose:
Initial:
1 2 3
4 _ 6
7 5 8
Goal:
1 2 3
4 5 6
7 8 _
Possible heuristic:
Misplaced Tiles
Count tiles that are not in their goal positions.\[ h(n)=\text{number of misplaced tiles} \]
Another heuristic:
Manhattan Distance
\[ h(n)= \sum_i (|x_i-x_i^*|+|y_i-y_i^*|) \]
Then A* calculates:\[ f(n)=g(n)+h(n) \]
and chooses the state with the smallest \(f(n)\).
20. Why Manhattan Distance Is Useful
Suppose a tile is:
Current Goal
1 1
5 2
2 5
The Manhattan distance measures how many horizontal and vertical moves are required to place the tile correctly, ignoring interactions with other tiles.
For each tile:\[ d_i=|x_i-x_i^*|+|y_i-y_i^*| \]
Total:\[ \boxed{h(n)=\sum_i d_i} \]
For the standard 8-Puzzle, Manhattan distance is an admissible heuristic.
21. Properties of A*
Completeness
A* is complete under standard assumptions such as appropriate positive lower bounds on step costs.
Optimality
A* is optimal when the heuristic and search conditions satisfy the required assumptions.
Time Complexity
In the worst case, A* can require exponential time.
A common textbook characterization is approximately:\[ O(b^d) \]
where:
- \(b\) = branching factor
- \(d\) = depth of the optimal solution.
Space Complexity
A* can require substantial memory because it stores frontier nodes and explored information.
Often represented as approximately:\[ O(b^d) \]
This memory requirement is one of A*’s major practical limitations.
22. Advantages of A*
1. Optimal
With a suitable admissible/consistent heuristic, it can find an optimal solution.
2. Complete
Under standard search assumptions, it can find a solution if one exists.
3. Efficient with Good Heuristic
A good heuristic can dramatically reduce unnecessary exploration.
4. Flexible
The algorithm can be adapted to many domains.
5. Combines Two Important Ideas
It combines:\[ \text{Actual cost} + \text{Estimated future cost} \]
23. Disadvantages
1. High Memory Usage
A* may store a large number of nodes.
2. Computationally Expensive
Large search spaces can make A* expensive.
3. Heuristic Design Is Important
A poor heuristic may make A* behave closer to Uniform Cost Search.
4. Large Problems
For extremely large state spaces, standard A* may become impractical.
This leads to Memory-Bounded Heuristic Search, which is the next part of your syllabus.
24. Applications of A*
A* is widely used in:
1. Route Finding
Finding paths between locations.
2. GPS and Navigation
Finding efficient routes.
3. Robotics
Path planning for robots.
4. Games
Character movement and game-world navigation.
5. Puzzle Solving
- 8-Puzzle
- 15-Puzzle
6. Network Routing
Finding efficient paths through networks.
7. Motion Planning
Planning movement of autonomous systems.
25. A* in GPS Example
Imagine a navigation system wants to travel from:
Ludhiana → Delhi
Suppose two candidate routes are:
| Route | Distance travelled \(g(n)\) | Estimated remaining \(h(n)\) | \(f(n)\) |
|---|---|---|---|
| Route A | 250 km | 100 km | 350 km |
| Route B | 180 km | 150 km | 330 km |
A* chooses Route B because:\[ 330 < 350 \]
Even though Route B has a larger estimated remaining distance, its total estimated cost is lower.
26. The Three Major Search Formulas
For your exam, memorize these together:
┌───────────────────────────────┐
│ Uniform Cost Search │
│ f(n) = g(n) │
├───────────────────────────────┤
│ Greedy Best-First Search │
│ f(n) = h(n) │
├───────────────────────────────┤
│ A* Search │
│ f(n) = g(n) + h(n) │
└───────────────────────────────┘
Memory Trick
UCS = Past
Greedy = Future
A* = Past + Future
More precisely:
- UCS → actual cost so far
- Greedy → estimated cost remaining
- A* → actual + estimated cost
27. A* Search — Complete Conceptual Flow
Problem
↓
Initial State
↓
Generate Nodes
↓
Calculate g(n), h(n)
↓
f(n) = g(n) + h(n)
↓
Put nodes in OPEN list
↓
Select minimum f(n)
↓
Goal?
/ \
YES NO
↓ ↓
Solution Expand
↓
Generate children
↓
Calculate values
↓
Update OPEN/CLOSED
↓
Repeat
28. Exam Answer — 5 Marks
A* Search is an informed heuristic search algorithm that finds a path from an initial state to a goal state by selecting the node with the minimum evaluation value \(f(n)=g(n)+h(n)\). Here, \(g(n)\) is the actual cost from the initial state to node \(n\), while \(h(n)\) is the estimated cost from \(n\) to the goal. A* maintains a priority queue of frontier nodes and repeatedly expands the node having the smallest \(f(n)\). When an admissible heuristic is used under the standard conditions, A* can find an optimal solution. A* is widely used in route finding, robotics, games, navigation and puzzle solving.
29. One-Page Revision
A* SEARCH
│
Informed Search
│
Heuristic Function
│
┌──────────┴──────────┐
↓ ↓
g(n) h(n)
Actual Cost Estimated Cost
Already Spent Remaining
\ /
\ /
└────────┬────────┘
↓
f(n)=g(n)+h(n)
↓
Select minimum f(n)
↓
Expand
↓
Generate successors
↓
Recalculate values
↓
Repeat
↓
Goal
Final points to remember
- A* is an informed search algorithm.
- Its formula is:\[ \boxed{f(n)=g(n)+h(n)} \]
- \(g(n)\) = actual cost so far.
- \(h(n)\) = estimated cost to goal.
- \(f(n)\) = estimated total cost.
- It uses a priority queue/frontier.
- A good heuristic makes A* more efficient.
- Admissible heuristic → optimality under standard conditions.
- A* combines the ideas of Uniform Cost Search and Greedy Best-First Search.
- Major limitation: high memory requirement.
