FIG - Floyd shortest path method implementation of FIG.

. 1, Dijkstra algorithm disposable obtained starting vertex to all other vertices of the shortest path, if you want the shortest path between any two vertices, the vertices may be used as the starting vertex drawing n times Dijkstra algorithm can be a ;

  

2, possible solutions:

      

       1, after the execution of the algorithm, the shortest path to j I value is stored in dist [i] [j] in. Precursor shortest path nodes is stored in the path [N] [N] in;

       2, the method is relatively soil;

      

3, the problem formulation:

       1 weighted, each side of a known weight of greater than 0 a directed graph, each vertex vi = vj, calculated vertex on the shortest path between vi and vj values ​​and the shortest path!;

         

4, Floyd algorithm core:

       1, an n-order square matrix defined sequence:

             

          among them:

           

       2, suspicion between two vertices current path is not the shortest path, so Floyd algorithm attempts by the other vertices transit, transit until you find the shortest path to a transit point;

         

5, n-order matrix elements of significance:

     

       1, the value is right adjacent to the square of the push;

       2, this algorithm is to obtain the shortest path between two vertices by a recursive manner;

       3, all the vertices are derived transit paths over, there is obtained a minimum path;

       4, behind the derivation square, the square containing the foregoing information, and each is derived minimum, until all the derived vertex, to give a final shortest path;

      

6, Floyd algorithm essence:

 

 

7, Floyd algorithm to achieve:

     

       1. Initialization:

              1, the essence of: using the adjacency matrix initialization A (-1);    

       2, A (0), ..., A (n-1) matrix is ​​derived:

    1, the essence of: using phase transfer vertex deriving a shortest path;

              2, the outermost layer in said loop A (k) matrices, derived after cycle, to obtain the shortest path matrix A (n-1), is also desired;

                   

 

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

       1, the auxiliary matrix is ​​defined:

              1, int path [N] [N]; // Path Matrix

                     1, path [i] [j] denotes the i to j path through which a first vertex;

                     2, initialization: path [i] [j] = -1; or paht [i] [j] = j;

                            1, there is a direct connection is set to j, denotes a final value for the vertex through the vertex J;

                            2, provided no two vertices connected -1;

                     3. Review:

1 if( (dist[i][k] + dist[k][j]) < dist[i][j]  )
2 {
3      dist[i][j] = dist[i][k] + dist[k][j];
4      path[i][j] = paht[i][k];
5 }

                            1, if the condition is true, the vertex k can be obtained a shorter transit path by the first vertex i to j path which passes from the first vertex i to k is this path passes because the transit by the k;

              2, an example of a channel matrix:

      1, pushed by the path of a point to other paths:

                     2, the secondary matrix and the path:

 

9, Floyd shortest path algorithm:

. 1     / * Floyd algorithm shortest path between each pair of nodes, the return value is the shortest path; core looking for shorter paths through the transit vertex * / 
2      SharedPointer <the Array < int >> Floyd ( int X, int Y, const E & the LIMIT )   // O (* n-n-n-*) 
. 3      {
 . 4          LinkQueue < int > RET;
 . 5  
. 6          IF (( 0 <= X) && (X <VCount ()) && ( 0 <= Y) && (Y <VCount ()))   // vertex coding number to be reasonable 
. 7          {
 . 8              DynamicArray <DynamicArray <E>> dist (VCount ());  // define the two-dimensional array,N * N 
. 9  
10              DynamicArray <DynamicArray < int >> path (VCount ());   // the shortest path of the auxiliary array 
. 11  
12 is              / * define a two-dimensional array * / 
13 is              for ( int K = 0 ; K <VCount (); ++ K )
 14              {
 15                  dist [K] .resize (VCount ());
 16                  path [K] .resize (VCount ());
 . 17              }
 18 is  
. 19              / * set the initial value * / 
20 is              for ( int I = 0 ; I <VCount (); I ++ )
21 is              {
 22 is                  for ( int j = 0 ; j <VCount (); j ++ )
 23 is                  {
 24                      path [I] [j] = - . 1 ;   // I and j are no edges 
25  
26 is                      dist [I] [j] ? = isAdjacent (I, J) (path [I] [J] = J, getEdge (I, J)): the LIMIT;   // adjacent to the set, using the comma expression, expression is the first argument comma vertex set, the second weight is provided 
27                  }                                                                           
 28              }
 29  
30              / * deriving a shortest path matrix * / 
31 is              for( Int K = 0 ; K <VCount (); K ++ )
 32              {
 33 is                  for ( int I = 0 ; I <VCount (); I ++ )
 34 is                  {
 35                      for ( int J = 0 ; J <VCount (); J ++ )
 36                      {   
 37                 / * inference rules, the intermediate vertex transfer data to see if there is a shortest path value * / 
38 is                          iF ((dist [I] [K] + dist [K] [J]) < dist [I] [J] )
 39                          {
 40                              dist [I] [J] = dist [I] [K] + dist [K] [J];  // If the most shortest path, the shortest path may be considered to update the value of 
41 is  
42 is                              path [I] [J] = path [I] [k];   // vertex k can be found by the minimum value, the vertex found 
43 is                           }
 44 is                      }
 45                  }
 46 is              }
 47  
48              the while ((X = -! . 1 ) && (X = Y)!)   // derived vertex of a termination 
49              {
 50                  ret.add (X);   // shortest path is added to each vertex of the return value 
51 is  
52 is                  x = path [x] [y];   // recursive the path [x] [y] via the first into a vertex xAnd then, starting from the next recurrence of the other vertices recursively x; 
53 is              }
 54 is  
55              IF (! X = - . 1 )
 56 is              {
 57 is                  ret.add (x);   // The final return value added to queue x a , as above x == y, terminates in a return queue is added, so there is added to 
58              }
 59          }
 60          the else 
61 is          {
 62 is              throw_exception (InvalidParameterException, " Index <X, Y> iS invalid ... " );
 63          }
 64-  
65          / * is there really a shortest path between two values to see the shortest target * / 
66          iF( ret.length() < 2 )
67         {
68             THROW_EXCEPTION(ArithmeticException, "There is no path from x to y ...");
69         }
70 
71         return toArray(ret);
72     }

 

10 Summary:

       1, Floyd recursive algorithm stepwise determined shortest path between all of the vertices;

       2, Floyd algorithm is essentially to find a shorter path through the transit vertices;

       3, the adjacency matrix is ​​derived starting shortest path matrix;

       4, the recording channel matrix of each vertex on the shortest path;

Guess you like

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