A * algorithm

Source: Internet
Author: User

A * algorithm

Before doing the project, often hear a * algorithm, feel very tall, today idle to Nothing, the algorithm to understand the next, the main reference

Http://zm8.sm-img5.com/?src=http%3A%2F%2Fblog.csdn.net%2Fshanshanpt%2Farticle%2Fdetails%2F8977512&uid= Aff73364c6b5346a25df05847b0ba813&hid=33f0813de0404e86840c9d053a4e27d2&pos=5&cid=9&time= 1439515838327&from=click&restype=1&pagetype=0040000002000408&bu=web&query=a%e7%ae%97%e6%b3 %95%e5%92%8ca%e6%98%9f%e7%ae%97%e6%b3%95&uc_param_str=dnntnwvepffrgibijbprsvpi

and combined with their own ideas, write down this article with the note!

A * data structures used by the algorithm

A * algorithm is very classical only heuristic search algorithm, other search algorithm dfs,bfs and so on.

We want to from the green point to the red point, need heuristic to find a path, in the end how to find it, start no way, only from the start point slowly try! We need to define an open table that contains the points that are currently possible as paths. All of these points have a parent node, which is convenient when we find the last point, we need to backtrack through the parent node and find a bit until the start node! A close list that holds all squares that do not need to be checked again, that is, the points that have been processed.

Like what

In the picture, the dark green squares are the center of your starting square. It is stroked with a light blue color to indicate that it is added to the closed list. All adjacent cells are now in the open list, and they are stroked with a light green color. Each square has a gray pointer that refers to their parent square, which is the starting square.

Next we should choose which point to continue to consider it! I think you should know the so-called heuristic function, based on the possible cost of this path, in F = f = G + H

G = from Start a, along the resulting path, moves to the path of the specified squares on the grid.

H = Estimated movement cost of moving from that square on the grid to end B.

Since the G-value is to determine the path cost based on the route, it is generally the cost of taking advantage of the parent node of the current point to the current point, and at the time of calculation H, this part because the route to the end is uncertain, we just follow a uniform standard to describe the relative distance of the target g+, it is just a guess, This is often referred to as heuristic. This standard can be European-style distance or "Manhattan" method (current point, to the target point, horizontal and vertical lattice number added), A * algorithm used in the data structure is basically introduced.

A * the idea of arithmetic

When at some point we can go (stored in the Open table), each point in this set is assumed to be on the final route, and if we know what the cost of this route is (measured by the F-value), then we can select the minimum f value directly in this set (with heap sorting Open Table node, easy to find the minimum f value ) as our next step to go to the node (note that because this node may not be on the final route, so the close list is required to store the points already traversed, while the open table to eliminate the point), This is done until the end of the open table or in the Close list appears.

Here are the specific procedures:

For the starting point and the G-value of the surrounding point, H-value and F-value, you can see clearly! (the upper-left corner is F, the lower-left corner is G, the lower-right corner is h) here the cost of moving one step is 10, diagonal 14

Then calculate:

Then from the Open table, we use the heap sort to process the nodes in this table, you can quickly select an F value of the smallest, and then consider its neighbors, recycle processing! Until the end joins close, seek the end! or open empty, did not find the path!

As you can see, the minimum f is the point at the right of the starting point, and the following box indicates the ~

Then consider the neighbors:

There are several possibilities for its neighbors at this time !

1: The neighbor is already in the close table, so there is no need to consider ~

2: Neighbors are obstacles, no need to consider E

3: The neighbor is not in the open table, then joins the neighbor to open and assigns the parent node of the next neighbor to the current node

4: Neighbor in open, then you need to see this neighbor point of the G value and the current point (G value + The current point to the neighbor Point distance (10 or 14)), if the point is closer (that is, the value of the G is small), then the parent node of this point is replaced by the current point!

Then we follow the above thinking, know the end to join the close table! (The ultimate illustration is as follows)

Finally we can get the path: (as we've said before, it's easy to get the path from the parent node backwards)

Said so much, also do not know to say the line is not good, still need to summarize!

Algorithm description

1: Add the starting point to the open table

2: Loop until open is empty or the end is added to the close table

Otherwise: Find the node with the lowest F-value in the Open table (using heap sorting to get the small point), delete this point from open, add close!

(When the smallest point has been removed, you need to sort the open table from the new one, and the first point is the minimum F point!) )

To process 8 neighbors:

If: 1: The neighbor is already in the close table, then no need to consider ~

2: Neighbors are obstacles, no need to consider E

3: The neighbor is not in the open table, then joins the neighbor to open and assigns the parent node of the next neighbor to the current node

4: Neighbor in open, then you need to look at the G value of this neighbor point with the current point (G value + distance from the current point to the neighbor point

From (10 or 14)), if the point is closer (that is, the G value is small), then the parent node of this point is replaced with the current point! (Note: For the same point, under the G value, then f must be small!) Because H is the same! )

Note: When the parent node changes, the smallest point in the OPEN table may change, then it needs to be sorted again to get the most

a little bit!

3: End, according to the conditions of the exit cycle can know whether or not to find the path!

A * algorithm

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.