Write a program for Distance Vector Algorithm to find suitable path for transmission.
Theory: -
Routing algorithm is a part of network layer software which is responsible for deciding which
output line an incoming packet should be transmitted on. If the subnet uses datagram
internally, this decision must be made anew for every arriving data packet since the best route
may have changed since last time. If the subnet uses virtual circuits internally, routing
decisions are made only when a new established route is being set up. The latter case is
sometimes called session routing, because a rout remains in force for an entire user session
(e.g., login session at a terminal or a file).
Routing algorithms can be grouped into two major classes: adaptive and non-adaptive. Non-
adaptive algorithms do not base their routing decisions on measurement or estimates of
current traffic and topology. Instead, the choice of route to use to get from I to J (for all I and
J) is compute in advance, offline, and downloaded to the routers when the network ids
booted. This procedure is sometime called static routing.
Adaptive algorithms, in contrast, change their routing decisions to reflect changes in the
topology, and usually the traffic as well. Adaptive algorithms differ in where they get
information (e.g., locally, from adjacent routers, or from all routers), when they change the
routes (e.g., every ΔT sec, when the load changes, or when the topology changes), and what
metric is used for optimization (e.g., distance, number of hops, or estimated transit time).
Two algorithms in particular, distance vector routing and link state routing are the most
popular. Distance vector routing algorithms operate by having each router maintain a table
(i.e., vector) giving the best known distance to each destination and which line to get there.
These tables are updated by exchanging information with the neighbors.
In distance vector routing, each router maintains a routing table indexed by, and containing
one entry for, each router in subnet. This entry contains two parts: the preferred outgoing line
to use for that destination, and an estimate of the time or distance to that destination. The
metric used might be number of hops, time delay in milliseconds, total number of packets
queued along the path, or something similar.
The router is assumed to know the “distance” to each of its neighbor. If the metric is hops, the
distance is just one hop. If the metric is queue length, the router simply examines each queue.
If the metric is delay, the router can measure it directly with special ECHO packets hat the
receiver just time stamps and sends back as fast as possible
Program: -
#include<stdio.h>
#define INFINITY 99
struct node
{
unsigned dist[20]; unsigned from[20];
}rt[10];
void main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++) for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
printf("\t\nnode %d via %d Distance %d",j+1,rt[i].from[j+1],rt[i].dist[j]);
Output:
Enter the number of nodes : 3
Enter the cost matrix :
015
102
520
For router 1
node 1 via 1 Distance 0
node 2 via 2 Distance 1
node 3 via 2 Distance 3
For router 2
node 1 via 1 Distance 1
node 2 via 2 Distance 0
node 3 via 3 Distance 2
For router 3
node 1 via 2 Distance 3
node 2 via 2 Distance 2
node 3 via 3 Distance 0