forked from kushrajpurohit18/Hacktoberfest-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Floyd_Warshall.cpp
63 lines (50 loc) · 1.34 KB
/
Floyd_Warshall.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Problem Statement
//The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem.
//The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph.
//Time Complexity: O(V^3)
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> floyd_warshall(vector<vector<int>> graph) {
vector<vector<int>> dist(graph);
int V = graph.size();
// Iterate over all phases 1,2,....,V
for(int k=0;k<V;k++) {
//Iterate over the 2-D matrix
for(int i=0;i<V;i++) {
for(int j=0;j<V;j++) {
// If vertex k is on the shortest path from
// i to j, then update the value of dist[i][j]
if(dist[i][k]!=INT_MAX && dist[k][j]!=INT_MAX) {
if(dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
//Check for negative edge cycle
for(int i=0;i<V;i++) {
if(dist[i][i] < 0) {
cout<<"Negative edge cycle detected\n";
break;
}
}
return dist;
}
int main() {
vector<vector<int>> graph = {
{0,INT_MAX,-2,INT_MAX},
{4,0,3,INT_MAX},
{INT_MAX,INT_MAX,0,2},
{INT_MAX,-1,INT_MAX,0}
};
auto it = floyd_warshall(graph);
//The shortest path from ith node to jth node
for(int i=0;i<it.size();i++) {
for(int j=0;j<it.size();j++) {
cout<<it[i][j]<<" ";
}
cout<<endl;
}
return 0;
}