Description:

描述:

This is a very popular interview problem to find all pair shortest paths in any graph. This problem has been featured in interview rounds of Samsung.

这是一个非常流行的面试问题,用于在任何图中找到所有对最短路径。 该问题已在三星的采访回合中提到。

Problem statement:

问题陈述:

Given a weighted directed graph, the problem is to find the shortest distances between every pair of vertices. The Graph is represented by an adjacency matrix, and any cell arr[i][j] denotes the weight of the edge (path cost) from node i to node j (if it exists) else INF.

给定一个加权有向图,问题在于找到每对顶点之间的最短距离。 该图由邻接矩阵表示,任何单元格arr [i] [j]表示从节点i到节点j (如果存在)或其他INF的边的权重(路径成本)。

Input: N=5

输入: N = 5

Adjacency matrix:

邻接矩阵:

Example:

例:

So the graph for the above input is,

因此,上述输入的图形为

Figure 1: Directed Graph for which all pair shortest distance path needed to be found

图1:有向图,需要找到所有对的最短距离路径

    A → B: 5
A → C: 1
A → D: 3
A → E: 10
B → A: INF
B → C: INF
B → D: INF
B → E: 4
C → A: INF
C → B: 2
C → D: INF
C → E: INF
D → A: INF
D → B: INF
D → C: INF
D → E: 5
E → A: INF
E → B: INF
E → C: INF
E → D: INF

Problem solution:

问题方案:

The Floyd Warshall algorithm computes the all pair shortest path in any weighted graph from the adjacency matrix. It also works for negative weight edges.

Floyd Warshall算法根据邻接矩阵计算任何加权图中的所有对最短路径。 它也适用于负重量边缘。

The algorithm is very simple to compute. Basically to compute the shortest path between ith node to jth node we check whether there is an intermediate node that reduces the distance, i.e., the path cost.

该算法非常容易计算。 基本上,为了计算第i 节点到 j 节点之间的最短路径,我们检查是否存在缩短距离的中间节点,即路径成本。

Let,

让,

    D(i,j) = Distance from ith node to ith node

We check for whether there is any intermediate node, say v such that,

我们检查是否存在中间节点,例如v,

    D(i,u) + D(u,j) < D(i,j), for any intermidiate node u,uЄ[1,n]and u≠i,u≠j

Initially we consider the adjacency matrix to be the shortest distance table.

最初,我们认为邻接矩阵是最短距离表。

Based on this concept, the Floyd-Warshall algorithm is designed.

基于此概念,设计了Floyd-Warshall算法。

So, initially the shortest distance table is,

因此,最短距离表最初是

After updating the shortest distance from A to other nodes,

更新了从A到其他节点的最短距离后,

So on.

等等。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
void FloydWarshal(long long int** arr, int n)
{for (int i = 0; i < n; i++) { //source node
for (int j = 0; j < n; j++) { //destination node
for (int k = 0; k < n; k++) { //intermediate node
// if shortest path via the intermediate node exists
if (arr[i][k] != INT_MAX && arr[k][j] != INT_MAX && arr[i][j] > arr[i][k] + arr[k][j])
arr[i][j] = arr[i][k] + arr[k][j]; //update shortest distance
}
}
}
cout << "Printing all pair shortest path distance\n";
for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cout << char(i + 'A') << "->" << char(j + 'A') << ":";
if (arr[i][j] == INT_MAX)
cout << "INF"
<< "\n";
else
cout << arr[i][j] << "\n";
}
}
}
int main()
{int n;
string item;
cout << "Number of node in the graph is:\n";
cin >> n;
long long int** arr = (long long int**)(malloc(sizeof(long long int*) * n));
cout << "Enter the weights\n";
//build the adjacency matrix
for (int j = 0; j < n; j++) {arr[j] = (long long int*)(malloc(sizeof(long long int) * n));
for (int k = 0; k < n; k++) {cout << "Enter weight of " << char(j + 'A') << " -> " << char(k + 'A') << endl;
cin >> item;
if (item == "INF")
arr[j][k] = INT_MAX;
else
arr[j][k] = stoi(item);
}
}
// function to compute all pair shortest distance
FloydWarshal(arr, n);
return 0;
}

Output

输出量

Number of node in the graph is:
5
Enter the weights
Enter weight of A -> A
0
Enter weight of A -> B
5
Enter weight of A -> C
1
Enter weight of A -> D
3
Enter weight of A -> E
10
Enter weight of B -> A
INF
Enter weight of B -> B
0
Enter weight of B -> C
INF
Enter weight of B -> D
INF
Enter weight of B -> E
4
Enter weight of C -> A
INF
Enter weight of C -> B
2
Enter weight of C -> C
0
Enter weight of C -> D
INF
Enter weight of C -> E
INF
Enter weight of D -> A
INF
Enter weight of D -> B
INF
Enter weight of D -> C
INF
Enter weight of D -> D
0
Enter weight of D -> E
5
Enter weight of E -> A
INF
Enter weight of E -> B
INF
Enter weight of E -> C
INF
Enter weight of E -> D
INF
Enter weight of E -> E
0
Printing all pair shortest path distance
A->A:0
A->B:3
A->C:1
A->D:3
A->E:7
B->A:INF
B->B:0
B->C:INF
B->D:INF
B->E:4
C->A:INF
C->B:2
C->C:0
C->D:INF
C->E:6
D->A:INF
D->B:INF
D->C:INF
D->D:0
D->E:5
E->A:INF
E->B:INF
E->C:INF
E->D:INF
E->E:0

