Professional Documents
Culture Documents
Distance Vector Routing Program With Comments
Distance Vector Routing Program With Comments
#include<stdio.h>
#include<conio.h>
void main()
{
static int s,q,a[20],p[20],x,b[20],c[20],f[20][20],e[20][20],i,j,n,d,max=1000;
char ch;
clrscr();
printf(\n ***************************** \n);
printf(Distance Vector Routing Table implementation );
printf(\n **************************** \n);
printf("Enter the No of Nodes=");
scanf("%d",&n);
printf("\n is there exists a Direct Link, in the graph (0 or 1) from the node to -:\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
printf("%c--%c :=", 65+i, 65+j);
scanf("%d",&e[i][j]); /* This edge matrix is for storing the edges 1-for existence 0-for nonexistance*/
e[j][i]=e[i][j]; /* if edge A to B =0 then keeping B to A also =0* in the same matrix/
}
}
for(j=0;j<n;j++) /* This is for displaying the column NODES say A,B,C,D,E,F etc for matrix display purpose
Horizontally */
{
printf("%c",65+j);
}
do
{
printf("\n Do u want to find the Routing table (y/n) :=");
scanf("%c",&ch);
switch(ch)
{
case 'n': exit();
break;
case 'y':
printf("\n Enter the node no, for which you want To Find Routing Table := ");
scanf("%d",&d); /* here variable d stores the given NODE num,for ex: say 2 for NODE C*/
for(i=d;i<=d;i++)
{
for(j=0,q=0;j<n;j++)
{
if(e[i][j]==1) /* Here we are finding the neighbouring nodes for the given node, say for C*/
{
b[q]=j;
q++;
b[q] array */
}
} /* j close */
} /* i close*/
printf("\n The Neighbhouring nodes of %c node are :=",65+d );
for(i=0;i<q;i++)
{
printf("%c",65+b[i]); /* displaying given node C neighbours {ie. B,D,E} on screen*/
}
printf("\n Enter Delays from %c Neighbouring Node :=\n",65+d); /* here var d has NODE C*/
for(i=0;i<q;i++)
{
printf("%c--%c",65+d,65+b[i]);
scanf("%d",&c[i]); /* storing initial Delays from C to B,C to D ,C to E into c[i] array */
}
printf("\n Now Enter Delays from :");
for(i=0;i<n;i++)
{
for(j=0;j<q;j++)
{
printf("%c--%c =",65+i,65+b[j]);
scanf("%d",&f[i][j]); /* read&store the neighbor nodes B,D,E vectors values into f[i][j] array*/
}
} /* i close*/
for(j=0;j<q;j++)
{
printf("\t%c",65+b[j]); /* again dispay B D E horizontally*/
}
for(i=0;i<n;i++)
{
printf("\n%c",65+i);
for(j=0;j<q;j++)
{
/* now display f[i][j] entries on to the screen */
printf("\t%d",f[i][j]);
}
} /* i close */
for(j=0;j<q;j++)
for(i=0;i<n;i++)
f[i][j]=c[j]+f[i][j]; /* vector f[i][j] delay + array c[j] delays are adding and storing them
into the same array f[i][j]*/
for(i=0;i<n;i++)
{
if(i==d) /* if it is node C */
{
p[i]=29; /* then in via matrix entry p[i] wil be hypen - or ^ */
a[i]=0; /* C to C delay will be kept zero in delay matrix entry a[i]*/
}
/* if close*/
else /* otherwise i.e for form C-A, C-B,C-D,C-E,C-F calucation */
{
for(j=0,x=0;j<q;j++)
{
s=f[i][j]; /* finding the minimum delay in each row of new f[i][j] table */
if(s<min)
{
min=s; /* and storing that minm delay in min variable*/
x=j; /* taking that minm delay index j */
}
} /* for j close*/
p[i]=b[x]; /* p[i] is final via vector =: b[x] is the min delay node*/
a[i]=min; /* keep that min value in final delay vector a[i] */
min=1000;
} /* else close */
} /* I close */
printf("\n Routing table for NODE %c is : --", 65+d);
for(i=0;i<n;i++)
{
/* here displaying the a[i],p[i] results */
printf("\n %d\t%c", a[i], 65+p[i]); /* a[i] is delay vector , p[i] is via vector */
}
getche();
break;
}
} while(ch!='n'); /* do while close */
getche();
} /* main close*/
Input
Consider the subnet of below Fig. 5-13(a). Distance vector routing is used, and the following vectors have just come
in to router C: from B: (5, 0, 8, 12, 6, 2); from D: (16, 12, 6, 0, 9, 10); and from E: (7, 6, 3, 9, 0, 4). The measured
delays to B, D, and E, are 6, 3, and 5, respectively. What is C's new routing table? Give both the outgoing line to use
and the expected delay.
delays-
Vector
B
Vector
D
Vector
E
subnet
16
12
12
10
b)
Output:
*****************************
Distance Vector Routing Table implementation
****************************
Enter the No of Nodes=6
is there exists a direct Link in the graph (0 or 1) from the node -:
A--B :1
A--C :0
A--D :1
A--E :1
A--F :0
-----B--C :1
B--D :0
B--E :0
B--F :1
-----C--D :1
C--E :1
C--F :0
-----D--E :0
D--F :1
-----E--F :1
-----------
ABCDEF
A010110
B101001
C010110
D101001
E101001
F010110
Do u want to find the Routing table (y/n) :=
Do u want to find the Routing table (y/n) := y
Enter the node no for which you want to Find Routing table (say NODE C) := 2
The Neighbhouring nodes of C node are:=BDE
Delay from C Neighbouring Node:=
CB :=6
CD :=3
CE :=5
A
B
C
B
5
0
8
D
16
12
6
E
7
6
3
D
E
F
12
6
2
0
9
10
9
0
4