最短路径Dijkstra matlab

Dijkstra:

function  [dist,pre, full_path]=MinRoad_Dijkstra(G,v0)
n=0;
if  isfield(G,'w')  && ~isempty(G.w)  &&  size(G.w,1)==size(G.w,2)
    W=G.w;n=size(W,1);
end

dist=W(v0,:);
pre=v0*ones(1,n);
full_path=cell(1,n);
for i=1:n
    if i~=v0
        full_path{i}=v0;
    end
end
for k=2:n-1
    for i=1:n  
        for j=1:n
            if (j==i) continue; end
            d=dist(j)+W(j,i);
            if d<dist(i)
                dist(i)=d;
                pre(i)=j;
                full_path{i}=[full_path{j}  j];
            end
        end 
    end
end

  

猜你喜欢

转载自www.cnblogs.com/wander-clouds/p/9351178.html
今日推荐