该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
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
这个算第一篇精品吧