日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Floyd Warshall算法

發(fā)布時(shí)間:2025/3/11 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Floyd Warshall算法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

這是一個(gè)非常流行的面試問題,用于在任何圖中找到所有對最短路徑。 該問題已在三星的采訪回合中提到。

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.

給定一個(gè)加權(quán)有向圖,問題在于找到每對頂點(diǎn)之間的最短距離。 該圖由鄰接矩陣表示,任何單元格arr [i] [j]表示從節(jié)點(diǎn)i到節(jié)點(diǎn)j (如果存在)或其他INF的邊的權(quán)重(路徑成本)。

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: 5A → C: 1A → D: 3A → E: 10B → A: INFB → C: INFB → D: INFB → E: 4C → A: INFC → B: 2C → D: INFC → E: INFD → A: INFD → B: INFD → C: INFD → E: 5E → A: INFE → B: INFE → C: INFE → 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算法根據(jù)鄰接矩陣計(jì)算任何加權(quán)圖中的所有對最短路徑。 它也適用于負(fù)重量邊緣。

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.

該算法非常容易計(jì)算。 基本上,為了計(jì)算第i 個(gè)節(jié)點(diǎn)到 j 個(gè)節(jié)點(diǎn)之間的最短路徑,我們檢查是否存在縮短距離的中間節(jié)點(diǎn),即路徑成本。

Let,

讓,

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

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

我們檢查是否存在中間節(jié)點(diǎn),例如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.

最初,我們認(rèn)為鄰接矩陣是最短距離表。

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

基于此概念,設(shè)計(jì)了Floyd-Warshall算法。

So, initially the shortest distance table is,

因此,最短距離表最初是

After updating the shortest distance from A to other nodes,

更新了從A到其他節(jié)點(diǎn)的最短距離后,

So on.

等等。

C++ implementation:

C ++實(shí)現(xiàn):

#include <bits/stdc++.h> using namespace std;void FloydWarshal(long long int** arr, int n) {for (int i = 0; i < n; i++) { //source nodefor (int j = 0; j < n; j++) { //destination nodefor (int k = 0; k < n; k++) { //intermediate node// if shortest path via the intermediate node existsif (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";elsecout << 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 matrixfor (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;elsearr[j][k] = stoi(item);}}// function to compute all pair shortest distanceFloydWarshal(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

總結(jié)

以上是生活随笔為你收集整理的Floyd Warshall算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。