Module 2 Lecture 5
Module 2 Lecture 5
LECTURE-5
A* Search Algorithm
To approximate the shortest path in real-life situations, like- in maps, games
Motivationwhere there can be many hindrances.
We can consider a 2D Grid having several obstacles and we start from a source
cell (colored red below) to reach towards a goal cell (colored green below)
What is A* Search Algorithm?
A* Search algorithm is one of the best and popular technique used in path-finding and
graph traversals.
And it is also worth mentioning that many games and web-based maps use this algorithm
to find the shortest path very efficiently (approximation).
Explanation
Consider a square grid having many obstacles and we are given a starting cell and a target
cell. We want to reach the target cell (if possible) from the starting cell as quickly as
possible. Here A* Search Algorithm comes to the rescue.
What A* Search Algorithm does is that at each step it picks the node according to a
value-‘f’ which is a parameter equal to the sum of two other parameters – ‘g’ and ‘h’. At
each step it picks the node/cell having the lowest ‘f’, and process that node/cell.
g = the movement cost to move from the starting point to a given square on the grid,
following the path generated to get there.
h = the estimated movement cost to move from that given square on the grid to the final
destination.
This is often referred to as the heuristic, which is nothing but a kind of smart guess. We
really don’t know the actual distance until we find the path, because all sorts of things can
be in the way (walls, water, etc.). There can be many ways to calculate this ‘h’ which are
discussed in the later sections.
The main idea of A* is to evaluate each node based on two parameters:
1.g(n): the actual cost to get from the initial node to node n. It represents the sum of the
costs of node n outgoing edges.
2.h(n): Heuristic cost (also known as "estimation cost") from node n to destination node n.
This problem-specific heuristic function must be acceptable, meaning it never
overestimates the actual cost of achieving the goal. The evaluation function of node n is
defined as f(n) = g(n)+h(n).
A* Algorithm extends the path that minimizes the following
function-
f(n) = g(n) + h(n)
Here,
•‘n’ is the last node on the path
•g(n) is the cost of the path from start node to node ‘n’
•h(n) is a heuristic function that estimates cost of the cheapest path from node
‘n’ to the goal node
Solution-
Consider the following graph
Step-01:
•f(B) = 6 + 8 = 14
•f(F) = 3 + 6 = 9
Node G and Node H can be reached from node F. A* Algorithm calculates f(I).
f(I) = (3+1+3) + 1 = 8
A* Algorithm calculates f(G) and f(H). It decides to go to node I.
•f(G) = (3+1) + 5 = 9
•f(H) = (3+7) + 3 = 13 Path- A → F → G → I
Step-04:
Since f(G) < f(H), so it decides to go to node G.
Node E, Node H and Node J can be reached from node I.
Path- A → F → G
A* Algorithm calculates f(E), f(H) and f(J).
•f(E) = (3+1+3+5) + 3 = 15
•f(H) = (3+1+3+2) + 3 = 12
•f(J) = (3+1+3+3) + 0 = 10
Path- A → F → G → I → J
This is the required shortest path from node A to node J.
It is important to note that-
Exploring F:
SHORTEST PATH FOUND
Advantages of A* Search Algorithm in
Artificial Intelligence
The A* search algorithm offers several advantages in artificial intelligence and
problem-solving scenarios:
1.Optimal solution: A* ensures finding the optimal (shortest) path from the start node to
the destination node in the weighted graph given an acceptable heuristic function. This
optimality is a decisive advantage in many applications where finding the shortest path is
essential.
2.Completeness: If a solution exists, A* will find it, provided the graph does not have an
infinite cost This completeness property ensures that A* can take advantage of a solution if it
exists.
3.Efficiency: A* is efficient ifan efficient and acceptable heuristic function is used. Heuristics
guide the search to a goal by focusing on promising paths and avoiding unnecessary
exploration, making A* more efficient than non-aware search algorithms such as breadth-
first search or depth-first search.
4.Versatility: A* is widely applicable to variousproblem areas, including wayfinding, route
planning, robotics, game development, and more. A* can be used to find optimal solutions
efficiently as long as a meaningful heuristic can be defined.
5.Optimized search: A* maintains a priority order to select the nodes with the minor f(n)
value (g(n) and h(n)) for expansion. This allows it to explore promising paths first, which
reduces the search space and leads to faster convergence.
6.Memory efficiency: Unlike some other search algorithms, such as breadth-first search, A*
stores only a limited number of nodes in the priority queue, which makes it memory
1.Tunable Heuristics: A*'s performancecan be fine-tuned by selecting different
heuristic functions. More educated heuristics can lead to faster convergence and less
expanded nodes.
3.Web search: A* can be used for web-based path search, where the algorithm
constantly updates the path according to changes in the environment or the
appearance of new It enables real-time decision-making in dynamic scenarios.
Disadvantages of A* Search Algorithm in
Artificial Intelligence
Although the A* (letter A) search algorithm is a widely used and powerful technique for
solving AI pathfinding and graph traversal problems, it has disadvantages and
limitations. Here are some of the main disadvantages of the search algorithm: