Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

//DISTANCE VECTOR ALGORITHM

#include <stdio.h>

int d[10][10], via[10][10];

int main() {

int i, j, k, n, g[10][10];

printf("\nEnter the no of nodes: ");

scanf("%d", &n);

for (i = 0; i < n; i++) {

printf("Enter the record for route %c\n", i + 97);

for (j = 0; j < n; j++) {

printf("(%c : %c) :: ", i + 97, j + 97);

scanf("%d", &g[i][j]);

if (g[i][j] != 999) {

d[i][j] = 1;

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

via[i][j] = i;

for (k = 0; k < n; k++) {

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

if (d[i][k] == 1 && d[k][j] == 1 && g[i][k] + g[k][j] < g[i][j]) {

g[i][j] = g[i][k] + g[k][j];

via[i][j] = k;

}
}

for (i = 0; i < n; i++) {

printf("cost of route %c:\n", i + 97);

for (j = 0; j < n; j++) {

printf("%c : %d via %c\n", j + 97, g[i][j], via[i][j] + 97);

return 0;

OUTPUT:

Enter the no of nodes: 3

Enter the record for route a

(a : a) :: 0

(a : b) :: 1

(a : c) :: 5

Enter the record for route b

(b : a) :: 1

(b : b) :: 0

(b : c) :: 2

Enter the record for route c

(c : a) :: 5

(c : b) :: 2

(c : c) :: 0

cost of route a:

a : 0 via a

b : 1 via a

c : 3 via b

cost of route b:

a : 1 via b

b : 0 via b
c : 2 via b

cost of route c:

a : 3 via b

b : 2 via c

c : 0 via c

You might also like