-
Notifications
You must be signed in to change notification settings - Fork 4
/
floyd_warshall(dynamic_programming).cpp
executable file
·102 lines (93 loc) · 2.37 KB
/
floyd_warshall(dynamic_programming).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
#include <iostream>
using namespace std;
#define n 4
#define INF 9999
class floyd_warshall
{
private:
int ch;
public:
void class_main();
void warshall(int W[][n]);
void display(int d[][n]);
};
void floyd_warshall::class_main()
{
int G[n][n] = { {0, 5, INF, INF},
{50, 0, 15, 5},
{30, INF, 0, 15},
{15, INF, 5, 0}
};
cout<<"\nThe graph is (D0) : \n";
display(G);
cout<<"\n1. Display all the distance graphs from D1 to D"<<n<<"\n2. Display the final shortest distance graph D"<<n;
cout<<"\nEnter your choice : ";
cin>>ch;
cout<<"\nShortest Path between all vertices using Floyd Warshall's Algorithm is : \n";
warshall(G);
}
void floyd_warshall::warshall(int G[][n])
{
int d[n][n], i, j, k;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
d[i][j] = G[i][j];
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if ( d[i][j] > (d[i][k] + d[k][j]) )
d[i][j] = d[i][k] + d[k][j];
}
}
if(ch==1)
{
cout<<"D"<<k+1<<" : \n";
display(d);
}
}
if(ch==2)
{
display(d);
}
}
void floyd_warshall::display(int d[][n])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (d[i][j] == INF)
cout<<"\t"<<"INF";
else
cout<<"\t"<<d[i][j];
}
cout<<endl;
}
cout<<endl;
}
int main()
{
floyd_warshall w;
w.class_main();
return 0;
}
/*OUTPUT
C:\Users\Admin\Downloads\DAA Codes>g++ warshall.cpp
C:\Users\Admin\Downloads\DAA Codes>a
The graph is (D0) :
0 5 INF INF
50 0 15 5
30 INF 0 15
15 INF 5 0
1. Display all the distance graphs from D1 to D4
2. Display the final shortest distance graph D4
Enter your choice : 2
Shortest Path between all vertices using Floyd Warshall's Algorithm is :
0 5 15 10
20 0 10 5
30 35 0 15
15 20 5 0
*/