-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance_vector_routing.c
More file actions
71 lines (65 loc) · 1.44 KB
/
Copy pathdistance_vector_routing.c
File metadata and controls
71 lines (65 loc) · 1.44 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
68
69
70
71
#include <stdio.h>
int cost_matrix[20][20];
int n; // size of cost matrix
struct routers
{
int distance[20];
int adjnode[20];
}node[20];
void readCostMatrix()
{
printf("\nEnter the cost Matrix: \n\n");
for(int i = 0;i<n;i++)
{
cost_matrix[i][i] = 0;
for(int j=0;j<n;j++)
{
scanf("%d", &cost_matrix[i][j]);
// setting a large value to indicate that there is no direct connection
if(cost_matrix[i][j] < 0)
{
cost_matrix[i][j] = 10000;
}
node[i].distance[j] = cost_matrix[i][j];
node[i].adjnode[j] = j;
}
}
}
void calc_routing_table()
{
for(int i = 0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
if (node[i].distance[j] > cost_matrix[i][k] + node[k].distance[j])
{
node[i].distance[j] = cost_matrix[i][k] + node[k].distance[j];
node[i].adjnode[j] = k;
}
}
}
}
}
void print_routes()
{
for(int i = 0;i<n;i++)
{
printf("\nRouter - %d\n\n", i+1);
for(int j=0;j<n;j++)
{
printf("node - %d || via - %d || distance = %d\n", j+1, node[i].adjnode[j]+1, node[i].distance[j]);
}
printf("\n");
}
}
int main()
{
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
readCostMatrix();
calc_routing_table();
print_routes();
return 0;
}