-
Notifications
You must be signed in to change notification settings - Fork 6
/
topology.cc
263 lines (226 loc) · 5.58 KB
/
topology.cc
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include "topology.h"
Topology::Topology()
{}
Topology::~Topology()
{
for (deque<Node*>::iterator i=nodes.begin();
i!=nodes.end(); ++i) {
delete *i;
}
nodes.clear();
for (deque<Link*>::iterator i=links.begin();
i!=links.end(); ++i) {
delete *i;
}
links.clear();
}
deque<Node*>::iterator Topology::FindMatchingNodeIt(const Node *n)
{
for (deque<Node*>::iterator i=nodes.begin();
i!=nodes.end(); ++i) {
if ((**i).Matches(*n)) {
return i;
}
}
return nodes.end();
}
Node * Topology::FindMatchingNode(const Node *n)
{
deque<Node*>::iterator i=FindMatchingNodeIt(n);
if (i!=nodes.end()) {
return *i;
} else {
return 0;
}
}
deque<Link*> * Topology::GetOutgoingLinks(const Node *src)
{
deque<Link*> *out = new deque<Link*>;
for (deque<Link*>::iterator i=links.begin();
i!=links.end(); ++i) {
if ((*i)->GetSrc()==src->GetNumber()) {
out->push_back(*i);
}
}
return out;
}
deque<Node*> *Topology::GetNeighbors(const Node *n)
{
deque<Link*> *temp = GetOutgoingLinks(n);
deque<Node*> *nodes = new deque<Node*>;
for (deque<Link*>::iterator i=temp->begin();
i!=temp->end(); ++i) {
Node x = Node((*i)->GetDest(),0,0,0);
nodes->push_back(FindMatchingNode(&x));
}
return nodes;
}
deque<Link*>::iterator Topology::FindMatchingLinkIt(const Link *l)
{
for (deque<Link*>::iterator i=links.begin();
i!=links.end(); ++i) {
if ((**i).Matches(*l)) {
return i;
}
}
return links.end();
}
Link *Topology::FindMatchingLink(const Link *l)
{
deque<Link*>::iterator i=FindMatchingLinkIt(l);
if (i!=links.end()) {
return *i;
} else {
return 0;
}
}
void Topology::AddNode(Node *n)
{
if (FindMatchingNode(n)) {
ChangeNode(n);
} else {
nodes.push_back(n);
}
}
void Topology::DeleteNode(const Node *n)
{
deque<Node*>::iterator i=FindMatchingNodeIt(n);
if (i!=nodes.end()) {
delete *i;
nodes.erase(i);
}
}
void Topology::ChangeNode(const Node *n)
{
deque<Node*>::iterator i=FindMatchingNodeIt(n);
if (i!=nodes.end()) {
**i=*n;
}
}
void Topology::AddLink(Link *l)
{
if (FindMatchingLink(l)) {
ChangeLink(l);
} else {
links.push_back(l);
}
}
void Topology::DeleteLink(const Link *l)
{
deque<Link*>::iterator i=FindMatchingLinkIt(l);
if (i!=links.end()) {
delete *i;
links.erase(i);
}
}
void Topology::ChangeLink(const Link *l)
{
deque<Link*>::iterator i=FindMatchingLinkIt(l);
if (i!=links.end()) {
**i=*l;
}
Node x = Node(l->GetSrc(),0,0,0);
Node *n=FindMatchingNode(&x);
n->LinkHasBeenUpdated(l);
}
void Topology::WriteDot(const string &n) const
{
FILE *out = fopen(n.c_str(),"w");
if (out==0) {
return;
}
fprintf(out,"digraph topo {\n");
for (deque<Node*>::const_iterator i=nodes.begin(); i!=nodes.end();++i) {
fprintf(out,"%u\n",(*i)->GetNumber());
}
for (deque<Link*>::const_iterator i=links.begin(); i!=links.end();++i) {
fprintf(out,"%u -> %u [ label=\"%5.1lf\" ];\n",(*i)->GetSrc(),(*i)->GetDest(), (*i)->GetLatency());
}
fprintf(out,"}\n");
fclose(out);
}
void Topology::DrawTopology() const
{
WriteDot(string("_topo.in"));
system("dot _topo.in > _topo.out");
system("dotty _topo.out");
}
ostream &Topology::Print(ostream &os) const
{
os << "Topology(nodes={";
for (deque<Node*>::const_iterator i=nodes.begin();
i!=nodes.end();
++i) {
os << **i <<",";
}
os <<"}, links={";
for (deque<Link*>::const_iterator i=links.begin();
i!=links.end();
++i) {
os << **i <<",";
}
os <<"})";
return os;
}
//
// This is totally disgusting
//
/** @param links is initially empty and will store the result */
void Topology::CollectShortestPathTreeLinks(const Node &src, deque<Link> &links)
{
vector<double> distance(nodes.size());
vector<unsigned> pred(nodes.size());
deque<unsigned> visited;
deque<unsigned> unvisited;
// initialize the distance vector to "infinity"
for (deque<Node*>::const_iterator i=nodes.begin();i!=nodes.end();++i) {
unvisited.push_back((**i).GetNumber());
// Invent a new "dummy" node N+1 which is initially the predecessor of all nodes.
// At the end, any unreachable nodes will still having this precedessor.
pred[(**i).GetNumber()]=nodes.size()+1;
if ((**i).GetNumber()!=src.GetNumber()) {
distance[(**i).GetNumber()]=99e99;
} else {
distance[(**i).GetNumber()]=0;
}
}
// visit each node
while (unvisited.size()>0) {
// visit the node that is currently closest to the source
double curmin=100e99;
deque<unsigned>::iterator c;
unsigned closest;
for (deque<unsigned>::iterator i=unvisited.begin(); i!=unvisited.end();++i) {
if (distance[*i]<curmin) {
curmin=distance[*i];
c=i;
}
}
closest=*c;
unvisited.erase(c);
visited.push_back(closest);
// add edge to this node to the shortest path tree.
if (closest!=src.GetNumber()) {
links.push_back(Link(pred[closest],closest,0,0,0));
}
// relax each outgoing edge from the node we're visiting
Node x = Node(closest,0,0,0);
deque<Link*> *adj= GetOutgoingLinks(FindMatchingNode(&x));
for (deque<Link*>::const_iterator i=adj->begin();i!=adj->end();++i) {
unsigned dest=(**i).GetDest();
double dist=(**i).GetLatency() + curmin;
if (dist<distance[dest]) {
distance[dest]=dist;
pred[dest]=closest;
}
}
delete adj;
}
}
void Topology::CollectShortestPathLinks(const Node &src, const Node &dest, deque<Link> &links)
{
CollectShortestPathTreeLinks(src,links);
}