-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_pair_shortest_FLYOD.c
111 lines (99 loc) · 2.42 KB
/
all_pair_shortest_FLYOD.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
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
103
104
105
106
107
108
109
110
111
/* All pair shortest path FLYOD'S METHOD */
/* FLYOD.C */
# include<stdio.h>
# include<conio.h>
# define Infinity 9999
# define size 20
int i,k,j;
void Input(int , int a[size][size]);
void Output(int , int a[size][size]);
void Flyod(int , int a[size][size], int r[size][size]);
/* Input function */
void Input(int n, int a[size][size])
{
int i, j;
printf("\n Input adjacency matrix (one row at a time with spaces and 9999 for infinity): \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
printf("\n");
}
}
/* Output function */
void Output (int n, int a[size][size])
{
int i, j;
for (i =0; i < n; i++)
{
for (j =0; j < n; j++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
}
/* Shortest path evaluation function */
void Flyod(int n, int a[size][size], int r[size][size])
{
for (i = 0; i < n; i ++)
a[i][i] = 0; /* Set diagonal elements zero */
printf("\n After setting diagonal elements to zero \n");
Output(n, a);
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if ((a[i][j] == 0) && (i != j))
r[i][j] = Infinity;
else
r[i][j] = a[i][j];
}
}
printf("\n Replacing zeros by very large value \n");
Output(n, r);
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (r[i][k] + r[k][j]< r[i][j])
{
printf("\n r[%d][%d] = %d ", i, k, r[i][k]);
printf("\n r[%d][%d] = %d ", k, j, r[k][j]);
printf("\n r[%d][%d] = %d ", i, j, r[i][j]);
r[i][j] = r[i][k] + r[k][j];
printf("\n r[%d][%d] = r[%d][%d] + r[%d][%d] = %d ", i, j, i, k, k, j, r[i][j]);
}
else
{
r[i][j] = r[i][j];
printf("\n r[%d][%d] = r[%d][%d] = %d ", i, j, i, j, r[i][j]);
}
}
printf("\n");
}
printf("\n Step %d \n", k);
Output (n,r);
}
}
/* Function main */
void main()
{
int a[size][size];
int r[size][size];
int n;
clrscr();
printf("\n Input number of vertices in the graph: ");
scanf("%d", &n);
Input(n,a);
printf("\n Adjacency matrix (infinity means 9999) \n");
Output(n,a);
Flyod(n,a,r);
printf("\n Shortest path matrix \n");
Output(n,r);
getch();
}