Introduction to Shortest Path Algorithms

Source: Internet
Author: User
As far as Drew knows, the shortest path algorithm currently has important applications such as computer network routing algorithms, robot road exploration, transportation route navigation, AI, and game design. The D * (D Star) algorithm is used as the core pathfinding algorithm of the US Mars detector.

The shortest path is calculated by static Shortest Path and dynamic shortest path.

The static path shortest path algorithm is used to calculate the shortest path without changing the external environment. It mainly includes Dijkstra algorithm and A * (A Star) algorithm.

The shortest path of a dynamic path is constantly changing in the external environment, that is, the shortest path cannot be calculated and predicted. If enemies or obstacles are constantly moving in the game. Typical algorithms include D.

 

This is the shortest path of the random road network of the 10000 nodes implemented by the Drew program.

 

Example of K paths for real road network computing:From node 5696 to node 3006, the three fastest roads show that the path basically follows the ring line or main road. The black line is the first line, the blue line is the second line, and the red line is the third line. The constraints coefficient is 1.2. Share some road sections. The computing part is completely completed by the program developed by Drew.

 

See K-Path Algorithm testing programs

Dijkstra Algorithm for Shortest Path:

Dijkstra is a typical Shortest Path used to calculate the shortest path from one node to all other nodes. The main feature is to expand horizontally at the center of the starting point until the end point is reached. Dijkstra algorithm can obtain the optimal solution of the shortest path, but it is inefficient because it traverses Many computing nodes.

Dijkstra algorithm is a representative short-circuit algorithm. It has been described in detail in many professional courses, such as data structure, graph theory, and operational research.

Generally, Dijkstra can be expressed in two ways: Permanent and Temporary label, and OPEN or CLOSE table, to make Drew consistent with the expression of the * algorithm and D * algorithm described below, OPEN and CLOSE tables are used here.

General process:
Create two tables, OPEN and CLOSE.
The OPEN Table stores all generated nodes that have not been investigated. The CLOSED table records the accessed nodes.
1. Place the point closest to the start point and not checked in the road network in the OPEN Group for inspection.
2. Locate the point closest to the start point in the OPEN table, locate all the subnodes of the point, and place the point in the CLOSE table.
3. traverse the child nodes of This vertex. Find the distance between these subnode points and the starting point, and place the subnode in the OPEN table.
4. Repeat steps 2 and 3. Until the OPEN table is empty or the target point is found.

This is a demonstration of the shortest path for searching the Dijkstra algorithm on the 4000 nodes in the drew program, the black circle indicates the vertices that have been traversed and computed. From the figure, we can see that the Dijkstra algorithm expands layer by layer from the starting point. After a large number of nodes are computed, the algorithm reaches the target point. Therefore, the speed is slow and the efficiency is low.

There are many ways to increase the search speed of Dijkstra. According to Drew, there are commonly used data structures using the Binary heap method, and the methods to search at the same time from the start point and end point using Dijkstra.

Recommendation webpage:Http://www.cs.ecnu.edu.cn/assist/js04/ZJS045/ZJS04505/zjs045050a.htm

The Dijkstra algorithm is briefly introduced, including graphic display and source code download.

A * (A Star) algorithm: heuristic Algorithm

The A * (A-Star) algorithm is the most effective method for solving the most short circuit in A static network.

Formula: f (n) = g (n) + h (n ),
F (n) is the evaluation function of node n from the initial point to the target point,
G (n) is the actual cost from the initial node to the n node in the state space,
H (n) is the estimated cost of the optimal path from n to the target node.

