• 注册
当前位置:1313e > 默认分类 >正文

数学建模入门之图论dijkstra

程序名为:tulun1.m

% 本程序为主程序,请进行修改。
weight=[0     8   Inf   Inf   Inf   Inf     7     8   Inf   Inf   Inf;Inf     0     3   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf;Inf   Inf     0     5     6   Inf     5   Inf   Inf   Inf   Inf;Inf   Inf   Inf     0     1   Inf   Inf   Inf   Inf   Inf    12;Inf   Inf     6   Inf     0     2   Inf   Inf   Inf   Inf    10;Inf   Inf   Inf   Inf     2     0     9   Inf     3   Inf   Inf;Inf   Inf   Inf   Inf   Inf     9     0   Inf   Inf   Inf   Inf;8   Inf   Inf   Inf   Inf   Inf   Inf     0     9   Inf   Inf;Inf   Inf   Inf   Inf     7   Inf   Inf     9     0     2   Inf;Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf     2     0     2;Inf   Inf   Inf   Inf    10   Inf   Inf   Inf   Inf   Inf     0;];
% 我们修改的就是邻接矩阵的值
[dis, path]=dijkstra(weight,1, 11)
% 111,分别代表起始点,如果我们要求28的最短距离,则更改为2-8即可。

本程序名为dijkstra.m

% 求一个顶点到另一个定点的最短路径,实际上能求从出发点到其他所有节点的最短路径。
% 修改的是带权邻接矩阵:
% [0 1 3     v1:v1的距离是0,v1:v2的距离是1, v1:v3的距离是3,v2:v1的距离是1,v2:v2的距离是2
%   1 0 2   距离无穷的为Inf
%  1 1 0]
% 本程序为子程序,请找tulun1修改主程序。function [min,path]=dijkstra(w,start,terminal)
n=size(w,1); label(start)=0; f(start)=start;
for i=1:nif i~=startlabel(i)=inf;
end, end
s(1)=start; u=start;
while length(s)for i=1:nins=0;for j=1:length(s)if i==s(j)ins=1;end,  endif ins==0v=i;if label(v)>(label(u)+w(u,v))label(v)=(label(u)+w(u,v)); f(v)=u;end, end, end   
v1=0;k=inf;for i=1:nins=0;for j=1:length(s)if i==s(j)ins=1;end, endif ins==0v=i;if k>label(v)k=label(v);  v1=v;end,  end,  ends(length(s)+1)=v1;  u=v1;
end
min=label(terminal); path(1)=terminal;
i=1; 
while path(i)~=startpath(i+1)=f(path(i));i=i+1 ;
end
path(i)=start;
L=length(path);
path=path(L:-1:1);

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录