A * algorithm detailed introductory tutorial

Source: Internet
Author: User

A * algorithm detailed introductory tutorial 
One: Why we need a * algorithm

There are many algorithms for finding the shortest path or minimum cost. Its essence is the search strategy of the graph. There are many kinds of direct search methods for graphs, which are more typical of breadth-first search and depth-first search. The so-called breadth-first search is all the neighboring nodes that take precedence over the node every time they reach a node. The corresponding depth-first search refers to a node that has never been reached before. The shortest path algorithm based on the above two basic ideas has Dijkstra algorithm and Floyd algorithm. When the search is complete, the entire graph is traversed, the time overhead is very large, especially when the graph is very large, this time complexity is unacceptable. The above algorithm is expensive because the process of searching does not evaluate the current state and is therefore based on poor search. Another way of thinking is to use some appropriate evaluation functions in the search process to prune, first of all to remove those who are not likely to produce the optimal solution of the branch. This is called heuristic search. and A * algorithm is based on this idea of the search algorithm.


Two: What is a * algorithm

A * algorithm is a best-in-one algorithm (BFS) that can be adopted.

The estimate function of a * algorithm can be expressed as:

F ' (n) = G ' (n) + h ' (n)

Here, F ' (n) is the valuation function, and G ' (n) is the shortest path value from the starting point to the node N, and H ' (n) is the heuristic value of the shortest pathway from N to the target. Since this f ' (n) is actually not known in advance, we use the previous estimate function f (n) to approximate it. g (n) instead of G ' (n), but G (n) >=g ' (n) can (in most cases, be satisfied, without consideration), h (n) instead of H ' (n), but H (n) <=h ' (n) (this is particularly important). It can be proved that the application of such a valuation function can find the shortest path, that is, can be adopted. We say that the best first algorithm to use this valuation function is a * algorithm.

For example, the breadth-first algorithm is a special case of a * algorithm. where g (n) is the number of layers where the node is located, h (n) = 0, this h (n) is definitely less than H ' (n), so the above-mentioned known breadth-first algorithm is a possible adoption. Actually, too. Of course it is a worst-case * algorithm.


Three: The process of a * algorithm

The basic principle of a * algorithm is briefly introduced in the previous section. So how to use a * algorithm for a specific problem? The following is a * algorithm execution process.


1. Initialization

Put the start node in the Open list (the F and G values of the start node are treated as 0);


2. Repeat the following procedure
① : Finds the node with the minimum F value in the Open list and takes the found node as the current node ; ② : Remove the current node from the Open list and add it to the closed list ; ③ : perform the following steps in turn for each node adjacent to the current node :

1. If the adjacent node is not accessible or the adjacent node is already in the closed list, then what operation does not execute, continue to verify the next node;

2. If the adjacent node is not in the open list, add the node to the open list and set the parent node of the neighboring node as the current node, while saving the G and F values of the adjacent node;

3. If the adjacent node is in an open list, determine if the G value of the adjacent node reached through the current node is less than the original saved G value, or less, the parent node of the neighbor node is set to the current node and the G and F values of the adjacent node are reset.

④ Loop End condition :

When the endpoint is added to the open list as the node to be tested, it indicates that the path is found and the loop should be terminated;

Or when the open list is empty, indicating that no new nodes can be added, but no endpoint node in the tested node means that the path cannot be found, and the loop ends;

3. Print the path

Traversing the parent node from the endpoint node and saving the entire traversed node coordinates, the resulting node is the last obtained path;


Four: Pseudo-code for A * algorithm

while (Open!=null) {The value of the N;IF (n node = = target node) break;for (each child of the current node N) {calculates the value of x) {from the OPEN table, the least-valued f (N) node, and if (Xinopen) if ( The value of x is less than the value of the Open table) {The Father whose n is set to X; Update the value in the Open table;//Take the value of the minimum path}if (xinclose) continue;if (xnotinboth) {set N to X's father; and insert X into the Open table;//There is no sort}}//endfor the N node into the close table, sorting the nodes in the open table according to the estimate value, or actually comparing the size of the node F in the Open table, from the node with the smallest path down. }//endwhile (Open!=null)



V: A small example of a * algorithm

In order to get a better understanding of the A * algorithm, we give a simple idea of the shortest distance between two points to understand the implementation process of a * algorithm.


The problem is, for example, to find the shortest path from s->t. A * algorithm executes as follows:

STEP1:

STEP2:

STEP3:

STEP4:


STEP5:

STEP6:


Because T is the target node, we get the solution:


V4 -T ,V1 , S


Six: Solve eight digital problems with a * algorithm
1. Description of the problem

Eight digital problems are also called nine problems. On the 3x3 chessboard, there are eight pieces, each of which is marked with a number 1 to 8, and the numbers on each piece are different. There is also a space on the board, and a piece adjacent to the space can be moved to a space. The problem to be solved is to give an initial state and a target state, and to find a moving step with the fewest steps of moving pieces from the initial transition to the target State.
A state of the so-called problem is a pendulum on a chess board. When the pawn moves, the state changes. The problem of solving eight digital problems is actually to find out a series of intermediate transition states from the initial state to the target State.
Eight digital problems generally use the search method to solve. Search method has breadth-first search method, Depth-first search method, A * algorithm, etc.

The following picture shows eight digital problems.



Initial state transition state end State


2. A * algorithm solves eight digital problems

The core of solving eight digital problems with a * algorithm is to find the cost function.

F (n) =g (n) + H (n)

F is the actual path (valuation);

G is the number of States that have traversed;

H is the degree of difficulty in reaching the final state by State N (here is a different number than the final state).

Note that the H here is the number of state N and the final state, which is only suitable for eight digital problems, for 15 of digital and more problems are not suitable, need to find a better valuation.

Here, a one-dimensional array represents a state from top to bottom, from left to right in order of arrangement.

With this we can follow the procedure of a * algorithm to deal with eight digital problems.

3. Implementation of the algorithm

Ⅰ Experimental Environment

Hardware environment: PC

Development environment: VisualStudio 2013

Development language: C + +

Ⅱ algorithm Flowchart

Ⅲ Data structure

State node of the Ⅰ State graph:

struct Node

{

int state[9];//The state of the node

int parent;//The parent node of the node in the state diagram

The degree of difficulty of the int h;//node reaching the goal

The actual path of the int g;//node

int f;//The total path of the node

};

Save the current state array:

static int end[9]; Target status

static int start[9]; Initial state

static int invalid[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; Invalid state

Ⅲclose and Open tables

Static Node open[500];

Static Node close[500];

Ⅳ Experimental Results

Test data:

Initial state final State

A total of 4 steps have been run as follows:

The manual algorithm process for testing data is as follows:


You can see that the result is the same as the one we got, stating that this program is no problem.

Ⅴ Program Source Code http://download.csdn.net/detail/simon_world/8303877


A * algorithm detailed introductory tutorial

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.