路径及方案选择
tulun2.m
a= [ 0,50,inf,40,25,10;
50,0,15,20,inf,25;
inf,15,0,10,20,inf;
40,20,10,0,10,25;
25,inf,20,10,0,55;
10,25,inf,25,55,0];
[D, path]=floyd(a)
floyd.m
function [D,path,min1,path1]=floyd(a,start,terminal)
D=a;n=size(D,1);path=zeros(n,n);
for i=1:n
for j=1:n
if D(i,j)~=inf
path(i,j)=j;
end,
end,
end
for k=1:n
for i=1:n
for j=1:n
if D(i,k)+D(k,j)<D(i,j)
D(i,j)=D(i,k)+D(k,j);
path(i,j)=path(i,k);
end,
end,
end,
end
if nargin==3
min1=D(start,terminal);
m(1)=start;
i=1;
path1=[ ];
while path(m(i),terminal)~=terminal
k=i+1;
m(k)=path(m(i),terminal);
i=i+1;
end
m(i+1)=terminal;
path1=m;
end