The idea of the algorithm A* or A star, is to avoid developing paths estimated too expensive compared to those known. For that the algorithm will refer to a function of evaluation of its total cost. The principle is similar to branch & bound or branch & price.
Let s be the initial state, T the set of terminal states, k(x, y) the cost from x to y, F the set of developed states – whose successors have been generated (set of closed) – and O all the states generated but not developed. The algorithm of A* is as follows:
We will take as an example the problem of the 8-puzzle. This problem is a 3*3 matrix containing 8 boxes filled with numbers from 1 to 8 and an empty box. Each box can move on an adjacent box in the matrix, and only on an empty box. In this case, we allow the value of the box with the empty box. The objective of the 8-puzzle is to sort the boxes in ascending order from left to right and from top to bottom of the matrix.
The heuristic used for this example is the Manhattan Distance. This distance is the sum of the movements « as the crow flies » to make each box is in the right place. It is indeed an admissible and consistent heuristic because we consider that all the movements of the boxes are legal. The function g(n) will have for value n the number of displacements carried out to reach the configuration n.
Let’s take a random configuration of departure, we have for first iteration the following tree:
The algorithm has in the set F the root, and in the set O the 4 generated configurations. The configuration on the right has the smallest estimate of the total cost, so it is removed from O to be explored and finally placed in F. We get the following tree:
We have found a final solution such that its cost is minimal, the algorithm stops.