Floyd algorithm is a classical multi-source Shortest path algorithm in graph theory, which is the shortest path between any two points.
It can adopt the idea of dynamic programming, because it satisfies the optimal substructure property, that is, the sub-sequence of the shortest path sequence is also the shortest path.
The shortest path sequence of 1th to 5th is <1,2,4,5>, and its subsequence <1,2,4> is also the shortest path, for example, the optimal substructure properties.
In the dynamic programming algorithm, it is the definition of state that is in the first place and one of the core concepts.
The basic idea of dynamic transfer can be considered as a kind of transfer representation to establish a state and a former state.
D[K][I][J] is defined as the shortest path length between points I point J only when 1th to point K is used as intermediate medium.
According to the previous definition, D[k][i][j] is a state using the number 1th to the K point, you can find a way to move the state through the dynamic, the statute to the use of 1th to (k-1) status, that is D[k-1][i][j].
For D[k][i][j] (that is, the shortest path between I and J when using all points in the 1th to K Point as intermediate Medium) can be divided into two situations:
(i) The shortest path from I to J does not go through K;
(II) I to j the shortest path has been K.
D[K][I][J]=D[K-1][I][J] without the shortest circuit of point K.
After the shortest circuit of point K, D[k][i][j]=d[k-1][i][k]+d[k-1][k][j].
Therefore, the dynamic transfer equation of the Floyd algorithm can be obtained by combining the above two conditions:
D[k][i][j] = min (D[k-1][i][j], d[k-1][i][k]+d[k-1][k][j]) (K,i,j∈[1,n])
Here, it is important to note the initial (boundary) condition of the above dynamic transfer equation, i.e. d[0][i][j]=w (i, J),
That is, the length of the shortest path between two points is the weight of the edge between two points without using any point (the initial of the "slack Operation").
Method: "Slack" point, "five lines code" through n-1 relaxation node
Reference: https://www.cnblogs.com/chenying99/p/3932877.html
The shortest Path Floyd algorithm for graph theory