Deep Analytic Shortest Path algorithm

Source: Internet
Author: User
Tags data structures
Reprinted from: http://blog.csdn.net/fengchaokobe/article/details/7478774
The problem of the first section and the solution method
The so-called shortest path problem can be said to be described in two different situations.
Description One: In graph theory, refers to finding the shortest distance between two nodes in a graph. Figure below

Description Two: In real life, refers to finding the closest distance from one place to another. Figure below

The essence of the two cases is the same, that is, the shortest path to one point to another. Well, the problem has been raised, how to solve it. The method of solving this problem is still more, but because of the different problem condition of each path algorithm, we can choose different path algorithm according to different situation.
This paper introduces three shortest path algorithms, namely: Dykstra algorithm (Dijkstra algorithm), Freud algorithm (Floyd algorithm) and a * search algorithm.

Section II Dykstra algorithm (Dijkstra algorithm)
This algorithm solves the problem of the shortest path of a single source point to another vertex in a graph.

The implementation process of the Dykstra algorithm is as follows:
The first step: using the weighted matrix Weiarcs to represent the weighted graph, if the figure of two vertices VI and VJ is connected, then use WEIARCS[I][J] to represent the two vertices formed by the weight of the edge; if VI and VJ are not connected, that is, <vi,vj> this edge does not exist, Then set weiarcs[i][j] to ∞.
Step Two: Set S to the set of the end point of the shortest path originating from a vertex V, and the initial state of S is empty, and the originating vertex is placed in the S collection when initialized. Then the initial value of the shortest path length from V to the remaining vertex VI in the figure is d[i].
The third step: Select a vertex VJ, so that VJ is currently the end of a shortest path from vertex v. This makes S = S∪{VJ}.
Fourth step: Modify the shortest path length of any vertex VK from V to set V-s (V is a collection of graph vertices). If D[J]+WEIARCS[J][K] < d[k], then d[k] = D[j] + weiarcs[j][k].
Fifth step: Repeat the third and fourth steps of N-1 times, thus the shortest path from V to the remaining vertices in the graph can be obtained.

Well, that's the way it's done. But the light has a literal description, to be more straightforward to express the process, I think the use of image representation is a good choice. As shown in the figure below



From the calculation process table, we know the shortest path of V0 to the remaining points, as shown below

The code for the Dykstra algorithm described in the above procedure is as follows:
int ShortPath (mgraph g,int v0,pathmatrix &p,shortpathtable &d) {//Dykstra algorithm is used to find the shortest path to the v0 vertex to the remaining vertex v in Figure G P[v] and the weighted length D  
    [v].  
    If P[V][W] is true, then W is the vertex on the shortest path currently obtained from V0 to V.  
      
    FINAL[V] is true when and only if v∈s, that is, the shortest path from V0 to V has been obtained.  
        for (v = 0;v < g.vexmun;v++) {final[v] = FALSE;  
        D[V] = G.weiarcs[v0][v];    for (w = 0;w < g.vexnum;w++) p[v][w] = FALSE;  
            Set empty path if (D[v] < INFINITY) {p[v][v0] = TRUE;  
        P[V][V] = TRUE; }} D[v0] = 0;final[v0] = TRUE; Initialize, the v0 vertex belongs to the S collection//start main loop, each time the shortest path to a vertex v is evaluated and the V is added to the S collection for (i = 1; i < g.vexnum; i++)//Other G.vexnum-  
            1 vertices {min = INFINITY;//The nearest distance from V0 point for (w = 0;w < G.vexnum; i++) {  
                    if (!final[w])//w vertex in V-s {if (D[w] < min)//w vertex is closer to V0 {  
                    v = w; Min = d[w];    }}} Final[v] = TRUE;  Nearest V to V0 Vertex added to s for (w = 0;w < g.vexnum;w++)//update current Max path and distance {if (!final[w]  
                && (min + g.weiarcs < d[w]) {D[w] = min + g.weiarcs[v][w];  
                P[W] = P[v] + p[w];  
                P[W] = P[v];  
            P[W][W] = TRUE;  
}}} return 0;   }


Ok,dijkstra algorithm introduced.

Section III Freud algorithm (Floyd algorithm)
The algorithm solves the problem of the shortest path between two vertices in a weighted graph.

