Design and Analysis of Algorithms [] Floyd-Warshall algorithm for finding the shortest path between any two points

Disclaimer: This article is a blogger original article, reproduced, please indicate the source https://blog.csdn.net/C2681595858/article/details/85759269


Experiment code (GitHub)

First, the experiment content

  • Floyd-Warshall algorithm to achieve the shortest path between any two points.
  • Test chart
    Here Insert Picture Description
  • Expected output
    Here Insert Picture Description

Second, the theoretical preparation

  • The core idea: it is to find a path between two points each, and then choose the shortest path to the piece, because it is looking for all the paths resulting in very high complexity.
  • Conditions of use: can not contain any negative circle. You may have negative side.
  • There is a page in English animation floyd-warshall algorithm , it is recommended to look at, easy to understand.
  • Pseudo code
    Here Insert Picture Description
  • Pseudo-code analysis:
    • First outermost loop control is an intermediate node, then two inner loop sequentially access each vertex of the matrix, this feels like is that this algorithm is found not only contain two or three vertices the shortest path vertex, of course not.
    • Do not forget that there is an iterative process, think about it, the first iteration when it finds indeed said above that the shortest path because it one by one to try and write down the value of the shortest path. And then when it began the second iteration, d i k k 1 d_ {} ik ^ {k-1} Remember that the last time that the shortest path to find ah, that is to find the shortest path through up to an intermediate node, and this time i to find the shortest path between j k through the first when i see the distance between the k, but this time d i k fat} Last shortest path is obtained, so this is again seeking the shortest path in the last basis. To such a conclusion, the shortest path between the i to j, a maximum of K vertices (comprising i and j, k is the number of vertices in the figure).

Third, the experimental environment

  • Operating system and version: windows10
  • Compiler software and version: g ++ 6.3.0
  • Computer language used: c ++ language

Fourth, the experiment

  • Due to the need to use the adjacency matrix storage, so before the code can not be reused.
  • But the algorithm is very simple and very easy to implement, but note that it is time to initialize the weight matrix elements on the diagonal, which is a vertex to their own distance is 0, and the distance to other vertices is infinite . Then, when truly did not need to use the N D matrix, because each calculation to be used in the last calculation, you only need to save the last of the calculation results, and earlier results can be overwritten.
  • The core code is as follows:
for(int k = 0; k < size; k++)
  {
    for(int i = 0; i <size; i++)
    {
      for(int j=0; j < size; j++)
      {
        int temp = D0[i][k] + D0[k][j];
        if(D0[i][k] == INT_MAX || D0[k][j] == INT_MAX)//防止溢出
          temp = INT_MAX;
        D[i][j]  = min(D0[i][j], temp);
      }
    }
    for(int i = 0; i <size; i++)
    {
      for(int j=0; j < size; j++)
        D0[i][j] = D[i][j];
    }
  }

Fifth, the experimental results

  • Most of the final output from the matrix
    Here Insert Picture Description

Six experimental summary

  • Is relatively simple, the main problem lies in the types of data overflow. In the initialization of off-diagonal elements are initialized to INT_MAX, results in the calculation of an overflow has occurred, causing the output are complex, bug is relatively easy to find, so a basic understanding of arithmetic thought later, to achieve relatively simple.

Guess you like

Origin blog.csdn.net/C2681595858/article/details/85759269