A* (A-star search) is an informed search algorithm that finds the optimal path from a start node to a goal node. It balances the cost to reach a node and an estimate of the cost to reach the goal.

  • Completeness: Yes, when the heuristic is admissible
  • Optimality: Yes, when the heuristic is admissible

Evaluation function

Recall that the Uninformed Cost Search (UCS) minimises the cost so far (), greedy search minimises the estimated cost to the goal (). A* combines UCS and greedy search. Its evaluation function is given below: Where:

  • = cost so far to reach
  • = estimated cost from to the goal
  • = estimated total cost of path through to the goal

Pseudocode

Similar to UCS, A-start uses a priority queue that ranks nodes in terms of their value calculated by the evaluation function.

A_Star(Graph, start, goal):
    1. Create a priority queue (PQ) and insert (start, f(start))
    2. Create a dictionary g_score to store the cost from start to each node (initialize with infinity)
    3. Set g_score[start] = 0
    4. Create a dictionary came_from to store the parent of each node
    5. While PQ is not empty:
        a. Dequeue the node with the lowest f(n) value (current)
        b. If current == goal, reconstruct and return the path
        c. For each neighbor of current:
            i. Compute tentative_g_score = g_score[current] + edge cost
            ii. If tentative_g_score < g_score[neighbor]:
                - Update g_score[neighbor]
                - Update f(neighbor) = tentative_g_score + h(neighbor)
                - Add (neighbor, f(neighbor)) to PQ
                - Update came_from[neighbor] = current
    6. If goal is not found, return failure

Admissible heuristic

If the heuristic is an admissible heuristic, then A* is complete and optimal. The heuristic is admissible if and only if never overestimates the true cost to the goal.

Let’s define:

  • : a state
  • : All possible states

is admissible if and only if:

Consistent (monotone) heuristic

A consistent heuristic implies admissibility.

Definition

A heuristic function is consistent if, for every state (node) and its successor (neighbours) (with step cost ): Convert them into full mathematical notation:

The term is the cost from to any its neighbour.

Intuition (triangle inequality)

Remember that the is the estimated cost from to the goal node, and the definition of a consistent defines that the estimated cost from to the gaol should never be greater than the actual cost to a successor + the estimated cost from that successor to the goal.

The triangle inequality

The triangle inequality is a fundamental concept in mathematics (geometry), stating that:

For any triangle, the sum of the lengths of any two sides must be greater than or equal to the length of the remaining side.

In mathematical notation: In a triangle, Where are sides of the triangle.

In the below diagram, we have these elements that form a triangle:

  • A node
  • The estimated cost from to the goal
  • A neighbour of :
  • The estimated cost from to the goal

The dashed-line edges and nodes contribute to whatever paths between the and the goal and the and the goal. To obey the triangle inequality rule, has to be less than or equal to . This means when moving from to , the heuristic estimate cannot decrease by more than the actual step cost . The consistency condition ensures that the heuristic estimate at node is never over-optimistic.

Consequence of consistent heuristic

When a heuristic is consistent, A* search never needs to revisit a node with a lower cost, meaning that the first time A* visits a node, it is guaranteed to be on the optimal path.

Any heuristic that is consistent, implies its admissibility. In another word, if a heuristic is consistent, then it is also admissible (i.e. it never overestimates the true cost to the goal).

Search efficiency

The admissibility guarantees A* finds the optimal path, but it does not suggest the search is efficient (how fast it finds that path). If is very small or zero, A* behaves like UCS. Consider the below scenario:

The heuristic priority of is:

Which is equivalent to UCS that accumulates the cost to current node.

Therefore, we want the heuristic to be as large as possible but still being admissible.

Summary

  • If is admissible, A* is optimal

  • If is consistent, A* is optimally efficient

  • Admissible does not imply consistent

  • Consistent implies admissible

If we can’t design an accurate admissible or consistent heuristic, it may be better to settle for a non-admissible heuristic that works well in practice or for a local search algorithm even though completeness and optimality are no longer guaranteed. Also, although dominant (i.e. good) heuristics are better, they may need a lot of time to compute (time and space complexity are exponential); it may be better to use a simpler heuristic - more nodes will be expanded but overall the search may be faster.

When A-star behaves like BFS?

Breadth-First Search (BFS) searches node level by level and does not use heuristic information. If we set: This can be think of:

Since is the depth of the node, A* expands nodes purely based on their depth and will not go deeper (since they have greater depth value) unless the current depth is fully expanded.


Back to parent page: Problem Solving and Search

AI AI_Algorithm Problem_Solving Informed_Search A-Start_Search