A Star Algorithm
A Star Algorithm
• g(n): The actual cost from the starting node to any node n.
• h(n): The heuristic estimated cost from node n to the goal. This is where A*
integrates knowledge beyond the graph to guide the search.
• Initialization: Start by adding the initial node to the open set with its f(n).
• Loop: While the open set is not empty, the node with the lowest f(n) value is removed from the
queue.
• Goal Check: If this node is the goal, the algorithm terminates and returns the discovered path.
• Node Expansion: Otherwise, expand the node (find all its neighbors), calculating g, h, and f
values for each neighbor. Add each neighbor to the open set if it’s not already present, or if a
better path to this neighbor is found.
• Repeat: The loop repeats until the goal is reached or if there are no more nodes in the open set,
indicating no available path.
Heuristic Function in A* Algorithm
• The effectiveness of the A* algorithm largely depends on the heuristic used. The choice of heuristic can
dramatically affect the performance and efficiency of the algorithm. A good heuristic is one that helps the
algorithm find the shortest path by exploring the least number of nodes possible. The properties of a
heuristic include:
• Admissibility: A heuristic is admissible if it never overestimates the cost of reaching the goal. The classic
example of an admissible heuristic is the straight-line distance in a spatial map.
• Consistency (or Monotonicity): A heuristic is consistent if the estimated cost from the current node to the
goal is always less than or equal to the estimated cost from any adjacent node plus the step cost from the
current node to the adjacent node.
• Common heuristics include the Manhattan distance for grid-based maps (useful in games and urban
planning) and the Euclidean distance for direct point-to-point distance measurement.
How Does the A* Algorithm Work?
Consider the weighted graph depicted here, which contains nodes and the
distance between them. Let's say you start from A and have to go to D.
Now, since the start is at the source A, which will have some initial
heuristic value. Hence, the results are
f(A) = g(A) + h(A)
f(A) = 0 + 6 = 6
Next, take the path to other neighbouring vertices :
f(A-B) = 1 + 4
f(A-C) = 5 + 2
Now take the path to the destination from these nodes, and calculate the
weights :
f(A-B-D) = (1+ 7) + 0
f(A-C-D) = (5 + 10) + 0
It is clear that node B gives you the best path, so that is the node you need
to take to reach the destination.
Applications of A*
• The A* algorithm’s ability to find the most efficient path with a given heuristic
makes it suitable for various practical applications:
• Pathfinding in Games and Robotics: A* is extensively used in the gaming industry
to control characters in dynamic environments, as well as in robotics for
navigating between points.
• Network Routing: In telecommunications, A* helps in determining the shortest
routing path that data packets should take to reach the destination.
• AI and Machine Learning: A* can be used in planning and decision-making
algorithms, where multiple stages of decisions and movements need to be
evaluated.
Advantages of A*
• Optimality: When equipped with an admissible heuristic, A* is guaranteed to
find the shortest path to the goal.
• Completeness: A* will always find a solution if one exists.
• Flexibility: By adjusting heuristics, A* can be adapted to a wide range of
problem settings and constraints.
Limitations and Considerations
• While A* is powerful, it’s not without its limitations. The memory
consumption can be significant, as it needs to maintain all explored and
unexplored nodes in memory.
• Furthermore, the choice of heuristic heavily influences the algorithm’s
performance; a poor heuristic can lead to inefficient exploration and increased
computation.