The design process for the Freud algorithm is as follows:
Using the weighted matrix Weiarcs to represent the weighted graph, if the two vertices VI and VJ are connected, the weights of the edges formed by the two vertices are represented by weiarcs[i][j]; if VI and VJ are not connected, that is, <vi,vj> this edge does not exist, Then set weiarcs[i][j] to ∞.
Requirements: Find the shortest path of node VI to the node VJ.
Set D (i,j,k) to the length of the shortest path from node VI to node VJ with VK (Vk∈ (0,1,... k)) nodes as intermediate nodes. For example: From VI to VJ this path through node VMs and node VK, then can be expressed as: VI--&GT;VM--&GT;VK--&GT;VJ.

So, there are: 1. If the shortest path passes through the node VK, then D (i,j,k) = d (i,k,k-1) + D (k,j,k-1);
2. If the shortest path does not pass through the node VK, then D (i,j,k) = d (i,j,k-1).
Therefore, the shortest path of the VI to VJ can be expressed as:
D (i,j,k) = min (d (i,k,k-1) + D (k,j,k-1), D (i,j,k-1)).
The old way, the process of illustration is as follows:

The process of solving is shown in the following diagram:



The code for the Freud algorithm described in the above procedure is as follows:
int ShortPath (mgraph g,int v0,pathmatrix &p,shortpathtable &d)  
{  
    //Use the Floyd algorithm to find the shortest path between each pair of vertices V and W in the forward graph p[v][ W] and its weighted length d[v][w].  
    //If P[V][W][U] is true, then U is the vertex for (v = 0;v < g.vexnum;v++) on the shortest path currently evaluated from V to W for (  
        w = 0;w < g.vexnum;w++)  
        {  
            D[v][w] = g.arcs[v][w];  
            if (D[v][w] < INFINITY)   //from V to W there is a direct path  
            {  
                P[v][w][u] = TRUE;  
                P[V][W][W] = TRUE;  
            }  
        }  
          
    for (U = 0;u < g.vexnum;u++) for  
        (v = 0;v < g.vexnum;v++) for  
            (w = 0;w < g.vexnum;w++)  
            {  
                if (D[v] [u] + d[u][w] < D[v][w])  //A shorter path from V through u to w  
                    d[v][w] = D[v][u] < d[u][w];  
                for (i = 0;i < g.vexnum;i++)  
                    p[v][w][i] = P[v][u][i] | | P[u][w][i];  
            }  
    return 0;  
}  


Fourth section A * search algorithm
A * search algorithm, commonly known as a-star algorithm. This is a path with multiple nodes on the tiling surface, and the lowest pass cost algorithm is obtained. It is often used for mobile computing of the NPCs in the game, or for mobile computing on the online game bot. The algorithm, like the Dijkstra algorithm, can find a shortest path and, like BFS, makes heuristic searches.

The core part of a * algorithm is the design of one of its valued functions: F (N) =g (n) +h (n). where G (n) represents the actual distance from the starting point to a point N, and H (n) represents the estimated distance from the arbitrary vertex N to the target vertex, and f (n) is the estimate of each possible test point. This valuation function follows the following characteristics:
• if H (n) is 0, only the g (n) is required, that is, the shortest path from the starting point to any vertex n is converted to the single source shortest path problem, the Dijkstra algorithm;
• if h (n) <= "N to the actual distance of the target", the optimal solution must be obtained. And the smaller the H (N), the more nodes you need to compute, and the less efficient the algorithm is.

We can describe this: the shortest distance from the starting point (StartPoint, abbreviated to SP) to the end point (EndPoint, abbreviated to EP) is certain, so we can write an estimation function to estimate the shortest distance from the starting point to the end point. If the program attempts to move from the starting point along a route to another point on the path (Otherpoint, abbreviated to OP), then we think that the estimated distance from SP to EP that this scheme obtains is the distance from SP to op actually taken, plus the estimated distance from OP to ep that the estimate function estimates. So, regardless of where our program search unfolds, we get an estimate that, after each decision, sort the evaluation values with the pending scenarios, and then pick out the scenarios that are most likely to be part of the shortest route in each of the scenarios to be processed, to the next step, to loop until the object moves to the destination, Or all scenarios have been tried, but no path to the destination has been found to end.

The graphical process of a * search algorithm can be seen in: http://blog.vckbase.com/panic/archive/2005/03/20/3778.html

Section Fifth related notes
Resources: Data Structures (Min), Wikipedia's A * search algorithm

Sixth quarter concluding remarks
Think, write, draw ...

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.