Classic Algorithm Research Series: 1. A * Search Algorithm

Source: Internet
Author: User

Classic Algorithm Research Series: 1. A * Search Algorithm


Author: July, January 2011
----------------------------------
Bloggers:
1. In this series of classical algorithm research, I hope this series of articles is not good enough.
2. This classic algorithm research series is my reference. It is an original article, which must indicate the author's July and source.
3. This series of classical algorithms is continuously optimized, updated, and inspected.

Welcome to join me in studying, discussing, and exchanging research.
If any error occurs, correct it.
-------------------------------------------

Introduction
A paper published in 1968, "P. e. hart, N. j. nilsson, and B. raphael. A formal basis for the heuristic determination of minimum cost paths in graphs. IEEE Trans. syst. sci. and Cybernetics, SSC-4 (2): 100-107,196 8 ". Since then, A sophisticated and efficient algorithm-A * algorithm has been widely used in related fields.


Heuristic Search Algorithm
To understand the * search algorithm, you must start with the heuristic search algorithm.
The so-called heuristic search means that when the current search node selects the next node, you can use an heuristic function to select the node, select the node with the least cost as the node for the next search and jump to it (when there is a node with the least cost, you may wish to select the one that is last expanded from the current search point for the next search ).

Both DFS and BFS are blind searches when the child node is expanded. That is to say, it does not select a node that is better in the next search and jumps to the node for the next search. When you are not lucky, you need to test the complete solution set space. Obviously, it can only be applied to search problems with a small scale.

What is different from DFS and BFS is that a carefully designed heuristic function can quickly obtain the optimal solution of a search problem. For NP problems, A better solution can also be obtained in polynomial time. Yes, the key is how to design this function.


A * Search Algorithm
A * search algorithm, also known as the astar algorithm, is one of the heuristic search algorithms. It is A path with multiple nodes on the graphic plane to find the lowest cost. It is often used in the mobile computing of the NPC in the game or the mobile computing of the BOT in the online game. Like Dijkstra, this algorithm can find a shortest path and perform heuristic search like BFS.

A * the most important part of an algorithm lies in the design of A valuation function:
F (n) = g (n) + h (n)

F (n) is the valuation of every possible test point. It consists of two parts:
In part, g (n) indicates the cost from the start point to the current point (usually expressed by the depth of a node in the Search Tree ).
Another part is h (n), which indicates the most important part in Heuristic Search, that is, the valuation from the current node to the target node,
The Design of h (n) directly affects whether A * algorithm is A heuristic algorithm with such heuristic functions.

A Heuristic Algorithm with A f (n) = g (n) + h (n) strategy can be A sufficient condition for A * algorithm:
1. There is an optimal path from the start point to the end point on the search tree.
2. The problem domain is limited.
3. The search value for all nodes is greater than 0.
4. h (n) =

When all four conditions are met, A heuristic algorithm with f (n) = g (n) + h (n) strategy can become A * algorithm, and the optimal solution can be found.

For a search question, it is clear that conditions 1, 2, and 3 are easily met, while conditions 4: h (n) <= h * (n) need to be carefully designed, h * (n) is obviously unknown, so h (n), an inspiration strategy that meets Condition 4, is very valuable.

However, for the Optimal Path search and Digital 8 problems of graphs, some related strategies h (n) are not only well understood, but also theoretically proved to meet Condition 4, this plays a decisive role in promoting this algorithm.

H (n) Distance from h * (n) cannot be too large. Otherwise, h (n) will not have a strong differentiation capability, and the algorithm efficiency will not be very high. A good evaluation of h (n) is that h (n) is under the lower bound of h * (n) and is as close as possible to h * (n ).

 

A * Search Algorithm Implementation
Let's take a small example: Find the V0-> V5 path. During the V0-> V5 process, you can use V1, V2, V3, and V4 to reach the destination V5. The following problem is to find the shortest path of the starting vertex V0-> any vertex V1, V2, V3, V4-> Target vertex v5.

// Yes. The image references rickone.
We can see that the most core process of the * algorithm is to select the next search point each time, select the node with the minimum F value from all the points that have been found but not searched (may be different layers or not on the same branch.
All vertices that have been acquired but have not been searched can be sorted by a queue in ascending order of the F value (that is, a priority queue.
In this way, in the overall search process, as long as the first element (F value) is displayed from the priority queue according to the algorithm framework similar to the breadth-first ), calculate the values of g, h, and F for its subnodes until the priority queue is empty (unsolvable) or the end point is found.

A * The Relationship Between the algorithm and the breadth, depth priority, and Dijkstra algorithm is: When g (n) = 0, the algorithm is similar to DFS. When h (n) = 0, this algorithm is similar to BFS. At the same time, if h (n) is 0, you only need to find g (n), that is, find the shortest path from the starting point to any vertex n, then it is converted to a single-source shortest path problem, dijkstra algorithm. You can set h (n) to 0 or g (n) to 0 in the specific process of the preceding A * search tree.

A * algorithm flow:
First, add the starting node S to the OPEN table, and leave the CLOSE table empty. When the algorithm starts:
1. If the OPEN table is not empty, fetch a node n from the header. If the table is empty, the algorithm fails.
2. Is n a target solution? Yes. Find a solution (continue searching or terminate the algorithm ).
3. Expand all the successor nodes of n, that is, the nodes (subnodes) that can be directly associated from n. If they are not in the CLOSE table, they are placed in the OPEN table, put S into the CLOSE table, calculate the estimated value of f (n) for each subsequent node, sort the OPEN table by f (x), and put the smallest value on the header, repeat the algorithm, and return to 1.

// OPEN --> CLOSE, start --> any vertex g (n) --> Target vertex h (n)
Closedset: = the empty set // set of estimated nodes
Openset: = set containing the initial node // set of nodes to be estimated
G_score [start]: = 0 // g (n)
H_score [start]: = heuristic_estimate_of_distance (start, goal) // h (n)
F_score [start]: = h_score [start]

While openset is not empty // if the OPEN table is not empty
X: = the node in openset having the lowest f_score [] value // x is the smallest value in the OPEN table.
If x = goal // if x is a solution
Return reconstruct_path (came_from, goal )//
Remove x from openset
Add x to closedset // add x

CLSOE table
For each y in neighbor_nodes (x)
If y in closedset
Continue
Tentative_g_score: = g_score [x] + dist_between (x, y)

If y not in openset
Add y to openset
Tentative_is_ B

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.