Simulate and implement distance vector routing algorithm in Computer Network

Aim:- 

Simulate and implement distance vector routing algorithm 

Program:-

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define NUM_NODES 5
  4. struct N{
  5.        char to;
  6.        int cost;
  7.        char next;
  8.        };
  9. void share_update(struct N DV[], struct N DV_recvd[], int node, int neighbor);
  10. void initialization(struct N DV[],int net[][],int node);
  11. int main(){
  12.     int net[NUM_NODES][NUM_NODES]={
  13.         {0,5,2,3,999},
  14.         {5,0,4,999,3},
  15.         {2,4,0,999,4},
  16.         {3,999,999,0,999},
  17.         {999,3,4,999,0}
  18.     };
  19.     int i,j,k;
  20.     struct N DV[NUM_NODES][NUM_NODES];
  21.     for(i=0;i<NUM_NODES;i++){
  22.         initialization(DV[i],net,i);                     
  23.     }
  24.     for(i=0;i<NUM_NODES;i++){
  25.        for(j=0;j<NUM_NODES;j++){
  26.           share_update(DV[i],DV[j],i,j);
  27.        }
  28.     }
  29.     for(k=0;k<NUM_NODES;k++){
  30.        printf("Distance vector of node %c\n", (char)('A'+k));
  31.        printf("To\t Cost\t Next\n");
  32.        printf("------------------\n");
  33.        for(i=0;i<NUM_NODES;i++){
  34.           printf("%c\t%d\t%c\n",DV[k][i].to,DV[k][i].cost,DV[k][i].next);
  35.        }
  36.     }
  37.     system("pause");
  38.     return 0;
  39. }
  40. void initialization(struct N DV[], int net[][NUM_NODES],int node){
  41.     int i;
  42.     for(i=0;i<NUM_NODES;i++){
  43.        DV[i].to=(char)('A'+i);
  44.        DV[i].cost=net[node][i];
  45.        DV[i].next='-';
  46.     }     
  47. }
  48. void share_update(struct N DV[], struct N DV_recvd[], int node, int neighbor){
  49.     int cost_between=DV[neighbor].cost;
  50.     int i;
  51.     for(i=0;i<NUM_NODES;i++){
  52.        int cal_cost=cost_between+DV_recvd[i].cost;
  53.        if(cal_cost<DV[i].cost){
  54.           DV[i].cost=cal_cost;
  55.           DV[i].next=(char)('A'+neighbor);
  56.        }
  57.     }
  58. }

 

Comments