-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdvr.cpp
More file actions
67 lines (65 loc) · 1.35 KB
/
dvr.cpp
File metadata and controls
67 lines (65 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<stdio.h>
#define N 6
int nexthop[N][N];
int cost[N][N];
int old_value[N][N];
int new_value[N][N];
int hopcount[N][N];
int n;
void printresult(){
int i,j;
for(i=0;i<n;i++){
printf("Router %d\n",i);
printf("DestinationRouter\t\t\tDistance\t\tNexthop\t\tHopcount\n\n");
for(j=0;j<n;j++){
if(cost[i][j]!=1000){
printf("%d\t\t\t%d\t\t\t%d\t\t\t%d\t\t\n",j,new_value[i][j],nexthop[i][j],hopcount[i][j]);
}
}
printf("\n");
}
}
void updatetable(){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
old_value[i][j] = new_value[i][j];
}
}
}
void DVR(){
int i,j,k,flag;
do{
flag = 0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
for(k=0;k<n;k++){
if(old_value[i][j]>old_value[i][k]+old_value[k][j] && cost[i][j]!=1000){
new_value[i][j] = old_value[i][k] + old_value[k][j];
flag = 1;
nexthop[i][j] = k;
hopcount[i][j] = 1+hopcount[k][j];
}
}
}
}
updatetable();
}while(flag == 1);
}
int main(){
printf("Enter no of routers:");
scanf("%d",&n);
int i,j;
printf("\nEnter the cost matrix:");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&cost[i][j]);
old_value[i][j] = new_value[i][j] = cost[i][j];
nexthop[i][j] = j;
hopcount[i][j] = 0;
}
}
DVR();
printresult();
return 0;
}