暴强Dijkstra算法求任意两点间最短路径(matlab程序)_第1页
暴强Dijkstra算法求任意两点间最短路径(matlab程序)_第2页
暴强Dijkstra算法求任意两点间最短路径(matlab程序)_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精品好资料学习推荐效果展示:开头输入的是点的序列号(表示第几个点),显示的是最短路径的走法(同样以点的序列号显示,表示途径的第几个点)。%编写m文件function distance,path=dijkstra(A,s,e)% DISTANCE,PATH=DIJKSTRA(A,S,E)% returns the distance and path between the start node and the end node.% A: adjcent matrix% s: start node% e: end node% initializen=size(A,1); % node number

2、D=A(s,:); % distance vectorpath=; % path vectorvisit=ones(1,n); % node visibilityvisit(s)=0; % source node is unvisibleparent=zeros(1,n); % parent node% the shortest distancefor i=1:n-1 % BlueSet has n-1 nodes temp=zeros(1,n); count=0; for j=1:n if visit(j) temp=temp(1:count) D(j); else temp=temp(1:

3、count) inf; end count=count+1; end value,index=min(temp); j=index; visit(j)=0; for k=1:n if D(k)D(j)+A(j,k) D(k)=D(j)+A(j,k); parent(k)=j; end endenddistance=D(e);% the shortest distance pathif parent(e)=0 return;endpath=zeros(1,2*n); % path preallocationt=e; path(1)=t; count=1;while t=s & t0 p=pare

4、nt(t); path=p path(1:count); t=p; count=count+1;endif count=2*n error(The path preallocation length is too short.,. Please redefine path preallocation parameter.);endpath(1)=s;path=path(1:count);%算法实现clc; clear; close all;% 载入设置数据lines = load(Distance.txt);%点与点之间的距离矩阵A=lines;A(find(A10)=inf; %对步长的限制

5、,根据自己的要求决定!我们在此选择10.% A就是连接矩阵,其中对角线为0,表示本身% 有连接关系的就对应线的长度% 没有连接关系的就对应inf% 下面的是dijstra算法,有两种方式可以调用s =input(输入起点); % 起点(点的序号)e =input(输入终点); % 终点(点的序号)distance,path0 = dijkstra(A,s,e);fprintf(n Use Dijkstra the Min Distance is: %.5f n, distance);fprintf(n Use Dijkstra the Min Distance path is: n);disp(path0);A1 = A;A1(isinf(A1) = 0;d, p, pred = graphshortestpath(sparse(A1), s, e);fprintf(n Use graphshortestpath the Min Distance is: %.5f n, d);fprintf(n Use graphshortestpath the Min Distance path is: n);disp(p);for i = 1 : length(path0)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论