--dijkstra algorithm for Shortest path algorithm

Source: Internet
Author: User

As with the Floyd-warshall algorithm, a two-dimensional array e is still used to store the relationship of the edges between vertices, with the initial values as follows.

We also need to use a one-dimensional array dis to store the initial distance of vertex number 1th to the rest of the vertices, as follows.

        We will refer to the values in the DIS array as the shortest-circuiting "estimate".         since it is the shortest distance from vertex 1th to the rest of the vertices, find the closest vertex to the vertex of number 1th. By using the array dis, we know that the nearest vertex of number 1th is the vertex number 2nd. When the number 2nd vertex is selected, the value of dis[2] has changed from "Estimated value" to "determined value", i.e. the shortest distance from vertex 1th to vertex 2nd is the current dis[2] value. Why is it? You know, at the moment, the nearest vertex at number 1th is the vertex of number 2nd, and all the edges of the graph are positive, so it is certainly not possible to pass through the third vertex, making the distance from vertex 1th to vertex 2nd shorter. Because the distance from vertex number 1th to other vertices is definitely not 1th to 2nd vertices short, right O (∩_∩) o~        Now that the 2nd vertices are selected, let's look at the edges of the 2nd vertices. There are two sides of the 2->3 and 2->4. Let's discuss whether this side of the 2->3 can shorten the distance from vertex 1th to vertex 3rd. That is to compare the size of dis[3] and dis[2]+e[2][3]. Where Dis[3] represents the distance from vertex number 1th to vertex 3rd. DIS[2]+E[2][3] Dis[2] represents the distance from vertex number 1th to vertex 2nd, e[2][3] means 2->3 this edge. So dis[2]+e[2][3] means from vertex number 1th to vertex number 2nd, and then through 2->3 this edge, reached the distance of vertex 3rd.         We found dis[3]=12,dis[2]+e[2][3]=1+9=10,dis[3]>dis[2]+e[2][3], so dis[3] to be updated to 10. This process has a professional term called "slack". The distance from vertex 1th to number 3rd is dis[3], which is 2->3 by the edge of the strip. This is the main idea of the Dijkstra algorithm: "Edge" to relax the number 1th vertex to the rest of the vertices of the distance.          similarly through 2->4 (E[2][4]), the value of dis[4] can be relaxed from ∞ to 4 (Dis[4] initially ∞,dis[2]+e[2][4]=1+3=4,dis[4] >DIS[2]+E[2][4], so dis[4] to be updated to 4).         We have just loosened all the edges of vertex 2nd. After the relaxation is complete, the DIS array is:

Next, continue with the remaining vertices of 3, 4, 5, and 6th, and select the vertex closest to the vertex of number 1th. By updating the DIS array above, the nearest vertex of number 1th is the vertex number 4th. At this point, the value of dis[4] has changed from estimated value to OK value. The following continues the relaxation of all the out edges (4->3,4->5 and 4->6) of vertex 4th with the method just described. After the relaxation is complete, the DIS array is:

Continue with the remaining vertices of 3, 5, and 6th, select the vertex closest to the 1th vertex, this time choose the 3rd vertex. At this point, the value of dis[3] has changed from estimated value to OK value. Relaxes all the out edges (3->5) of vertex number 3rd. After the relaxation is complete, the DIS array is:

Continue in the remaining 5 and 6th vertices, select the vertex closest to the 1th vertex, this time choose the 5th vertex. At this point, the value of dis[5] has changed from estimated value to OK value. Relaxes all the out edges (5->4) of vertex number 5th. After the relaxation is complete, the DIS array is:

Finally, all the point out edges of vertex 6th are relaxed. Because this example has no edge 6th vertices, so no processing is necessary.       In this scenario, all values in the DIS array have been changed from estimated value to OK value. The final dis array is as follows, which is the shortest path from vertex 1th to the rest of the vertices.

