haige 发表于 2009-5-23 14:23

迪杰斯特拉(Dijkstra)算法

带权图的最短路径问题的matlab函数代码
function y = shortest_path(i,j)
a=load('A.mat');
A=a.a;
N = length(A);                                   t
S = zeros(1,N);                                 
S(1) = i;                                       
dist = A(i,:);                                   
flag = 0;                                       
count = 1;                                       
while (flag~=1)
    = min(dist);               
    dist(i) = inf;                              
    if (position == j)
        flag = 1;
    else
        count = count + 1;
        S(count) = position;
        for o = 1:N
            dist(o) = min(dist(o),dist(position)+A(position,o));
        end
        dist(position) = inf;                    % point can't back to itself,so weight = inf
    end
end
y = value;

fantimond 发表于 2009-5-25 15:58

This Algorithm is designed to get one available shortest path vertex at every step.Then when it ends, it gave the shortest path value of every vertex from the source.
页: [1]
查看完整版本: 迪杰斯特拉(Dijkstra)算法