-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydWarshell_Algorithm_All_Source_Shortest_Path.cpp
More file actions
91 lines (75 loc) · 2.38 KB
/
Copy pathFloydWarshell_Algorithm_All_Source_Shortest_Path.cpp
File metadata and controls
91 lines (75 loc) · 2.38 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <iomanip>
#define V 4
#include "climits"
using namespace std;
void printPath(int predecessor[][V], int i, int j)
{
if (predecessor[i][j] == i)
return;
printPath(predecessor, i, predecessor[i][j]);
cout << predecessor[i][j] << " ";
}
void FloydWarshell_Algorithm_All_Source_Shortest_Path(int adjMatrix[V][V]){
int distance[V][V],predecessor[V][V];
//initialising distance and predecessor
for(auto i = 0 ;i < V; i++){
for(auto j = 0; j < V ;j++){
distance[j][i] = adjMatrix[j][i];
if(i == j) predecessor[j][i] = 0 ;
else if (distance[j][i] != INT_MAX) predecessor[j][i] = j;
else predecessor[j][i] = -1;
}
}
//run floyd warshall
for (int k = 0; k < V; ++k) {
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
if(distance[i][k] != INT_MAX && distance[k][j] != INT_MAX && distance[i][k] + distance[k][j] < distance[i][j]){
distance[i][j] = distance[i][k] + distance[k][j];
predecessor[i][j] = predecessor[k][j];
}
}
if(distance[i][i] < 0) {
cout <<"Negative Weight Cycle Found!";
return;
}
}
}
//printing the adjacencry matrix of distance [][]
for (int v = 0; v < V; v++)
{
for (int u = 0; u < V; u++)
{
if (distance[v][u] == INT_MAX)
cout << setw(5) << "inf";
else
cout << setw(5) << distance[v][u];
}
cout << endl;
}
cout << endl;
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (j != i && predecessor[i][j] != -1)
{
cout << "Shortest Path from vertex " << i << " to vertex " << j << " is (" << i << " ";
printPath(predecessor, i, j);
cout << j << ")" << endl;
}
}
}
}
int main(){
int adjMatrix[V][V] =
{
{ 0, INT_MAX, -2, INT_MAX },
{ 4, 0, 3, INT_MAX },
{ INT_MAX, INT_MAX, 0, 2 },
{ INT_MAX, -1, INT_MAX, 0 }
};
FloydWarshell_Algorithm_All_Source_Shortest_Path(adjMatrix);
return 0;
}