Dijkstra最短路径算法的优化和改进

改进Dijkstra算法II的MATLAB程序实现如下:

function a=dij2_m(a)

n=length(a);

for i=2:n

    for j=1:(i-1)

        a(i,j)=a(j,i);

    end

end

for k=1:(n-1)

    b=[1:(k-1),(k+1):n];

    kk=length(b);

    a_id=k;

    b1=[(k+1):n];

    kk1=length(b1);

    while kk>0

        for j=1:kk1

            te=a(k,a_id)+a(a_id,b1(j));

            if te

                a(k,bi(j))=te;

            end

        end

        miid=1;

        for j=2:kk

            if a(k,b(j))

                miid=j;

            end

        end

        a_id=b(miid);

        b=[b(1:(miid1-1)),b((miid1+1):kk];

            kk=length(b);

            if a_id>k

            miid1=find(b1==a_id);

            b1=[b1(1:(miid1-1))b1((miid1+1):kk1)];

            kk1=length(b1);

            end

    end

    for j=(k+1):n

        a(j,k)=a(k,j);

    end

end

Dijkstra最短路径算法的优化和改进_第1张图片

 

你可能感兴趣的:(MATLAB,板块5:网络通信,板块7:优化类问题)