-
Notifications
You must be signed in to change notification settings - Fork 0
/
Warshall's Algorithm.c
57 lines (49 loc) · 1.43 KB
/
Warshall'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
57
#include <stdio.h>
void warshall(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][j] == 0 && (d[i][k] ==1 && d[k][j] ==1) )
{
d[i][j] = 1;
}
}
}
}
}
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]);
}
}
warshall(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");
}
}