Exploring the dynamic programming nature of Floyd algorithm

Source: Internet
Author: User

The Floyd–warshall (abbreviated Floyd algorithm) is a well-known algorithm for solving the shortest path between any two points (all Paris shortest paths,apsp). On the surface, the Floyd algorithm is a very simple triple loop, and the Pure Floyd algorithm loops inside the statement is very concise. In my opinion, it is precisely because "Floyd algorithm is a dynamic programming (dynamical programming) algorithm" nature, it leads to Floyd algorithm so exquisite. Therefore, here I will from the Floyd algorithm's state definition, the dynamic transfer equation and the rolling array and other important aspects, to simply analyze this important dynamic programming based algorithm--floyd algorithm.

In the dynamic programming algorithm, it is the definition of state that is in the first place and one of the core concepts. Here, D[k][i][j] is defined as:

The shortest path length between points I point J can only be used when the 1th to K point is the intermediate medium. ”

The graph has n points, numbering starting from 1 to N. Therefore, here, K can be considered as a dynamic programming algorithm at the time of the process of a level, or called "slack operation." D[1][I][J] Indicates the shortest path length between points I point J when using only 1th points as intermediate medium, and d[2][i][j] represents the shortest path length between points I to J when using all points in point 1th to point 2nd as intermediate Medium; D[n-1][i][j] means using point 1th to When all points in the (n-1) point are used as intermediaries, the shortest path length between points I point J d[n][i][j] represents the shortest path length between points I to point J when using the number 1th to n points. With the definition of the state, we can construct the dynamic transfer equation according to the dynamic programming idea.

The basic idea of dynamic transfer can be considered as a kind of transfer representation to establish a state and a former state . 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] (i.e., 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 cases: (1) I to J Short circuit not through K, (2) I to J short circuit by 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])

Finally, D[n][i][j] is the length of the shortest path between all two points in the requested graph. Here, it is important to note the initial (boundary) condition of the dynamic transfer equation described above, i.e. d[0] [I][j]=w (i, J), that is, if no point is used (the initial of "slack operation"), the length of the shortest path between two points is the weight of the edge between two points (if there is no edge between two points, the , and I prefer to use the graph in the Floyd algorithm to represent the data structure of the adjacency matrix, because it is easy to operate. Of course, there are d[i][i]=0(i∈[1,n]).

So we can write the most preliminary Floyd algorithm code:

123456789101112 void floyd_original() {    for(int i = 1; i <= n; i++)        for(int j = 1; j <= n; j++)            d[0][i][j] = graph[i][j];    for(int k = 1; k <= n; k++) {        for(int i = 1; i <= n; i++) {            for(int j = 1; j <= n; j++) {                d[k][i][j] = min(d[k-1][i][j], d[k-1][i][k] + d[k-1][k][j]);            }        }    }}

Almost all of the most famous "0/1 knapsack" problem in the dynamic planning algorithm books, will further introduce the use of rolling array techniques to further reduce the space complexity of the algorithm, so that the 0/1 backpack only need to use a one-dimensional array to obtain the best solution. In all kinds of data, the most common Floyd algorithm uses a two-dimensional array to represent the state. So, in the Floyd algorithm, how to use the scrolling array?

Observing the dynamic transfer equation D[k][i][j] = min (D[k-1][i][j], d[k-1][i][k]+d[k-1][k][j]), you can find the state of each K-phase (D[k][i][j]), which depends on the previous stage (that is, the k-1 order (e.g. D[k-1][i][j],d[k-1][i][k] and d[k-1][k][j]).

The two-dimensional matrix of the shortest path length between two points in the figure is described in the case of the first Floyd algorithm, which calculates the state d[k][i][j], d[k-1][][] and d[k][][] (d[k-1][][) for the k-1 stage. d[k][][ ] represents the two-dimensional matrix of the shortest path length between two points in the figure of phase K. A directed line segment with arrows in red indicates the planning direction. Gray represents an array element that has already been counted, and white represents an element that has not yet been counted. Because d[k-1][][] and d[k][][] are two independent two-dimensional arrays, there is no problem using d[k-1][i][j],d[k-1][i][k] and D[k-1][k][j] (both in the two-dimensional array above) to calculate d[k] [i][j].

So how do you use a two-dimensional array to implement a scrolling array to reduce the complexity of space?

Is the case when using a scrolling array, in phase K, to calculate d[i][j]. At this point, because the two-dimensional array using d[][] is used as a scrolling array, it is reused in each phase of the calculation, so that dimension of the representation phase in the array is also canceled. In this figure, the white lattice represents the newest calculated element (the new value of the K-stage), and the value of the element in the gray lattice is actually saved as the old value of the previous stage (that is, the k-1 phase). Therefore, when the new D[i][j] has not been computed, the value saved in D[i][j] actually corresponds to the value d[k-1][i][j] when the scroll array was not used previously. At this point, the dynamic transfer equation becomes after the hidden phase index:

D[i][j] = min (D[i][j], d[i][k]+d[k][j]) (k,I,j∈[1,n])

Assignment number to the left D[i][j] is the shortest path length between I and J, which is the k phase we are going to calculate. Here, you need to make sure that the value of d[i][j], d[i][k] and d[k][j] on the right of the assignment is the value of the previous stage (K-1 stage). Before the new D[i][j] is calculated, the value reserved by the D[i][j] element is indeed the old value of the previous stage. But as for d[i][k] and D[k][j]? We cannot determine whether the two elements fall in the white area (the new value) or the gray area (the old value). Fortunately there is such an important nature that dp[k-1][i][k] and dp[k-1][k][j] will not change in the size of phase K. In other words, the value of the K-phase will not change if the side is connected to the K-node. How to prove it simply? We can put j=k into the previous d[k][i][j]=min (D[k-1][i][j], d[k-1][i][k]+d[k-1][k][j]) equation, namely:


= Min (D[k-1][i][k], d[k-1][i][k]+d[k-1][k][k])

= Min (d[k-1][i][k], d[k-1][i][k]+0)

= D[k-1][i][k]

That is to say, the shortest path length between point I and point k is constant in phase k-1 and phase K. The same can be proved that in these two stages, the shortest path length between point K and Point J is constant. Therefore, for the transfer equation using the scrolling array d[i][j] = min (D[i][j], d[i][k]+d[k][j]), the values of d[i][j], D[i][k], and d[k][j] to the right of the assignment number are the values of the previous stage (the k-1 phase). Can be used with confidence to calculate the value of d[i][j] at phase K.

The code for the Floyd algorithm that is rewritten with the scrolling array is as follows:

123456 void floyd() {    for(int k = 1; k <= n; k++)        for(int i = 1; i <= n; i++)            for(int j = 1; j <= n; j++)                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);}

Therefore, through the analysis of this article, we can find that the Floyd algorithm is indeed a typical dynamic programming algorithm, understanding the Floyd algorithm can also help us to further understand the dynamic planning ideas.

Reprint: http://www.cnblogs.com/chenying99/p/3932877.html

Exploring the dynamic programming nature of Floyd algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.