Repository navigation
Expand file tree
/
Copy pathBoostGraph_i.h
More file actions
390 lines (345 loc) · 14.4 KB
/
Copy pathBoostGraph_i.h
File metadata and controls
390 lines (345 loc) · 14.4 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
/*______________________________________________________________________________
BoostGraph_i.h
Description: This library provides a simple interface to the Boost Graph
C++ Libraries so that other applications can ignore much of the templating
and syntax details. This library implements accessors to algorithms within
boost graph that are applicable to both directed and undirected graphs
______________________________________________________________________________
*/
#ifndef _BOOSTGRAPH_I_H_
#define _BOOSTGRAPH_I_H_
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <utility>
#include <algorithm>
#include "TwoDArray.h"
#include <boost/config.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/pending/indirect_cmp.hpp>
#include <boost/range/irange.hpp>
#include <boost/static_assert.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/johnson_all_pairs_shortest.hpp>
#include <boost/graph/floyd_warshall_shortest.hpp>
using namespace std;
using namespace boost;
typedef property<edge_weight_t, double> Weight;
typedef std::pair<int,int> Pair;
typedef std::pair<Pair*,double> GEdge; // Edge nodes with weight
typedef std::pair<std::vector<int>, double> Path; // Path of nodes with path weight
//______________________________________________________________________________
// CLASS DEFINITION
template <typename G>
class BoostGraph_i
{
public:
// Type declarations
typedef typename graph_traits<G>::vertices_size_type size_type;
typedef typename graph_traits<G>::edge_descriptor edge_descriptor; // Boost edge
typedef typename graph_traits<G>::vertex_descriptor vertex_descriptor; // Boost vertex
struct dijkstraPath { // Hold node distances and parent paths for dijkstras shortest paths algorithm
int sourceNodeId;
std::vector<int>* distances;
std::vector<vertex_descriptor>* parents;
};
BoostGraph_i();
virtual ~BoostGraph_i();
G* boostGraph;
virtual bool addNode(int nodeId);
virtual bool addEdge(int nodeIdSource, int nodeIdSink, double weightVal);
virtual int nodeCount() const;
virtual int edgeCount() const;
// ALGORITHMS
virtual std::vector<int> breadthFirstSearch(int startNodeId);
virtual std::vector<int> depthFirstSearch(int startNodeId);
virtual Path dijkstraShortestPath(int nodeIdStart, int nodeIdEnd);
virtual double allPairsShortestPathsJohnson(int nodeIdStart, int nodeIdEnd);
virtual double allPairsShortestPathsFloydWarshall(int nodeIdStart, int nodeIdEnd);
//protected:
int isDirected;
int _changed; // -1 for no graph object, 0 for no change, 1 for change in graph
std::vector<GEdge*>* _edges; // holds the edge pairs
std::map<int,int>* _nodes; // holds a slot for each node
typename property_map<G,edge_weight_t>::type _weightmap;
std::map<int,dijkstraPath> _dijkstraPaths; // Dijkstra's shortest paths storage
TwoDArray<double>* _allPairsDistanceJohnson; // Path distance for Johnson All Pairs Shortest Paths
TwoDArray<double>* _allPairsDistanceFloydWarshall; // Path distance for Floyd-Warshall APSP
virtual typename graph_traits<G>::vertex_descriptor _getNode(int nodeId);
virtual void _fillGraph(); // instantiate boostGraph and adds nodes and edges
};
//______________________________________________________________________________
// IMPLEMENTATION
template <typename G>
BoostGraph_i<G>::BoostGraph_i() {
_changed = -1;
_edges = new std::vector<GEdge*>;
_nodes = new std::map<int,int>;
_allPairsDistanceJohnson = NULL;
}
//______________________________________________________________________________
template <typename G>
BoostGraph_i<G>::~BoostGraph_i() {
// delete GEdge and Pair objects
for(unsigned int i=0; i<_edges->size(); i++) {
delete (*_edges)[i]->first; // Pair pointer
delete (*_edges)[i];
}
for(unsigned int i=0; i<_dijkstraPaths.size(); i++) {
delete _dijkstraPaths[i].distances;
delete _dijkstraPaths[i].parents;
}
delete boostGraph;
delete _edges;
delete _nodes;
}
//______________________________________________________________________________
template <typename G>
bool BoostGraph_i<G>::addNode(int nodeId) {
if((*_nodes)[nodeId]==nodeId) return false; // node exists
(*_nodes)[nodeId]=nodeId;
_changed=1;
return true;
}
//______________________________________________________________________________
template <typename G>
bool BoostGraph_i<G>::addEdge(int nodeIdSource, int nodeIdSink, double weightVal=1.0) {
Pair* twoNodes = new Pair(nodeIdSource,nodeIdSink);
GEdge* thisEdge = new GEdge(twoNodes,weightVal);
addNode(nodeIdSource);
addNode(nodeIdSink);
_edges->push_back(thisEdge);
_changed=1;
return true;
}
//______________________________________________________________________________
template <typename G>
int BoostGraph_i<G>::nodeCount() const {
return _nodes->size();
}
//______________________________________________________________________________
template <typename G>
int BoostGraph_i<G>::edgeCount() const {
return _edges->size();
}
//______________________________________________________________________________
template <typename G>
typename graph_traits<G>::vertex_descriptor BoostGraph_i<G>::_getNode(int nodeId) {
typename graph_traits<G>::vertex_descriptor n;
if((*_nodes)[nodeId]==nodeId) {
n = vertex(nodeId, *this->boostGraph);
}
return n;
}
//______________________________________________________________________________
template <typename G>
void BoostGraph_i<G>::_fillGraph() {
int numNodes = nodeCount();
// zero out stored data
_dijkstraPaths.clear();
_allPairsDistanceJohnson = NULL;
this->boostGraph = new G(numNodes); // Boost Graph instance
// add nodes, in edges or not
for(std::map<int,int>::iterator Ni = _nodes->begin(); Ni != _nodes->end(); Ni++) {
vertex((*Ni).first,*this->boostGraph);
}
// add edges
_weightmap = get(edge_weight, *this->boostGraph);
for(unsigned int i=0; i<_edges->size(); i++) {
GEdge* ge = (*_edges)[i];
edge_descriptor e; bool inserted;
if (ge->first->first>=0 && ge->first->second>=0) { // skip any malformed edges
tie(e, inserted) = add_edge(ge->first->first, ge->first->second, Weight(ge->second), *this->boostGraph);
_weightmap[e] = ge->second;
}
}
_changed=0;
return;
}
//______________________________________________________________________________
// ALGORITHMS
// Breadth First Search
// - parameters:
// - startNodeId, the start node for the traversal
// - returns: a vector of integers identifying the node order of the traversal
// BFS time visitor
template <typename TimeMap>
class bfs_time_visitor : public default_bfs_visitor
{
typedef typename property_traits < TimeMap >::value_type T;
public:
bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { }
template < typename Vertex, typename Graph >
void discover_vertex(Vertex u, const Graph & g) const
{
put(m_timemap, u, m_time++);
}
TimeMap m_timemap;
T & m_time;
};
template <typename G>
std::vector<int> BoostGraph_i<G>::breadthFirstSearch(int startNodeId) {
std::vector<int> ret; // list of nodes to return
if(_changed!=0) this->_fillGraph();
int N = num_vertices(*this->boostGraph);// number of nodes
// Typedefs
typedef typename graph_traits<G>::vertices_size_type Size;
typedef Size* Iiter;
// a vector to hold the discover time property for each vertex
std::vector <Size> dtime(num_vertices(*this->boostGraph));
Size time = 0;
bfs_time_visitor<Size*> vis(&dtime[0], time);
breadth_first_search(*this->boostGraph, vertex(startNodeId, *this->boostGraph), visitor(vis));
// Use std::sort to order the vertices by their discover time
std::vector<size_type> discover_order(N);
integer_range<int> range(0, N);
std::copy(range.begin(), range.end(), discover_order.begin());
std::sort(discover_order.begin(), discover_order.end(),
indirect_cmp< Iiter, std::less<Size> >(&dtime[0]));
for (int i = 0; i < N; ++i)
ret.push_back(int(discover_order[i]));
return ret;
}
//______________________________________________________________________________
// Depth First Search
// - parameters:
// - startNodeId, the start node for the traversal
// - returns: a vector of integers identifying the node order of the traversal
// DFS time visitor
template < typename TimeMap > class dfs_time_visitor:public default_dfs_visitor {
typedef typename property_traits < TimeMap >::value_type T;
public:
dfs_time_visitor(TimeMap dmap, TimeMap fmap, T & t)
: m_dtimemap(dmap), m_ftimemap(fmap), m_time(t) {
}
template < typename Vertex, typename Graph >
void discover_vertex(Vertex u, const Graph & g) const
{
put(m_dtimemap, u, m_time++);
}
template < typename Vertex, typename Graph >
void finish_vertex(Vertex u, const Graph & g) const
{
put(m_ftimemap, u, m_time++);
}
TimeMap m_dtimemap;
TimeMap m_ftimemap;
T & m_time;
};
template <typename G>
std::vector<int> BoostGraph_i<G>::depthFirstSearch(int startNodeId) {
std::vector<int> ret; // list of nodes to return
if(_changed!=0) this->_fillGraph();
int N = num_vertices(*this->boostGraph);// number of nodes
// Typedefs
typedef size_type* Iiter;
// discover time and finish time properties
std::vector<size_type> dtime(num_vertices(*this->boostGraph));
std::vector<size_type> ftime(num_vertices(*this->boostGraph));
size_type t = 0;
dfs_time_visitor<size_type*> vis(&dtime[0], &ftime[0], t);
depth_first_search(*this->boostGraph, visitor(vis));
// use std::sort to order the vertices by their discover time
std::vector<size_type> discover_order(N);
integer_range<size_type> r(0, N);
std::copy(r.begin(), r.end(), discover_order.begin());
std::sort(discover_order.begin(), discover_order.end(),
indirect_cmp<Iiter, std::less<size_type> >(&dtime[0]));
for (int i = 0; i < N; ++i)
ret.push_back(int(discover_order[i]));
return ret;
}
//______________________________________________________________________________
// Dijksta's Shortest Paths
// - parameters:
// - nodeIdStart, the root node id. the paths starts from here
// - nodeIdEnd, the end node id in the path
// - returns: a Path were the vector gives the path order and the double give the weight
// - note:
template <typename G>
Path BoostGraph_i<G>::dijkstraShortestPath(int nodeIdStart, int nodeIdEnd) {
if(_changed!=0) this->_fillGraph();
Path ret;
// compute shortest paths if not done already
if(_dijkstraPaths[nodeIdStart].sourceNodeId >=0) {
std::vector<vertex_descriptor>* p = new std::vector<vertex_descriptor>(num_vertices(*this->boostGraph));
std::vector<int>* d = new std::vector<int>(num_vertices(*this->boostGraph));
_dijkstraPaths[nodeIdStart].distances = d;
_dijkstraPaths[nodeIdStart].parents = p;
vertex_descriptor source = vertex(nodeIdStart, *this->boostGraph);
dijkstra_shortest_paths(*this->boostGraph, source,
predecessor_map(boost::make_iterator_property_map(p->begin(), get(boost::vertex_index, *this->boostGraph))).
distance_map(boost::make_iterator_property_map(d->begin(), get(boost::vertex_index, *this->boostGraph))));
}
// retrieve path and distance
Path tmp; // push nodes on backwards, then reverse for return
int Vi=nodeIdEnd;
tmp.first.push_back(Vi);
while(nodeIdStart != Vi && Vi>=0) {
int Vt = (*_dijkstraPaths[nodeIdStart].parents)[Vi];
tmp.first.push_back(Vt);
Vi = Vt;
}
std::vector<int>* distances = _dijkstraPaths[nodeIdStart].distances;
ret.second = (*distances)[nodeIdEnd];
// reverse tmp path
for (std::vector<int>::iterator i=tmp.first.end()-1; i!=tmp.first.begin(); i--) {
ret.first.push_back((*i));
}
ret.first.push_back((*tmp.first.begin()));
return ret;
}
//______________________________________________________________________________
// Johnson All Pairs Shortest Paths (Good for sparse interaction graphs)
// - parameters:
// - nodeIdStart, the root node id. the paths starts from here
// - nodeIdEnd, the end node id in the path
// - returns: a Path were the vector gives the path order and the double give the weight
// - note: the all-pairs distance matrix is stored and stable until alteration of the graph,
// at which point it will
template <typename G>
double BoostGraph_i<G>::allPairsShortestPathsJohnson(int nodeIdStart, int nodeIdEnd) {
double pathWt=0.0;
// compute all pairs matrix if needed
if(this->_allPairsDistanceJohnson == NULL || _changed!=0) {
if(_changed!=0) this->_fillGraph();
int V = num_vertices(*this->boostGraph);
this->_allPairsDistanceJohnson = new TwoDArray<double>(V,V); // the distance matrix (D)
std::vector<double>* d = new std::vector<double>(V, std::numeric_limits < double >::max()); // path distances
johnson_all_pairs_shortest_paths(*this->boostGraph, (*this->_allPairsDistanceJohnson), distance_map(&(*d)[0]));
}
// return path weight
pathWt = (*this->_allPairsDistanceJohnson)[nodeIdStart][nodeIdEnd];
return pathWt;
}
//______________________________________________________________________________
// Floyd-Warshall All Pairs Shortest Paths (Good for dense interaction graphs)
// - parameters:
// - nodeIdStart, the root node id. the paths starts from here
// - nodeIdEnd, the end node id in the path
// - returns: a Path were the vector gives the path order and the double give the weight
// - note: the all-pairs distance matrix is stored and stable until alteration of the graph,
// at which point it will
template <typename G>
double BoostGraph_i<G>::allPairsShortestPathsFloydWarshall(int nodeIdStart, int nodeIdEnd) {
double pathWt=0.0;
// compute all pairs matrix if needed
if(this->_allPairsDistanceFloydWarshall == NULL || _changed!=0) {
if(_changed!=0) this->_fillGraph();
int V = num_vertices(*this->boostGraph);
this->_allPairsDistanceFloydWarshall = new TwoDArray<double>(V,V); // the distance matrix (D)
std::vector<double>* d = new std::vector<double>(V, std::numeric_limits < double >::max()); // path distances
floyd_warshall_all_pairs_shortest_paths(*this->boostGraph, (*this->_allPairsDistanceFloydWarshall), distance_map(&(*d)[0]));
}
// return path weight
pathWt = (*this->_allPairsDistanceFloydWarshall)[nodeIdStart][nodeIdEnd];
return pathWt;
}
//______________________________________________________________________________
#endif // _BOOSTGRAPH_I_H_