圖論之最短路03——任意指定點到所有其它點的最短路徑及距離

來源:互聯網
上載者:User

標籤:圖論   最短路演算法   路徑問題   任意指定點到所有其它點的最短路徑及距離   

========================================================
重要程度 *****
求任意點到其它點間最短距離及其路徑。
輸入:權值矩陣,起點
輸出如下:
      點i—>點j
      路徑  ¥¥¥
      距離  ¥¥¥

說明:必須調用E:\matlab M檔案\liangdianzuiduanlu.m,請查看網址:

http://blog.csdn.net/lzx19901012/article/details/47832213《圖論之最

短路02-1——任意兩點間最短距離及路徑》

========================================================
function yiduiduozuiduanlu(W)
qidian=input(‘請輸入起點:‘);
n=size(W,1);
D=zeros(1,n);
for zhongdian=1:n
    [p d]=liangdianzuiduanlu(W,qidian,zhongdian);
    Pm{zhongdian}=p;
    D(zhongdian)=d;
end
clc
fprintf(‘點%d到其他點的路徑和最短距離如下:‘,qidian)
for zhongdian=1:n
     fprintf(‘\n            點%d->點%d\n‘,qidian,zhongdian)
    disp(‘================================‘)
    lujing=Pm{zhongdian}
    juli=D(zhongdian)
    disp(‘================================‘)
end
================================================================
評:類似於C語言中的函數調用,本程式中調用了《liangdianzuiduanlu.m》,
調用時要求兩函數位於同一目錄下(可自訂)。

================================================================

例:求中點3到其他所有點的最短路徑及距離(想想消防路徑規劃時是不是可以用呢?)


解:

(1)寫權值矩陣

quanzhijuzhen=[ 0     2     8     1   Inf   Inf   Inf   Inf
     2     0     6   Inf     1   Inf   Inf   Inf
     8     6     0     7     5     1     2   Inf
     1   Inf     7     0   Inf   Inf     9   Inf
   Inf     1     5   Inf     0     3   Inf     8
   Inf   Inf     1   Inf     3     0     4     6
   Inf   Inf     2     9   Inf     4     0     3
   Inf   Inf   Inf   Inf     8     6     3     0]

(2)帶入程式(格式整理後輸出如下)

yiduiduozuiduanlu(quanzhijuzhen)

點3到其他點的路徑和最短距離如下:
                          點3->點1
================================
lujing =     3     6     5     2     1     1
juli =     7
================================


                         點3->點2
================================
lujing =     3     6     5     2     2
juli =     5
================================


                         點3->點3
================================
lujing =     3
juli =     0
================================


                         點3->點4
================================
lujing =     3     4
juli =     7
================================


                        點3->點5
================================
lujing =     3     6     5     5
juli =     4
================================


                          點3->點6
================================
lujing =     3     6
juli =     1
================================


                         點3->點7
================================
lujing =     3     7
juli =     2
================================


                        點3->點8
================================
lujing =     3     7     8
juli =     5
================================


著作權聲明:博主文章可以被非商用轉載,但請務必註明出處,因水平有限,難免出錯,在此免責。

圖論之最短路03——任意指定點到所有其它點的最短路徑及距離

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.