OK, now to summarize the algorithm you just made. The basic idea of the algorithm is to find the closest vertex of the source point (the source point of the above example is the vertex of number 1th) each time, and then extend the vertex as the center, and finally get the shortest path from the source point to all the remaining points. The basic steps are as follows:
    • Divides all the vertices into two parts: the vertex set P and the unknown shortest path of the vertices collection Q for the shortest distance known. At first, only the source point is a vertex in the vertex collection p of the known shortest path. Here we use a book[i] array to record which points are in the set p. For example, for a vertex i, if book[i] is 1 it means that the vertex is in the set P, if book[I] is 0 it means that the vertex is in the set Q.

    • Set the source point S to its own shortest path of 0 that is dis=0. dis[i] is set to e[s][i] if there is a vertex I that can be reached directly from the source point. At the same time, all other vertices (which are not directly reachable by the source point) are set to ∞ with the shortest path.

    • Select one of the closest vertices of the source point S (that is, the dis[u] minimum) in all vertices of the set Q to join the set p. It also investigates all edges with point u as the starting point and relaxes each edge. For example, if there is an edge from u to V, you can expand a path from S to V by adding the edge u->v to the tail, which is the length of dis[u]+e[u][v]. If this value is smaller than the value currently known as Dis[v], we can replace the value in the current Dis[v] with the new value.

    • Repeat the 3rd step, if the set Q is empty, the algorithm ends. The value in the final dis array is the shortest path from the source point to all vertices.

The code is as follows:

#include <iostream>#include<cstring>#defineMAX 6#defineINF 0xFFFFFFFF/** Dijkstra Shortest path. * That is, the shortest path of the "vertex" in the chart to each of the other vertices. * * Parameter Description: * Vmatrix--adjacency Matrix * Apex--Starting vertex (start vertex). That is, the shortest path of vertex apex to other vertices is computed. * Prepoint--the predecessor vertex array. The value of Prepoint[i] is the vertex from vertex apex to the shortest path of vertex I that goes through all the vertices that precede vertex i. * Finalpointval--length array. Dist[i] is the length of the shortest path of vertex apex to vertex i. */voidDijkstra (unsignedintVmatrix[][max],intApex, unsignedintPrepoint[], unsignedintfinalpointval[]) {        intI, K; unsignedinttemp, min; intFlag[max] = {0};//Flag[i]=1 indicates that the shortest path to vertex apex to vertex i has been successfully obtained.      for(intI=0; i<max; i++) {Flag[i]=0;//the shortest path to vertex i is not yet available. Prepoint[i] =0;//the predecessor vertex of vertex i is 0. Finalpointval[i] = Vmatrix[apex][i];//The shortest path to vertex i is the right of vertex apex to vertex i.     }        //Initialize vertex apex itselfFlag[apex] =1; Prepoint[apex]=0; //Traverse, finding the shortest path to a vertex at a time.      for(i=1; i<max; i++) {min=INF;  for(intj=0; j<max; J + +)//find the current smallest path, that is, the array finalpointval, the smallest right point in the        {            if(flag[j]==0&& finalpointval[j]<min) {min=Finalpointval[j]; K=J; }} Flag[k]=1;//Mark "Vertex K" for the shortest path already acquired                 for(intj=0; j<max; J + +)//fix the current shortest and precursor vertices, i.e., after the shortest path to vertex K has been updated, update the shortest path and precursor vertex for vertices that do not get the shortest path.{Temp= (Vmatrix[k][j]==inf?) INF: (min+vmatrix[k][j])); if(flag[j]==0&& temp<Finalpointval[j]) {Finalpointval[j]=temp; PREPOINT[J]=K; }        }    }         for(i =0; i < MAX; i++) Std::cout<<"Shortest (1,"<< i+1<<") = "<< Finalpointval[i] <<Std::endl; }intMainintargcChar*argv[]) {unsignedintPrepoint[max]; unsignedintDespoint[max]; unsignedintVmatrix[max][max] = {{0,1, A, INF, INF, inf}, {INF,0,9,3, INF, INF}, {INF, INF ,0Inf5, INF}, {INF, INF,4,0, -, the}, {inf , INF, INF, INF,0,4}, {inf , INF, INF, INF, INF,0}}; memset (Prepoint,0,sizeof(Prepoint)); memset (Despoint,0,sizeof(Despoint)); Dijkstra (Vmatrix,0, Prepoint, Despoint); return 0;}

Reference Blog: 51014824

http://blog.51cto.com/ahalei/1387799

--dijkstra algorithm for Shortest path 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.