A* algorithm analysis and introduction of sample questions

1. The basic principle of A* algorithm

(A-Star) algorithm is a direct search method for solving the shortest path in a static road network, and it is also a common heuristic algorithm for many other problems. Note-is the most effective direct search algorithm.

The formula is expressed as: f(n)=g(n)+h(n), where
f(n) is the cost estimate from the initial state to the target state through state n, and
g(n) is the cost estimate from the initial state in the state space The actual cost
from state to state n, h(n) is the estimated cost of the best path from state n to the target state.
(For the path search problem, the state is the node in the graph, and the cost is the distance)

Selection of h(n): to
ensure that the shortest path (the optimal solution) condition is found, the key lies in the selection of the evaluation function f(n) (or the selection of h(n)), the closer the distance estimate is to the actual value, the evaluation function is obtained The better .
We use d(n) to express the distance from state n to the target state, so h(n) can be selected roughly in the following three situations:
1. If h(n)< d(n) the actual distance to the target state, this In this case, the number of search points is large, the search range is large, and the efficiency is low. But the optimal solution can be obtained.
2. If h(n)=d(n), that is, the estimated distance h(n) is equal to the shortest distance, then the search will strictly follow the shortest path, and the search efficiency at this time is the highest.
3. If h(n)>d(n), the number of search points is small, the search range is small, and the efficiency is high, but the optimal solution cannot be guaranteed.

2. Introduction to A* Algorithm Examples

As shown in the figure below, the search starting point is A and the target point is the optimal path B. Note that the solid square area in the picture is a wall and cannot be crossed. The movement direction only allows vertical and horizontal movement. The cost of each movement is the same, which can be set to 1.
Insert picture description here
Give the node information in each step of the search process (you can select some intermediate key step results for display) mainly including the starting point (A), the end point (B), the open table (green node), the closed table (red node), The calculated value of each node (f(n)/g(n)/h(n)).

3. Theoretical analysis of examples

The specific implementation of this question can be divided into the following steps: 1. Set the search area; 2. Set the open list and close list; 3. Start the search; 4. Sort the path; 5. According to f(n)=g(n)+h( n) Continue to search; 6. Determine the path according to the search results.

1. Set the search area: set the 9*9 table as the search area, and set the wall in the table as the non-searchable area.

2. Set open list and close list: put A and its surrounding area into the open list, and put the smallest area of ​​f(n) after each search into the close list.

3. Start the search: start the search from the starting point A, and determine the f(n) of the adjacent area according to f(n)=g(n)+h(n).

4. Path sorting: After searching for points in the searchable area adjacent to the point in step 2, sort these areas according to the size of f(n).

5. Continue to search: select the area with the smallest f(n) from the sorted areas, and continue to search for adjacent areas according to f(n)=g(n)+h(n). And update the open list and close list.

6. If the adjacent area is already in the open list (that is, it has been searched), judge from this area whether its area f(n) can be updated and become smaller.

7. Keep repeating the search process until the end point B is also placed in the close list.

Fourth, the program flow chart

Insert picture description here

5. Definition of heuristic function h(n)

static double hManhattanDistance(Point pnt)
{
    
    
return Math.abs(pnt.x - END_PNT.x) + Math.abs(pnt.y - END_PNT.y);
}

In the implementation, h(n) adopts the Manhattan distance, that is, the distance between the area i: (x1, y1) and the area j: (x2, y2) is:

d(i,j)=|X1-X2|+|Y1-Y2|

6. Node information during A* search

The upper right corner is h(n), the upper left corner is g(n), and the lower left corner is f(n)

Seven, the difference between A* algorithm and A algorithm, width first

1. A* algorithm solution: see above.

2. Breadth-first search algorithm solution:

  1. First put the starting point in the sequence table;
  2. Read the first area from the sequence table;
  3. Search the adjacent area of ​​the read area;
  4. Determine whether the adjacent area is the end point, and stop if it is the end point;
  5. If it is not the end point, put these adjacent areas into the sequence table;
  6. Repeat 2-5 until you find the end point.

Guess you like

Origin blog.csdn.net/weixin_43824348/article/details/111030193
Recommended