-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdvr.c
More file actions
75 lines (68 loc) · 1.63 KB
/
Copy pathdvr.c
File metadata and controls
75 lines (68 loc) · 1.63 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
72
73
74
75
#include <stdio.h>
int costMatrix[20][20], n;
struct routers
{
int distance[20];
int adjNodes[20];
} node[20];
// function to read the cost matrix
void readCostMatrix()
{
int i, j;
printf("\nEnter cost matrix\n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &costMatrix[i][j]);
// distance from X to X is 0
costMatrix[i][i] = 0;
node[i].distance[j] = costMatrix[i][j];
node[i].adjNodes[j] = j;
}
}
}
void calcRoutingTable()
{
int i, j, k;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
for (k = 0; k < n; ++k)
{
// if the cost of the path from X to Y is less than the cost of the path from X to Z
if (node[i].distance[j] > costMatrix[i][k] + node[k].distance[j])
{
// substitute with minimum distance
node[i].distance[j] = node[i].distance[k] + node[k].distance[j];
// substitute with minimum path
node[i].adjNodes[j] = k;
}
}
}
}
}
void displayRoutes()
{
int i, j;
for (i = 0; i < n; ++i)
{
printf("\nRouter %d\n", i + 1);
for (j = 0; j < n; ++j)
{
printf("Node %d via %d : Distance %d\n", j + 1, node[i].adjNodes[j] + 1, node[i].distance[j]);
}
printf("\n");
}
}
int main()
{
int i, j;
printf("Number of nodes: ");
scanf("%d", &n);
readCostMatrix();
calcRoutingTable();
displayRoutes();
return 0;
}