Classic Algorithm Research Series: 1. A * Search Algorithm

Source: Internet
Author: User

classical algorithms Research Series: 1. A * search algorithm

 

July November January 2011

====================

Bloggers:
1. This classic Algorithm Research Series, this seriesArticleI hope you can forgive me for not writing well 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 classic Algorithm Research Series,Keep improving, continuous optimization,Update permanently and perform surveys permanently.

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

-------------------------------------------

 

A * Search Algorithm

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 ------ * Algorithms were born and widely used in related fields.

DFSAndBFSWhen a child node is expanded, it is a blind search. 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. In bad luck, we need to test a complete solution set space.,Obviously, it can only be applied to search problems of small scale.

 

So where is the efficiency of the * Algorithm in a heuristic algorithm more efficient than they are?

First, let's talk about heuristic algorithms.

The so-called heuristic search, which is the biggest difference from the blind search such as DFS and BFS, is that when the current search node selects the next node, it can be selected through an heuristic function, 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 ).

A well-designed heuristic function can quickly obtain the optimal solution for a search problem,
For NP problems, a better solution can be obtained in polynomial time.

Yes, the key is how to design this function.

A * AlgorithmAs an important heuristic algorithm, it is widely used in Optimal Path solving and some policy design problems.
The most important part of a * 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) = <H * (N) (H * (N) is the substitute value of the actual problem ).

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:H (n) is under the lower bound of H * (N) and is close to H * (N) as much as possible ).

 

Wikipedia's explanation of this a * search algorithm:

A * search algorithm, commonly known as the astar algorithm. This is an algorithm with multiple node paths 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.

In this algorithm,
G (n) indicates the actual distance from the starting point to any vertex N,
H (n) indicates the estimated distance from any vertex N to the target vertex.
// That is, starting point --> any vertex g (n) --> Target vertex H (N)

Therefore, the formula of the * algorithm is as follows:
F (n) = g (n) + H (n ).

This formula follows the following features:
I. 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 the single-source shortest path problem, that is, the Dijkstra algorithm.
II. If H (n) <= n is the actual distance from the target, the optimal solution can be obtained. In addition, the smaller the H (n), the more nodes to be calculated, and the lower the algorithm efficiency.

 

A * Algorithm Implementation of search algorithms: 

A * is to solve such a problem related to the shortest path. Of course, you can use a * to calculate the simple shortest path problem. For G (N), it is [S, N]. computing in the search process, and H (n) I don't think of a good way, for an abstract Graph Search, it is difficult to find a good H (N), because h (N) related to specific problems. The result is H (n) = 0, which is returned to the ordered search. Here is a small example:

The sum of V0-> V1, V2, V3, V4, and V5 paths. It is like a person starting from V0 to reach V1. V5 and five. The vertices that have been reached in the figure can be used as a bridge to connect to other vertices.

The following question is:Starting vertex V0-> path any vertex V1, V2, V3, V4-> Target vertex V5.

// Yes. The image references rickone.

We can see that:
A * the most core process of an algorithm is to select the next current search point from all known but not searched points (possibly different layers, or not on the same branch), select the node with the smallest F value to expand.
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-first and depth-first is that when G (n) = 0, the algorithm is similar to DFS. When H (n) = 0, this algorithm is similar to BFs, which can be obtained by setting 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 // X to the 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_better: = true
Else if tentative_g_score <g_score [y]
Tentative_is_better: = true
Else
Tentative_is_better: = false
If tentative_is_better = true
Came_from [y]: = x
G_score [y]: = tentative_g_score
H_score [y]: = heuristic_estimate_of_distance (Y, goal )// X --> Y --> goal
F_score [y]: = g_score [y] + h_score [y]
Return failure

Function reconstruct_path (came_from, current_node)
If came_from [current_node] is set
P = reconstruct_path (came_from, came_from [current_node])
Return (p + current_node)
Else
Return the empty path

 

The value written with the node indicates the value of the node F (N). When the Open table is empty, the shortest path from V0 to all other nodes is obtained in the close table.

Considering the algorithm performance, each time an element is retrieved from the Open table in an External Loop, n times are taken (N nodes in total). O (n) is required for each subsequent knots that expand a node) and then sort the Open table again. The size of the Open table is O (n) Magnitude. If you use the fast sorting, It is O (nlogn ), the total complexity of a non-multiplication loop is O (n ^ 2 * logn ),

If you do not sort open tables every time, because new nodes are always added, you do not need to sort them. Instead, you need to find the smallest node from the Open Table each time, it only requires O (n) Complexity, so the total complexity is O (n * n), which is equivalent to Dijkstra algorithm.

A slight improvement on this algorithm is the Dijkstra algorithm. Open tables often contain such table items: (VK, fk1) (VK, fk2) (VK, fk3). In terms of algorithms, only the minimum FK is useful, then they can be merged. The entire Open table indicates the shortest path from V0 to other points, with a fixed length of N, in the initial phase, the weight that can be directly reached by V0 (the value cannot be infinity), so it becomes the Dijkstra algorithm.

This article is complete.
July, updated on April 9, February 10, 2011.

------------------------------------------------
Follow-up: July, updated on March 13, March 1, 2011.
Briefly describe the method of a * shortest path algorithm:
Objective: Find the shortest path from the current location a to the target location B.

Method: from point A, traverse all accessible paths and record them to a structure. The record content is (Location Point, minimum number of steps)
When any second time arrives at a point, judge whether the minimum step is smaller than the recorded content. If yes, update the original minimum step until all the path points cannot continue, in the end, the minimum number of steps marked at the vertex is the shortest path,
In the reverse direction, the number of steps that are connected to it is connected by less than one point, which forms the shortest path. When multiple points are the same, you can take any one.

Summary:
A * the algorithm is actually a poor algorithm, which is similar to the shortest path algorithm taught in the textbook. In the textbook, the two sides are taken in the middle, and all paths are taken once. The shortest value is indicated for each vertex.

 

author's statement:

my July is copyright to all content and materials in this blog, for more information, see the author's July and source.

always, pay tribute to you. Thank you. July and July December 23, 2010.

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.