① matlab中編寫floyd演算法問題
定義沒有錯誤啊。得看你寫的函數之後才知道啊。
② 關於MATLAB。floyd演算法的求助
這個是M文件中的函數啊,只有運行在主界面並且這樣運行:
floyd(a)(把a輸入括弧這里才行)
單獨運行當然沒有定義a啊
%floyd.m
%採用floyd演算法計算圖a中每對頂點最短路
%d是矩離矩陣
%r是路由矩陣
function
[d,r]=floyd(a)
n=size(a,1);
d=a;
for
i=1:n
for
j=1:n
r(i,j)=j;%原始默認路徑都是各節點間直接到達的距離
end
end
r
for
k=1:n
for
i=1:n
for
j=1:n
if
d(i,k)+d(k,j)
d(i,j)=d(i,k)+d(k,j);%這里是不是輸入錯了?看不懂中間為甚麼有個空格;
但總體意思是說如果從i節點先到k節點再到j節點間距離比從i直接到j要近的話就替換掉原先那條路徑
r(i,j)=r(i,k)
end
end
end
k
d
r
end
③ matlab實現floyd演算法的程序存在問題
調用的時候輸入一個實際參數a就可以了。
因為你現在函數裡面a只是一個形式參數,沒有具體的值。
④ matlab實現floyd演算法 已知距離矩陣和權值矩陣 求最短路徑
希望可以幫到你。
function [D,path]=floyd(a)
n=size(a,1);
D=a;
path=zeros(n,n);
for i=1:n
for j=1:n
if D(i,j)~=inf
path(i,j)=j;
end
end
end
for k=1:n
for i=1:n
for j=1:n
if D(i,k)+D(k,j)<D(i,j)
D(i,j)=D(i,k)+D(k,j);
path(i,j)=path(i,k)
end
end
end
end
function [L,R]=router(D,path,s,t)
L=zeros(0,0);
R=s;
while 1
if s==t
L=fliplr(L);
L=[0,L];
return
end
L=[L,D(s,t)];
R=[R,path(s,t)];
s=path(s,t);
end
⑤ Floyd演算法怎麼用MATLAB編寫
這個是M文件中的函數啊,只有運行在主界面並且這樣運行: floyd(a)(把a輸入括弧這里才行) 單獨運行當然沒有定義a啊 %floyd.m %採用floyd演算法計算圖a中每對頂點最短路 %d是矩離矩陣 %r是路由矩陣 function [d,r]=floyd(a) n=size(a,1); d=a; for i=...
⑥ matlab實現弗洛伊德演算法的代碼,。
function
[d,r]=floyd(a)
%floyd.m
%採用floyd演算法計算圖a中每對頂點最短路
%d是矩離矩陣
%r是路由矩陣
n=size(a,1);
d=a;
for
i=1:n
for
j=1:n
r(i,j)=j;
end
end
r
for
k=1:n
for
i=1:n
for
j=1:n
if
d(i,k)+d(k,j)
評論
0
0
0
載入更多
⑦ matlab!!!!
a=[0 15 inf inf 24 inf 18 inf inf inf inf inf;
15 0 22 inf inf inf inf inf inf inf inf inf;
inf 22 0 18 16 inf inf inf 20 inf inf inf;
inf inf 18 0 inf 12 inf inf inf inf inf inf;
24 inf 16 inf 0 inf inf 12 24 inf inf inf;
inf inf inf 12 inf 0 inf inf 12 inf inf 22;
18 inf inf inf inf inf 0 15 inf 22 inf inf;
inf inf inf inf 12 inf 15 0 30 inf 25 inf;
inf inf 20 inf 24 12 inf 30 0 inf 19 19;
inf inf inf inf inf inf 22 inf inf 0 19 inf ;
inf inf inf inf inf inf inf 25 19 19 0 21;
inf inf inf inf inf 22 inf inf 19 inf 21 0];
>> [D,path]=floyd(a)
D =
0 15 37 55 24 60 18 33 48 40 58 67
15 0 22 40 38 52 33 48 42 55 61 61
37 22 0 18 16 30 43 28 20 58 39 39
55 40 18 0 34 12 61 46 24 62 43 34
24 38 16 34 0 36 27 12 24 49 37 43
60 52 30 12 36 0 57 42 12 50 31 22
18 33 43 61 27 57 0 15 45 22 40 61
33 48 28 46 12 42 15 0 30 37 25 46
48 42 20 24 24 12 45 30 0 38 19 19
40 55 58 62 49 50 22 37 38 0 19 40
58 61 39 43 37 31 40 25 19 19 0 21
67 61 39 34 43 22 61 46 19 40 21 0
path =
1 2 2 2 5 5 7 7 5 7 7 5
1 2 3 3 3 3 1 1 3 1 3 3
2 2 3 4 5 4 5 5 9 9 9 9
3 3 3 4 3 6 3 3 6 6 6 6
1 3 3 3 5 9 8 8 9 8 8 9
9 4 4 4 9 6 9 9 9 9 9 12
1 1 8 8 8 8 7 8 8 10 8 8
7 7 5 5 5 9 7 8 9 7 11 11
5 3 3 6 5 6 8 8 9 11 11 12
7 7 11 11 7 11 7 7 11 10 11 11
8 9 9 9 8 9 8 8 9 10 11 12
9 9 9 6 9 6 11 11 9 11 11 12
⑧ matlab floyd 演算法注釋
A矩陣是鄰接矩陣,對角線上為o,其餘位置數字表示的是兩點之間距離,比如A(1,2)=2,表示從第一個點到第二個點的距離為2.inf是無窮大的意思,這里表示沒有直接溝通這兩點的路。
n=length(D);設定n為D矩陣的長度。
接下來的兩重循環,得到的R矩陣是n*n的矩陣,它每個數據表示的是路徑,比如:R(1,3)=1;表示路徑為:1-1-3.這里是初始化路徑了。
後面的三重循環是floyd演算法的關鍵所在,就是更新路線了。裡面的那個判斷指的是:
假設有3個點,1
2
3;如果我從1-2-3之間總距離小於1-3的距離,那麼我R(1,3)=2;這就是選取更近的路線了。
最後的兩個判斷是為了不讓曾經走過的點再次被遍歷。就是不回頭的意思了,這個一般都可以忽略了,你照打上去就是了。
不知道這樣的解釋你是否滿意。
⑨ 求matlab高手,幫我解決一下floyd演算法!!急急急
//假期在家做的,網上這類演算法很多,思想都差不多
#include<iostream>
using namespace std;
int N; //頂點個數
int max = 10000; //max代表兩點之間沒有路徑存在
float ** inPut(){
int edge,m,n,w,i;
cout<<"請輸入頂點數:";
cin>>N;
N = N+1; //矩陣(數組)下標從1開始計數,方便操作
float **C = new float*[N]; //矩陣C為圖的初始鄰接矩陣
for(i = 1; i<N; i++)
*(C+i) = new float[N];
for( i = 1; i<N; i++) //鄰接矩陣初始化
for(int j = 1; j<N; j++){
if(i == j)
C[i][j] = 0; //矩陣對角線上的值初始為0,其他為max
else C[i][j] = max;
}
cout<<"請輸入邊數:";
cin>>edge;
cout<<"請輸入邊及權值:"<<endl;
for(i = 0; i<edge; i++){
cin >> m >> n >> w;
C[m][n] = w;
}
return C;
}
void Floyd(float **C){
int i,j;
float **A = new float*[N]; //矩陣A最終存放修改後的路徑長度
int **path = new int*[N]; //矩陣path記錄兩點之間的路徑
for(i = 1; i<N; i++)
*(A+i) = new float[N];
for(i = 1; i<N; i++)
*(path+i) = new int[N];
for(i = 1; i<N; i++) //設置矩陣A和path的初值
for(j = 1; j<N; j++){
if(C[i][j] != max)
path[i][j] = j; //存在路徑,記錄下終點下標值
else path[i][j] = -1; //不存在路徑用-1表示
A[i][j] = C[i][j];
}
//修改路徑長度(矩陣A)和路徑(矩陣path)
for(int k = 1; k<N; k++) //試圖將頂點k擴充到最短路徑path矩陣中
for(i = 1; i <N; i++)
for(j = 1; j<N; j++)
if( A[i][j] > ( A[i][k]+A[k][j] ) ){//判斷頂點i到j的權值是否大於從i經k到j的權值,取二者較小者記錄下來
A[i][j] = ( A[i][k]+A[k][j]);
path[i][j] = path[i][k]; //記錄下中間頂點k值
}
cout << endl << endl;
cout << "初始鄰接矩陣C[N][N]" << endl;
for( i = 1; i<N; i++){
for(j = 1; j<N; j++){
cout << C[i][j] << "\t";
}
cout << endl;
}
cout << endl << endl;
cout << "修改後的路徑長度矩陣" << endl;
for( i = 1; i<N; i++){
for(j = 1; j<N; j++){
cout << A[i][j] << "\t";
}
cout << endl;
}
cout << endl;
cout << endl;
cout<<"路徑矩陣"<<endl;
for( i = 1; i<N; i++){
for(j = 1; j<N; j++){
cout << path[i][j] << "\t";
}
cout << endl;
}
cout << endl << endl;
cout << "所有頂點對之間的最短路徑的權值及其路徑" << endl;
for(i = 1; i<N; i++){
cout << endl;
for(j = 1; j<N; j++){
cout << A[i][j] << "\t"; //輸出i-->j的權值
int next = path[i][j]; //頂點i到j的路徑,i後的第一個後繼頂點
if(next == -1) //路徑不存在
cout << i << " 到 " << j << " 沒有路徑" << endl;
else {
cout<<i; //起點
while(next != j){ //i、j之間存在中間頂點
cout << "-->" << next;
next = path[next][j]; //尋找下一個中間頂點
}
cout << "-->" << j << endl; //終點
}
}
}
}
int main(int argc, char* argv[]){
float **C;
C = inPut(); //鄰接矩陣的初始化
Floyd(C);
return 0;
}
/*
1 2 10
1 4 30
1 5 100
2 3 50
3 5 10
4 3 20
4 5 60
*/