PS: This article is based on other people's blog to be modified, will be from the AK (I,J) derivation Ak-1 (i,j) from Ak-1 (i,j) derivation Ak (i,j), in order to conform to their own thinking habits, better understanding Floyd algorithm, and easy to view later.
first of all, thank the original author.
Original Address--http://blog.csdn.net/roofalison/article/details/5651806
Enter the text below
Floyd's code implementation is actually simple: [CPP] view plain copy void Floyd () {for (k=0;k<n;k++) for (i=0;i<n;i + +) for (j=0;j<n;j++) a[i][j]=min (A[i][j],a[i][k]+a[k][j]); }
The Floyd algorithm is a classic dynamic programming algorithm with the goal of finding the shortest path from point I to J (any two points in the diagram). From the point of view of dynamic planning, we need to make a reinterpretation of this goal (this interpretation is the essence of dynamic planning the most creative). Assuming that the graph has n vertices, then the 0,1,..., N-1,floyd algorithm joins this concept
Ak (I,J): Represents the shortest path from vertex i to vertex J and the ordinal of an intermediate vertex is not greater than K .
The important thing about this limitation is that it restricts the concept of the shortest path to an opportunity to meet the iterative relationship , which is the study: assuming Ak-1 (I,J) is known, it is possible to deduce the AK (I,J). To solve this problem, we adopt the mathematical induction method, which is solved in two steps:
1, by the definition of AK (I,J) can be the initial state (that is, the middle does not go through any other vertices): A-1 (i,j) = Cost (I,J). (Cost (I,J) is the weight of the edge connecting vertex i and Vertex J, if the two vertices are unbounded, the value is infinity)
2, assuming at this time Ak-1 (I,J) is known, and on this basis to solve the AK (I,J), then we can be divided into two situations to view the problem: 1. Ak (I,J) passes a point k;2 along the way. Ak (I,J) does not pass the dot K. If you pass the point K, it is clear that Ak (i,j) = Ak-1 (i,k) + Ak-1 (k,j). Why is it Ak-1? Because Ak-1 is the best solution we've got right now . Then in the second case, AK (I,J) does not pass the point K, because there is no point K, so according to the concept, can be derived Ak (i,j) =ak-1 (i,j). Now, we are sure that there are only these two cases---not after the point K, is not through the point K, there is no third case, the condition is complete, then choose which one. Very simple, ask for the shortest path, of course, which is the shortest, which, therefore, to obtain a formula:
Ak (i,j) = min (Ak-1 (i,j), Ak-1 (i,k) + Ak-1 (k,j))
Here, the entire algorithm for the AK (I,J) is listed, but the final goal is to find Dist (I,J), the shortest path from I to J, and how to convert the AK (I,J) to Dist (I,J). This is actually very simple, when K=n-1 (n indicates the number of vertices in the graph), that is, An-1 (i,j) =dist (i,j). That is because when K is the largest, there is no more index than the point of K, then the An-1 (I,J) is actually the vertex i to the vertex J shortest path.
From the point of view of dynamic programming algorithm design, to solve the optimization problem of decision-making process, we should firstly decompose the problem to be solved into several sub-problems, determine the quantitative relationship between the problem to be solved and sub-problem , solve the sub-problem first, and then get the solution of the original problem from the solution of these sub-problems. This paper can be regarded as the solution to the quantitative relationship (i.e., the problem of the original problem).
Personal Summary: The process of dividing is from the top down, the process of solving is from bottom to top.