演算法思路
路徑矩陣
透過一個圖的權值矩陣求出它的每兩點間的最短路徑矩陣。從圖的帶權鄰接矩陣A=[a(i,j)] n×n開始,遞歸地進行n次更新,即由矩陣D(0)=A,按一個公式,構造出矩陣D(1) ;又用同樣地公式由D(1)構造出D(2),以此類推。最後又用同樣的公式由D(n-1)構造出矩陣D(n)。矩陣D(n)的i行j列元素便是i號頂點到j號頂點的最短路徑長度,稱D(n)為圖的距離矩陣,同時還可引入一個後繼節點矩陣path來記錄兩點間的最短路徑。
狀態轉移方程式
其狀態轉移方程式如下: map[i,j]:=min{map[i,k]+map[k,j],map[i,j]};map[i,j ]表示i到j的最短距離,K是窮舉i,j的斷點,map[n,n]初值應為0。
當然,如果這條路沒有通的話,還必須特殊處理,例如沒有map[i,k]這條路。
核心演算法
1,從任一條單邊路徑開始。所有兩點之間的距離是邊的權,如果兩點之間沒有邊相連,則權為無窮大。
2,對於每一對頂點 u 和 v,看看是否存在一個頂點 w 使得從 u 到 w 再到 v 比已知的路徑更短。如果是更新它。
把圖用鄰接矩陣G表示出來,如果從Vi到Vj有路可達,則G[i,j]=d,d表示該路的長度;否則G[i,j]=無窮大。定義一個矩陣D用來記錄所插入點的資訊,D[i,j]表示從Vi到Vj需要經過的點,初始化D[i,j]=j。把各個頂點插入圖中,比較插點後的距離與原來的距離,G[i,j] = min( G[i,j], G[i,k]+G[k,j] ),如果G[i,j]的值變小,則D[i,j]=k。在G中包含有兩點之間最短道路的信息,而在D中則包含了最短通路徑的信息。
時間複雜度與空間複雜度
時間複雜度:因為核心演算法是採用鬆弛法的三個for循環,因此時間複雜度為O(n^3)
空間複雜度:整個演算法空間消耗是一個n*n的矩陣,因此其空間複雜度為O(n^2)
C++程式碼
// floyd.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include"iostream" #include"fstream" #define maxlen 20 #define maximum 100 using namespace std; typedef struct graph { int vertex; int edge; int matrix[maxlen][maxlen]; }; int _tmain(int argc, _TCHAR* argv[]) { ofstream outwrite; outwrite.open("h.txt",ios::app|ios::out); outwrite<<"welcome to the graph world!\n"; outwrite<<"the initial matrix is:\n"; int vertexnumber; int edgenumber; int beginning,ending,weight; int mindistance[maxlen][maxlen]; int interval[maxlen][maxlen]; graph floydgraph; cout<<"welcome to the graph world!"<<endl; cout<<"input the number of the vertex: "; cin>>vertexnumber; cout<<"input the number of the edge: "; cin>>edgenumber; for (int i = 0; i < vertexnumber; i++) { for (int j = 0; j < vertexnumber; j++) { floydgraph.matrix[i][j]=maximum; } } for (int i = 0; i <edgenumber; i++) { cout<<"please input the beginning index: "; cin>>beginning; cout<<"please input the ending index: "; cin>>ending; cout<<"please input the distance of the two dot: "; cin>>weight; floydgraph.matrix[beginning][ending]=weight; } for (int i = 0; i <vertexnumber; i++) { for (int j = 0; j < vertexnumber; j++) { mindistance[i][j]=floydgraph.matrix[i][j]; outwrite<<floydgraph.matrix[i][j]<<"\t"; interval[i][j]=-1; } outwrite<<"\n"; } for (int k = 0; k <vertexnumber; k++) { for (int i = 0; i < vertexnumber; i++) { for (int j = 0; j < vertexnumber; j++) { if(mindistance[i][j]>mindistance[i][k]+mindistance[k][j]) { mindistance[i][j]=mindistance[i][k]+mindistance[k][j]; interval[i][j]=k; } } } } outwrite<<"\n"<<"after the floyd transition, the matrix is: "<<"\n"; for (int i = 0; i < vertexnumber; i++) { for (int j = 0; j < vertexnumber; j++) { cout<<"the mindistance between "<<i<<" and "<<j <<" is: "; cout<<mindistance[i][j]<<endl; cout<<"the two points pass through the point: "<<interval[i][j]; cout<<endl; outwrite<<mindistance[i][j]<<"\t"; } outwrite<<"\n"; } outwrite<<"\n"; outwrite<<"the points between the beginning point and the ending point is:"<<"\n"; for (int i = 0; i < vertexnumber; i++) { for (int j = 0; j < vertexnumber; j++) { outwrite<<interval[i][j]<<"\t"; } outwrite<<"\n"; } outwrite.close(); getchar(); getchar(); getchar(); return 0; }