Matlab打卡第五天——图的最短路径

目录

一、图的概念

二、图的建立

三、图的最短路径

        Ⅰ、有向图

                方法一:每对点的最短路径

                方法二:图论算法

        Ⅱ、无向图

                方法一:每对点的最短路径

                方法二:shortestpath函数

四、总结


一、图的概念

        ①有向图:图的边有方向,只能从箭头方向从一点到另一点。

         ②无向图:图的边无方向,可以双向

        ③权值,边的费用,可以形象的理解为边的长度

二、图的建立

        一般来说我们可以使用矩阵来储存图,这里我们先用邻接矩阵储存,再转化为稀疏矩阵来创建图。我们可以使用sparse函数建立图:

G=sparse(w);
%w为路径
或
G=sparse(i,j,v)
%i为起点,j为终点,v为权值

clc;
clear;
w=zeros(6,6);
w(1,2)=8;w(2,6)=5;#1
w(2,5)=6;w(5,4)=9;
w(4,3)=3;
G=sparse(w)#2

 代码分析

#1 w(起点,终点)=权值;

#2 函数sparse为稀疏矩阵创建函数

#3 特别的,这里是一个无向图,我们应该还要写上比如w(2,1)=8,w(6,2)=5这样的才符合双向路径,但其实邻接矩阵是一个对称的图,在Matlab里我们这需要使用矩阵的下三角元素就可以。

运行结果

另一种方法可以得到相同效果。

clc;
clear;
w=zeros(6,6);
G=sparse([1 2 2 5 4],[2 5 6 4 3],[8 6 5 9 3])

三、图的最短路径

        Ⅰ、有向图

                方法一:每对点的最短路径

        因为时候我们的起点到终点并不是直接连接的,其中有可能会有多个中转站,所以我们需要找出其最短路径。

这里会用到函数graphallshortestpaths

[dist] = graphallshortestpaths(G)
[dist] = graphallshortestpaths(G, ...'Directed', DirectedValue, ...)
[dist] = graphallshortestpaths(G, ...'Weights', WeightsValue, ...)

参数:

1.G为邻接矩阵

2.Directed为指示图形是有向还是无向的属性

3.DirectedValue值为0为无向,值为1为有向,忽略时默认为有向

首先创建一个有向图

clear all
clc
G = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],[41 99 51 32 15 45 38 32 36 29 21])
view(biograph(G,[],'ShowWeights','on'))%将创建的图可视化

 使用函数graphallshortestpaths

clear all
clc
G = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],[41 99 51 32 15 45 38 32 36 29 21])
juz=graphallshortestpaths(G)

我们来分析这个结果,136代表1到2的最短路径值,94代表3到2的最短路径值,以此类推。

                方法二:图论算法

其实对于图的最短路径我们有很多的算法。比如Floyed算法、 Dijkstra算法、Ford算法、SPFA算法等等。

这里我们使用Dijkstra算法来实现。

首先编写Dijkstra函数

function [min,path]=dijkstra(w,start,terminal)
n=size(w,1); label(start)=0; f(start)=start;
for i=1:n
   if i~=start
       label(i)=inf;
end, end
s(1)=start; u=start;
while length(s)<n
   for i=1:n
      ins=0;
      for j=1:length(s)
         if i==s(j)
            ins=1;
         end
      end
      if ins==0
         v=i;
         if label(v)>(label(u)+w(u,v))
            label(v)=(label(u)+w(u,v)); 
         f(v)=u;
         end 
      end
   end   
v1=0;
   k=inf;
   for i=1:n
         ins=0;
         for j=1:length(s)
            if i==s(j)
               ins=1;
            end
         end
         if ins==0
            v=i;
            if k>label(v)
               k=label(v);  v1=v;
            end
         end
   end
   s(length(s)+1)=v1;  
   u=v1;
end
min=label(terminal); path(1)=terminal;
i=1; 
while path(i)~=start
      path(i+1)=f(path(i));
      i=i+1 ;
end
path(i)=start;
L=length(path);
path=path(L:-1:1);

当我们使用算法来实现是需要用邻接矩阵来储存图

% 构造邻接矩阵
a = zeros(6);
a = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],[41 99 51 32 15 45 38 32 36 29 21]);
a = a + a';
a(a==0) = inf; % 零元素换成inf
a(eye(6,6)==1)=0; % 对角线换成 0 
[min,path]=dijkstra(a,2,4) % dijkstra模型求解2到4最短路径

min代表最短路径值为66

path为路径2——3——4

        Ⅱ、无向图

                方法一:每对点的最短路径

        使用函数graphallshortestpaths

clear all
clc
G = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],[41 99 51 32 15 45 38 32 36 29 21]);
UG = tril(G + G')%构造邻接矩阵;
graphallshortestpaths(UG,'directed',0)

                方法二:shortestpath函数

P = shortestpath(G,s,t)

计算从源节点 s 处开始到目标节点 t 处结束的最短路径

clc
clear all
G = zeros(6);
G = graph([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],[41 99 51 32 15 45 38 32 36 29 21])
[P,d] = shortestpath(G,1,6)

 P:路径1——5——3——6

d:最短路径值:82

四、总结

        ①最短路径问题是经典图论问题,如果对其算法感兴趣可以去百度一它的一些算法。

        ②最短路径问题的基本思想其实是动态规划。

        ③函数graphallshortestpaths在2021的Matlab中被弃用。

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值