-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdistanceVector.c
101 lines (75 loc) · 3.56 KB
/
distanceVector.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "graph.h"
#include "distanceVector.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void updatedBellmanFord(graph *topology, int numVertex, int numEdges,int count){
int v = topology->numVertex;
int e = MAXIMUM_INTERFACE_PER_NODE;
interface *intf;
for(int times =0; times <v; times++){
for(int i =0; i<v; i++){
node *currentNode = &(topology->routersArray[i]);
for(int j=0; j<e; j++){
for(int k = 0; k < v; k++){
// int index = getIndexOfNode(neighbourNode,topology);
intf = topology->routersArray[i].intf[j];
if(!intf)
break;
node* neighbourNode = getNeighbourNode(intf);
int distanceFromCurrentToNeighbourNode = intf->attachedEdge->cost;
// printf("\n%s %s\n",currentNode->routerName,neighbourNode->routerName);
if(neighbourNode->rt.costArray[k] != INT_MAX && currentNode->rt.costArray[k] > neighbourNode->rt.costArray[k] + distanceFromCurrentToNeighbourNode){
currentNode->rt.costArray[k] = neighbourNode->rt.costArray[k] + distanceFromCurrentToNeighbourNode;
currentNode->rt.viaRouters[k] = *neighbourNode;
}
}
}
}
}
}
void bellmanFord(graph *topology, int numVertex, int numEdges, node* sourceNode,int index){
int distance[numVertex];
for(int i=0;i<numVertex;i++){
distance[i] = INT_MAX;
}
distance[index] =0;
for(int i =0;i <numVertex-1; i++){ // looping n-1 time
for(int j =0; j<numEdges; j++){ // looping over arrayEdges
edge link = topology->edgesArray[j];
int fromNode = getIndexOfNode(link.intf1.attachedNode,topology);
int toNode = getIndexOfNode(link.intf2.attachedNode,topology);
int dis = link.cost;
if(distance[fromNode] != INT_MAX && distance[toNode] > distance[fromNode] + dis){
distance[toNode] = distance[fromNode] + dis;
}
if(distance[toNode] != INT_MAX && distance[fromNode] > distance[toNode] + dis){
distance[fromNode] = distance[toNode] + dis;
}
}
}
for(int i =0; i<numEdges; i++){
edge link = topology->edgesArray[i];
int toNode = getIndexOfNode(link.intf2.attachedNode,topology);
int fromNode = getIndexOfNode(link.intf1.attachedNode,topology);
int dis = link.cost;
if(distance[fromNode] != INT_MAX && distance[toNode] > distance[fromNode] + dis){
printf("Negative Weight Cycle is Present. Inspect your Topology.");
exit(0);
}
if(distance[toNode] != INT_MAX && distance[fromNode] > distance[toNode] + dis){
printf("Negative Weight Cycle is Present. Inspect your Topology.");
exit(0);
}
}
printf("\nRouting Table for %s:\n",sourceNode->routerName);
// for(int i =0;i <numVertex; i++){
// printf("Cost: %d\n",distance[i]);
// }
for(int i =0;i <numVertex; i++){
topology->routersArray[index].rt.costArray[i] = distance[i];
// printf("hi %d\n",i);
printf("\t%s %d\n",
topology->routersArray[index].rt.destinationRouters[i].routerName, topology->routersArray[index].rt.costArray[i]);
}
}