Floyd算法简单实现(C++)
图的最短路径问题主要包括三种算法:
(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
Floyd算法简单实现(C++)的更多相关文章
- 算法笔记_069:Floyd算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 使用Floyd算法得到最短距离示例 2.2 具体编码 1 问题描述 何为Floyd算法? Floyd算法功能:给定一个加权连通图,求取从每一个顶点到其它所 ...
- POJ-2240(floyd算法简单应用)
Arbitrage poj-2240 #include<iostream> #include<cstdio> #include<cstring> #include& ...
- floyd算法 青云的机房组网方案(简单)
青云的机房组网方案(简单) 青云现在要将 nn 个机房连成一个互相连通的网络.工程师小王设计出一个方案:通过在 nn 个机房之间铺设 n-1n−1 条双向的光纤,将所有的机房连接.可以假设数据在两个机 ...
- CCF(通信网络):简单DFS+floyd算法
通信网络 201709-4 一看到题目分析了题意之后,我就想到用floyd算法来求解每一对顶点的最短路.如果一个点和任意一个点都有最短路(不为INF),那么这就是符合的一个答案.可是因为题目超时,只能 ...
- 最短路径之Floyd算法
Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...
- 最短路径问题——floyd算法
floyd算法和之前讲的bellman算法.dijkstra算法最大的不同在于它所处理的终于不再是单源问题了,floyd可以解决任何点到点之间的最短路径问题,个人觉得floyd是最简单最好用的一种算法 ...
- 最短路径——Floyd算法
如何求一张图中任意两顶点之间的最短路径长度,这里写一种最简单的算法——Floyd算法: #include<stdio.h> #define inf 9999 int main() { ][ ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- Floyd 算法的动态规划本质
[转载自:http://www.cnblogs.com/chenying99/p/3932877.html] Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(A ...
随机推荐
- HK算法模板+小优化(跑的快一点点)
HUST 2604 #include <iostream> #include <cstdlib> #include <cstdio> #include <cs ...
- 在 React项目中使用 bootstrap
在使用create-react-app 创建的项目中使用 bootstrap; 安装react-bootstrap; npm install react-bootstrap --savenpm ins ...
- 强连通分量再探 By cellur925
我真的好喜欢图论啊. (虽然可能理解的并不深hhh) 上一次(暑假)我们初探了强联通分量,这一次我们再探.(特别感谢pku-lyc老师的课件.有很多引用) 上次我们忘记讨论复杂度了.tarjan老爷爷 ...
- JAVA实训总结:继承-——子类创建对象原理
继承关键字:extends Java只允许单继承存在 不可以被继承的东西: 1.private成员 体现了java的封装特点,外部类只允许set()get()方法调用,若无set()方法,则类创建的对 ...
- (转) ACM必备(学完一个就加亮一个)不多,就这些!
时间复杂度(渐近时间复杂度的严格定义,NP问题,时间复杂度的分析方法,主定理)排序算法(平方排序算法的应用,Shell排序,快速排序,归并排序,时间复杂度下界,三种线性时间排 序,外部排序)数论(整 ...
- Windows下DVWA安装指南
注意:DVWA需要依赖httpd.PHP.MySQL.php-mysql等应用或组件,最简单的方法是安装wampserver(http://www.wampserver.com/),安装完了所需的各种 ...
- ACM_求第k大元素(两次二分)
求第k大 Time Limit: 6000/3000ms (Java/Others) Problem Description: 给定两个数组A和B,大小为N,M,每次从两个数组各取一个数相乘放入数组C ...
- 转 python 将一个文件中内容添加到另一个文件指定位置
http://blog.csdn.net/huguangshanse00/article/details/14624601
- Backbone学习记录(3)
创建视图 同前面创建模型和集合的方式一样,Backbone.View.extend()即可创建视图 var UserView=Backbone.View.extend(); var view1=new ...
- oozie.log报提示:org.apache.oozie.service.ServiceException: E0104错误 An Admin needs to install the sharelib with oozie-setup.sh and issue the 'oozie admin' CLI command to update sharelib
不多说,直接上干货! 问题详情 关于怎么启动oozie,我这里不多赘述. Oozie的详细启动步骤(CDH版本的3节点集群) 然后,我在查看 [hadoop@bigdatamaster logs]$ ...