QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2020|回复: 0
打印 上一主题 下一主题

数学建模入门之图论dijkstra

[复制链接]
字体大小: 正常 放大
浅夏110 实名认证       

542

主题

15

听众

1万

积分

  • TA的每日心情
    开心
    2020-11-14 17:15
  • 签到天数: 74 天

    [LV.6]常住居民II

    邮箱绑定达人

    群组2019美赛冲刺课程

    群组站长地区赛培训

    群组2019考研数学 桃子老师

    群组2018教师培训(呼伦贝

    群组2019考研数学 站长系列

    跳转到指定楼层
    1#
    发表于 2018-11-2 08:57 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta |邮箱已经成功绑定
    [color=rgba(0, 0, 0, 0.75)]数学建模入门之图论dijkstra2 j3 K$ G4 i5 _

    " ?2 J! M. u) l9 b) Z
    2 R3 ~" |; x/ \7 d; I
    # j1 P8 ^/ z+ w4 v& r
    / }  w9 B$ @5 Q* D

    程序名为:tulun1.m

    % 本程序为主程序,请进行修改。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]=dijkstra(weight,1, 11)% 111,分别代表起始点,如果我们要求28的最短距离,则更改为2-8即可。
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
      - u! V7 q) E" ]8 f

    本程序名为dijkstra.m

    % 求一个顶点到另一个定点的最短路径,实际上能求从出发点到其他所有节点的最短路径。% 修改的是带权邻接矩阵:% [0 1 3     v1:v1的距离是0,v1:v2的距离是1, v1:v3的距离是3,v2:v1的距离是1,v2:v2的距离是2%   1 0 2   距离无穷的为Inf% 本程序为子程序,请找tulun1修改主程序。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, ends(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;endmin=label(terminal); path(1)=terminal;i=1; while path(i)~=start      path(i+1)=f(path(i));      i=i+1 ;endpath(i)=start;L=length(path);path=path(L:-1:1);
    ; ^" e& q! r( N

    ' V# R% C& m  U- [$ L+ u6 T& g: J% m( ~' @0 ?9 [9 K: Y
    & m' x; M9 Q% P0 O: a" v
    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-4-14 05:00 , Processed in 0.397300 second(s), 51 queries .

    回顶部