A_ Search (A-Star)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

A* Search

(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:

● g(n): Actual cost to reach node n from the start.


● h(n): Heuristic estimate of the cost from node n to the goal.

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.

Efficiency with Heuristic Guidance:

● 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.

Flexible and Adaptable:

● 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.

Known Heuristic Information: A* is ideal when an admissible heuristic is available to estimate


the cost to the goal, as this allows the algorithm to prioritize promising paths.

Optimal Solution Requirements: A* provides an optimal solution if the heuristic is admissible,


ensuring that it finds the least-cost path to the goal.
Pros and Cons of A*
Advantages Disadvantages

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):

● Start at node 0 with f(0) = g(0) + h(0) = 0 + 10 = 10.


● Add neighboring nodes to the priority queue based on their total cost f(n) = g(n) + h(n):
○ Path to node 1: f(1) = g(1) + h(1) = 4 + 8 = 12
○ Path to node 2: f(2) = g(2) + h(2) = 3 + 6 = 9
○ Path to node 4: f(4) = g(4) + h(4) = 4 + 2 = 6

Expand Node with Lowest f(n):

● 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):

● Expand node 2 (f(2) = 9).


● From node 2, add paths to reachable nodes:
○ Path to node 4 already exists in the queue with a lower f(4), so we don’t re-add it.

Goal Node Check:

● Expand node 5 (f(5) = 10), as it is the goal node.


● We stop here, as A* has guaranteed this to be the least-cost path to the goal.

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

Expanding the Lowest f(n):

● Node 4 has the lowest f(n) = 6, so we expand it next.


● From node 4, we add its neighboring nodes to the priority queue with updated f(n) values:
○ Node 5: f(5) = g(5) + h(5) = 10 + 0 = 10 (goal node)
○ Node 3: f(3) = g(3) + h(3) = 6 + 4 = 10
A* Traversal Explained for the Graph
Continuing with the Next Lowest f(n):

● 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.

Goal Node Reached:

● 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: Find optimal paths for characters or objects in a game world.


● Example: Navigation for AI in open-world games.
● Benefit: Balances path cost and heuristic for efficient, realistic movement.

Robotics and Autonomous Vehicles

● Use: Pathfinding in dynamic environments with obstacles.


● Example: Robot navigation in warehouses.
● Benefit: Allows for efficient routing with real-time obstacle avoidance.
Real-World Applications of A*
Logistics and Delivery Optimization

● Use: Optimize delivery routes with multiple destinations.


● Example: Route planning for courier services.
● Benefit: Reduces delivery time and cost by finding efficient paths.

Network Routing with Heuristic Cost Estimates

● 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).

Best Suited For:

● 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:

● Game AI and Navigation: Optimal pathfinding for character movement.


● Robotics: Efficient pathfinding in environments with obstacles.
● Logistics: Route optimization for deliveries.
● Network Routing: Cost-effective packet routing.

You might also like