图的最短路径问题主要包括三种算法:

(1)Dijkstra (没有负权边的单源最短路径)

(2)Floyed (多源最短路径)

(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

最新文章

  1. PMO是什么?如何与其他部门协作配合提高项目成功率?
  2. 通过例子学习 Keystone - 每天5分钟玩转 OpenStack(19)
  3. SharePoint 2013 Error - TypeError: Unable to get property &#39;replace&#39; of undefined or null reference
  4. CCF 模拟A 无脑大循环
  5. java通过jdbc连接impala
  6. .NET同页面内用户控件与父页面以及控件之间方法调用
  7. java 生成随机数
  8. mui开发
  9. javascript 实现htmlEncode htmlDecode
  10. hdu 5510 Bazinga KMP+尺取法
  11. 通达OA 小飞鱼工作流在线培训教程文件夹及意见征集
  12. 关于Winsock编程中IO重叠的概念
  13. Colorful(Folders星语多彩文件夹) v1.7绿色版
  14. object detection技术演进:RCNN、Fast RCNN、Faster RCNN
  15. bind、apply与call
  16. Windows Server 2016-启用默认Windows搜索服务
  17. android利用ContentResolver访问者获取手机联系人信息
  18. Touch事件在移动端web开发中的详解
  19. css预处理器:Sass LASS Stylus
  20. linux的tar命令

热门文章

  1. c#删除指定文件夹中今天之前的文件
  2. IT兄弟连 JavaWeb教程 转发和重定向的区别
  3. (二分图最大匹配)51NOD 2006 飞行员配对
  4. oracle错误:1067进程意外终止
  5. sublime text 3 文件列表忽略特定格式的文件
  6. [未读]JavaScript高效图形编程
  7. bootstrap div 固定
  8. nodejs on raspi
  9. android开发学习 ------- json数据与实体类之间的相互转换
  10. 解决spring boot websocket