-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDVR.c
More file actions
53 lines (43 loc) · 1.37 KB
/
Copy pathDVR.c
File metadata and controls
53 lines (43 loc) · 1.37 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
#include <stdio.h>
struct node {
unsigned dist[20];
unsigned from[20];
} rt[10];
int main() {
int costmat[20][20];
int nodes, i, j, k, count = 0;
printf("\nEnter the number of nodes : ");
scanf("%d", &nodes);
printf("\nEnter the cost matrix :\n");
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
scanf("%d", &costmat[i][j]);
costmat[i][i] = 0;
rt[i].dist[j] = costmat[i][j]; // Initialize distance equal to cost matrix
rt[i].from[j] = j; // Initialize source node
}
}
do {
count = 0;
for (i = 0; i < nodes; i++) {
// Choose arbitrary vertex k
for (j = 0; j < nodes; j++) {
for (k = 0; k < nodes; k++) {
// Calculate minimum distance
if (rt[i].dist[j] > costmat[i][k] + rt[k].dist[j]) {
rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j];
rt[i].from[j] = k;
count++;
}
}
}
}
} while (count != 0);
for (i = 0; i < nodes; i++){
printf("\nFor Router %d\n", i + 1);
for (j = 0; j < nodes; j++){
printf("\tNode %d via %d Distance %d \n", j + 1, rt[i].from[j] + 1, rt[i].dist[j]);
}
}
printf("\n");
}