算法思路
路徑矩陣
通過一個圖的權值矩陣求出它的每兩點間的最短路徑矩陣。從圖的帶權鄰接矩陣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]這條路。
時間復雜度與空間復雜度
時間復雜度:因為核心算法是采用松弛法的三個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;
}