Simulate and implement distance vector routing algorithm in Computer Network
Aim:-
Simulate and implement distance vector routing algorithm
Program:-
- #include<stdio.h>
- #include<stdlib.h>
- #define NUM_NODES 5
- struct N{
- char to;
- int cost;
- char next;
- };
- void share_update(struct N DV[], struct N DV_recvd[], int node, int neighbor);
- void initialization(struct N DV[],int net[][],int node);
- int main(){
- int net[NUM_NODES][NUM_NODES]={
- {0,5,2,3,999},
- {5,0,4,999,3},
- {2,4,0,999,4},
- {3,999,999,0,999},
- {999,3,4,999,0}
- };
- int i,j,k;
- struct N DV[NUM_NODES][NUM_NODES];
- for(i=0;i<NUM_NODES;i++){
- initialization(DV[i],net,i);
- }
- for(i=0;i<NUM_NODES;i++){
- for(j=0;j<NUM_NODES;j++){
- share_update(DV[i],DV[j],i,j);
- }
- }
- for(k=0;k<NUM_NODES;k++){
- printf("Distance vector of node %c\n", (char)('A'+k));
- printf("To\t Cost\t Next\n");
- printf("------------------\n");
- for(i=0;i<NUM_NODES;i++){
- printf("%c\t%d\t%c\n",DV[k][i].to,DV[k][i].cost,DV[k][i].next);
- }
- }
- system("pause");
- return 0;
- }
- void initialization(struct N DV[], int net[][NUM_NODES],int node){
- int i;
- for(i=0;i<NUM_NODES;i++){
- DV[i].to=(char)('A'+i);
- DV[i].cost=net[node][i];
- DV[i].next='-';
- }
- }
- void share_update(struct N DV[], struct N DV_recvd[], int node, int neighbor){
- int cost_between=DV[neighbor].cost;
- int i;
- for(i=0;i<NUM_NODES;i++){
- int cal_cost=cost_between+DV_recvd[i].cost;
- if(cal_cost<DV[i].cost){
- DV[i].cost=cal_cost;
- DV[i].next=(char)('A'+neighbor);
- }
- }
- }
Comments
Post a Comment