原文链接:
【1】为什么需要弗洛伊德算法?
带权图中单个源点到所有顶点的最短路径问题可以用《》求解。
那如果要求图中每一个顶点与其它顶点之间的最短路径呢?类似可以想到的方法为:
每次以一个顶点为源点,重复执行地杰斯特拉算法算法n次。
这样,理论上我们便可以求得每一个顶点与其它顶点的最短路径,总的执行时间为O(n3)。
好吧!为了实现这个中需求,可以采用另外一种求解算法:弗洛伊德算法。
为了更好的理解弗洛伊德算法的精妙,我们先看简单的案例。
如下图是一个最简单的3个顶点连通网图:
【2】弗洛伊德算法
弗洛伊德算法是非常漂亮的算法,简洁直观大气上档次。
不过很可惜由于它的三重循环,因此也是O(n*n*n)的时间复杂度。
如果你面临需要求所有顶点至所有顶点的最短路径问题?
它是很好的选择。算法代码如下图:
关于本算法再不做详细赘述。如若感兴趣,下面的代码案例可以自己琢磨琢磨。
【3】弗洛伊德算法实现
注意:本算法的实现案例与迪杰斯特拉算法相同都是在求同一个图的最短路径问题。
不同的是这个算法可以求得所有顶点到所有顶点的最短路径。
模拟实现代码如下:
#include#include "SeqList.h"#include using namespace std;#define INFINITY 65535typedef int* pInt;typedef pInt* ppInt;template class Graph{private: SeqList Vertices; DistType **Edges; int nVer, nEdges;public: Graph() : Edges(NULL) , nEdges(0) , nVer(0) {} ~Graph() {}public: int GetVer() const { return nVer; } istream & operator>>(istream &in) { int v, u, value; int i, j; NameType item; cout << "请输入顶点的个数: " << endl; in >> nVer; cout << "请输入顶点的数据信息: " << endl; for (i = 0; i < nVer; ++i) { in >> item; Vertices.push_back(item); // 保存全部顶点 } /二维数组的创建并初始化 Edges = new DistType*[nVer]; // DistType *ar[10]; for (i = 0; i < nVer; ++i) { Edges[i] = new DistType[nVer]; for (j = 0; j < nVer; ++j) { Edges[i][j] = 0; } } cout << "请输入边的个数: " << endl; in >> nEdges; cout << "请输入边的信息:" << endl; for (i = 0; i < nEdges; ++i) { in >> v >> u >> value; Edges[v][u] = value; Edges[u][v] = value; } return in; } ostream & operator<<(ostream &out) const { int i, j; out << "顶点信息 " << endl; for (i = 1; i <= nVer; ++i) { out << Vertices[i] << setw(5); } out << endl; out << "矩阵信息:" << endl; out << setw(10); for (i = 1; i <= nVer; ++i) { out << Vertices[i] << setw(5); } out << endl; for (i = 0; i < nVer; ++i) { out << Vertices[i+1] << setw(5); for (j = 0; j < nVer; ++j) { if (0 == Edges[i][j] && i != j) Edges[i][j] = INFINITY; cout << Edges[i][j] << setw(5); } out << endl; } out << endl; return out; } // 弗洛伊德算法实现 void ShortestPath_Floyd(int** p, int** D) { int v = 0, w = 0, k = 0; // 初始化数据 for (v = 0; v < nVer; ++v) { for (w = 0; w < nVer; ++w) { D[v][w] = Edges[v][w]; p[v][w] = w; } } for (k = 0; k < nVer; ++k) { for (v = 0; v < nVer; ++v) { for(w = 0; w < nVer; ++w) { if (D[v][w] > D[v][k] + D[k][w]) { D[v][w] = D[v][k] + D[k][w]; p[v][w] = p[v][k]; } } } } } // 打印矩阵信息 void PrintArray(ppInt pp) { cout << setw(10); for (int i = 1; i <= nVer; ++i) { cout << Vertices[i] << setw(5); } cout << endl; for (int i = 0; i < nVer; ++i) { cout << Vertices[i+1] << setw(5); for (int j = 0; j < nVer; ++j) { cout << pp[i][j] << setw(5); } cout << endl; } cout << endl; } // 求解完成后打印所以路径信息 void PrintPath(ppInt pp, ppInt DD) { int v, k, w; for (v = 0; v < nVer; ++v) { for (w = v+1; w < nVer; ++w) { cout << "V" << v << "-->" << "V" << w << " weight:" << DD[v][w] << endl; k = pp[v][w]; cout << "Path:V" << v; while (k != w) { cout << "-->V" << k; k = pp[k][w]; } cout << "-->V" << w << endl; } } cout << endl; }};template istream & operator>>(istream &in, Graph &g){ g >> in; return in;}template ostream & operator<<(ostream &out, const Graph &g){ g << out; return out;}void main(){ Graph myg; cin >> myg; cout << "打印所有输入信息:" << endl; cout << myg << endl; cout << "求最短路径....." << endl; int numVer = myg.GetVer(); ppInt pPathmatirx = new pInt[numVer]; for (int i = 0; i < numVer; ++i) { pPathmatirx[i] = new int[numVer]; for (int j = 0; j < numVer; ++j) { pPathmatirx[i][j] = 0; } } ppInt pShortPath = new pInt[numVer]; for (int i = 0; i < numVer; ++i) { pShortPath[i] = new int[numVer]; for (int j = 0; j < numVer; ++j) { pShortPath[i][j] = 0; } } myg.ShortestPath_Floyd(pPathmatirx, pShortPath); cout << "分别打印矩阵结果:" << endl; cout << "各顶点最短路径:" << endl; myg.PrintArray(pShortPath); cout << "各最短路径前驱顶点下标值:" << endl; myg.PrintArray(pPathmatirx); cout << "打印全部最短路径:"<< endl; myg.PrintPath(pPathmatirx, pShortPath); // 释放二维数组 for (int i = 0; i < numVer; ++i) delete []pPathmatirx[i]; delete []pPathmatirx; for (int i = 0; i < numVer; ++i) delete []pShortPath[i]; delete []pShortPath; pPathmatirx = NULL; pShortPath = NULL;}// 备注:// 最短路径弗洛伊德算法实现// 整理于2013-12-05// 测试输入程序为:/*请输入顶点的个数:9请输入顶点的数据信息:A B C D E F G H I请输入边的个数:16请输入边的信息:0 1 10 2 51 2 31 3 71 4 52 4 12 5 73 4 23 6 34 5 34 6 64 7 95 7 56 7 26 8 77 8 4打印所有输入信息:顶点信息A B C D E F G H I矩阵信息:A B C D E F G H IA 0 1 5655356553565535655356553565535B 1 0 3 7 565535655356553565535C 5 3 065535 1 7655356553565535D65535 765535 0 265535 36553565535E65535 5 1 2 0 3 6 965535F6553565535 765535 3 065535 565535G655356553565535 3 665535 0 2 7H65535655356553565535 9 5 2 0 4I655356553565535655356553565535 7 4 0求最短路径.....分别打印矩阵结果:各顶点最短路径:A B C D E F G H IA 0 1 4 7 5 8 10 12 16B 1 0 3 6 4 7 9 11 15C 4 3 0 3 1 4 6 8 12D 7 6 3 0 2 5 3 5 9E 5 4 1 2 0 3 5 7 11F 8 7 4 5 3 0 7 5 9G 10 9 6 3 5 7 0 2 6H 12 11 8 5 7 5 2 0 4I 16 15 12 9 11 9 6 4 0各最短路径前驱顶点下标值:A B C D E F G H IA 0 1 1 1 1 1 1 1 1B 0 1 2 2 2 2 2 2 2C 1 1 2 4 4 4 4 4 4D 4 4 4 3 4 4 6 6 6E 2 2 2 3 4 5 3 3 3F 4 4 4 4 4 5 7 7 7G 3 3 3 3 3 7 6 7 7H 6 6 6 6 6 5 6 7 8I 7 7 7 7 7 7 7 7 8打印全部最短路径:V0-->V1 weight:1Path:V0-->V1V0-->V2 weight:4Path:V0-->V1-->V2V0-->V3 weight:7Path:V0-->V1-->V2-->V4-->V3V0-->V4 weight:5Path:V0-->V1-->V2-->V4V0-->V5 weight:8Path:V0-->V1-->V2-->V4-->V5V0-->V6 weight:10Path:V0-->V1-->V2-->V4-->V3-->V6V0-->V7 weight:12Path:V0-->V1-->V2-->V4-->V3-->V6-->V7V0-->V8 weight:16Path:V0-->V1-->V2-->V4-->V3-->V6-->V7-->V8V1-->V2 weight:3Path:V1-->V2V1-->V3 weight:6Path:V1-->V2-->V4-->V3V1-->V4 weight:4Path:V1-->V2-->V4V1-->V5 weight:7Path:V1-->V2-->V4-->V5V1-->V6 weight:9Path:V1-->V2-->V4-->V3-->V6V1-->V7 weight:11Path:V1-->V2-->V4-->V3-->V6-->V7V1-->V8 weight:15Path:V1-->V2-->V4-->V3-->V6-->V7-->V8V2-->V3 weight:3Path:V2-->V4-->V3V2-->V4 weight:1Path:V2-->V4V2-->V5 weight:4Path:V2-->V4-->V5V2-->V6 weight:6Path:V2-->V4-->V3-->V6V2-->V7 weight:8Path:V2-->V4-->V3-->V6-->V7V2-->V8 weight:12Path:V2-->V4-->V3-->V6-->V7-->V8V3-->V4 weight:2Path:V3-->V4V3-->V5 weight:5Path:V3-->V4-->V5V3-->V6 weight:3Path:V3-->V6V3-->V7 weight:5Path:V3-->V6-->V7V3-->V8 weight:9Path:V3-->V6-->V7-->V8V4-->V5 weight:3Path:V4-->V5V4-->V6 weight:5Path:V4-->V3-->V6V4-->V7 weight:7Path:V4-->V3-->V6-->V7V4-->V8 weight:11Path:V4-->V3-->V6-->V7-->V8V5-->V6 weight:7Path:V5-->V7-->V6V5-->V7 weight:5Path:V5-->V7V5-->V8 weight:9Path:V5-->V7-->V8V6-->V7 weight:2Path:V6-->V7V6-->V8 weight:6Path:V6-->V7-->V8V7-->V8 weight:4Path:V7-->V8 */