-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeshalgs.cc
More file actions
415 lines (341 loc) · 11.9 KB
/
Copy pathmeshalgs.cc
File metadata and controls
415 lines (341 loc) · 11.9 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/***************************************************************************
meshalgs.cc - description
-------------------
begin : Mon Mar 31 2008
copyright : (C) 2005 by Knut-Helge Vik
email : knuthelv@ifi.uio.no
***************************************************************************/
#include "meshalgs.h"
#include "../treealgs/treestructure.h"
#include "../simdefs.h"
#include "../treealgs/fheap.h"
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/reverse_graph.hpp>
#include <boost/graph/graph_utility.hpp>
#include <functional>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
using namespace boost;
using namespace TreeAlgorithms;
//typedef TreeAlgorithms::COLOR COLOR;
namespace GraphAlgorithms
{
// dijkstra's shortest path tree
void maxDistance(const GraphN &g, const VertexSet &V, int src, DistanceVector &zdistance, DistanceVector &hops, ParentVector &zparent, double &eccentricity, int &node_in_eccentricity_path)
{
out_edge_iteratorN oit, oit_end;
//enum COLOR { WHITE = 0, GRAY, BLACK};
std::vector<TreeAlgorithms::COLOR> colorVector(num_vertices(g));
HeapD<FHeap> heapD;
Heap *heap = heapD.newInstance(num_vertices(g));
for(VertexSet::iterator vit = V.begin(), vit_end = V.end(); vit != vit_end; ++vit)
{
zdistance[*vit] = (std::numeric_limits<double>::max)();
zparent[*vit] = *vit;
hops[*vit] = 0;
colorVector[*vit] = WHITE;
}
colorVector[src] = GRAY;
zdistance[src] = 0;
heap->insert(src, 0.0);
while(heap->nItems() > 0)
{
int u = heap->deleteMin();
for(boost::tuples::tie(oit, oit_end) = out_edges(u, g); oit != oit_end; ++oit)
{
int v = target(*oit, g);
double path_weight = zdistance[u] + g[*oit].weight;
if((path_weight < zdistance[v]))
{
zdistance[v] = path_weight;
zparent[v] = u;
hops[v] = hops[u] + 1;
if(colorVector[v] == WHITE)
{
colorVector[v] = GRAY;
heap->insert(v, zdistance[v]);
}
else if(colorVector[v] == GRAY)
{
heap->decreaseKey(v, zdistance[v]);
}
if(zdistance[v] >= eccentricity)
{
eccentricity = zdistance[v];
node_in_eccentricity_path = v;
}
}
}
colorVector[u] = BLACK;
}
delete heap;
}
void maxDistance(const TreeStructure &T, int src, DistanceVector &zdistance, DistanceVector &hops, ParentVector &zparent, double &eccentricity, int &node_in_eccentricity_path)
{
maxDistance(T.g, T.V, src, zdistance, hops, zparent, eccentricity, node_in_eccentricity_path);
}
// dijkstra's shortest path tree
void maxDistance(const TreeStructure &T, int src, DistanceVector &zdistance, DistanceVector &hops, ParentVector &zparent)
{
double ecc = 0;
int node = -1;
maxDistance(T, src, zdistance, hops, zparent,ecc, node);
}
double eccentricity_distance(const TreeStructure &T, int src)
{
return eccentricity_distance(T.g, T.V, src);
}
double eccentricity_distance(const GraphN &g, const VertexSet &V, int src)
{
DistanceVector distance(num_vertices(g));
DistanceVector hops(num_vertices(g));
ParentVector parent(num_vertices(g));
double ecc = 0;
int node = -1;
maxDistance(g, V, src, distance, hops, parent, ecc, node);
return ecc;
}
double eccentricity_distance(const GraphN &g, const VertexSet &V, int src, DistanceVector &distance, DistanceVector &hops, ParentVector &parent)
{
double ecc = 0;
int node = -1;
maxDistance(g, V, src, distance, hops, parent, ecc, node);
return ecc;
}
// breadth first search tree
void maxDistanceHops(const TreeStructure &T, int src, DistanceVector &distance, DistanceVector &hops, ParentVector &parent)
{
const GraphN &g = T.g;
ASSERTING(T.V.contains(src));
for(VertexSet::iterator vit = T.V.begin(), vit_end = T.V.end(); vit != vit_end; ++vit)
{
distance[*vit] = (std::numeric_limits<double>::max)(); // set to maximum distance
hops[*vit] = (std::numeric_limits<double>::max)(); // set to maximum distance
parent[*vit] = *vit;
}
distance[src] = 0;
hops[src] = 0;
parent[src] = src;
typedef list<int> IntList;
IntList neighbors;
neighbors.push_back(src);
while(!neighbors.empty())
{
int v = neighbors.front();
neighbors.pop_front();
ASSERTING(T.V.contains(v));
out_edge_iteratorN oit, oit_end;
for(boost::tuples::tie(oit, oit_end) = out_edges(v, g); oit != oit_end; ++oit)
{
int u = target(*oit, g);
ASSERTING(T.V.contains(u));
if(hops[u] == (std::numeric_limits<double>::max()))
{
hops[u] = hops[v] + 1;
distance[u] = distance[v] + g[*oit].weight;
parent[u] = v;
neighbors.push_back(u);
}
}
}
}
// breadth first search tree
double maxDistanceHops(const TreeStructure &T, int src, DistanceVector &distance)
{
const GraphN &g = T.g;
ASSERTING(T.V.contains(src));
for(VertexSet::iterator vit = T.V.begin(), vit_end = T.V.end(); vit != vit_end; ++vit)
distance[*vit] = (std::numeric_limits<double>::max)();
distance[src] = 0;
double diameter = 0;
typedef list<int> IntList;
IntList neighbors;
neighbors.push_back(src);
while(!neighbors.empty())
{
int v = neighbors.front();
neighbors.pop_front();
out_edge_iteratorN oit, oit_end;
for(boost::tuples::tie(oit, oit_end) = out_edges(v, g); oit != oit_end; ++oit)
{
int u = target(*oit, g);
ASSERTING(T.V.contains(u));
if(distance[u] == (std::numeric_limits<double>::max()))
{
distance[u] = distance[v] + 1;
if(distance[u] > diameter) diameter = distance[u];
neighbors.push_back(u);
}
}
}
return diameter;
}
double eccentricity_hops(const TreeStructure &T, int src)
{
DistanceVector distance(num_vertices(T.g));
return maxDistanceHops(T, src, distance);
}
double diameter_hops(const TreeStructure &T)
{
double max = 0;
DistanceVector distance(num_vertices(T.g));
for(VertexSet::iterator vit = T.V.begin(), vit_end = T.V.end(); vit != vit_end; ++vit)
{
int v = *vit;
double d = maxDistanceHops(T, v, distance);
max = d > max ? d : max;
}
return max;
}
double diameter_distance(const TreeStructure &T)
{
double diameter = 0;
PathList diameterPath;
diameter_distance(T, diameterPath, diameter);
return diameter;
}
void diameter_distance(const TreeStructure &T, PathList &diameterPath, double &diameter)
{
DistanceVector distance(num_vertices(T.g));
DistanceVector hops(num_vertices(T.g));
ParentVector parent(num_vertices(T.g));
for(VertexSet::iterator vit = T.V.begin(), vit_end = T.V.end(); vit != vit_end; ++vit)
{
double eccentricity = 0;
int node_in_eccentricity_path = -1;
int v = *vit;
maxDistance(T, v, distance, hops, parent, eccentricity, node_in_eccentricity_path);
if(diameter < eccentricity)
{
diameter = eccentricity;
diameterPath.clear();
ASSERTING(node_in_eccentricity_path != v);
int path_node = node_in_eccentricity_path;
while(path_node != v)
{
diameterPath.push_back(path_node);
path_node = parent[path_node];
}
diameterPath.push_back(v);
//cerr << WRITE_FUNCTION << " diameter : " << diameter << endl;
}
}
}
// matrix of the shortest distances between any two vertices.
void distanceMatrix(const TreeStructure &T, DistanceVectorMatrix &distance, DistanceVectorMatrix &hops, ParentVectorMatrix &parent)
{
for(VertexSet::iterator vit = T.V.begin(), vit_end = T.V.end(); vit != vit_end; ++vit)
{
int v = *vit;
maxDistance(T, v, distance[v], hops[v], parent[v]);
}
}
void distanceMatrixHops(const TreeStructure &T, DistanceVectorMatrix &distance)
{
for(VertexSet::iterator vit = T.V.begin(), vit_end = T.V.end(); vit != vit_end; ++vit)
{
int v = *vit;
maxDistanceHops(T, v, distance[v]);
}
}
void printAllPairsSP(ParentMatrix &parent, int src, int targ, PathList &path)
{
if(src == targ)
{
cerr << src << " ";
path.push_back(src);
}
else if(parent[src][targ] == src)
{
cerr << " no path from " << src << " to " << targ << " exists " << endl;
}
else
{
printAllPairsSP(parent, src, parent[src][targ], path);
cerr << targ << " ";
path.push_back(targ);
}
}
};
/*
double maxDistanceHops(const TreeStructure &T, int src, DistanceVector &distance)
{
const GraphN &g = T.g;
ASSERTING(T.V.contains(src));
for(VertexSet::iterator vit = T.V.begin(), vit_end = T.V.end(); vit != vit_end; ++vit)
distance[*vit] = (std::numeric_limits<double>::max)(); // set to maximum distance
distance[src] = 0;
double depth = 1; // next distance in BFS
int cnt = 0;
VertexSet neighborSet;
neighborSet.insert(src);
while(!neighborSet.empty())
{
VertexSet nextNeighbors;
for(VertexSet::iterator vit = neighborSet.begin(), vit_end = neighborSet.end(); vit != vit_end; ++vit)
{
int v = *vit;
out_edge_iteratorN oit, oit_end;
ASSERTING(T.V.contains(v));
for(boost::tuples::tie(oit, oit_end) = out_edges(v, g); oit != oit_end; ++oit)
{
int u = target(*oit, g);
if(distance[u] == (std::numeric_limits<double>::max)()) // first time reachable?
{
cnt++;
distance[u] = depth;
nextNeighbors.insert(u);
}
}
}
// next try set of vertices further away
neighborSet = nextNeighbors;
depth++;
}
return cnt == T.V.size() ? depth-1 : T.V.size();
}
*/
/*void maxDistance(const TreeStructure &T, int src, DistanceVector &distance, DistanceVector &hops, ParentVector &parent, double &eccentricity, int &node_in_eccentricity_path)
{
const GraphN &g = T.g;
ASSERTING(T.V.contains(src));
for(VertexSet::iterator vit = T.V.begin(), vit_end = T.V.end(); vit != vit_end; ++vit)
{
distance[*vit] = (std::numeric_limits<double>::max)(); // set to maximum distance
hops[*vit] = (std::numeric_limits<double>::max)(); // set to maximum distance
parent[*vit] = *vit;
}
distance[src] = 0;
hops[src] = 0;
parent[src] = src;
typedef list<int> IntList;
IntList neighbors;
neighbors.push_back(src);
while(!neighbors.empty())
{
int v = neighbors.front();
neighbors.pop_front();
ASSERTING(T.V.contains(v));
out_edge_iteratorN oit, oit_end;
for(boost::tuples::tie(oit, oit_end) = out_edges(v, g); oit != oit_end; ++oit)
{
int u = target(*oit, g);
ASSERTING(T.V.contains(u));
if(distance[u] > distance[v] + g[*oit].weight)
{
hops[u] = hops[v] + 1;
distance[u] = distance[v] + g[*oit].weight;
parent[u] = v;
neighbors.push_back(u);
if(distance[u] >= eccentricity)
{
eccentricity = distance[u];
node_in_eccentricity_path = u;
}
//cerr << u << " hops " << hops[u] << " distance " << distance[u] << " parent " << parent[u] << endl;
}
}
}
}*/