




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、最短路径问题的实现4.1 Dijkstra 数组实现代码主程序部分close all ;clear all ;clc;W=zeros(5);index=1,2,3,4,5;W(1,2)=10;W(1,4)=30;W(1,5)=100;W(2,3)=50;W(3,5)=10;W(4,2)=10;W(4,3)=20;W(4,5)=60;for i=1:5for j=1:5if i=j && W(i,j)=0 W(i,j)=inf;endstart=1;S=start,zeros(1,4);D=index;W(S(1),:);for i=1:4 mid_1,min_1 =Get_mi
2、n( S,D );S(1,i+1)=mid_1;mid_D=index;W(mid_1,:);D=Get_new(S,mid_D,D,min_1,index);end% S=1,1,1,2,3,4,4,4;% E=2,4,5,3,5,2,3,5;% W=10,30,100,50,10,10,20,60;% G(9,9)=0;% G=sparse(S,E,W); % G(5,5)=0;% P=biograph(G,'ShowWeights','on');% H=view(P);% set(H.Nodes(Path),'Color',1 0.4 0.
3、4);%edges=getedgesbynodeid(H,get(H.Nodes(Path),'ID')% set(edges,'LineColor',1 0 0);% set(edges,'LineWidth',2.0);函数部分function mid_index,min_D =Get_min( S,D)S=S(S=0);mid_D=D;mid_D(:,S)=;min_D=min(mid_D(2,:);,mid_index=find(D=min_D);mid_index=min(mid_index);endfunction D = Get_n
4、ew(S,mid_D,D,min_1,index)W=mid_D;S=S(S=0);col=setdiff(index,S);W(2,:)=W(2,:)+min_1;for i=1:colif W(2,i)<D(2,i)D(2,i)=W(2,i);endend4.2 Dijkstra 堆实现代码主程序部分close all ;clear all ;clc;S=1 1 2 2 3 3 4 4 4 4 5 6 6 7 8;% startE=2 3 5 4 4 6 5 7 8 6 7 8 9 9 9;% endW=17 20 12 60 23 45 44 15 77 21 17 17 15 3
5、710;%G(9,9)=0;G=sparse(S,E,W);G(9,9)=0;P=biograph(G, 'ShowWeights' , 'on' );H=view(P);Dist=cell(9,1);Path=cell(9,1);i=1;for j=1:9Distj,Pathj=graphshortestpath(G,i,j,'Method' , 'Dijkstra' );end% set(H.Nodes(Path),'Color',125 125 125/255);%edges=getedgesbynodeid
6、(H,get(H.Nodes(Path),'ID');% set(edges,'LineColor',1 0 0);% set(edges,'LineWidth',2.0);函数部分function dist,path,pred =graphshortestpath(G,S,varargin)algorithms = 'bfs' , 'dijkstra' , 'bellman-ford' , 'acyclic' ; algorithmkeys = 'spb' ,
7、39;dij' , 'bel' , 'spa' ;debug_level = 0;% set defaults of optional input argumentsD = 1:length(G); % will return shortest path to all other nodesW = ;% no custom weightsalgorithm = 2;% defaults to dijkstradirected = true;% find out signature of input argumentsif nargin>2 &
8、;& isnumeric(varargin1)D = varargin1;varargin(1) = ;end% read in optional PV input argumentsnvarargin = numel(varargin);if nvararginif rem(nvarargin,2) = 1error(message( 'bioinfo:graphshortestpath:IncorrectNumberOfArguments' , mfilename);endokargs = 'method' , 'directed'
9、, 'weights' ; for j=1:2:nvarargin-1pname = vararginj;pval = vararginj+1;k =find(strncmpi(pname,okargs,numel(pname);if isempty(k)error(message( 'bioinfo:graphshortestpath:UnknownParameterName', pname);elseiflength(k)>1error(message('bioinfo:graphshortestpath:AmbiguousParameterN
10、ame', pname);elseswitch (k)case 1 % 'method'algorithm =find(strncmpi(pval,algorithms,numel(pval);if isempty(algorithm)error(message('bioinfo:graphshortestpath:NotValidMethod' , pval)elseif numel(algorithm)>1error(message('bioinfo:graphshortestpath:Ambiguou sMethod' , p
11、val)endcase 2 % 'directed'directed =bioinfoprivate.opttf(pval,okargsk,mfilename);case 3 % 'weights'W = pval(:);endendendend% call the mex implementation of the graph algorithmsif nargout>1if isempty(W)dist,pred =graphalgs(algorithmkeysalgorithm,debug_level,di rected,G,S);elsedist,
12、pred =graphalgs(algorithmkeysalgorithm,debug_level,di rected,G,S,W);endelseif isempty(W)dist =graphalgs(algorithmkeysalgorithm,debug_level,di rected,G,S);elsedist =graphalgs(algorithmkeysalgorithm,debug_level,di rected,G,S,W);endend dist = dist(D);% calculate pathsif nargout>1path = graphpred2path(pred,D);end结果示例:随机生成的有向交通图:val =(1,(2) 17(1,(3) 20(1,(4) 60(3,4)23(1,(5) 12(4,(5) 44(3,6
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工程委托授权协议书
- 糕点配方协议书范本
- 毕业协议书寄回学校
- 公司奖励入股协议书
- 短期民间借款协议书
- 2024-2025学年下学期初中历史八年级第二单元A卷
- 2024年五月份敕勒歌战车减震材料研发实验
- 工厂食品安全培训
- 2022-2024年中考化学试题分类汇编:信息给予题(解析版)
- 内脏动脉瘤的健康宣教
- 计算机系毕业论文
- JJG 814-2015自动电位滴定仪
- 部编版二年级下册语文课件小企鹅心灵成长故事
- FZ/T 07019-2021针织印染面料单位产品能源消耗限额
- 初中生职业生涯规划课件两篇
- 低利率时代家庭财富管理课件
- 北京七年级下学期生物期中考试试卷
- 拖欠房租起诉书【5篇】
- 护理人员仪容仪表及行为规范
- 汽车品牌马自达课件
- 第六章广播电视的传播符号
评论
0/150
提交评论