/* Implementation of Shortest Path Computation in OSPF (Dijikstra’s Algorithm) */
#include<stdio.h>
main()
{
int i,j,k,n,a[10][10],b[10][10];
printf("\n Enter the no.of nodes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter the distance between the host %d to %d : ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n The given Matrix is : \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d\t",a[i][j]);
}
printf("\n");
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
a[i][j]=a[i][k]+a[k][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[i][j];
if(i==j)
b[i][j]=0;
}
}
printf("\n The output Matrix is : \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d \t",b[i][j]);
}
printf("\n");
}
}
OUTPUT :
Enter the no.of nodes : 4
Enter the distance between the host 1 to 1 : 0
Enter the distance between the host 1 to 2 : 2
Enter the distance between the host 1 to 3 : 5
Enter the distance between the host 1 to 4 : 3
Enter the distance between the host 2 to 1 : 2
Enter the distance between the host 2 to 2 : 0
Enter the distance between the host 2 to 3 : 6
Enter the distance between the host 2 to 4 : 1
Enter the distance between the host 3 to 1 : 3
Enter the distance between the host 3 to 2 : 1
Enter the distance between the host 3 to 3 : 0
Enter the distance between the host 3 to 4 : 6
Enter the distance between the host 4 to 1 : 3
Enter the distance between the host 4 to 2 : 1
Enter the distance between the host 4 to 3 : 5
Enter the distance between the host 4 to 4 : 0
The given Matrix is :
0 2 5 3
2 0 6 1
3 1 0 6
3 1 5 0
The output Matrix is :
0 2 5 3
2 0 6 1
3 1 0 2
3 1 5 0
No comments:
Post a Comment