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

========================================================
重要程度 *****
求任意點到其它點間最短距離及其路徑。
輸入:權值矩陣,起點
輸出如下:
      點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
================================


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章