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.pswp.cn/news/540675.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540675.shtml
英文地址,請注明出處:http://en.pswp.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;然后以線程安全的方式交換數據…

Python默認參數

Python | 默認參數 (Python | default parameters) A default parameter is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesnt provide a value for the parameter with the default value. …

最長公共前綴_最長的公共前綴

最長公共前綴Problem statement: 問題陳述&#xff1a; Write a function to find the longest common prefix string amongst an array of strings. 編寫函數以在字符串數組中找到最長的公共前綴字符串 。 If there is no common prefix, return an empty string "&quo…

物聯網聽起來像是一個和互聯網不同的網,萬物互聯又把網給弄丟了,正向我們撲面而來的是萬物互聯網。...

物聯網聽起來像是一個和互聯網不同的網&#xff0c;"萬物互聯"又把"網"給弄丟了&#xff0c;正向我們撲面而來的是"萬物互聯網"。轉載于:https://www.cnblogs.com/beingonline/p/7484135.html

sdram trp_TRP的完整形式是什么?

sdram trpTRP&#xff1a;電視收視點 (TRP: Television Rating Point) TRP is an abbreviation of "Television Rating Point". TRP是“電視評分點”的縮寫 。 It is a system or standard of measurement which signifies the demand and popularity of a televisi…

Controller計算值傳到jsp頁面,用session傳值

HttpSession session request.getSession(); session.setAttribute("key",value); jap 用 ${key}來接收該值 轉載于:https://www.cnblogs.com/douder/p/7484491.html

CBT的完整形式是什么?

CBT&#xff1a;基于計算機的培訓 (CBT: Computer Based Training) CBT is an abbreviation of "Computer-based training". CBT是“基于計算機的培訓”的縮寫 。 It is a training program which entails the use of a personal system or networked computer. The…

論道社會化商業

主持人 用友優普副總裁傅毅&#xff1a; 謝謝各位嘉賓&#xff0c;我們會留一些時間讓在座的嘉賓提問。請各位嘉賓用一個非常簡單的一句話&#xff0c;或者幾個關鍵詞&#xff0c;總結一下你認為的社會化商業是什么&#xff1f; 用友優普執行總裁 徐洋&#xff1a; 社會化商業為…

CChelper彩虹SDK可視遠程客服解決方案

本文講的是 : CChelper彩虹SDK可視遠程客服解決方案 , 在智能生態產業鏈中&#xff0c;智能硬件終端是把握消費者的直接環節&#xff0c;隨著物聯網時代邁向成熟&#xff0c;智能家居領域的硬件逐漸成為智能硬件終端的主角。目前的市場環境下&#xff0c;智能家居領域的自身硬…

matlab 簡介_MATLAB簡介

matlab 簡介MATLAB簡介 (MATLAB Introduction) MATLAB was designed by Cleve Moler for his student in 1970s but after some time jack little, an engineer realized its potential and rewrote it at the MathWorks, and it was rewritten in C language by the date of 1…

Scala中的嵌套循環

Scala中的嵌套循環 (Nested loop in Scala) In programming, a nested loop is used in initializing or iterate multi-dimensional array or to print patterns. Scala provides an efficient method to use nested loops in the programming language. The most used nested…

python基礎-字典

字典 # 字典是python基本數據結構之一&#xff0c;相對于列表和元組&#xff0c;他是無序的&#xff0c;每次輸出都打亂了順序&#xff0c;沒有下標hello{110:{"name":"alex","age":28,"home":"shandong"},111:{"name&…

sql算術運算符_SQL中的算術運算符

sql算術運算符SQL | 算術運算符 (SQL | Arithmetic Operators) Different number-crunching administrators are utilized in SQL to be specific Addition (), Subtraction (-), Multiplication (*), Division (/), Modulus (%). SQL中使用了不同的數字運算管理員來表示特定的…

HDU 6188 Duizi and Shunzi

棧。 將數字排序后&#xff0c;一個一個壓入棧。如果棧頂兩個元素形成了對子&#xff0c;那么$ans1$&#xff0c;彈出棧頂兩個元素&#xff1b;如果棧頂三個元素形成了順子&#xff0c;那么$ans1$&#xff0c;彈出棧頂三個元素。 #include<bits/stdc.h> using namespace …

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…

Myeclipse有關的問題

Myeclipse配置問題 1.行數顯示 window ----preference----General-----Editors-----TextEditors----show line numbers 2.編碼設置 window ---preference----workspace-----設置 3.jsp編碼設置 window ---preference----myeclipse------Files And Editors------jsp 4.jsp的視圖…

weak-to-strong-generalization始終比母體更智能的人工智能,能否被它的母體所監管supervision,從而變的更強

正如supervison這個詞&#xff0c;就像就是母親對孩子的超級super愿景vision&#xff0c;比母親更聰明更強&#xff0c;也就意味著要按照母親期望的那樣成長&#xff0c;不合理的行為要能夠糾正supervison。 一代比一代強&#xff0c;一代比一代好。 弱模型監督能否激發出更強…

最小跳數

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…