Floyd算法简单实现(C++)
2024-08-23 22:29:46
图的最短路径问题主要包括三种算法:
(3)Bellman (含有负权边的单源最短路径)
本文主要讲使用C++实现简单的Floyd算法,Floyd算法原理参见 Floyd–Warshall algorithm
Floyd算法简单实现(C++)
#include<iostream>
using namespace std; #define MAXVEX 10
#define INFINITY 65535 typedef int Patharc[MAXVEX][MAXVEX];
typedef int ShortPathTable[MAXVEX][MAXVEX]; typedef struct {
int vex[MAXVEX];
int arc[MAXVEX][MAXVEX];
int numVertexes;
} MGraph; // 构建图
void CreateMGraph(MGraph *G){
int i, j, k; // 初始化图
G->numVertexes = ;
for(i = ; i < G->numVertexes; ++i){
G->vex[i] = i;
}
for(i = ; i < G->numVertexes; ++i){
for(j = ; j < G->numVertexes; ++j){
if(i == j)
G->arc[i][j] = ;
else
G->arc[i][j] = G->arc[j][i] = INFINITY;
}
} G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ; // 设置对称位置元素值
for(i = ; i < G->numVertexes; ++i){
for(j = i; j < G->numVertexes; ++j){
G->arc[j][i] = G->arc[i][j];
}
}
} // Floyd algorithm
void ShortPath_Floyd(MGraph G, Patharc P, ShortPathTable D){
int i, j, k;
// 二重循环,初始化P, D
for(i = ; i < G.numVertexes; ++i){
for(j = ; j < G.numVertexes; ++j){
D[i][j] = G.arc[i][j];
P[i][j] = j;
}
}
// 三重循环, Floyd algorithm
for(k = ; k < G.numVertexes; ++k){
for(i = ; i < G.numVertexes; ++i){
for(j = ; j < G.numVertexes; ++j){
if(D[i][j] > D[i][k]+D[k][j]){
D[i][j] = D[i][k]+D[k][j];
P[i][j] = P[i][k];
}
}
}
}
} // 打印最短路径
void PrintShortPath(MGraph G, Patharc P, ShortPathTable D){
int i, j, k;
cout<<"各顶点之间的最短路径如下: "<<endl;
for(i = ; i < G.numVertexes; ++i){
for(j = i+; j < G.numVertexes; ++j){
cout<<"v"<<i<<"--"<<"v"<<j<<" "<<"weight: "<<D[i][j]<<" Path: "<<i<<" -> ";
k = P[i][j];
while(k != j){
cout<<k<<" -> ";
k = P[k][j];
}
cout<<j<<endl;
}
cout<<endl;
}
} int main(int argc, char const *argv[]) {
MGraph G;
Patharc P;
ShortPathTable D;
CreateMGraph(&G);
ShortPath_Floyd(G, P, D);
PrintShortPath(G, P, D);
return ;
}
运行结果:
各顶点之间的最短路径如下:
v0--v1 weight: Path: ->
v0--v2 weight: Path: -> ->
v0--v3 weight: Path: -> -> -> ->
v0--v4 weight: Path: -> -> ->
v0--v5 weight: Path: -> -> -> ->
v0--v6 weight: Path: -> -> -> -> ->
v0--v7 weight: Path: -> -> -> -> -> ->
v0--v8 weight: Path: -> -> -> -> -> -> -> v1--v2 weight: Path: ->
v1--v3 weight: Path: -> -> ->
v1--v4 weight: Path: -> ->
v1--v5 weight: Path: -> -> ->
v1--v6 weight: Path: -> -> -> ->
v1--v7 weight: Path: -> -> -> -> ->
v1--v8 weight: Path: -> -> -> -> -> -> v2--v3 weight: Path: -> ->
v2--v4 weight: Path: ->
v2--v5 weight: Path: -> ->
v2--v6 weight: Path: -> -> ->
v2--v7 weight: Path: -> -> -> ->
v2--v8 weight: Path: -> -> -> -> -> v3--v4 weight: Path: ->
v3--v5 weight: Path: -> ->
v3--v6 weight: Path: ->
v3--v7 weight: Path: -> ->
v3--v8 weight: Path: -> -> -> v4--v5 weight: Path: ->
v4--v6 weight: Path: -> ->
v4--v7 weight: Path: -> -> ->
v4--v8 weight: Path: -> -> -> -> v5--v6 weight: Path: -> ->
v5--v7 weight: Path: ->
v5--v8 weight: Path: -> -> v6--v7 weight: Path: ->
v6--v8 weight: Path: -> -> v7--v8 weight: Path: -> [Finished in .2s]
参考资料:
Floyd–Warshall algorithm, Wikipedia
Floyd Warshall Algorithm | DP-16 , geeksforgeeks
最新文章
- PMO是什么?如何与其他部门协作配合提高项目成功率?
- 通过例子学习 Keystone - 每天5分钟玩转 OpenStack(19)
- SharePoint 2013 Error - TypeError: Unable to get property &#39;replace&#39; of undefined or null reference
- CCF 模拟A 无脑大循环
- java通过jdbc连接impala
- .NET同页面内用户控件与父页面以及控件之间方法调用
- java 生成随机数
- mui开发
- javascript 实现htmlEncode htmlDecode
- hdu 5510 Bazinga KMP+尺取法
- 通达OA 小飞鱼工作流在线培训教程文件夹及意见征集
- 关于Winsock编程中IO重叠的概念
- Colorful(Folders星语多彩文件夹) v1.7绿色版
- object detection技术演进:RCNN、Fast RCNN、Faster RCNN
- bind、apply与call
- Windows Server 2016-启用默认Windows搜索服务
- android利用ContentResolver访问者获取手机联系人信息
- Touch事件在移动端web开发中的详解
- css预处理器:Sass LASS Stylus
- linux的tar命令