FIG - Dijkstra shortest path method implementation of FIG.

1, the shortest path concept:

       1 route, to the other vertices in a directed graph from the vertex (vertex start) (end apex), which is the weighted sum and a minimum path;

      

2, the problem formulation:

 

       1, given a weighted directed graph G to the starting vertex v, G v to request from the other vertex of the shortest path (on each side there is a meaningful weight);

       2, Dijkstra core algorithm is to find the shortest path through the unknown known shortest path;

      

3. Solutions:

       1, Dijkstra proposed by ascending order of path length, gradually results in the shortest path;

              1, one first obtains the shortest length of the shortest path, then it is determined with reference to a short length of time a shortest path, and so on, until the starting vertex v to vertex of each other all the shortest path is obtained so far;

       2, the core is by a recursive manner starting from the vertex to each of the shortest path seek out all other vertices;

      

4, ready to work:

       1, the auxiliary array: Array <E> dist;

              1, dist [i] from the starting vertex v0 represents the current path length to the vertex vi;

       2, initial settings:

              1, when the starting vertex to the vertex v0 vi edged: dist [i] for the edge weight;

              2, when starting from the vertex to the vertex v0 vi infinity: dist [i] to infinity;

             

5, Dijkstra algorithm demo:

 

       1, the basic definition found non-empty set of vertices and edges of a set of two graphs, the analysis algorithm of FIG importance set in, and usually set in a data structure array;

       2, each concerned with just joined the vertex S set of connections to other vertices, the shortest path value just determined to find the shortest possible path to reach a value other vertex, which is the core;

         

6, Dijkstra algorithm steps:

 

7, Dijkstra algorithm essence:

       1, the vertex S set in the shortest path is already found vertices;

       2, v0 to w shortest path only through vertices within the set S;

       3, dist [w] may change:

 

             

8, how to record each vertex on the shortest path?

       1, the definition of an auxiliary array:

              1,Array<int> path;

                     1, path [i] denotes the i precursor vertexes on the current path;

                     2, Initialization: path = {-1};

                     3. Review:

                           

                          

9, Dijkstra algorithm flow chart:

 

10, Dijkstra shortest path algorithm:

