Вы находитесь на странице: 1из 9

/* Implementation of Distance Vector Routing */

#include<stdio.h> #include<string.h> #include<conio.h> void main() { int i,j,k,n,a[10][10],b[10][10],src,s,d; char ch; clrscr(); printf("\n Enter the number of nodes = "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) a[i][j]=0; else { printf("\n Enter the distance between host : %d and %d ==> ",i,j); scanf("%d",&a[i][j]); } } } printf("\n "); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%d\t",a[i][j]); printf("\n "); } do { printf("\n Enter the node to display the routing table : "); scanf("%d",&src); for(j=1;j<=n;j++) { if(src!=j) { if(a[src][j]!=0) printf("\n The shortest path from %d to %d = %d \n",src,j,a[src][j]); else

printf("\n There is no path from %d to %d \n",src,j); } } printf("\n Do u want to continue(y/n) : "); scanf("%s",&ch); }while(ch!='n'); for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(a[i][j]>a[i][k]+a[k][j]) a[i][j]=a[i][k]+a[k][j]; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%d\t",a[i][j]); printf("\n"); } getch(); }

OUT PUT : SHOWS A SUBN ET


Enter the number of nodes = 6 Enter the distance between host : 1 and 2 ==> 2 Enter the distance between host : 1 and 3 ==> 5 Enter the distance between host : 1 and 4 ==> 1 Enter the distance between host : 1 and 5 ==> 2 Enter the distance between host : 1 and 6 ==> 10 Enter the distance between host : 2 and 1 ==> 2 Enter the distance between host : 2 and 3 ==> 3 Enter the distance between host : 2 and 4 ==> 2 Enter the distance between host : 2 and 5 ==> 3 Enter the distance between host : 2 and 6 ==> 8 Enter the distance between host : 3 and 1 ==> 5 Enter the distance between host : 3 and 2 ==> 3 Enter the distance between host : 3 and 4 ==> 3 Enter the distance between host : 3 and 5 ==> 1 Enter the distance between host : 3 and 6 ==> 5

Enter the distance between host : 4 and 1 ==> 1 Enter the distance between host : 4 and 2 ==> 2 Enter the distance between host : 4 and 3 ==> 3 Enter the distance between host : 4 and 5 ==> 1 Enter the distance between host : 4 and 6 ==> 3

Enter the distance between host : 5 and 1 ==> 6 Enter the distance between host : 5 and 2 ==> 4 Enter the distance between host : 5 and 3 ==> 1 Enter the distance between host : 5 and 4 ==> 1 Enter the distance between host : 5 and 6 ==> 2 Enter the distance between host : 6 and 1 ==> 10 Enter the distance between host : 6 and 2 ==> 8 Enter the distance between host : 6 and 3 ==> 5 Enter the distance between host : 6 and 4 ==> 3 Enter the distance between host : 6 and 5 ==> 2

ROUTING TABLE : 1 1 2 3 4 5 6
0 2 5 1 6 10

2
2 0 3 2 4 8

3
5 3 0 3 1 5

4
1 2 3 0 1 3

5
2 3 1 1 0 2

6
10 8 5 3 2 0

Enter the node to display the routing table : 2 The shortest path from 2 to 1 = 2 The shortest path from 2 to 3 = 3 The shortest path from 2 to 4 = 2 The shortest path from 2 to 5 = 3 The shortest path from 2 to 6 = 8 Do u want to continue (y/n) : n

ROUTING TABLE FOR 2 : 1 1 2 3 4 5 6


0 2 3 1 2 4

2
2 0 3 2 3 5

3
3 3 0 2 1 3

4
1 2 2 0 1 3

5
2 3 1 1 0 2

6
4 5 3 3 2 0

/* Implementation of Shortest Path Computation in OSPF (Dijikstras 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 3 3 2 0 1 1 5 6 0 5 3 1 6 0

The output Matrix is : 0 2 3 3 2 0 1 1 5 6 0 5 3 1 2 0

Вам также может понравиться