The key to finding the shortest path (optimal solution) is the selection of the evaluate function h (n:
The estimated value h (n) <= the distance from n to the target node is the actual value. In this case, the number of search points is large, the search range is large, and the efficiency is low. However, the optimal solution can be obtained.
If the estimated value is greater than the actual value, the number of points to be searched is small, the search range is small, and the efficiency is high, but the optimal solution cannot be obtained.
The closer the valuation value is to the actual value, the better the valuation function gets.
For example, for a geometric road network, the Euclidean distance (linear distance) between two nodes can be used as the estimated value, that is, f = g (n) + sqrt (dx-nx) * (dx-nx) + (dy-ny) * (dy-ny); in this way, evaluate the function f with a certain g value, it is more or less restricted by the estimated value h. The node is near the target point, the h value is small, and the F value is relatively small. This ensures that the shortest path is directed to the end point. It is obviously better than Dijstra algorithm to search around without any direction.

Conditions of heuristic
Optimistic (must be less than or equal to the real cost)
As close to the real cost as possible

Main search process:
Create two tables. The OPEN Table stores all the nodes that have been generated but not inspected. The CLOSED table records the accessed nodes.
Traverse each node of the current node, Put n nodes into CLOSE, and take the sub-node X of n nodes,-> calculate the estimated value of X->
While (OPEN! = NULL)
{
Obtain the node n with the smallest value of f from the OPEN table;
If (n node = target node) break;
Else
{
If (X in OPEN) compares the estimated values of two X. f // note that the estimated values of two different paths of the same node
If (the estimated value of X is less than the estimated value of OPEN table)
Update the estimate value in the OPEN table; // obtain the estimate value of the minimum path.

If (X in CLOSE) compares the estimated values of two X. // note that the estimated values of two different paths of the same node
If (the estimated value of X is less than the estimated value of CLOSE table)
Update the estimated value in the CLOSE table; put the X node into OPEN // obtain the estimated value of the minimum path.

If (X not in both)
Evaluate the value of X;
Insert X into the OPEN table. // No sorting has been performed.
}

Insert n nodes into the CLOSE table;
Sort the nodes in the OPEN table according to the estimated value. // It is actually the size of node f in the OPEN table, which is performed downward from the node in the smallest path.
}

Is the same as the previous Dijkstra algorithm using the same road network, the same starting point and ending point, using the * algorithm, the calculated points gradually expand from the starting point to the target point direction, the number of computing nodes is much smaller than that of Dijkstra, which is efficient and can obtain the optimal solution.

The difference between the * algorithm and the Dijistra algorithm is that there is no estimated value. The Dijistra algorithm is equivalent to the case where the estimated value in the * algorithm is 0.

 

Recommended article link:

AmitA PhD game site at Stanford University with an introduction to A * algorithm and many valuable links to http://theory.stanford.edu /~ Amui/GameProgramming/

SunwayI wrote two Chinese articles about heuristic and A * algorithm, and downloaded A * Source Code:

First knowledge of A * algorithm http://creativesoft.home.shangdu.net/AStart1.htm

Go deep into A * algorithm http://creativesoft.home.shangdu.net/AStart2.htm

Note thatSunwayIn the above article, "go deep into the * algorithm" and refer to A * game program for explanation and download the source code. However, it has A major Bug, the new subnode is placed in the OPEN table for sorting. When the subnode is in the Open table and Closed table, after re-calculating the estimated value, the nodes in the Open table are not re-ordered, this problem may cause the calculation to fail to obtain the optimal solution. In addition, when the weights of the road network vary greatly, the search range not only exceeds Dijkstra, but even searches for all road networks, greatly reducing the efficiency.

Drew corrected this problem as follows. When the subnode is in the Open table and Closed table, it recalculates the estimated value and deletes the old nodes in the OPEN table, insert the nodes with new values into the OPEN table and re-sort them. The test results are good. The modified code is as follows, and the red part is the code added by Drew. add it to the corresponding part of the program.

In the GenerateSucc () function
...................................
G = BestNode-> g + 1;/* g (Successor) = g (BestNode) + cost of getting from BestNode to Successor */
TileNumS = TileNum (int) x, (int) y);/* identification purposes */
If (Old = CheckOPEN (TileNumS ))! = NULL)
{
For (c = 0; c <8; c ++)
If (BestNode-> Child [c] = NULL)/* Add Old to the list of BestNode's Children (or Successors ).*/
Break;
BestNode-> Child [c] = Old;

If (g <Old-> g)
{
Old-> Parent = BestNode;
Old-> g = g;
Old-> f = g + Old-> h;

 
// Add the following red code to Drew
// Implement by Drew
NODE * q, * p = OPEN-> NextNode, * temp = OPEN-> NextNode;
While (p! = NULL & p-> NodeNum! = Old-> NodeNum)
{
Q = p;
P = p-> NextNode;
}
If (p-> NodeNum = Old-> NodeNum)
{
If (p = OPEN-> NextNode)
{
Temp = temp-> NextNode;
OPEN-> NextNode = temp;
}
Else
Q-> NextNode = p-> NextNode;
}
Insert (Old); // Insert Successor on OPEN list wrt f
}
........................................ ..............

 

Another A * (A Star) algorithm:

This algorithm can directly implement the * algorithm using the Dijkstra algorithm program without directly using the estimated value. Drew tests it to achieve the same computing effect as A * and is very simple.

Taking the adjacent matrix as an example, the Dij element in the j column of the original adjacent matrix I is changed to Dij + Djq-Diq; the direction from the start point to the target point is I-> j, and the end point is q. dij is (the weight or distance from I to j)

Where: Djq and Diq are equivalent to the estimated value Djq = (the straight line distance from j to q) and Diq = (the straight line distance from I to q)

Principle: The direction from I to q is Dij + Djq> Diq, and the value of Dij + Djq-Diq is small. If it is in the opposite direction, Dij + Djq-Diq will be very large. Therefore, the goal is to find the path.

 

Dynamic Road Network, shortest path algorithm D *

A * is very effective in A static road network (very efficient for static worlds), but not suitable for dynamic road networks, environments such as weights and other changing dynamic environments.

D * is the Stentz of the Dynamic A * (D-Star, Dynamic A Star) Kane and Mellon robot center in 1994 and 1995. It is mainly used for Robot exploration. It is the pathfinding algorithm used by the Mars detector.

Optimal and Efficient Path Planning for Partially-Known Environments

The Focussed D * Algorithm for Real-Time Replanning

Main methods (these are the personal understandings of Drew who have read the above materials and the preparation procedures. They cannot be completely correct and are for reference only ):

1. First Use the Dijstra algorithm to search from the target node G to the Start Node. The shortest path of the target node in the storage network and the actual value h from the location to the target node, k (k is the minimum value of all change h, and k is h currently. Each node contains the shortest information from the previous node to the target node, such as 1 (2), 2 (5), 5 (4), and 4 (7 ). The shortest circuit from 1 to 4 is 1-2-5-4.
Save the node information in the original OPEN and CLOSE operations.

2. the robot starts to move along the shortest path. When the next node to be moved does not change, no computation is required. Use the shortest path calculated by Dijstra in the previous step to trace it back from the starting point, when the status of the next node X is detected at point Y, such as congestion. The robot first adjusts the new weights c (X, Y) for the actual values h (Y) and h (Y) = X to Y from the current position to the target point G) + The original actual value of X is h (X ). X is the next node (to the target point direction Y-> X-> G), and Y is the current point. The K value is the minimum value before and after the h value change.

3. use A * or other algorithms for calculation. Assume that the * algorithm is used to traverse the child node of Y and put the point into CLOSE. Adjust the h value of child node a of Y, h () the weight C (Y, a) from h (Y) + Y to subnode a is used to compare whether Vertex a exists in OPEN and CLOSE. The method is as follows:

While ()
{
Take node Y with the smallest k value from the OPEN table;
Traverse subnode a of Y and calculate the weight C (Y, a) of h (a) = h (Y) + Y to subnode a of)
{
If (a in OPEN) compares the h values of two
If (the h value of a is smaller than the h value of OPEN Table)
{
Update the h value of a in the OPEN table; obtain the minimum h value of the K value.
An unaffected shortest path exists.
Break;
}
If (a in CLOSE) compares the h values of two a's. // note that it is the estimated values of two different paths of the same node.
If (the h value of a is smaller than the h value of CLOSE table)
{
Update the h value of a in the CLOSE table; obtain the minimum h value of K value; put node a in the OPEN Table
An unaffected shortest path exists.
Break;
}
If (a not in both)
Insert a into the OPEN table; // No sorting has been performed.
}
Place Y to CLOSE the table;
OPEN tables are sorted by comparing K values;
}
The robot uses the shortest path from point A to the target point calculated by step Dijstra.

D * the algorithm is very effective in the dynamic environment. when moving to the target point, it only checks the changes of the last or last node in the shortest path, such as robot pathfinding. It is not suitable for changes in the distance shortest path.

It is an analysis demonstration conducted by Drew on a random road network with 4000 nodes. The black line is the shortest path calculated for the first time, and the red point is the blockage point with the change in the path, when the robot is located at 982 points, it detects that a road section is blocked. At this point, the path is calculated based on the new information. You can see that the circle point is the point that has been re-computed and traversed, the shortest path is found when only a few points are calculated, which indicates that the calculation is very effective and fast. The Green Line is the new Shortest Path to bypass the blocked part.

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.