-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvector.c
More file actions
66 lines (57 loc) · 1.44 KB
/
Copy pathvector.c
File metadata and controls
66 lines (57 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
#include<stdio.h>
struct network
{
int dist[10], outgoing[10];
};
struct network nodes[10];
void intialize(int n)
{
int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
nodes[i].outgoing[j] = j;
if(i == j)
nodes[i].dist[j] = 0;
else
nodes[i].dist[j] = 999;
}
}
void floyd(int n)
{
int i, j, k;
for(k = 0; k < n; k++)
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
if(nodes[i].dist[j] > nodes[i].dist[k] + nodes[k].dist[j])
{
nodes[i].outgoing[j] = k;
nodes[i].dist[j] = nodes[i].dist[k] + nodes[k].dist[j];
}
}
int main()
{
int n, i, j;
printf("Enter number of Nodes: ");
scanf("%d",&n);
intialize(n);
printf("Enter the Distances\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i != j)
{
printf("Enter distance from %d to %d: ", i + 1, j + 1);
scanf("%d",&nodes[i].dist[j]);
}
floyd(n);
printf("\n\nDistance Vector Routing Algorithm\n");
for(i = 0; i < n; i++)
{
printf("\nNode: %d (Vector Table)\n", i + 1);
printf("\tDEST\tDIST\tHOP\n");
for(j = 0; j < n; j++)
if(i != j)
printf("\t%d\t%d\t%d\n", (j + 1), nodes[i].dist[j], nodes[i].outgoing[j] + 1);
}
return(0);
}