I. Introduction
The subject is reviewing the course "Mathematical Analysis and Modeling" recently. When reviewing the graph theory part, he came into contact with Dijkstra's algorithm and Floyd, two classic algorithms used to solve the shortest path. When understanding Floyd's algorithm, he consulted a lot of other people's understanding. Finally, he probably understood the working process of this algorithm. I wrote down my understanding as a record, hoping to help the latecomers.
Two, Floyd algorithm workflow analysis
Unlike Dijkstra's algorithm, Floyd's algorithm is used to solve the shortest path problem between two points. It works as follows:
(1) Construct the initial path example matrix , route matrix
Here is a direct reference to the shortest path problem in https://blog.csdn.net/qq_42518956/article/details/116705635 , the picture is the initial problem inside
So we first construct a distance matrix between two points of 5 5, indicating the distance from point i to point j, the distance matrix is as follows:
tip: due to negligence, here I regard the two points as two-way
The initial path matrix is:
= , each line is from 1 to the last point
(2) The key process of Floyd's algorithm is to insert points sequentially from the first point to the last point. When inserting the kth point, make , at that time , correct the corresponding position in the path, and replace the value there with k.
First, do the first point insertion and calculate d1(i,j)= , the calculation result is as follows:
Compare the value of each item of d1 with and select a smaller value as the first interpolation value. The original value is not greater than the value in d1, and the result is =
Carry out the second point insertion and calculate d2(i,j)= , the calculation result is as follows:
The red part has a smaller value:
Then change the distance matrix , the route matrix is =
Carry out the third point insertion, , the result is as follows
For value comparison, the red part is smaller:
Then , the route matrix
Carry out the fourth point insertion, , the result is as follows
The matrix does not change, ,
Carry out the fifth point insertion, the process is the same as before, and the obtained matrix is as follows:
The values are not very good, the matrix remains unchanged.
,
So we get the final distance matrix and route matrix, for example, we find the shortest path from point 1 to point 4, , which means the path is 1->3->4, and the distance is 8.
Three, Floyd algorithm Matlab simple implementation process
It is mainly a three-iteration execution, and the initial distance matrix and route matrix in the algorithm are generated according to the previous method.
clc,clear; %清空工作区和命令行窗口
D0=[]; %初始距离矩阵
P=[]; %路径矩阵
d=[];
m=size(D0);
for n=1:m %m次迭代
for i=1:m
for j=1:m
d(i,j)=D0(i,n)+D0(n,j);
if(d(i,j)<D0(i,j)) %判断值的大小
D0(i,j)=d(i,j);
P(i,j)=n;
end
end
end
end
4. About the shortest path between two points with more than one point
For example, the interval between 1 and 6 is 4, and neither 1 nor 6 is directly connected to 4. Then we will find the shortest path from 1 to 4 and 4 to 6, and decompose them in turn to get the shortest route.