-
Notifications
You must be signed in to change notification settings - Fork 0
/
Floyd's Algorithm.c
56 lines (49 loc) · 1.41 KB
/
Floyd's Algorithm.c
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
#include <stdio.h>
void floyd(int n, int cost[10][10],int d[10][10])
{
int i,j,k;
for ( i = 1; i <= n; i++)
{
for ( j = 1; j <= n; j++)
{
d[i][j] = cost[i][j];
}
}
for ( k = 1; k <= n; k++ )
{
for ( i = 1; i <= n; i++ )
{
for ( j = 1; j <= n; j++ )
{
if ( d[i][k] + d[k][j] < d[i][j] )
{
d[i][j] = d[i][k] + d[k][j];
}
}
}
}
}
void main()
{
int i,j,n,cost[10][10],p[10][10],d[10][10];
printf("Enter the no. of nodes\n");
scanf("%d",&n);
printf("Enter the cost adjacency matrix\n");
for ( i = 1; i <= n; i++)
{
for ( j = 1; j <= n; j++)
{
scanf("%d",&cost[i][j]);
}
}
floyd(n,cost,d);
printf("The distance matrix is shown below\n");
for ( i = 1; i <= n; i++)
{
for ( j = 1; j <= n; j++)
{
printf("%5d",d[i][j]);
}
printf("\n");
}
}