Floyd-warshall Algorithm(Floyd-warshall algorithm) is an algorithm for solving the shortest path between any two points. It can correctly handle the shortest path problem of Directed Graphs or negative weights, it is also used to calculate the transfer closure of a directed graph.
The shortest path matrix between each two points is obtained through the weight matrix of a graph. Recursively update n times from the weighted neighbor matrix A = [A (I, j)] n × n of the graph, that is, the matrix D (0) =, the matrix D (1) is constructed by a formula, and D (2) is constructed by D (1) in the same formula );......; Finally, the matrix D (n) is constructed from D (n-1) using the same formula ). The element in column J of line I of matrix D (n) is the shortest path length from vertex I to vertex J, and D (n) is the distance matrix of the graph, you can also introduce a successor node matrix path to record the shortest path between two points. The relaxation technique is used to relax all other points between I and j. Therefore, the time complexity is O (n ^ 3 );
The length of the shortest path of the intermediate node is set to "from".
- If the shortest path goes through vertex K;
- If the shortest path does not pass through vertex K, then.
Therefore ,.
The state transition equation is as follows: d [I, j]: = min {d [I, j], d [I, K] + d [K, J]} map [I, j] indicates the shortest distance from I to J, and K indicates the exhaustion.
I, j breakpoint, The initial value of d [N, N] should be 0, or do it according to the question. Of course, if this path is not accessible, special processing is required, for example, the path d [I, K] does not exist.
The time complexity of the Floyd-warshall algorithm is, and the space complexity is.
The Floyd-warshall algorithm is described as follows:
for k ← 1 to n do for i ← 1 to n do for j ← 1 to n do if () then ← ;
It indicates the price from point to point. If it is ∞, there is no connection between two points.
In actual algorithms, iteration can be performed directly in the original space to save space, so that the space can be reduced to two-dimensional.
The approximate code is:
For (int K = 1; k <= N; k ++) {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {If (ANS [k-1] [I] [k] = infinity | ans [k-1] [k] [J] = infinity) {ans [k] [I] [J] = ans [k-1] [I] [J]; // keep the original value, that is, the length of the shortest path from I to J is the same as that of continue ;} if (ANS [k-1] [I] [J] = infinity | ans [k-1] [I] [k] + ans [k-1] [k] [J] <ans [k-1] [I] [J]) {ans [k] [I] [J] = ans [k-1] [I] [k] + ans [k-1] [k] [J];} else {ans [k] [I] [J] = ans [k-1] [I] [J] ;}}
After so many iterations, the shortest path from A to B returns ans [N] [a] [B].
At the same time, we note that when the value of ANS [k] [I] [J] is obtained recursively, all ans [k] [I] [J] values will be calculated by ans [k-1] [I] [J] and ANS [k-1] [I] [k] + ans [k-1] [k] [J, but at the same time ans [k] [I] [k] and ANS [k] [k] [J] and ANS [k-1] [I] [k] and ANS [k-1] [k] [J] values are the same, that is, these values will not change because of this update. Therefore, we can simplify the code:
For (int K = 1; k <= N; k ++) {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {If (ANS [I] [k] = infinity | ans [k] [J] = infinity) continue; if (ANS [I] [J] = infinity | ans [I] [k] + ans [k] [J] <ans [I] [J]) {ans [I] [J] = ans [I] [k] + ans [k] [J] ;}}
In this way, the original space complexity O (N3) is changed to O (n2. Each time you connect to the new database, it will be OK directly on the two arrays.
Floyd shortest path algorithm