functionw=incandadf(F,f) if f==0 m=sum(sum(F))/2; n=size(F,1); w=zeros(n,m); k=1; fori=1:n forj=i:n if F(i,j)~=0 w(i,k)=1; w(j,k)=1; k=k+1; end end end elseif f==1 m=size(F,2); n=size(F,1); w=zeros(n,n); fori=1:m a=find(F(:,i)~=0); w(a(1),a(2))=1; w(a(2),a(1))=1; end else fprint('please input the right value of f'); end w;
functionw=mattransf(F,f) if f==0 m=sum(sum(F)); n=size(F,1); w=zeros(n,m); k=1; fori=1:n forj=i:n if F(i,j)~=0 w(i,k)=1; w(j,k)=-1; k=k+1; end end end elseif f==1 m=size(F,2); n=size(F,1); w=zeros(n,n); fori=1:m a=find(F(:,i)~=0); if F(a(1),i)==1 w(a(1),a(2))=1; else w(a(2),a(1))=1; end end else fprint('please input the right value of f'); end w;
四.最短路
4.1 Dijkstra(两点间距离)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
function[l,z]=Dijkstra(w) n=size(w,1); fori=1:n l(i)=w(1,i); z(i)=0; end i=1; whilei<=n forj=1:n if l(i)>l(j)+w(j,i) l(i)=l(j)+w(j,i); z(i)=j-1; ifj<i i=j-1; end end end i=i+1; end
4.2 Floyd(任意两点间距离)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
function[d,r]=floyd(a) n=size(a,1); d=a; fori=1:n forj=1:n r(i,j)=j; end end r; for k=1:n fori=1:n forj=1:n if d(i,k)+d(k,j)<d(i,j) d(i,j)=d(i,k)+d(k,j); r(i,j)=r(i,k); end end end end