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 ...
随机推荐
- 骨骼蒙皮动画(SkinnedMesh)的原理解析(一)
http://blog.csdn.net/jimoshuicao/article/details/9253999 一)3D模型动画基本原理和分类 3D模型动画的基本原理是让模型中各顶点的位置随时间变化 ...
- C#递归拷贝文件夹下文件以及文件夹
public bool CopyOldLabFilesToNewLab(string sourcePath, string savePath) { if (!Directory.Exists(save ...
- springboot&mybatis 增删改查系列(二)
数据库篇 我的数据库名为data0525,数据表名为user,其中有五列uid,uname,upass,usex,umessage.uid为主键并且自动生成,由于是练习表,所以并没有考虑设计的合理性. ...
- C语言精确微秒级的延时
//----------------------------------------------------------------------------- // Delay_us //------ ...
- MySQL系列:隐式类型转化可能带来的坑
在开发规范中,我们往往会要求研发避免在where条件中出现隐式类型转换,这么要求大概有以下两方面的原因: 隐式类型转换可能导致索引失效: 隐式类型转换可能产生非预期的结果. 注:这里说的是隐式类型转换 ...
- idea DeBug调试学习
在Intellij IDEA中使用Debug 目录 一.Debug开篇 二.基本用法&快捷键 三.变量查看 四.计算表达式 五.智能步入 六.断点条件设置 七.多线程调试 八.回退断点 九.中 ...
- Java开发笔记(九十五)NIO配套的文件工具Files
NIO不但引进了高效的文件通道,而且新增了更加好用的文件工具家族,包括路径组工具Paths.路径工具Path.文件组工具Files.先看路径组工具Paths,该工具提供了静态方法get,输入某个文件的 ...
- LN : leetcode 486 Predict the Winner
lc 486 Predict the Winner 486 Predict the Winner Given an array of scores that are non-negative inte ...
- jQuery选择器之可见性选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- 初学者可能不知道的vue技巧
前言 大家好,这里是@IT·平头哥联盟,我是首席甩锅官——老金,今天给大家分享的,一些日常中神秘而又简单的vue的实用小技巧,以及我在我司项目中实用vue的总结和坑,跟大家一起分享,希望能给其他攻城狮 ...