Distance-Vector Routing (DVR) Algorithm: Code in Java
Distance-Vector Routing (DVR) Algorithm: Code in Java
code in Java:
import java.io.*;
public class DVR
{
static int graph[][];
static int via[][];
static int rt[][];
static int v;
static int e;
System.out.print("Please enter the Source Node for the edge whose cost has
changed: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Please enter the Destination Node for the edge whose cost
has changed: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Please enter the new cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;
}
output:-
Please enter the number of Vertices:
4
Please enter the number of Edges:
5
Please enter data for Edge 1:
Source: 1
Destination: 2
Cost: 1
Please enter data for Edge 2:
Source: 1
Destination: 3
Cost: 3
Please enter data for Edge 3:
Source: 2
Destination: 3
Cost: 1
Please enter data for Edge 4:
Source: 2
Destination: 4
Cost: 1
Please enter data for Edge 5:
Source: 3
Destination: 4
Cost: 4
Please enter the Source Node for the edge whose cost has changed: 2
Please enter the Destination Node for the edge whose cost has changed: 4
Please enter the new cost: 10
int i,minpos=1,k,c,minimum;
for(i=1;i<=n;i++)
flag[i]=0;
this.distance[i]=this.cost[s][i];
}
c=2;
while(c<=n)
minimum=99;
for(k=1;k<=n;k++)
minimum=this.distance[i];
minpos=k;
flag[minpos]=1;
c++;
for(k=1;k<=n;k++)
this.distance[k]=this.distance[minpos]+this.cost[minpos][k];
int nodes,source,i,j;
nodes = in.nextInt();
for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
d.cost[i][j]=in.nextInt();
if(d.cost[i][j]==0)
d.cost[i][j]=999;
source=in.nextInt();
d.calc(nodes,source);
System.out.println("The Shortest Path from Source \t"+source+"\t to all other vertices are : \n");
for(i=1;i<=nodes;i++)
if(i!=source)
5
Enter the Cost Matrix Weights:
01020
10104
01003
20003
04330
Enter the Source Vertex :
1
The Shortest Path from Source 1 to all other vertices are :