DISTANCE VECTOR ROUTING
#include <stdio.h>
#define MAX_NODES 10
#define INF 9999
typedef struct {
int dist[MAX_NODES];
int from[MAX_NODES];
} Node;
int main() {
int costmat[MAX_NODES][MAX_NODES], nodes;
Node route[MAX_NODES];
printf("Enter the number of nodes: ");
scanf("%d", &nodes);
printf("Enter the cost adjacency matrix (use %d for infinity):\n", INF);
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
scanf("%d", &costmat[i][j]);
if (i == j)
costmat[i][j] = 0; // Distance to itself is zero
route[i].dist[j] = costmat[i][j];
route[i].from[j] = j;
}
}
int updated;
do {
updated = 0;
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
for (int k = 0; k < nodes; k++) {
if (route[i].dist[j] > costmat[i][k] + route[k].dist[j]) {
route[i].dist[j] = costmat[i][k] + route[k].dist[j];
route[i].from[j] = k;
updated = 1;
}
}
}
}
} while (updated);
// Display the routing table
for (int i = 0; i < nodes; i++) {
printf("\nRouting table for node %d:\n", i + 1);
printf("Destination\tNext Hop\tDistance\n");
for (int j = 0; j < nodes; j++) {
printf("%d\t\t%d\t\t%d\n", j + 1, route[i].from[j] + 1, route[i].dist[j]);
}
}
return 0;
}