-
Notifications
You must be signed in to change notification settings - Fork 1
/
pathmeasure.cpp
335 lines (278 loc) · 9.9 KB
/
pathmeasure.cpp
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
//
// usage : allpaths-montecarlo list/[graph].csv > [graph].c
// compile : g++ -W -Wall -pedantic -O3 -funroll-loops -lboost_graph -s allpaths-montecarlo.cpp -g -o allpaths-montecarlo
//
// Linux profiling :
// g++ -W -Wall -pedantic -O3 -funroll-loops -lboost_graph allpaths-montecarlo.cpp -g -pg -c -o allpaths-montecarlo.o
// g++ -g -pg -o allpaths-montecarlo allpaths-montecarlo.o
// objdump -t allpaths-montecarlo
// ./allpaths-montecarlo 10 <list/random.csv >t
// gprof ./allpaths-montecarlo | less
//
// Mac OS X profiling :
// instruments -t /Developer/Applications/Instruments.app/Contents/Resources/templates/Time\ Profiler.tracetemplate ./allpaths-montecarlo
// after modifying allpaths-montecarlo.cpp to load list/random.csv and limiting the path length
//
// recall :
// /opt/local/bin/g++-mp-4.4
// g++ -I/opt/local/include -L/opt/local/lib
#include <stdlib.h>
#include <getopt.h> // getlongopt
// #include <unistd.h> // getopt
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
#include <climits>
#include <ctime>
#include <cmath>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/symmetric.hpp>
#include <boost/numeric/ublas/io.hpp>
namespace ublas = boost::numeric::ublas;
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/subgraph.hpp>
#include <boost/graph/filtered_graph.hpp>
// #include <boost/graph/copy.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/connected_components.hpp>
#include <boost/graph/biconnected_components.hpp>
// #include <boost/graph/johnson_all_pairs_shortest.hpp>
// #include <boost/graph/floyd_warshall_shortest.hpp>
using namespace boost; // Ain't no boost::graph :(
// Ick Global Variables //
static int verbosity = 1;
// Bernoulli Trials Generator //
#include "randomc.h"
class Bernoulli : public CRandomMersenne {
public:
static const unsigned int_max = INT_MAX; // 32768-1; // 2147483648-1;
double p;
Bernoulli(double _p,int seed = (int)time(0)) :
CRandomMersenne(seed), p(_p) { }
bool trial(double _p)
{ return IRandom(0,int_max) < _p*int_max; }
bool trial()
{ return this->trial(p); }
};
// Graph Types //
struct vertex_properties { string id; }; // float p;
typedef property<edge_index_t, size_t, property<edge_weight_t, int> > edge_properties;
// struct edge_properties { size_t edge_index; int edge_weight; };
typedef property<graph_name_t, string> graph_properties;
typedef adjacency_list < vecS, vecS, undirectedS,
vertex_properties, edge_properties, graph_properties > raw_graph_t;
typedef subgraph< raw_graph_t > graph_t;
typedef graph_traits<graph_t>::edge_descriptor edge_descriptor_t;
typedef graph_traits<graph_t>::vertex_descriptor vertex_discripter_t;
// Algorithm //
typedef ublas::symmetric_matrix<double> matrix_t;
// Ideally, this template should handle a non-square symmetric_matrix
// using upper and lower.
template <typename T> inline T norm_max(ublas::symmetric_matrix<T>& M) {
size_t n1 = M.size1(); size_t n2 = M.size2(); T m = 0;
for (size_t i=0; i<n1; i++) for (size_t j=i; j<n2; j++)
m = max(M(i,j),m);
return m;
}
template <typename T> inline T norm_max(ublas::matrix<T>& M) {
size_t n1 = M.size1(); size_t n2 = M.size2(); T m = 0;
for (size_t i=0; i<n1; i++) for (size_t j=0; j<n2; j++)
m = max(M(i,j),m);
return m;
}
struct bernoulli_edge_predicate {
typedef graph_traits<graph_t>::vertices_size_type vertex_size_type;
typedef ublas::symmetric_matrix<int> bool_matrix;
Bernoulli *bernoulli;
graph_t *G;
bool_matrix edge_matrix;
bernoulli_edge_predicate() { }
bernoulli_edge_predicate(graph_t& g, Bernoulli& b) :
bernoulli(&b), G(&g), edge_matrix(num_vertices(g)) { }
bool_matrix::reference
edge(const edge_descriptor_t& e) {
vertex_size_type i = get(vertex_index, *G, source(e,*G));
vertex_size_type j = get(vertex_index, *G, target(e,*G));
return edge_matrix(i,j);
} // cout << '(' << i << ',' << j << ')' << endl;
bool_matrix::const_reference
edge(const edge_descriptor_t& e) const {
vertex_size_type i = get(vertex_index, *G, source(e,*G));
vertex_size_type j = get(vertex_index, *G, target(e,*G));
return edge_matrix(i,j);
} // cout << '(' << i << ',' << j << ')' << endl;
void trial(double p) {
graph_traits<graph_t>::edge_iterator e, e_end;
for (tie(e, e_end) = edges(*G); e != e_end; ++e)
edge(*e) = bernoulli->trial(p)? 1:0;
}
void trial() { trial(bernoulli->p); }
bool operator()(const edge_descriptor_t& e) const
{ return (bool)edge(e); }
};
struct run_trials_paramaters {
int algorithm,seed;
int trials, reports;
double p,precision;
run_trials_paramaters(int s = (int)time(0), double _p = -1) {
p = _p; seed = s;
algorithm = 0;
trials = 1000;
reports = 100;
precision = 0; // 0.00000001;
}
};
matrix_t run_trials(graph_t& G, struct run_trials_paramaters params) {
int n = num_vertices(G);
matrix_t M(n);
switch (n) {
case 2: M(1,1)=1; M(0,1) = params.p; M(1,0)=params.p;
case 1: M(0,0) = 1; return M;
case 0: cerr << "run_trials cannot process an empty graph\n"; abort();
}
Bernoulli bernoulli(params.p,params.seed);
bernoulli_edge_predicate edge_pred(G, bernoulli);
ublas::symmetric_matrix<unsigned long> L = ublas::zero_matrix<unsigned long>(n);
matrix_t P(n); int t,nc=0;
if (verbosity)
{ cerr << "Round\t max. norm\n"; }
for (t=0; t<params.trials; t++) {
edge_pred.trial();
filtered_graph<graph_t, bernoulli_edge_predicate> F(G, edge_pred);
vector<int> components(n);
nc += connected_components(F, &components[0]);
for (int i=0; i<n; i++) for (int j=i; j<n; j++)
if (components[i] == components[j])
++L(i,j);
if (params.reports && !(t % params.reports)) {
M = (matrix_t)L / (double)t;
matrix_t Q = M - P;
double norm = norm_max(Q);
if (t > params.reports) {
if (params.precision && norm < params.precision) return M;
if (verbosity)
{ cerr << '#' << t << "\t" << norm << endl; }
}
P = M;
}
}
M = (matrix_t)L / (double)t;
return M;
}
/*
* johnson_all_pairs_shortest_paths(g,d); // sparce
* floyd_warshall_all_pairs_shortest_paths(g,d); // dense
*/
// Files //
inline void add_vertex_once(vertex_discripter_t v, graph_t& g)
{ if (! g.find_vertex(v).second) add_vertex(v,g); }
size_t biconnected_components(graph_t& g, vector<graph_t*>& bicomponents) {
map< edge_descriptor_t, unsigned > bicomponent_edges;
vector<vertex_discripter_t> articulation_points;
articulation_points.reserve(num_vertices(g));
int n = biconnected_components(g,
make_assoc_property_map(bicomponent_edges),
back_inserter(articulation_points) ).first;
int o = bicomponents.size();
for (int i=0; i<n; i++)
bicomponents.push_back( &(g.create_subgraph()) );
graph_traits<graph_t>::edge_iterator e, e_end;
for (tie(e, e_end) = edges(g); e != e_end; ++e) {
add_vertex_once( source(*e,g), *bicomponents[o+bicomponent_edges[*e]] );
add_vertex_once( target(*e,g), *bicomponents[o+bicomponent_edges[*e]] );
}
// cout << "Graph #" << ... << " : " << get(graph_name, g);
// cout << " |V|=" << num_vertices(g) << endl;
// cout << " |Component|=" << bicomponents.size() << endl;
vector<vertex_discripter_t>::iterator v,v_end;
cout << "Art.pts.\tComponents\n";
// cout << articulation_points.size() << endl;
for (v=articulation_points.begin(), v_end=articulation_points.end();
v != v_end; v++) {
cout << get(&vertex_properties::id, g, *v) << "\t\t";
for (int i=0; i<n; i++)
if ((bicomponents[o+i]->find_vertex(*v)).second)
cout << ' ' << o+i << ' ';
cout << endl;
}
return n;
}
bool dot_load_istream(istream& in, vector<graph_t>& graphs, char *fn = "") {
graph_t g(0, string(fn) );
dynamic_properties dp;
dp.property("id", get(&vertex_properties::id, g) );
if (! read_graphviz(in,g,dp,"id") ) return false;
graphs.push_back(g);
return true;
}
void output_matrix(ostream& out, matrix_t& M, unsigned n) {
out << "matrix_t M[" << n << "] = {\n";
for (unsigned i=0; i<M.size1(); ++i) {
out << "\t{ ";
out << M(i,0);
for (unsigned j=1; j<M.size2() ; ++j)
out << ',' << M(i,j);
out << " },\n";
}
out << "}\n";
}
// void merge_trials(vector<graph_t*>& bicomponents, vector<matrix_t>& distances)
// Main //
void usage() {
cerr << "Usage : allpaths-montecarlo -p=<probability> [-s=<seed>] [-t=<trials>] [-f=<reports>/-q] <graph>" << endl;
abort();
}
int main(int argc, char **argv) {
vector<graph_t> graphs;
vector<graph_t*> bicomponents;
struct run_trials_paramaters params((int)time(0));
static struct option long_options[] = {
{"johnson", no_argument, ¶ms.algorithm, 1}, // sparse
{"floydwarshall", no_argument, ¶ms.algorithm, 2}, // dense
{"trials", required_argument, 0, 'q'},
{"quiet", no_argument, 0, 'q'},
{"help", no_argument, 0, 'h'},
{"precision", required_argument, 0, 1},
};
if (argc == 1) usage();
while (1) {
int option_index = 0;
int c = getopt_long (argc, argv, "p:s:t:f:qh?", long_options, &option_index);
if (c == -1) break;
char* arg=optarg;
if (*arg == '-') do { ++arg; } while(*arg && *arg != '=');
switch (c) {
case 'p': params.p = atof(arg); break;
case 's': params.seed = atoi(arg); break;
case 't': params.trials = atoi(arg); break;
case 'f': params.reports = atoi(arg); break;
case 'q': verbosity = 0; break;
case 'h': case '?': default: usage();
case 1: params.precision = atof(arg); break;
}
}
if (params.p <= 0 || params.p > 1) usage();
if (optind == argc)
dot_load_istream(cin,graphs,(char*)"");
while (optind < argc) {
ifstream in;
in.open(argv[optind], ifstream::in);
dot_load_istream(in,graphs,argv[optind]);
in.close();
++optind;
}
for (unsigned i=0; i<graphs.size(); i++)
biconnected_components(graphs[i],bicomponents);
for (unsigned i=0; i<bicomponents.size(); i++) {
matrix_t M;
M = run_trials(*bicomponents[i],params);
output_matrix(cout,M,i);
}
// if (bicomponents.size())
// merge_trials(bicomponents);
}