DISTANCE VECTOR ROUTING PROTOCOL
Nandana J
CSE C
3
#include <stdio.h>
#include <stdbool.h>
#define MAX_ROUTERS 100 // Maximum number of routers
#define INF 1000 // Represents infinity
// Router structure with fixed-size arrays
typedef struct {
int distance[MAX_ROUTERS]; // Distance to each router
int next_hop[MAX_ROUTERS]; // Next hop to reach each router
} Router;
int cost_matrix[MAX_ROUTERS][MAX_ROUTERS]; // Global cost matrix
Router routers[MAX_ROUTERS]; // Global router array
// Run distance vector algorithm to find shortest paths
void distance_vector_algorithm(int n) {
bool updated;
// Initialize distance and next_hop arrays
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
routers[i].distance[j] = (i == j) ? 0 :
(cost_matrix[i][j] != -1) ? cost_matrix[i][j] : INF;
routers[i].next_hop[j] = (i == j || cost_matrix[i][j] != -1) ? j : -1;
}
}
// Run algorithm until no updates
do {
updated = false;
for (int i = 0; i < n; i++) { // For each router
for (int j = 0; j < n; j++) { // For each destination
if (i == j) continue;
for (int k = 0; k < n; k++) { // For each neighbor
if (cost_matrix[i][k] == -1) continue;
int new_cost = cost_matrix[i][k] + routers[k].distance[j];
if (new_cost < routers[i].distance[j]) {
routers[i].distance[j] = new_cost;
routers[i].next_hop[j] = k;
updated = true;
}
}
}
}
} while (updated);
}
// Print the shortest path from start to destination
void print_path(int start, int dest, int n) {
if (routers[start].distance[dest] == INF) {
printf("No path exists from %d to %d\n", start, dest);
return;
}