MATLAB求最短路怎样编程,来发个精品~matlab实现最短路

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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 numberD=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: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

end

end

distance=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 && t>0

p=parent(t);

path=[p path(1:count)];

t=p; count=count+1;

end

if count>=2*n,

error(['The path preallocation length is too short.',... 'Please redefine path preallocation parameter.']);

end

path(1)=s;

path=path(1:count);

end

这个算第一篇精品吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值