La comprensión del algoritmo Floyd FLOYD
Puede realizar el camino más corto desde todos los vértices a todos los vértices
Pero su complejidad de tiempo es alta, pero el código es relativamente conciso (menos)
Primero defina una matriz D para registrar los pesos entre vértices,
y luego defina una matriz P para registrar los valores de vértice subsiguientes de los vértices
for (v = 0; v < G.NumVer; ++v)
{
for (w = 0; w < G.NumVer; ++w)
{
(D)[v][w] = G.Arc[v][w];//记录权值
(P)[v][w] = w;
}
}
Encuentra la parte del camino más corto
for (k = 0; k < G.NumVer; ++k)
{
for (v = 0; v < G.NumVer; ++v)
{
for (w = 0; w < G.NumVer; ++w)
{
if ((D)[v][w] > ((D)[v][k] + (D)[k][w]))
{
//k是一个中间过程 中间路径判断一下经过k时 会不会更新
//否则将权值改为经过k时的路径
//P保存该结点的后继 根据权值大小和终点选择
(D)[v][w] = ((D)[v][k] + (D)[k][w]);
(P)[v][w] = (P)[v][k];
}
}
}
}
k es una ruta intermedia para determinar si se actualizará al pasar k
El código completo
es similar a
lo que se escribió antes, pero un problema sin resolver es cómo inicializarlo si define una matriz de punteros. ? ?
#include<iostream>
#include <iomanip>
using namespace std;
typedef char Vertextype;//顶点类型
typedef int Edgetype;//边缘权值
typedef int Status;
#define MAXVER 9//最大顶点数
#define INF 65535//代表无穷
#define NULL 0
typedef int Pathmatirx[MAXVER][MAXVER];
typedef int ShortPathTable[MAXVER][MAXVER];
typedef struct Graph
{
Vertextype Ver[MAXVER];
Edgetype Arc[MAXVER][MAXVER];
int NumVer, NumEdg;
}MGraph;
//生成图——邻接矩阵
Status CreatGraph(MGraph &G)
{
int i, j, w;
cout << "Please enter the number of verticesof the graph : " << endl;
cin >> G.NumVer;
cout << "Please enter the number of edges the graph : " << endl;
cin >> G.NumEdg;
cout << "Please enter the name of vex : " << endl;
for (i = 0; i < G.NumVer; i++){
//cout << "Please enter the NO." << i + 1 << "%d name of vex : " << endl;
cin >> G.Ver[i];
}
cout << "Diagonal infinity ..." << endl;
for (i = 0; i < G.NumVer; i++)
for (j = 0; j < G.NumVer; j++)
{
G.Arc[i][j] = INF;//简单图 不循环
}
cout << "...Diagonal infinity" << endl;
for (int k = 0; k < G.NumEdg; k++){
//因为具体哪条边存在不一定 所以选择性输入边
cout << "Enter the subscripts and weights from vertex vi to vertex vj : " << endl;
cin >> i >> j >> w;
/*cout << "Please enter the subscript j of the edge : " << endl;
cin >> j;
cout << "Please enter the weight from vertex "<<i<<" to vertex "<<j<<" : " << endl;
cin >> w;*/
G.Arc[i][j] = w;
G.Arc[j][i] = G.Arc[i][j];//无向图 边的信息 是对称的 //有向图的话 无需设置
}
return 0;
}
Status ShortestPath_Floyd(MGraph &G, Pathmatirx &P,ShortPathTable &D)
{
int v, w, k;
//初始化
for (v = 0; v < G.NumVer; ++v)
{
for (w = 0; w < G.NumVer; ++w)
{
(D)[v][w] = G.Arc[v][w];//记录权值
(P)[v][w] = w;
}
}
for (k = 0; k < G.NumVer; ++k)
{
for (v = 0; v < G.NumVer; ++v)
{
for (w = 0; w < G.NumVer; ++w)
{
if ((D)[v][w] > ((D)[v][k] + (D)[k][w]))
{
//k是一个中间过程 中间路径判断一下经过k时 会不会更小
//否则将权值改为经过k时的路径
//P保存该结点的前驱或者后继 根据权值大小和终点选择
(D)[v][w] = ((D)[v][k] + (D)[k][w]);
(P)[v][w] = (P)[v][k];
}
}
}
}
return 0;
}
int main()
{
MGraph G;
Pathmatirx P;
ShortPathTable D;
CreatGraph(G);
ShortestPath_Floyd(G,P,D);
system("pause");
return 0;
}