0% found this document useful (0 votes)
7 views1 page

nandunetwork

The document describes the implementation of a Distance Vector Routing Protocol using C programming language. It defines a router structure, initializes distance and next hop arrays, and executes the distance vector algorithm to find the shortest paths between routers. The code also includes functionality to print the shortest path from a starting router to a destination router.

Uploaded by

padusindia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

nandunetwork

The document describes the implementation of a Distance Vector Routing Protocol using C programming language. It defines a router structure, initializes distance and next hop arrays, and executes the distance vector algorithm to find the shortest paths between routers. The code also includes functionality to print the shortest path from a starting router to a destination router.

Uploaded by

padusindia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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;
}

You might also like