Freud (Floyd) algorithm is an algorithm for finding the shortest path between vertices in a given weighted graph. The algorithm is named after one of the founders, 1978 Turing Award winner, and Stanford University professor of computer science Robert Floyd.
Basic ideas
by Floyd calculating the shortest path of each vertex in Figure g= (v,e), you need to introduce a matrix s, the element in Matrix S a[i][j] represents the distance from vertex i (vertex i) to Vertex J (J Vertex).
Assuming that the number of vertices in Figure g is n, you need to update the Matrix S n times. Initially, the distance from vertex A[i][j] in matrix S is the weight of vertex i to vertex j, or a[i][j]=∞ if I and J are not adjacent. Next, the matrix S is updated n times. On the 1th update, if the distance of "a[i][j" > "A[i][0]+a[0][j]" (a[i][0]+a[0][j) means "the distance between the 1th vertices between I and J"), then update a[i][j] is "a[i][0]+a[0][j". Similarly, on the K update, if "a[i][j" Distance ">" a[i][k]+a[k][j] ", then update a[i][j] to" a[i][k]+a[k][j ". After updating n times, the operation is complete!
The MATLAB code functions are as follows:
function [Dist,mypath]=myfloyd (a,sb,db);% input: A-adjacency matrix (AIJ) refers to the distance between I and J, which can be the marking of the sb-starting point of the forward direction; the marking of the db-end point% output: dist-shortest distance; mypath-Shortest Path N=size (a,1); Path=zeros (n); for I=1:nfor j=1:nif A (i,j) ~=infpath (i,j) =j; %j is the follow-up point of I endendendfor k=1:nfor i=1:nfor j=1:nif A (i,j) >a (i,k) +a (k,j) A (i,j) =a (i,k) +a (k,j);p ath (i,j) =path (i,k); Endendendenddist=a (sb,db); MYPATH=SB; T=sb;while T~=dbtemp=path (t,db); Mypath=[mypath,temp];t=temp;endreturn
Realization of multi-source shortest path Floyd algorithm ———— matlab