-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFloyd_Marshall_Algorithm
More file actions
61 lines (57 loc) · 1.19 KB
/
Floyd_Marshall_Algorithm
File metadata and controls
61 lines (57 loc) · 1.19 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
#include<bits/stdc++.h>
using namespace std;
#define V 4;
void floydWarshall(int graph[][4])
{
int i,j,k;
int dist[4][4];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
dist[i][j] =graph[i][j];
}
}
for(k=0;k<4;k++)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(dist[i][j]>(dist[i][k]+dist[k][j]))
{
dist[i][j] = dist[i][k]+dist[k][j];
}
}
}
}
cout<<"The following matrix shows the shortest distances between every pair of vertices \n";
for (i = 0; i <4; i++) {
for (j = 0; j<4; j++) {
if (dist[i][j]==9999)
cout << "INF"<< " ";
else
cout<<dist[i][j] << " ";
}
cout<<endl;
}
}
int main()
{
/* Let us create the following weighted graph
10
(0)------->(3)
| /|\
5 | |
| | 1
\|/ |
(1)------->(2)
3 */
int graph[4][4] = { { 0, 5, 9999, 10 },
{ 9999, 0, 3, 9999 },
{ 9999, 9999, 0, 1 },
{ 9999, 9999, 9999, 0 } };
// Print the solution
floydWarshall(graph);
return 0;
}