-
Notifications
You must be signed in to change notification settings - Fork 3
/
floyd-warshall.cpp
117 lines (102 loc) · 2.42 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <climits>
#include <iomanip>
using namespace std;
// Total number of vertices in the `adjMatrix`
#define N 4
// Define infinity
#define I INT_MAX
// Recursive function to print path of given
// vertex `u` from source vertex `v`
void printPath(int path[][N], int v, int u)
{
if (path[v][u] == v) {
return;
}
printPath(path, v, path[v][u]);
cout << path[v][u] << " ";
}
// Function to print the shortest cost with path
// information between all pairs of vertices
void printSolution(int cost[N][N], int path[N][N])
{
for (int v = 0; v < N; v++)
{
for (int u = 0; u < N; u++)
{
if (u != v && path[v][u] != -1)
{
cout << "The shortest path from " << v << " —> " << u << " is ("
<< v << " ";
printPath(path, v, u);
cout << u << ")" << endl;
}
}
}
}
// Function to run the Floyd–Warshall algorithm
void floydWarshall(int adjMatrix[][N])
{
// `cost[]` and `parent[]` stores shortest path
// (shortest-cost/shortest route) information
int cost[N][N], path[N][N];
// initialize `cost[]` and `parent[]`
for (int v = 0; v < N; v++)
{
for (int u = 0; u < N; u++)
{
// initially, cost would be the same as the weight of the edge
cost[v][u] = adjMatrix[v][u];
if (v == u) {
path[v][u] = 0;
}
else if (cost[v][u] != INT_MAX) {
path[v][u] = v;
}
else {
path[v][u] = -1;
}
}
}
// run Floyd–Warshall
for (int k = 0; k < N; k++)
{
for (int v = 0; v < N; v++)
{
for (int u = 0; u < N; u++)
{
// If vertex `k` is on the shortest path from `v` to `u`,
// then update the value of `cost[v][u]` and `path[v][u]`
if (cost[v][k] != INT_MAX && cost[k][u] != INT_MAX
&& cost[v][k] + cost[k][u] < cost[v][u])
{
cost[v][u] = cost[v][k] + cost[k][u];
path[v][u] = path[k][u];
}
}
// if diagonal elements become negative, the
// graph contains a negative-weight cycle
if (cost[v][v] < 0)
{
cout << "Negative-weight cycle found!!";
return;
}
}
}
// Print the shortest path between all pairs of vertices
printSolution(cost, path);
}
int main()
{
// given adjacency representation of the matrix
int adjMatrix[N][N] =
{
{ 0, I, -2, I },
{ 4, 0, 3, I },
{ I, I, 0, 2 },
{ I, -1, I, 0 }
};
// Run Floyd–Warshall algorithm
floydWarshall(adjMatrix);
return 0;
}