資源簡介
求最短路徑的Floyd算法實現,無向圖和有向圖均適用。1先區別有向圖和無向圖,2輸入頂點數和邊數并檢查合法性,3輸入每邊的起點、終點、權重并檢查合法性,并初始化鄰接矩陣和路徑矩陣,4調用自定義函數Floyd

代碼片段和文件信息
%求最短路徑的Floyd算法實現
%1先區別有向圖和無向圖,2輸入頂點數和邊數并檢查合法性,3輸入每邊的起點、終點、權重并檢查合法性,并初始化鄰接矩陣和路徑矩陣,4調用自定義函數Floyd
%1
bool=0;
while?bool<1
????type=input(‘輸入圖的種類1代表無向圖2代表有向圖>>‘);
????if?type==1?||?type==2
????????bool=1;
????else
????????display(‘請重新輸入圖像類型‘);
????end
end
%2
boolean=0;
while?boolean<1
????np=input(‘輸入頂點個數>>‘);
????nl=input(‘輸入路徑數>>‘);
????if?nl<=type*np*(np-1)/2;
????????boolean=1;
????else
????????display(‘輸入的頂點數和邊數不合法,請重新輸入‘);
????end
end
%3
inf=9999;
D=inf*ones(npnp);
for?i=1:np
????D(ii)=0;
end
P=ones(npnp);
for?i=1:np
????P(:i)=i;
end
i=1;
while?i<=nl
????a=input(‘以[sew]形式輸入每邊的起點編號(從1開始)、終點編號、權重‘);
????startp=a(11);
????endp=a(12);
????weight=a(13);
????if?startp<=0?||?endp<=0?||?weight<=0?||?startp>np?||endp>np
????????display(‘本組數據不合法,請重新輸入本組數據‘);
????else?if?type==1
????????D(startpendp)=weight;
????????D(endpstartp)=weight;??????????????????????????????????????????%可以證明無向圖的鄰接矩陣是對稱矩陣
????????end
????????if?type==2
????????????D(startpendp)=weight;
????????end
????????i=i+1;
????end
end
%4
if?type==1
????Floyd1(npDP);
else
????Floyd2(npDP);
end
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1334??2018-08-29?14:17??floyd.m
?????文件???????1227??2018-08-29?14:33??Floyd1.m
?????文件????????955??2018-08-29?14:37??Floyd2.m
-----------?---------??----------?-----??----
?????????????????3516????????????????????3
- 上一篇:MATLAB 3層BP神經網絡資源
- 下一篇:噴泉碼之LT碼的編解碼
評論
共有 條評論