Floyd Warshall算法

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:

邻接矩阵:

floyd warshall algorithm (1)

Example:

例:

So the graph for the above input is,

因此,上述输入的图形为

floyd warshall algorithm (2)


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算法。

floyd warshall algorithm (5)

So, initially the shortest distance table is,

因此,最短距离表最初是

floyd warshall algorithm (3)

After updating the shortest distance from A to other nodes,

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

floyd warshall algorithm (4)

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540675.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java多线程系列--“基础篇”09之 interrupt()和线程终止方式

2019独角兽企业重金招聘Python工程师标准>>> Java多线程系列--“基础篇”09之 interrupt()和线程终止方式 概要 本章&#xff0c;会对线程的interrupt()中断和终止方式进行介绍。涉及到的内容包括&#xff1a;1. interrupt()说明2. 终止线程的方式 2.1 终止处于“阻…

mac活动监视器_什么是活动监视器?

mac活动监视器活动监控 (Activity Monitor) Apple OS X provides the services of which one of them is Activity Monitor. Activity Monitor is used to monitor the activities of computer like active processes, processor load, applications that are running, and the…

concurrent包下的Exchanger练习

Exchanger可以在两个线程之间交换数据&#xff0c;只能是2个线程&#xff0c;他不支持更多的线程之间互换数据。 当线程A调用Exchange对象的exchange()方法后&#xff0c;他会陷入阻塞状态&#xff0c;直到线程B也调用了exchange()方法&#xff0c;然后以线程安全的方式交换数据…

CChelper彩虹SDK可视远程客服解决方案

本文讲的是 : CChelper彩虹SDK可视远程客服解决方案 , 在智能生态产业链中&#xff0c;智能硬件终端是把握消费者的直接环节&#xff0c;随着物联网时代迈向成熟&#xff0c;智能家居领域的硬件逐渐成为智能硬件终端的主角。目前的市场环境下&#xff0c;智能家居领域的自身硬…

php 单例模式有什么缺点_PHP的完整形式是什么?

php 单例模式有什么缺点PHP&#xff1a;超文本预处理器 (PHP: Hypertext Preprocessor ) PHP is an abbreviation of Hypertext Preprocessor, earlier called Personal Home Page. PHP is extensively used HTML-embedded, open-source server-side scripting language create…

最小跳数

Description: 描述&#xff1a; This problem is a standard interview problem which has been featured in interview rounds of Adobe, Amazon, Oyo rooms etc. 此问题是标准的采访问题&#xff0c;已在Adobe&#xff0c;Amazon&#xff0c;Oyo房间等的采访回合中出现。 P…

BE的完整形式是什么?

工学学士 (BE: Bachelor of Engineering) BE is an abbreviation of Bachelor of Engineering. It is a bachelors degree program for under graduation in engineering and the duration of this course is 4 years. It is provided in many countries like India, Canada, S…

史上最详细Windows版本搭建安装React Native环境配置

说在前面的话: 感谢同事金晓冰倾情奉献本环境搭建教程 之前我们已经讲解了React Native的OS X系统的环境搭建以及配置&#xff0c;鉴于各大群里有很多人反应在Windows环境搭建出现各种问题&#xff0c;今天就特意更新一贴来说明。关于os x环境搭建以及react native入门学习资料…

Web浏览器端通过https 使用mqtt通讯

做的产品简介 这次需要做一个web端的上课平台&#xff0c;有音视频通讯&#xff0c;有白板(画板)功能&#xff0c;有文字通讯等。技术点 音视频通讯需要走Webrtc需要跟ios, android, windows, mac 客户端互联互通一般通讯通过mqtt协议MQTT简介 MQTT&#xff08;Message Queuing…

vga显示模式_VGA的完整形式是什么?

vga显示模式VGA&#xff1a;视频图形阵列 (VGA: Video Graphics Array) VGA is an abbreviation of "Video Graphics Array". VGA是“视频图形阵列”的缩写 。 It is a three-row 15-pin DE-15 connector display hardware developed by IBM in 1987. It was first …

【iCore4 双核心板_FPGA】例程十一:FSMC总线通信实验——独立地址模式

实验原理&#xff1a; STM32F767上自带FMC控制器&#xff0c;本实验将通过FMC总线的地址独立模式实现STM32与FPGA 之间通信&#xff0c;FPGA内部建立RAM块,FPGA桥接STM32和RAM块&#xff0c;本实验通过FSMC总线从STM32向 RAM块中写入数据&#xff0c;然后读取RAM出来的数据进行…

http 412 precondition failed

2019独角兽企业重金招聘Python工程师标准>>> 今天在谷歌浏览器上刷新页面的时候&#xff0c;出现了 如下失败信息&#xff1a; HTTP 412 (Precondition Failed) 想想当时的动作是在发送ajax请求失败之后&#xff0c;再刷新&#xff0c;就会出现上面的失败问题。百度…

Python | Pyplot标签

There are the following types of labels, 标签有以下几种&#xff0c; 1)X轴贴标 (1) X-axis labelling) plt.xlabel(Number Line)# Default labellingplt.xlabel(Number Line, colorgreen)#Font colour Changedplt.xlabel(Number Line, colorGreen, fontsize15)#Font size …

MySQL Index Condition Pushdown

2019独角兽企业重金招聘Python工程师标准>>> 一、Index Condition Pushdown简介 ICP&#xff08;index condition pushdown&#xff09;是mysql利用索引&#xff08;二级索引&#xff09;元组和筛字段在索引中的where条件从表中提取数据记录的一种优化操作。ICP的思…

java.util (Collection接口和Map接口)

1&#xff1a;Collection和Map接口的几个主要继承和实现类 1.1 Collection接口 Collection是最基本的集合接口&#xff0c;一个Collection代表一组Object&#xff0c;即Collection的元素&#xff08;Elements&#xff09;。一些Collection允许相同的元素而另一些不行。一些能排…

asp.net MVC5为WebAPI添加命名空间的支持

前言 默认情况下&#xff0c;微软提供的MVC框架模板中&#xff0c;WebAPI路由是不支持Namespace参数的。这导致一些比较大型的项目&#xff0c;无法把WebApi分离到单独的类库中。 本文将提供解决该问题的方案。 微软官方曾经给出过一个关于WebAPI支持Namespace的扩展&#xff0…

python无符号转有符号_Python | 散布符号

python无符号转有符号There are multiple types of Scatter Symbols available in the matplotlib package and can be accessed through the command marker. In this article, we will show some examples of different marker types and also present a list containing all…

在eclipse中启动Tomcat访问localhost:8080失败项目添加进Tomcat在webapp中找不到

软件环境&#xff1a;Eclipse oxygen&#xff0c; Tomcat8.5 #在eclipse中启动Tomcat访问localhost:8080失败 在eclipse中配置tomcat后&#xff0c;打开tomcat后访问localhost:8080后无法出现登陆成功的界面,即无法出现下面的界面 在eclipse中的servers状态栏中双击tomcat&…

程序员简历工作模式_简历的完整形式是什么?

程序员简历工作模式简历&#xff1a;简历 (CV: Curriculum Vitae) The CV is an abbreviation of Curriculum Vitae. It is a written outline summary of a persons educational training and qualifications and his other experiences. It is an absolute profile of a cand…

ajax的访问 WebService 的方法

转自原文 ajax的访问 WebService 的方法 如果想用ajax进行访问 首先在web.config里进行设置 添加在 <webServices> <protocols> <add name "HttpPost" /> <add name "HttpGet" /> </protocols> </webServices> <s…