2、==min(d(tb)));temp=tb(tmpb(1));pb(temp)=1;index1=[index1,temp];index=index1(find(d(index1)==d(temp)-a(temp,index1)));iflength(index)>=2index=index(1);endindex2(temp)=index;endd;index1;index2;求任意两点间最短路的Floyd算法的Matlab程序function[D,R]=floyd(a)n=size(a,1);D
3、=a;fori=1:nforj=1:nR(i,j)=j;endendfork=1:nfori=1:nforj=1:nifD(i,k)+D(k,j)1returnendn=max(max(E(:,1:2)));
4、m=size(E,1);fori=1:nb(i)=0;forj=1:mifE(j,1)==i
5、E(j,2)==ib(i)=b(i)+1;endendendrp=rem(b,2);srp=sum(rp);switchsrpcase0,eu=1;case2,eu=0.5;otherwise,returnendifsrp==0v1=1;elsev1=find(rp);v1=v1(1);endvc=v1;m=size(E,1);E1=[E(:,1:2),[1:m]'];while~isempty(E1)ev
6、c=find((E1(:,1)==vc)
7、(E1(:,2)==vc));levc=length(evc);iflevc==1cEu=[cEu;E1(evc,3)];vcold=vc;vc=sum(E1(evc,1:2))-vc;E1=E1(setdiff([1:size(E1,1)],evc),:);E2=E1(:,1:2);E2gv=E2>vcold;E2(E2gv)=E2(E2gv)-1;E1(:,1:2)=E2;ifvc>vcoldvc=vc-1;endifv1>vcoldv1=v1-1;en
8、delsefork=1:levcE2=E1(setdiff([1:size(E1,1)],evc(k)),:);ncv=arComp(E2);nco=max(ncv);if(max(ncv)==1)cEu=[cEu;E1(evc(k),3)];vc=sum(E1(evc(k),1:2))-vc;E1=E2;break;endendendendreturn求最小生成树的Prim算法的Matlab程序function[Te]=prim(a)T=[];e=0;v=1;n=size(a,1);c=2:n;f
9、orj=2:nb(1,j-1)=1;b(2,j-1)=j;b(3,j-1)=a(1,j);endwhilesize(T,2)10、Matlab程序function[Tc]=kruskal(a)n=size(a,1);m=0;fori=1:n-1forj=i+1:nifa(i,j)>0&a(i,j)