0% found this document useful (0 votes)
96 views2 pages

(A-Star) Search Algorithm

BAD402 lab

Uploaded by

gladykiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views2 pages

(A-Star) Search Algorithm

BAD402 lab

Uploaded by

gladykiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

The A* (A-star) search algorithm is a popular and efficient

algorithm used for pathfinding and graph traversal. It finds the


shortest path from a start node to a goal node in a weighted graph.
A* is widely used in computer science, particularly in artificial
intelligence and robotics.
Key Concepts
1. Heuristic Function (h): An estimate of the cost to reach the
goal from the current node.
2. Cost Function (g): The cost to reach the current node from
the start node.
3. Evaluation Function (f): The sum of the cost function and the
heuristic function, i.e., f(n) = g(n) + h(n), where n is the
current node.
A* uses a priority queue to explore nodes with the lowest f value
first. It guarantees the shortest path if the heuristic is admissible
(never overestimates the true cost) and consistent.
Algorithm Steps
1. Initialize:
• Create an open set (priority queue) and add the start node.
• Create a closed set (visited nodes).
• Initialize g and f values for nodes, with g(start) = 0 and
f(start) = h(start).
2. Main Loop:
• While the open set is not empty:
o Get the node current with the lowest f value from
the open set.
o If current is the goal, reconstruct the path and
return it.
o Move current from the open set to the closed set.
o For each neighbor of current:
✓ If the neighbor is in the closed set, skip it.
✓ Calculate tentative g score (tentative_g =
g(current) + cost(current, neighbor)).
✓ If the neighbor is not in the open set or tentative
g is lower than the known g:
❖ Set g(neighbor) = tentative_g.
❖ Set f(neighbor) = g(neighbor) + h(neighbor).
❖ Set the parent of the neighbor to current.
❖ If the neighbor is not in the open set, add it.
3. Reconstruction of Path:
• Starting from the goal node, trace back to the start node
using the parent references.
Graph Representation
Here’s a simple graph to visualize the example usage:

In this graph:
• Node A has neighbors B and C with respective costs 1 and 3.
• Node B has neighbor D with cost 5.
• Node C has neighbor D with cost 1.
The heuristic values are given in parentheses, indicating the
estimated cost to reach the goal node D.
Running the provided code will result in finding the path from node
A to node D, which in this case is A -> C -> D with a total cost of
4.

You might also like