Distance vector



import java.io.*;
class distance_vector
{
public static void main(String arg[])throws Exception
{
int n,src,i,j,k;
String ch;
int a[][]=new int[10][10];
System.out.println("Enter the no. of nodes..");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
 {
   if(i==j)
    a[i][j]=0;
   else
   {
    System.out.println("Enter the distance between" + i + "and" + j);
    a[i][j]=a[j][i]=Integer.parseInt(br.readLine());
   }  
 }
for(k=1;k<=n;k++)
          for(i=1;i<=n;i++)
           for(j=1;j<=n;j++)
             if(a[i][k]+a[k][j]<a[i][j])
    {
       a[i][j]=a[i][k]+a[k][j];
    }
do
{
System.out.println("Enter the src node..");
src=Integer.parseInt(br.readLine());
for(j=1;j<=n;j++)
{
System.out.println("Distance from "+ src +"to" + j +"is"+ a[src][j]);
}
System.out.println("Do u want to continue..(yes/no)");
ch=br.readLine();
}while(ch.equals("yes"));

}
}




SAMPLE OUTPUT FOR ABOVE GRAPH:



No comments:

Post a Comment