Distance Vector  

Posted in

#include"stdio.h"
#include"stdlib.h"
#define nul 1000
#define nodes 10
int no;

struct node
{
int a[nodes][4];
}router[nodes];

void init(int r)
{
int i;
for(i=1;i<=no;i++) { router[r].a[i][1]=i; router[r].a[i][2]=999; router[r].a[i][3]=nul; } router[r].a[r][2]=0; router[r].a[r][3]=r; } void inp(int r) { int i; printf("Enter distance from the node %d to other nodes\n",r); printf("Enter 999 if there is no direct route\n"); for(i=1;i<=no;i++) { if(i!=r) { printf("Enter distance to node %d\n",i); scanf("%d",&router[r].a[i][2]); router[r].a[i][3]=i; } } } void display(int r) { int i; printf("The routing table for node %d is as follows :\n",r); for(i=1;i<=no;i++) { if(router[r].a[i][2]>=999)
printf("\t\t %d \t no link \t no hop\n",router[r].a[i][1]);
else
printf("\t\t %d \t %d \t\t %d\n",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);
}
}

void dv_algo(int r)
{
int i,j,z;
for(i=1;i<=no;i++) { if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0) { for(j=1;j<=no;j++) { z=router[r].a[i][2]+router[i].a[j][2]; if(router[r].a[j][2]>z)
{
router[r].a[j][2]=z;
router[r].a[j][3]=i;
}
}
}
}
}

int main()
{
int i,j,x,y;
char choice;
printf("Enter the number of nodes required\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
init(i);
inp(i);
}
printf("The configuration of the nodes after initialization is as follows :\n");
for(i=1;i<=no;i++)
display(i);
for(i=1;i<=no;i++)
dv_algo(i);
printf("The configuration of the nodes after computation of path is as follows:\n");
for(i=1;i<=no;i++)
display(i);
while(1)
{
printf("Continue(y/n)\n");
scanf("%s",&choice);
if(choice=='n')
break;
printf("Enter the nodes between which shortest path is to be found\n");
scanf("%d%d",&x,&y);
printf("The length of the shortest path is %d\n",router[x].a[y][2]);
}
return 0;
}

This entry was posted at 4:49 AM and is filed under . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment

Reverse Engineering :Subscribe Now

Zts - ZTS

DISCLAIMER

This Blog is ONLY for educational purposes,
and any such codes/snippets provided are to be executed on
your sole discretion. The author is not responsible for the codes.