暴强Dijkstra算法求任意两点间最短路径(matlab程序).doc

暴强Dijkstra算法求任意两点间最短路径(matlab程序).doc

ID:52762053

大小:56.50 KB

页数:3页

时间:2020-03-30

暴强Dijkstra算法求任意两点间最短路径(matlab程序).doc_第1页
暴强Dijkstra算法求任意两点间最短路径(matlab程序).doc_第2页
暴强Dijkstra算法求任意两点间最短路径(matlab程序).doc_第3页
资源描述:

《暴强Dijkstra算法求任意两点间最短路径(matlab程序).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、效果展示:开头输入的是点的序列号(表示第几个点),显示的是最短路径的走法(同样以点的序列号显示,表示途径的第几个点)。%编写m文件function[distance,path]=dijkstra(A,s,e)%[DISTANCE,PATH]=DIJKSTRA(A,S,E)%returnsthedistanceandpathbetweenthestartnodeandtheendnode.%%A:adjcentmatrix%s:startnode%e:endnode%initializen=size(A,1);%nodenumberD=A(s,

2、:);%distancevectorpath=[];%pathvectorvisit=ones(1,n);%nodevisibilityvisit(s)=0;%sourcenodeisunvisibleparent=zeros(1,n);%parentnode%theshortestdistancefori=1:n-1%BlueSethasn-1nodestemp=zeros(1,n);count=0;forj=1:nifvisit(j)temp=[temp(1:count)D(j)];elsetemp=[temp(1:count)inf];

3、endcount=count+1;end[value,index]=min(temp);j=index;visit(j)=0;fork=1:nifD(k)>D(j)+A(j,k)D(k)=D(j)+A(j,k);parent(k)=j;endendenddistance=D(e);%theshortestdistancepathifparent(e)==0return;endpath=zeros(1,2*n);%pathpreallocationt=e;path(1)=t;count=1;whilet~=s&&t>0p=parent(t);p

4、ath=[ppath(1:count)];t=p;count=count+1;endifcount>=2*nerror(['Thepathpreallocationlengthistooshort.',...'Pleaseredefinepathpreallocationparameter.']);endpath(1)=s;path=path(1:count);%算法实现clc;clear;closeall;%%载入设置数据lines=load('Distance.txt');%点与点之间的距离矩阵A=lines;A(find(A>10))=

5、inf;%对步长的限制,根据自己的要求决定!我们在此选择10.%A就是连接矩阵,其中对角线为0,表示本身%有连接关系的就对应线的长度%没有连接关系的就对应inf%%下面的是dijstra算法,有两种方式可以调用s=input('输入起点');%起点(点的序号)e=input('输入终点');%终点(点的序号)[distance,path0]=dijkstra(A,s,e);fprintf('UseDijkstratheMinDistanceis:%.5f',distance);fprintf('UseDijkstratheMinD

6、istancepathis:');disp(path0);A1=A;A1(isinf(A1))=0;[d,p,pred]=graphshortestpath(sparse(A1),s,e);fprintf('UsegraphshortestpaththeMinDistanceis:%.5f',d);fprintf('UsegraphshortestpaththeMinDistancepathis:');disp(p);fori=1:length(path0)ifi==length(path0)temp=[path0(1)p

7、ath0(i)];elsetemp=[path0(i)path0(i+1)];endend

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。