A_ Search (A-Star)
A_ Search (A-Star)
A_ Search (A-Star)
(A-Star)
Introduction to A* Search (A-Star)
Definition: A* Search is an informed search algorithm that finds the shortest path from a starting node to a goal node in a weighted
graph. It combines the principles of Uniform Cost Search (UCS) and heuristic-based search by using both the actual path cost and an
estimated cost to the goal.
Traversal Approach: A* explores paths by selecting nodes with the lowest estimated total cost, where the total cost is a combination
of the actual cost to reach the node and an estimated cost (heuristic) to reach the goal.
Core Mechanism: A* uses a priority queue, evaluating nodes based on their total cost, which is calculated as f(n) = g(n) + h(n),
where:
Typical Use: A* is commonly used in pathfinding for games, navigation, and other scenarios where an efficient, optimal path is
required.
Why We Use A*
Optimal Path in Weighted Graphs:
● A* finds the optimal path in weighted graphs when the heuristic function h(n) is admissible (never overestimates the cost to the
goal).
● Example: A* can be used in road networks to find the shortest path between cities, considering both travel distance and
estimated cost.
● A* is often more efficient than UCS because it uses a heuristic to prioritize paths that appear to lead to the goal faster.
● Example: In a navigation system, using the straight-line distance as a heuristic can guide A* to quickly find routes.
● A* can be adapted to prioritize different types of paths by adjusting the heuristic, making it suitable for scenarios where specific
criteria (like shortest time or minimal fuel) are prioritized.
● Example: A* in video games where the player character needs to find a path to a target, avoiding obstacles.
When to Use A*
Pathfinding in Complex Graphs: A* is highly effective in large, weighted graphs where finding an
optimal path quickly is important.
Optimal and Complete: Guarantees optimal path if Memory Intensive: Stores many nodes in the priority
the heuristic is admissible and consistent queue, especially in dense graphs
Efficient with Good Heuristic: Often faster than UCS Heuristic Dependence: Requires a suitable heuristic,
due to heuristic guidance which can be hard to design for complex domains
Versatile for Pathfinding: Adaptable to different Performance Degradation with Poor Heuristic: If the
heuristics for various applications heuristic is inaccurate, A* may perform poorly
A* Traversal Example in a Weighted Graph
Consider the following weighted graph where we want to find the least-cost path from node A to node G. The heuristic estimates for
each node to the goal node G are as follows:
● Heuristic estimates (h(n)) are values for each node, e.g., h(A), h(B), etc., representing the estimated distance from each node
to G.
A* Traversal Steps for Reaching Node 5
Heuristic Values (h-values)
Assuming estimated (heuristic) costs from each node to the goal node 5 as follows:
● h(0) = 10
● h(1) = 8
● h(2) = 6
● h(3) = 4
● h(4) = 2
● h(5) = 0 (Goal node has zero heuristic cost)
Step-by-Step Traversal
Starting Node (0):
● Expand node 4 (f(4) = 6), as it has the lowest f(n) in the queue.
● From node 4, add reachable nodes:
○ Path to node 5: f(5) = g(5) + h(5) = 10 + 0 = 10
○ Path to node 3: f(3) = g(3) + h(3) = 6 + 4 = 10
Step-by-Step Traversal
Continue Expanding Lowest f(n):
A Traversal Result*: The minimum-cost path from node 0 to node 5 is 0 -> 4 -> 5 with a cost of 10.
A* Traversal Explained for the Graph
Starting Point:
● Begin at the starting node 0 with a cumulative path cost (g(n)) of 0 and a heuristic (h(n)) of 10. The total cost for node 0 is
calculated as f(0) = g(0) + h(0) = 0 + 10 = 10.
● All neighboring nodes of 0 are added to the priority queue with their calculated f(n) values:
○ Node 1: f(1) = g(1) + h(1) = 4 + 8 = 12
○ Node 2: f(2) = g(2) + h(2) = 3 + 6 = 9
○ Node 4: f(4) = g(4) + h(4) = 4 + 2 = 6
● Node 2 is now the next node in the priority queue with f(2) = 9, so we expand it.
● Node 2 has connections to other nodes, but node 4 has already been expanded with a
lower cost, so no new paths are added from node 2.
● Node 5 has f(5) = 10 and is the goal node. Since A* guarantees that this is the least-cost
path, we stop the search here.
A* Pseudo-Code
Here’s an example of A* pseudo-code using a priority queue to manage nodes by their f(n) values.
This code finds the minimum-cost path in a weighted graph by prioritizing nodes with the lowest f(n) value.
Real-World Applications of A*
Pathfinding in Games
● Use: Optimize packet routing through networks by balancing cost and delay.
● Example: Routing protocols in communication networks.
● Benefit: Minimizes latency and bandwidth usage by prioritizing low-cost paths.
Summary
Definition: A* finds the shortest path by using a combination of path cost and heuristic estimates, expanding nodes with the lowest
f(n).
● Pathfinding in Weighted Graphs: Effective in graphs where optimal, efficient paths are needed.
● Complex Environments: Ideal when heuristic guidance can help prioritize efficient paths.
Applications: