Skip to content

Commit e3a8e58

Browse files
authored
Create Dynamic PrograFloydWarshallAlgorithm.cpp
1 parent 23c8357 commit e3a8e58

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define V 4
5+
#define INF 99999
6+
7+
8+
void printSolution(int dist[][V]);
9+
10+
void floydWarshall (int graph[][V])
11+
{ int dist[V][V], i, j, k;
12+
13+
for (i = 0; i < V; i++)
14+
for (j = 0; j < V; j++)
15+
dist[i][j] = graph[i][j];
16+
17+
18+
for (k = 0; k < V; k++)
19+
{ for (i = 0; i < V; i++)
20+
{
21+
for (j = 0; j < V; j++)
22+
{
23+
if (dist[i][k] + dist[k][j] < dist[i][j])
24+
dist[i][j] = dist[i][k] + dist[k][j];
25+
}
26+
}
27+
}
28+
29+
30+
printSolution(dist);
31+
}
32+
33+
34+
void printSolution(int dist[][V])
35+
{
36+
cout<<"The following matrix shows the shortest distances"
37+
" between every pair of vertices \n";
38+
for (int i = 0; i < V; i++)
39+
{
40+
for (int j = 0; j < V; j++)
41+
{
42+
if (dist[i][j] == INF)
43+
cout<<"INF"<<" ";
44+
else
45+
cout<<dist[i][j]<<" ";
46+
}
47+
cout<<endl;
48+
}
49+
}
50+
51+
52+
int main()
53+
{
54+
int graph[V][V] = { {0, 5, INF, 10},
55+
{INF, 0, 3, INF},
56+
{INF, INF, 0, 1},
57+
{INF, INF, INF, 0}
58+
};
59+
60+
61+
floydWarshall(graph);
62+
return 0;
63+
}
64+
65+

0 commit comments

Comments
 (0)