forked from The-Open-Source-Society/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Floyd_Warshall_Algorithm.cpp
66 lines (55 loc) · 1.62 KB
/
Floyd_Warshall_Algorithm.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
#include <iostream>
#include <limits.h>
#include <iomanip>
#define Infinity INT_MAX
#define A 4
using namespace std;
void FloydWarshall(int graph[A][A]);
void output(int length[A][A]);
void FloydWarshall(int graph[A][A])
{
int length[A][A],x,y,z;
for(x = 0; x < A; x++)
for(y = 0; y < A; y++)
length[x][y] = graph[x][y];
for(z = 0; z < A; z++)
for(x = 0; x < A; x++)
for(y = 0; y < A; y++)
{ if(length[x][z] != Infinity &&
length[z][y] != Infinity &&
length[x][z] + length[z][y] < length[x][y])
length[x][y] = length[x][z] + length[z][y];
}
output(length);
}
void output(int length[A][A])
{
cout << "The matrix below shows the shortest distances between each pair of vertices\n";
for (int x = 0; x < A; x++)
{
for (int y = 0; y < A; y++)
{
if (length[x][y] == Infinity)
cout << setw(12) << "INFINITY";
else
cout << setw(12) << length[x][y];
}
cout << endl;
}
}
int main() {
int graph[A][A] = { {0, 8, Infinity, 15},
{Infinity, 0, 7, Infinity},
{Infinity, Infinity, 0, 7},
{Infinity, Infinity, Infinity, 0}
};
FloydWarshall(graph);
return 0;
}
/* OUTPUT
The matrix below shows the shortest distances between each pair of vertices
0 8 15 15
INFINITY 0 7 14
INFINITY INFINITY 0 7
INFINITY INFINITY INFINITY 0
*/