翻译自: https://www.includehelp.com/icp/floyd-warshall-algorithm.aspx

Floyd Warshall算法相关推荐

  1. C++floyd warshall算法求最短路径(附完整源码)

    C++floyd warshall算法求最短路径 floyd warshall算法求最短路径的完整源码(定义,实现,main函数测试) floyd warshall算法求最短路径的完整源码(定义,实现 ...

  2. 浅谈Warshall算法

    Warshall算法 ​ 今天的离散数学课后作业里有需要求传递闭包的题目,不懂上课没听,本来想用matlab偷一下懒,但是搜到了Warshall算法,故参考百科及其它博客后写水篇博客. 传递性 ​ 了 ...

  3. 【算法】弗洛伊德(Floyd)算法

    这个算法主要要弄懂三个循环的顺序关系. 弗洛伊德(Floyd)算法过程: 1.用D[v][w]记录每一对顶点的最短距离. 2.依次扫描每一个点,并以其为基点再遍历所有每一对顶点D[][]的值,看看是否 ...

  4. 基于Warshall算法的连通图及欧拉图判定方法

    1736年欧拉解决了哥尼斯堡七桥问题.他在这一具体问题的基础上进一步研究,最终找到了一个简便的原则可以鉴别一个图(多重图)能否一笔画成. 本文中,笔者使用布尔矩阵来存储一个无向图,并结合集合论中&qu ...

  5. C语言用warshall算法求传递闭包transitive closure(附完整源码)

    用warshall算法求传递闭包transitive closure warshall算法求传递闭包完整源码 warshall算法求传递闭包完整源码 #include <stdbool.h> ...

  6. O-矩阵相乘-Warshall算法详解

    给出2个N * N的矩阵M1和M2,输出2个矩阵相乘后的结果. Input第1行:1个数N,表示矩阵的大小(2 <= N <= 100)  第2 - N + 1行,每行N个数,对应M1的1 ...

  7. 七、最短路径——弗洛伊德(Floyd)算法

    为了能讲明白弗洛伊德(Floyd)算法的精妙所在,我们先来看最简单的案例.下图是一个最简单的3个顶点连通网图. 我们先定义两个二维数组D[3][3]和P[3][3],D代表顶点到顶点的最短路径权值和的 ...

  8. 【数据结构】图—弗洛伊德(Floyd)算法

    前言 上文介绍了迪杰斯特拉(Dijkstra)算法,计算网图的某个源点到其余各个顶点的最短路径问题(边权值为非负值),本文介绍另一个求最短路径的算法--弗洛伊德算法,它是计算所有顶点到所有顶点的最短路 ...

  9. Warshall算法(用法详解,并转换成代码的形式)

    关于Warshall算法,我先通过离散数学中求传递闭包来解释他的使用规则. 一般的,给定一个矩阵A(行列相等),我们对其使用Warshall算法: //注,该矩阵上只有0或1两种元素,做加法时,1+1 ...

最新文章

  1. 在VC6.0中创建工程并输入源代码
  2. 中国文学发展史思维导图
  3. table中head表头固定,body滚动
  4. visual c 语言参考手册pdf,Microsoft-Visual-C-6-0语言参考手册(三).pdf
  5. [置顶] 均衡音效
  6. H2O_Hyper_V-master网页端管理程序源码
  7. uinty粒子系统子物体变大_新Unity 最新粒子系统如何用代码改变参数
  8. 云服务 华为p10 短信_苹果、小米、华为,手机云服务哪家强?
  9. android shape 绘制气泡图,气泡图-自定义 shape
  10. java hql 连接查询,java – 如何从HQL表单中的两个连接表查询中选择*?
  11. OpenCasCade – 载入IGES文件
  12. Linux卸载JDK的方法
  13. Web UI设计基础
  14. 实现黑客帝国中的代码雨 快进来学(附源代码)
  15. R 回归 虚拟变量na_互助问答第92期:虚拟变量问题
  16. 下列python语句的输出结果是_下列Python语句的输出结果是 __________ 。 print(数量%4d,单价%3.3f %(100,285.6)) (3.0分)_学小易找答案...
  17. [cadfil] 基于隐形飞机喷气式发动机双S弯喷管的纤维缠绕工艺
  18. 飞桨领航团AI达人创造营第一课2021.7.26
  19. jm8.6之参数,函数简介
  20. Scancode到Keycode的映射

热门文章

  1. (三)图像转灰度图Python实现
  2. oracle 10g rac 修改sga_target不生效,Oracle Rac 修改SGA_TARGET值无变化
  3. php 前端模板 yii,php – Yii2高级模板:添加独立网页
  4. java override 访问权限_java基础之——访问修饰符(private/default/protected/public)
  5. linux脚本格式模板,Linux Shell 常见的命令行格式简明总结
  6. skywalking使用方法_SkyWalking 源码分析—— Collector Server Component 服务器组件
  7. python爬取哔哩哔哩视频_Python实现视频爬取下载
  8. python进行数据分析需要安装哪两个库_对Python进行数据分析_关于Package的安装问题...
  9. (转)JVM监控工具介绍
  10. Shell字符串操作集合