数学建模(4)——图论(1)

数学建模(4)——图论(1)

1、图论

有点有线,点表示物,线表示两点的关系
此文章介绍求最短路径的模型
在这里插入图片描述

比如:v1,v2,v3…表示各个城市,之间的线上数字的大小表示两地点之间的距离。

2、Dijkstar算法

计算最短路径的算法

3、带权邻接矩阵

在这里插入图片描述
此为一个矩阵,11×11行列分别代表上图v1,v2,v3,v4…v11各个城市
矩阵数据怎么来的?做出解释:
如第一行一列,表示v1到v1的距离为0。一行二列,表示v1到v2的距离为2。
一行三列,表示v1到v3的距离为8。一行四列,表示v1到v4的距离为1。
一行五列表示v1到v5的距离为无穷大(因为没有直接去的路,此处标为无穷大)其它数据同上,,,

4、问题举例

题目:根据一开始的那个图,求从v1到v11的最短距离
在MATLAB中输入以下代码:
定义函数tulundijkstra:

function [min,path]=tulundijkstra(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);

之后可以定义一个脚本文件tulundijkstra1:

weight=    [0     2     8     1   Inf   Inf   Inf   Inf   Inf   Inf   Inf;
            2     0     6   Inf     1   Inf   Inf   Inf   Inf   Inf   Inf;
            8     6     0     7     5     1     2   Inf   Inf   Inf   Inf;
            1   Inf     7     0   Inf   Inf     9   Inf   Inf   Inf   Inf;
          Inf     1     5   Inf     0     3   Inf     2     9   Inf   Inf;
          Inf   Inf     1   Inf     3     0     4   Inf     6   Inf   Inf;
          Inf   Inf     2     9   Inf     4     0   Inf     3     1   Inf;
          Inf   Inf   Inf   Inf     2   Inf   Inf     0     7   Inf     9;
          Inf   Inf   Inf   Inf     9     6     3     7     0     1     2;
          Inf   Inf   Inf   Inf   Inf   Inf     1   Inf     1     0     4;
          Inf   Inf   Inf   Inf   Inf   Inf   Inf     9     2     4     0;];
[dis, path]=tulundijkstra(weight,1, 11)

执行脚本文件:
在这里插入图片描述
dis表示最短距离
path表示路径,从v1到v2到v5到…到v11最近。
其中脚本文件,weight矩阵可以根据具体情况改变,后边的(weight,1,11)1和11表示,起始地和终止地,也可根据情况改变。

在这里插入图片描述
求从2到10的最短路径?
此处应注意有的线段带有箭头,如1到7,则1到7的距离为7,但是7到1的距离就是无穷大,所以写带权邻接矩阵时要注意。
首先列些带权邻接矩阵:
在这里插入图片描述
在MATLAB中编写代码:

weight=   [0     8   Inf   Inf   Inf   Inf     7     8   Inf   Inf   Inf;
   Inf     0     3   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf;
   Inf   Inf     0     5     6   Inf     5   Inf   Inf   Inf   Inf;
   Inf   Inf   Inf     0     1   Inf   Inf   Inf   Inf   Inf    12;
   Inf   Inf     6   Inf     0     2   Inf   Inf   Inf   Inf    10;
   Inf   Inf   Inf   Inf     2     0     9   Inf     3   Inf   Inf;
   Inf   Inf   Inf   Inf   Inf     9     0   Inf   Inf   Inf   Inf;
     8   Inf   Inf   Inf   Inf   Inf   Inf     0     9   Inf   Inf;
   Inf   Inf   Inf   Inf     7   Inf   Inf     9     0     2   Inf;
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf     2     0     2;
   Inf   Inf   Inf   Inf    10   Inf   Inf   Inf   Inf   Inf     0;];

[dis, path]=tulundijkstra(weight,2, 10)

运行脚本文件得到:
在这里插入图片描述
所以从2到10最短路径为:2到3到5到6到9到10,总长为16

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值