A * algorithm in depth

Source: Internet
Author: User

A * algorithm is easy to understand, but if you want to design a good * algorithm, you will need to understand it thoroughly. This topic will be thoroughly explored in the next section of this article. If you do not understand the algorithm, please review the previous article, "A * Algorithm primer", and then read the article.

A: Theoretical chapter

discussion: Valuation function

A * algorithm is highly efficient because it is a heuristic search algorithm. It is based on the Dijkstra algorithm, which increases the evaluation of the information of the book Network, that is, to increase the constraint conditions, so as to change the direction of the algorithm--------that is, with a purposeful approach to the target node, rather than your Dijkstra algorithm as blind search. Therefore, the execution efficiency of a * algorithm is largely dependent on the valuation function, and the more accurate the valuation function constructs, the less time a * search will be.

The main task of the valuation function is to estimate how important the node is to be searched. It is defined as the cost estimate of the minimum cost path from the initial node through the current node to the target node. The formula: F (N) = g (n) + h (n), f (n) is the valuation function, g (n) is the known cost for the initial node to the current node, and h (N) is the predictive function of the current node to the target node, which is actually the so-called heuristic function. In fact, if the network point is determined, the value of H (n) is also deterministic, and the key is to see what kind of constructor H (N) chooses. Different h (n) values must be different, thus affecting the value of f (n).

The heuristic function h (n) is constructed so that it cannot be too far from the actual shortest path. The farther away, the search for A * algorithm is closer to the BFS, specifically, when h (n) = 0 o'clock, it is completely degraded to BFS. So the search time is getting higher. Therefore, if the heuristic function is exactly the same as the actual shortest path, then the search speed is the fastest in theory. But in practice, this is not possible, because it is just an estimate function.

In practice, if you want to be as fast as possible, you should try to construct the function closer to the actual shortest path, and to do this, you will need to refer to more heuristic information. Such as: The current node and the target node of the "relationship" or some other information in the system, such as: through the current to investigate the node to the target node weight and so on. However, the calculation of H (N) will also increase, so this is a tradeoff. Here are some of the heuristic functions of sleep.

discussion: Common heuristic functions

The following assumes that the currently pending node is N1 (x1, y1) and the target node is N2 (x2, y2).

A: Manhattan Distance

Manhattan distance is actually a city block distance, that is: similar to the city block, the calculation of the distance between two intersections is calculated according to the sum of the horizontal difference and the vertical difference of the two intersection coordinates. Therefore: H (n1) = ABS (X2-X1) + ABS (Y2-Y1). The Manhattan distance calculation formula has been tested to be more suitable for block type maps.

B: Diagonal Distance

If the map is allowed to move diagonally, then the Manhattan distance needs to be considered in 8 directions. Therefore, to simplify the calculation, you can simply use 4 directions instead. The distance from the diagonal distance is only the information that is larger than the gap in Manhattan. Therefore: H (n1) = MAX (ABS (X2-X1), ABS (Y2-Y1)).

C: Euclidean distance

If the map is allowed to move in any direction, you might consider using a straight line distance between two points. Therefore: H (n1) = sqrt (ABS (X2-X1) ^2 + ABS (Y2-Y1) ^2).

When the heuristic function is actually constructed, it is possible to consider the orientation information of the node to be examined. That is, consider whether the node to be examined (that is, the current node) is aligned with the starting node toward the target node. The more stable the direction, the more attention should be paid to the node. Therefore, it may be possible to set a coefficient for the heuristic function. The more consistent the orientation of the nodes, the lower the coefficients, and the higher the coefficients, the less the orientation. The more the heuristic information of this heuristic function is referenced.

Second: The realization of the article

Discussion: Open table, closed table

The different constructs of the heuristic function affect the path selection trend of the algorithm as a whole, thus affecting the overall performance of the algorithm. Once the heuristic function is clear, the overall direction of the algorithm is determined, and then the efficiency problem of the algorithm falls on the algorithm implementation level. The design of the open table and the closed table is particularly important, why? (For those who do not understand why, please refer again to the previous "A * Algorithm Primer" article until you understand).

The function of open table in algorithm is not only to store the nodes to be inspected, but also to take out the least of the F (N) value in the table frequently during the whole algorithm action, and to confirm whether the adjacency node of the current node has fallen in the open table. And close is also required to be frequently consulted whether the current node's adjacency node is already in the closed table. Therefore, the performance of open-table and closed-table is very important. Only open the table here, if the table design is good, nature to understand if the design of the closed table.

Scenario One: Linked list ( not available )

The use of linked lists can be facilitated on storage, while locating the minimum node of the f (N) value is less efficient and therefore absolutely non-fetching.

Scenario Two: Balanced binary search tree ( desirable, comprehensive performance is good, but not the best )

If you apply this scenario, the rb-tree is typically given priority. This structure is a kind of data structure which is proved by many experiments, which has high performance in inserting, locating and deleting, and it is designed and implemented in many languages.

Scenario Three: (Kenggen) heap ( desirable, good performance, but only for locating or extracting the minimum node of f (n) value )

In terms of locating or extracting the minimum node of the f (N) value, the heap scheme will definitely be higher than the binary search tree, but it can only traverse the table data when it locates the adjacency node of the current node, and this performance is much worse than scenario two.

In practice, individuals are more recommended for heap scenarios. The previously designed C + + version of A * algorithm is a heap solution. On the X 88 no mask map, the seek path between any two points is no more than 4ms. And the implementation version is applicable to any type of map. (In fact, the version still has the space to optimize, because in the valuation function, I did not do any optimization.) )

In fact, a * algorithm in the storage cost, but also need to pay attention to, different designers, design options, efficiency is certainly not the same, not discussed here. Ok, so far today, interested students, welcome to discuss together.

A * algorithm in depth

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.