1     / * two shortest paths between vertices returned array represents the shortest path above two vertices * / 
2      SharedPointer <the Array < int >> Dijkstra ( int I, int J, const E & the LIMIT)   // (n-O * n-) 
. 3      {
 . 4          LinkQueue < int > RET;   // save the shortest path over the vertex 
. 5  
. 6          IF (( 0 <= I) && (I <VCount ()) && ( 0 <= J) && (J < VCount ( )))
 . 7          {
 . 8              DynamicArray <E> dist (VCount ());   // for storing values path 
9             DynamicArray < int > path (VCount ());   // for storing the precursor node of the current node 
10              DynamicArray < BOOL > Mark (VCount ());   // flag whether the vertex set into the S 
. 11  
12 is              / * materials initialization * / 
13 is              for ( int K = 0 ; K <VCount (); K ++ )
 14              {
 15                  Mark [K] = to false ;   // vertex S set in no 
16                  path [K] = - . 1 ;   // path no vertices precursor 
17  
18                 dist [K] = isAdjacent (I, K) (path [K] =? I, getEdge (I, K)): the LIMIT;   // If the initial node and the other node has connected, it is set to the right edge value, otherwise the taste infinity; but also update the path array, comma expression k precursor current vertex is a vertex I 
20 is              }
 21 is                                                                   
22 is              Mark [I] = to true ;   // the initial set of vertices in S 
23 is  
24              / * big loop * / 
25              for ( int K = 0 ; K <VCount (); K ++ )
 26 is              {
 27                  E m = the LIMIT;
 28                  int U = - . 1 ;
 29  
30                  / *Traverse dist array, found in the non-S set to the minimum path value vertex S set * / 
31 is                  for ( int W = 0 ; W <VCount (); W ++ )
 32                  {
 33 is                      IF ! (Mark [W] && (dist [W] <m))   // not find the minimum value of S dot marks set, updating a small value is to find the minimum value of the path 
34 is                      {
 35                          m = dist [W];   // find the minimum value of the path 
36                          U W =;   // shortest path of the recording apex 
37                      }
 38                  }
 39  
40                  / * determine whether the minimum path is found, to prevent the edges not only vertex * / 
41 is                  iF(U == - . 1 )
 42 is                  {
 43 is                      BREAK ;   // only node without edges, the shortest path between two o'clock not found, because the arrays are dist theoretical maximum, can not find the minimum value, directly out 
44                  }
 45  
46 is                  Mark [u] = to true ;   // concentration values found path corresponding to the non-vertex u S is the minimum value, into a set of S 
47  
48                  / * core algorithms: a path by known, Release S S set to a non-set of vertices the shortest path from u to push the newly added; w non-point S set in here, its traverse; S into the set of all vertices in time, the algorithm ends * / 
49                  for ( int w = 0 ; w <VCount (); W ++ )
 50                  {
 51 is                      / *In the starting vertex i to vertex u based on the shortest path, to the other vertices to compute the minimum path is obtained by known shortest path, if smaller, dist update array, where W is the minimum weight of vertex u adjacent vertices, as long as the smaller, it is necessary to update * / 
52 is                      IF (! Mark [W] && isAdjacent (U, W) && (dist [U] + getEdge (U, W) < dist [W]))
 53 is                      {
 54 is                          dist [W] = dist [u] + getEdge (u, W);
 55                          path [W] = u;   // this precursor vertexes are a vertex u represents 
56 is                      }
 57 is                  }
 58              }
 59  
60              LinkStack < int > S;
 61 is  
62 is              s.push (J);   //Terminating the first vertex j placed on the stack; 
63 is  
64              / * from the start vertex i to vertex j of the final value to go into the stack; predecessor node access method, as the position value; value is the apex of the foregoing, Therefore, the value of the press-fitted directly into the stack * / 
65              for ( int K = path [J]; K = -! . 1 ; K = path [K])
 66              {
 67                  s.push (K);
 68              }
 69  
70              / * order of vertices is stored in reverse path through the stack transit, adjust over; * / 
71 is              the while (s.size ()> 0 )
 72              {
 73 is                  ret.add (s.top ());
 74  
75                  s.pop ();
 76              }
77          }
 78          the else 
79          {
 80              throw_exception (InvalidParameterException, " Index <i, j> IS invalid ... " );
 81          }
 82  
83          / * final shortest path experiences Vertices at least 2, i to j is otherwise unreachable , the maximum number of vertices is not known * / 
84          IF (ret.length () < 2 )
 85          {
 86              throw_exception (an ArithmeticException, " There iS nO path from I to J ... " );
 87          }
 88  
89          return toArray (RET);   //Placed inside the array 
90      }

 

11, Dijkstra shortest path algorithm test code:

 1 #include <iostream>
 2 #include "MatrixGraph.h"
 3 #include "ListGraph.h"
 4 
 5 using namespace std;
 6 using namespace DTLib;
 7 
 8 template< typename V, typename E >
 9 Graph<V, E>& GraphEasy()
10 {
11    static MatrixGraph<4, V, E> g;
12 
13     g.setEdge(0, 1, 1);
14     g.setEdge(0, 2, 3);
15     g.setEdge(1, 2, 1);
16     g.setEdge(1, 3, 4);
17    g.setEdge(2, 3, 1);
18 
19     return g;
20 }
21 
22 template< typename V, typename E >
23 Graph<V, E>& GraphComplex()
24 {
25    static ListGraph<V, E> g(5);
26 
27     g.setEdge(0, 1, 10);
28     g.setEdge(0, 3, 30);
29     g.setEdge(0, 4, 100);
30     g.setEdge(1, 2, 50);
31     g.setEdge(2, 4, 10);
32     g.setEdge(3, 2, 20);
33    g.setEdge(3, 4, 60);
34 
35     return g;
36 }
37 
38 int main()
39 {
40     Graph<int, int>& g = GraphComplex<int, int>();
41    SharedPointer< Array<int> > p = g.dijkstra(0, 4, 65535);
42 
43     for(int i=0; i<p->length(); i++)
44     {
45         cout << (*p)[i] << " ";
46    }
47 
48    cout << endl;
49 
50     return 0;
51 }

 

12 Summary:

       1, Dijkstra shortest path algorithm is based on the idea of ​​recursive completed;

       2, the starting vertex to another vertex of each shortest path is obtained by dynamic derivation;

       3, not only the shortest path vertices labeled by the tagged vertices calculation results;

       4, the final result of the algorithm that the start vertex to the other vertices of each shortest path;

Guess you like

Origin www.cnblogs.com/dishengAndziyu/p/10926621.html