The principle of the shortest path Dijkstra algorithm is popular and detailed and the Matlab code analysis

1. Dijkstra algorithm: a short path between two specified vertices

      The adjacency matrix between different points is an undirected graph, a(i,j) represents the distance from point i to point j, if two points are not connected, it is represented by infinity. The specific example is shown in the following table

       

       According to my understanding, the principle of the algorithm is to convert the search (complexity n\times n\times n\times n\cdots n=n^{n}) between n nodes into the search (complexity ) between two sets of n nodes (search set and unsearched set). The n\times nspecific process: from the starting point At the beginning, the strategy of greedy algorithm is adopted, and each time it traverses to the adjacent node of the vertex that is closest to the starting point and has not been visited, until it extends to the end point.

       To be more specific as shown below (different from the program case), find all the shortest routes from node 1 to node 6

         Dijkstra algorithm runs the animation process

       1. Start searching from node 1, put node 1 into the search set P, and put the remaining nodes 2, 3, 4, 5, 6 into the unsearched set W

         

       2. Find the set of W is not found in the set P 1 adjacent to the set from the nearest node (distance can be seen from the figure, six nodes corresponding to 7,9 \infty, , \infty14 7 is the smallest, join node 2, update nodes 3, 4), add the nearest node 2 to P, and update the distance from node 1 to each node. Here, update the shortest distance between nodes 3 and 4 connected to 2:

         Since the shortest distance of node 3: 1→2 is 7. Starting from node 2, the shortest distance from 2→3 is 10. Therefore, the shortest path between nodes 1 to 3 is a comparison of the old and new paths.

          1→2→3: the original shortest distance , 1→3: 9; Step 2 generates the shortest distance of the new path 7+10=17; therefore, the minimum value is 9

         Since the shortest distance of node 4:1→2 is 7. Starting from node 2, the shortest distance from 2→4 is 15. Therefore, the shortest path between nodes 1 to 4 is a comparison of the old and new paths.

          1→2→3: the original shortest distance , 1→4 \infty:; Step 2 generates the shortest distance of the new path 7+15=22; therefore, the minimum value is 22

         

        3. Repeat step 2 until the end (from the above figure, you can see that the distances corresponding \inftyto nodes 3, 4, 5, and 6 are the smallest of 9, 22 , and 14, add node 3, update 4 and 6)

          

         (From the above figure, we can see \inftythat 14 of 20, 14 is the smallest, add node 6, update 5)

          

         (From the above figure, we can see that 20 is the smallest among 20 and 23. Add node 4 and update 5)

         

          Finally, 5 is added. At this time, the collection element not found is 0, and the end!

Two, Matlab code and analysis

      Main function:

        The main function constructs the adjacency matrix. First, find the distance from node 1 to 2, 3, 4, 5, and 6, then find the distance from node 2 to 3, 4, 5, 6, and then down to node 6. Execute the sub-function dijkstra to find the shortest path from node 1 to node 4 and output

clc;
close all
clear all
a=zeros(6); a(1,2)=50;a(1,4)=40;a(1,5)=25;a(1,6)=10; a(2,3)=15;
a(2,4)=20;a(2,6)=25; a(3,4)=10;a(3,5)=20; a(4,5)=10;a(4,6)=25; 
a(5,6)=55; 
a=a+a'; 
a(a==0)=inf; 
[distance,path]=dijkstra(a,1,4);
p=num2str(path);
sprintf('最短距离为%d\n具体路线为%s。',distance,p)

        Sub function:

function [mydistance,mypath]=dijkstra(a,sb,db);
% 输入:a—邻接矩阵(aij)是指i到j之间的距离,无向矩阵
% sb—起点的标号, db—终点的标号
% 输出:mydistance—短路的距离, mypath—短路的路径
n=size(a,1);         %找出节点个数n
visited(1:n)=0;      %构建初步的查找矩阵visited,0代表未查找,1代表查找过
distance(1:n)=inf;   %保存起点到各顶点的短距离
distance(sb)=0;      %从起点sb到起点的距离为0
parent(1:n)=0;       
for i=1:n-1                   %循环查找n-1次   
    temp=distance;            %将distance中的值赋给temp,避免运算时丢失原值
    id1=find(visited==1);     %查找已经标号的点
    temp(id1)=inf;            %已标号点的距离换成无穷
    [t,u]=min(temp);          %找标号值小的顶点
    visited(u)=1;             %标记已经标号的顶点
    id2=find(visited==0);     %查找未标号的顶点
    for v=id2                 
        if  a(u,v)+distance(u)<distance(v)
            distance(v)=distance(u)+a(u,v);  %修改标号值
            parent(v)=u;
        end
    end
end
mypath=[];
if parent(db)~=0   %如果存在路!
    t=db;
    mypath=[db];
    while t~=sb
        p=parent(t);
        mypath=[p mypath];
        t=p;
    end
end
mydistance=distance(db);
return

The code is a case in the book, see Jiang Qiyuan for details. Mathematical Model (Second Edition)[M]// Mathematical Model (Second Edition). Higher Education Press, 1987.

Guess you like

Origin blog.csdn.net/weixin_41971010/article/details/107666810