资源描述:
《图论matlab程序大全.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.....................最新资料整理推荐.....................图论程序大全程序一:可达矩阵算法functionP=dgraf(A)n=size(A,1);P=A;fori=2:nP=P+A^i;endP(P~=0)=1;P;程序二:关联矩阵和邻接矩阵互换算法functionW=incandadf(F,f)iff==0m=sum(sum(F))/2;n=size(F,1);W=zeros(n,m);k=1;fori=1:nforj=i:nifF(i,j)~=0W(i,k)=1;W(j,k)=1;
2、50.....................最新资料整理推荐.....................k=k+1;endendendelseiff==1m=size(F,2);n=size(F,1);W=zeros(n,n);fori=1:ma=find(F(:,i)~=0);W(a(1),a(2))=1;W(a(2),a(1))=1;endelsefprint('Pleaseimputtherightvalueoff');endW;程序三:有向图关联矩阵和邻接矩阵互换算法functionW=mattransf(F,f)iff=
3、=050.....................最新资料整理推荐.....................m=sum(sum(F));n=size(F,1);W=zeros(n,m);k=1;fori=1:nforj=i:nifF(i,j)~=0W(i,k)=1;W(j,k)=-1;k=k+1;endendendelseiff==1m=size(F,2);n=size(F,1);W=zeros(n,n);fori=1:ma=find(F(:,i)~=0);ifF(a(1),i)==1W(a(1),a(2))=1;else50..
4、...................最新资料整理推荐.....................W(a(2),a(1))=1;endendelsefprint('Pleaseimputtherightvalueoff');endW;第二讲:最短路问题程序一:Dijkstra算法(计算两点间的最短路)function[l,z]=Dijkstra(W)50.....................最新资料整理推荐.....................n=size(W,1);fori=1:nl(i)=W(1,i);z(i)=0;en
5、di=1;whilei<=nforj=1:nifl(i)>l(j)+W(j,i)l(i)=l(j)+W(j,i);z(i)=j-1;ifj6、fd(i,k)+d(k,j)U(i,m)+U(m,j)U(i,j)=U(i,m)+U(m,j);endend
7、endm=m+1;endu=U(k1,k2);P1=zeros(1,n);k=1;P1(k)=k2;V=ones(1,n)*inf;kk=k2;whilekk~=k1fori=1:nV(1,i)=U(k1,kk)-W(i,kk);ifV(1,i)==U(k1,i)P1(k+1)=i;kk=i;50.....................最新资料整理推荐.....................k=k+1;endendendk=1;wrow=find(P1~=0);forj=length(wrow):-1:1P(k)=P1(wro
8、w(j));k=k+1;endP;程序四、n1short.m(计算某点到其它所有点的最短距离)function[PmD]=n1short(W,k)n=size(W,1);D=zeros(1,n);fori=1:n[Pd]=n2short(W,k,i