0% found this document useful (0 votes)
18 views4 pages

LAB-8 CN (Modified

lab

Uploaded by

MD FARHAN 3070
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)
18 views4 pages

LAB-8 CN (Modified

lab

Uploaded by

MD FARHAN 3070
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/ 4

Q-8. Implementation of Linked state routing in C.

PROGRAM:-
#include <stdio.h>
#include <stdlib.h>
#define NUM_NODES 5
typedef struct {
int cost[NUM_NODES];
} LinkState;
void updateLinkState(LinkState ls[], int node, int newCost[]) {
for (int i = 0; i < NUM_NODES; i++) {
ls[node].cost[i] = newCost[i];
ls[i].cost[node] = newCost[i]; } }
void printLinkState(LinkState ls[]) {
printf("Link State Table:\n");
for (int i = 0; i < NUM_NODES; i++) {
printf("Node %d: ", i);
for (int j = 0; j < NUM_NODES; j++) {
printf("%d ", ls[i].cost[j]); }
printf("\n"); } }

int main() {
LinkState ls[NUM_NODES];
// Initialize link state table with random costs
for (int i = 0; i < NUM_NODES; i++) {
for (int j = 0; j < NUM_NODES; j++) {
ls[i].cost[j] = (i == j) ? 0 : rand() % 10 + 1; } }
// Print initial link state table
printLinkState(ls);
// Update link state with new costs
int newCost[NUM_NODES] = {0, 5, 2, 999, 8};
updateLinkState(ls, 0, newCost);
// Print updated link state table
printLinkState(ls);
return 0; }

OUTPUT:-

Q-9. Implementation of distance vector routing in C.


PROGRAM:-
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define NUM_NODES 5
typedef struct {
int costs[NUM_NODES];
} DistanceVector;
void updateDistanceVector(DistanceVector dv[], int node, int newCost[]) {
for (int i = 0; i < NUM_NODES; i++) {
dv[node].costs[i] = newCost[i]; } }
void printDistanceVector(DistanceVector dv[]) {
printf("Distance Vector Table:\n");
for (int i = 0; i < NUM_NODES; i++) {
printf("Node %d: ", i);
for (int j = 0; j < NUM_NODES; j++) {
printf("%d ", dv[i].costs[j]); }
printf("\n"); } }

void computeDistanceVector(DistanceVector dv[], int node, int linkState[][NUM_NODES]) {


for (int i = 0; i < NUM_NODES; i++) {
int minCost = INT_MAX;
for (int j = 0; j < NUM_NODES; j++) {
int cost = dv[node].costs[j] + linkState[j][i];
if (cost < minCost) {
minCost = cost; } }
dv[node].costs[i] = minCost; } }

int main() {
DistanceVector dv[NUM_NODES];
// Initialize distance vector with maximum costs
for (int i = 0; i < NUM_NODES; i++) {
for (int j = 0; j < NUM_NODES; j++) {
dv[i].costs[j] = (i == j) ? 0 : INT_MAX; } }
// Initialize link state matrix
int linkState[NUM_NODES][NUM_NODES] = {
{0, 1, 3, INT_MAX, INT_MAX},
{1, 0, 1, 4, INT_MAX},
{3, 1, 0, 2, 7},
{INT_MAX, 4, 2, 0, 5},
{INT_MAX, INT_MAX, 7, 5, 0}
};
// Update distance vector with initial link state
for (int i = 0; i < NUM_NODES; i++) {
for (int j = 0; j < NUM_NODES; j++) {
dv[i].costs[j] = linkState[i][j];
}
}

// Compute distance vector for each node


for (int i = 0; i < NUM_NODES; i++) {
computeDistanceVector(dv, i, linkState);
}

// Print distance vector table


printDistanceVector(dv);

return 0;
}

OUTPUT:-

You might also like