Matlab shortest path problem in graph theory model code

The basic contents of Shortest Path Problem

Research on the shortest path is a network diagram between the dots formed by the connection, the corresponding path given a certain weight (may be understood as the distance between two points), and calculates how to walk between any two points, path the shortest problem. In the distance here it can be interpreted as some kind of overhead tasks between various points.

image

Network Diagram


Model calls

Solve shortest path problem, and can be taken dijkstraor floydtwo models, call the model form as follows:

[mydist,mypath]=mydijkstra(a,sb,db) % dijkstra模型
[mydist,mypath]=myfloyd(a,sb,db) % floyd模型

among them,

  • a is the adjacency matrix
  • sb as a starting point numbers
  • db to end label
  • mydist shortest path length
  • mypath shortest path


Model complete code

Dijkstra model code

function [mydistance,mypath]=mydijkstra(a,sb,db);
% 输入:a—邻接矩阵,a(i,j)是指i到j之间的距离,可以是有向的
% sb—起点的标号, db—终点的标号
% 输出:mydistance—最短路的距离, mypath—最短路的路径
n=size(a,1); visited(1:n) = 0;
distance(1:n) = inf; distance(sb) = 0; %起点到各顶点距离的初始化
visited(sb)=1; u=sb;  %u为最新的P标号顶点
parent(1:n) = 0; %前驱顶点的初始化
for i = 1: n-1
     id=find(visited==0); %查找未标号的顶点
     for v = id           
         if  a(u, v) + distance(u) < distance(v)
             distance(v) = distance(u) + a(u, v);  %修改标号值 
             parent(v) = u;                                    
         end            
     end
     temp=distance;
     temp(visited==1)=inf;  %已标号点的距离换成无穷
     [t, u] = min(temp);  %找标号值最小的顶点 
     visited(u) = 1;       %标记已经标号的顶点
 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);

Floyd model code

function [dist,mypath]=myfloyd(a,sb,db);
% 输入:a—邻接矩阵,元素(aij)是顶点i到j之间的直达距离,可以是有向的
% sb—起点的标号;db—终点的标号
% 输出:dist—最短路的距离;% mypath—最短路的路径
n=size(a,1); path=zeros(n);
for k=1:n
    for i=1:n
        for j=1:n
            if a(i,j)>a(i,k)+a(k,j)
                a(i,j)=a(i,k)+a(k,j);
                path(i,j)=k;
            end
        end
    end
end
dist=a(sb,db);
parent=path(sb,:); %从起点sb到终点db的最短路上各顶点的前驱顶点
parent(parent==0)=sb; %path中的分量为0,表示该顶点的前驱是起点
mypath=db; t=db;
while t~=sb
        p=parent(t); mypath=[p,mypath];
        t=p;
end


Case presentation

For the above network diagram, the shortest path from A to D.

image

Finishing adjacency matrix

First sort out the point of connection between the point and the relationship come to an adjacency matrix.

Sort assume the point is:

Points A B1 B2 C1 C2 C3 D
No. 1 2 3 4 5 6 7

Sorting out the 7 * 7 adjacency matrix:

image

The complete code

% 构造邻接矩阵
a = zeros(7);
a(1,2) = 2; a(1,3) = 4;
a(2,4) = 3; a(2,5) = 3; a(2,6) = 1;
a(3,4) = 2; a(3,5) = 3; a(3,6) = 1;
a(4,7) = 1;
a(5,7) = 3;
a(6,7) = 4;
a = a + a';
a(a==0) = inf; % 零元素换成inf
a(eye(7,7)==1)=0; % 对角线换成 0 

[mydist1,mypath1]=mydijkstra(a,1,7) % dijkstra模型求解
[mydist2,mypath2]=myfloyd(a,1,7) % floyd 模型求解

operation result

mydist1 =

     6


mypath1 =

     1     2     4     7


mydist2 =

     6


mypath2 =

     1     2     4     7

Reduced to the point number, i.e. the shortest path A → B1 → C1 → D

Guess you like

Origin www.cnblogs.com/gshang/p/11512005.html