graph_generation.cc 15.3 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
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
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006  Tiago de Paula Peixoto <tiago@forked.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include <algorithm>
#include <tr1/unordered_set>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/random.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <iomanip>
#include <map>

#include "graph.hh"
#include "histogram.hh"

using namespace std;
using namespace boost;
using namespace boost::lambda;
using namespace multi_index;
using namespace graph_tool;

typedef boost::mt19937 rng_t;

//==============================================================================
// sample_from_distribution
// this will sample a (j,k) pair from a pjk distribution given a ceil function
// and its inverse
//==============================================================================

template <class Distribution, class Ceil, class InvCeil>
struct sample_from_distribution
{
    sample_from_distribution(Distribution &dist, Ceil& ceil, InvCeil &inv_ceil, double bound, rng_t& rng)
	: _dist(dist), _ceil(ceil), _inv_ceil(inv_ceil), _bound(bound), _rng(rng), _uniform_p(0.0, 1.0) {}
    
    pair<size_t, size_t> operator()()
    {
	// sample j,k from ceil
	size_t j,k;
	double u;
	do
	{
	    tie(j,k) = _inv_ceil(_uniform_p(_rng), _uniform_p(_rng));
	    u = _uniform_p(_rng);
	}
	while (u > _dist(j,k)/(_bound*_ceil(j,k)));
	return make_pair(j,k);
    }

    Distribution& _dist;
    Ceil& _ceil;
    InvCeil& _inv_ceil;
    double _bound;
    rng_t &_rng;
    boost::uniform_real<double> _uniform_p;
};

// vertex type, with desired j,k values and the index in the real graph

struct vertex_t 
{
    vertex_t() {}
    vertex_t(size_t in, size_t out): in_degree(in), out_degree(out) {}
    vertex_t(const pair<size_t,size_t>& deg): in_degree(deg.first), out_degree(deg.second) {}
    size_t index, in_degree, out_degree;
    bool operator==(const vertex_t& other) const {return other.index == index;}
};

inline std::size_t hash_value(const vertex_t& v)
{
    size_t h = hash_value(v.in_degree);
    hash_combine(h, v.out_degree);
    return h;
}

inline size_t dist(const vertex_t& a, const vertex_t& b)
{
    return int(a.in_degree-b.in_degree)*int(a.in_degree-b.in_degree) + 
	int(a.out_degree-b.out_degree)*int(a.out_degree-b.out_degree);
}

struct total_deg_comp
{
    bool operator()(const pair<size_t,size_t>& a, const pair<size_t,size_t>& b)
    {
	return a.first + a.second < b.first + b.second;
    }
};

//==============================================================================
// degree_matrix_t
// this structure will keep the existing (j,k) pairs in the graph in a matrix,
// so that the nearest (j,k) to a given target can be found easily.
//==============================================================================

class degree_matrix_t: public vector<vector<vector<pair<size_t,size_t> > > >
{
public:
    typedef vector<vector<vector<pair<size_t,size_t> > > > base_t;
    degree_matrix_t(size_t N, size_t max_j, size_t max_k, size_t avg_deg)
    {
	_L = size_t(sqrt(N));
	base_t &base = *this;
	base = base_t(_L, vector<vector<pair<size_t,size_t> > >(_L));
	_cj = pow(max_j+1,1.0/(_L-1)) - 1.0;
	_ck = pow(max_k+1,1.0/(_L-1)) - 1.0;
125
126
127
	_avg_deg = avg_deg;
	_nearest_bins = base_t(_L, vector<vector<pair<size_t,size_t> > >(_L));
	_size = 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
128
129
130
131
132
133
    }
    
    void insert(const pair<size_t, size_t>& v)
    {
	size_t j_bin, k_bin;
	tie(j_bin, k_bin) = get_bin(v.first, v.second);
134
135
	if ((*this)[j_bin][k_bin].empty())
	    _size++;
Tiago Peixoto's avatar
Tiago Peixoto committed
136
	(*this)[j_bin][k_bin].push_back(v);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

    }

    void arrange_proximity()
    {
	for(size_t j = 0; j < _L; ++j)
	    for(size_t k = 0; k < _L; ++k)
	    {
		_nearest_bins[j][k].clear();
		if ((*this)[j][k].empty())
		{
		    for(size_t w = 1; w < _L; ++w)
		    {
			for (size_t i = ((j>w)?j-w:0); i < ((j+w<=_L)?j+w:_L); ++i)
			    for (size_t l = ((k>w)?k-w:0); l < ((k+w<=_L)?k+w:_L); ++l)
			    {
				if (!(*this)[i][l].empty())
				    _nearest_bins[j][k].push_back(make_pair(i,l));
			    }
			if (!_nearest_bins[j][k].empty())
			    break;
		    }
		}
	    }
Tiago Peixoto's avatar
Tiago Peixoto committed
161
162
    }

163

Tiago Peixoto's avatar
Tiago Peixoto committed
164
165
166
167
168
169
170
171
172
173
174
175
    void erase(const pair<size_t,size_t>& v)
    {
	size_t j_bin, k_bin;
	tie(j_bin, k_bin) = get_bin(v.first, v.second);
	for(size_t i = 0; i < (*this)[j_bin][k_bin].size(); ++i)
	{
	    if ((*this)[j_bin][k_bin][i] == v)
	    {
		(*this)[j_bin][k_bin].erase((*this)[j_bin][k_bin].begin()+i);
		break;
	    }
	}
176
177
178
179
180
181
182
	if ((*this)[j_bin][k_bin].empty())
	{
	    _size--;
	    if (_size > _L)
		arrange_proximity();
	}
	
Tiago Peixoto's avatar
Tiago Peixoto committed
183
184
    }

185
    pair<size_t,size_t> find_closest(size_t j, size_t k, rng_t& rng)
Tiago Peixoto's avatar
Tiago Peixoto committed
186
187
188
189
190
    {
	vector<pair<size_t,size_t> > candidates;
	size_t j_bin, k_bin;
	tie(j_bin, k_bin) = get_bin(j, k);

191
192
193
194
195
196
197
198
199
200
201
202
203
204
	if ((*this)[j_bin][k_bin].empty())
	{
	    if (_size > _L)
	    {
		if (_nearest_bins[j_bin][k_bin].empty())
		    arrange_proximity();
		for(size_t i = 0; i < _nearest_bins[j_bin][k_bin].size(); ++i)
		{
		    size_t jb,kb;
		    tie(jb,kb) = _nearest_bins[j_bin][k_bin][i];
		    search_bin(jb, kb, j, k, candidates);
		}
	    }
	    else
Tiago Peixoto's avatar
Tiago Peixoto committed
205
	    {
206
207
208
		for(size_t jb = 0; jb < _L; ++jb)
		    for(size_t kb = 0; kb < _L; ++kb)
			search_bin(jb, kb, j, k, candidates);
Tiago Peixoto's avatar
Tiago Peixoto committed
209
	    }
210
211
212
213
	}
	else
	{	    
	    search_bin(j_bin, k_bin, j, k, candidates);
Tiago Peixoto's avatar
Tiago Peixoto committed
214

215
216
217
218
219
220
221
222
223
224
225
226
227
	    size_t distance = size_t(sqrt(dist(candidates.front(), vertex_t(j,k))));
	    
	    for (int x = -1; x < 2; ++x)
		for (int y = -1; y < 2; ++y)
		{ 
		    size_t jb,kb;
		    tie(jb,kb) = get_bin(max(int(j+x*distance),0), max(int(k+y*distance),0));
		    if (tie(jb,kb) == tie(j_bin,k_bin))
			continue;
		    search_bin(jb, kb, j, k, candidates);
		}
	}
	
Tiago Peixoto's avatar
Tiago Peixoto committed
228
	uniform_int<size_t> sample(0, candidates.size() - 1);
229
	return candidates[sample(rng)];
Tiago Peixoto's avatar
Tiago Peixoto committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    }

private:
    pair<size_t,size_t> get_bin(size_t j, size_t k) 
    {
	// uses logarithmic binning...
	size_t j_bin, k_bin, lim;
	lim = size_t(1.5*_avg_deg);
	if (j < lim)
	    j_bin = j;
	else
	    j_bin = size_t(log(j)/log(_cj+1)) - size_t(log(lim)/log(_cj+1)) + lim;
	if (k < lim)
	    k_bin = k;
	else
	    k_bin = size_t(log(k)/log(_ck+1)) - size_t(log(lim)/log(_cj+1)) + lim;
	return make_pair(min(j_bin, _L-1), min(k_bin,_L-1));
    }
    
    void search_bin(size_t j_bin, size_t k_bin, size_t j, size_t k, vector<pair<size_t,size_t> >& candidates)
    {
	for (typeof((*this)[j_bin][k_bin].begin()) iter = (*this)[j_bin][k_bin].begin(); iter != (*this)[j_bin][k_bin].end(); ++iter)
252
253
254
255
256
257
	{
	    if (candidates.empty())
	    {
		candidates.push_back(*iter);
		continue;
	    }
Tiago Peixoto's avatar
Tiago Peixoto committed
258
259
260
261
262
263
264
265
266
	    if (dist(vertex_t(*iter), vertex_t(j,k)) < dist(vertex_t(candidates.front()),vertex_t(j,k)))
	    {
		candidates.clear();
		candidates.push_back(*iter);
	    }
	    else if (dist(vertex_t(*iter), vertex_t(j,k)) == dist(vertex_t(candidates.front()),vertex_t(j,k)))
	    {
		candidates.push_back(*iter);
	    }
267
	}
Tiago Peixoto's avatar
Tiago Peixoto committed
268
269
270
271
272
273
    }

    size_t _L;
    double _cj;
    double _ck;
    size_t _avg_deg;
274
275
    base_t _nearest_bins;
    size_t _size;
Tiago Peixoto's avatar
Tiago Peixoto committed
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
};

//==============================================================================
// GenerateCorrelatedConfigurationalModel
// generates a directed graph with given pjk and degree correlation
//==============================================================================
void GraphInterface::GenerateCorrelatedConfigurationalModel(size_t N, pjk_t pjk, pjk_t ceil_pjk, inv_ceil_t inv_ceil_pjk, double ceil_pjk_bound,
							    corr_t corr, corr_t ceil_corr, inv_corr_t inv_ceil_corr, double ceil_corr_bound, 
							    bool undirected_corr, size_t seed, bool verbose)
{
    _mg.clear();
    _properties = dynamic_properties();
    rng_t rng(seed);

    // sample the N (j,k) pairs

    sample_from_distribution<pjk_t, pjk_t, inv_ceil_t> pjk_sample(pjk, ceil_pjk, inv_ceil_pjk, ceil_pjk_bound, rng);
    vector<vertex_t> vertices(N);
    size_t sum_j=0, sum_k=0, max_j=0, max_k=0;
    if (verbose)
    {
	cout << "adding vertices: " << flush;
    }
    for(size_t i = 0; i < N; ++i)
    {
	if (verbose)
	{
	    static stringstream str;
	    for (size_t j = 0; j < str.str().length(); ++j)
		cout << "\b";
	    str.str("");
	    str << i+1 << " of " << N << " (" << (i+1)*100/N << "%)";
	    cout << str.str() << flush;
	}
	vertex_t& v = vertices[i];
	v.index = _vertex_index[add_vertex(_mg)];
	tie(v.in_degree, v.out_degree) = pjk_sample();
	sum_j += v.in_degree;
	sum_k += v.out_degree;
	max_j = max(v.in_degree,max_j);
	max_k = max(v.out_degree,max_k); 
    }

    if (verbose)
	cout << "\nfixing average degrees: " << flush;

    // <j> and <k> must be the same. Resample random pairs until this holds.
    uniform_int<size_t> vertex_sample(0, N-1);
    while (sum_j != sum_k)
    {
	vertex_t& v = vertices[vertex_sample(rng)];
	sum_j -= v.in_degree;
	sum_k -= v.out_degree;
	tie(v.in_degree, v.out_degree) = pjk_sample();
	sum_j += v.in_degree;
        sum_k +=  v.out_degree;
	max_j = max(v.in_degree,max_j);
	max_k = max(v.out_degree,max_k);
	if (verbose)
	{
	    static stringstream str;
	    for (size_t j = 0; j < str.str().length(); ++j)
		cout << "\b";
	    for (size_t j = 0; j < str.str().length(); ++j)
		cout << " ";
	    for (size_t j = 0; j < str.str().length(); ++j)
		cout << "\b";
	    str.str("");
	    str << min(sum_j-sum_k, sum_k-sum_j);
	    cout << str.str() << flush;
	}
    }

    size_t E = sum_k;
 
    vector<vertex_t> sources; // sources of edges
    typedef tr1::unordered_multimap<pair<size_t,size_t>, vertex_t, hash<pair<size_t,size_t> > > targets_t;
    targets_t targets; // vertices with j > 0
    typedef tr1::unordered_set<pair<size_t,size_t>, hash<pair<size_t,size_t> > > target_degrees_t;
    target_degrees_t target_degrees; // existing (j,k) pairs
    
    // fill up sources, targets and target_degrees
    sources.reserve(E);
    for(size_t i = 0; i < N; ++i)
    {
	for(size_t k = 0; k < vertices[i].out_degree; ++k)
	    sources.push_back(vertices[i]);
	if (vertices[i].in_degree > 0)
	{
	    targets.insert(make_pair(make_pair(vertices[i].in_degree, vertices[i].out_degree), vertices[i]));
	    target_degrees.insert(make_pair(vertices[i].in_degree, vertices[i].out_degree));
	}
    }

    typedef multiset<pair<size_t,size_t>, total_deg_comp> ordered_degrees_t;
    ordered_degrees_t ordered_degrees; // (j,k) pairs ordered by (j+k), i.e, total degree
    degree_matrix_t degree_matrix(target_degrees.size(), max_j, max_k, E/N); // (j,k) pairs layed out in a 2 dimensional matrix
    for(typeof(target_degrees.begin()) iter = target_degrees.begin(); iter != target_degrees.end(); ++iter)
	if (undirected_corr)
	    ordered_degrees.insert(*iter);
	else
	    degree_matrix.insert(*iter);
    
    // shuffle sources 
    for (size_t i = 0; i < sources.size(); ++i)
    {
	uniform_int<size_t> source_sample(i, sources.size()-1);
	swap(sources[i], sources[source_sample(rng)]);
    }

    if (verbose)
	cout << "\nadding edges: " << flush;

    // connect the sources to targets
    uniform_real<double> sample_probability(0.0, 1.0); 
    for (size_t i = 0; i < sources.size(); ++i)
    {
	vertex_t source = sources[i], target;
	size_t j = source.in_degree;
	size_t k = source.out_degree;
	
	//choose the target vertex according to correlation
	    
	pjk_t prob_func = lambda::bind(corr,lambda::_1,lambda::_2,j,k);
	pjk_t ceil = lambda::bind(ceil_corr,lambda::_1,lambda::_2,j,k);
	inv_ceil_t inv_ceil = lambda::bind(inv_ceil_corr,lambda::_1,lambda::_2,j,k);
	sample_from_distribution<pjk_t, pjk_t, inv_ceil_t> corr_sample(prob_func, ceil, inv_ceil, ceil_corr_bound, rng);
	
404
405
406
407
408
	size_t jl,kl;
	tie(jl,kl) = corr_sample(); // target (j,k)
	
	target_degrees_t::iterator iter = target_degrees.find(make_pair(jl,kl));
	if (iter != target_degrees.end())
Tiago Peixoto's avatar
Tiago Peixoto committed
409
	{
410
411
412
413
414
415
	    target = targets.find(*iter)->second; // if an (jl,kl) pair exists, just use that
	}
	else
	{	
	    pair<size_t, size_t> deg;
	    if (undirected_corr)
Tiago Peixoto's avatar
Tiago Peixoto committed
416
	    {
417
418
419
420
		// select the (j,k) pair with the closest total degree (j+k)
		ordered_degrees_t::iterator upper;
		upper = ordered_degrees.upper_bound(make_pair(jl,kl));
		if (upper == ordered_degrees.end())
Tiago Peixoto's avatar
Tiago Peixoto committed
421
		{
422
423
424
425
426
427
428
429
430
431
432
433
434
435
		    --upper;
		    deg = *upper;
		}
		else if (upper == ordered_degrees.begin())
		{
		    deg = *upper;
		}
		else
		{
		    ordered_degrees_t::iterator lower = upper;
		    --lower;
		    if (jl + kl - (lower->first + lower->second) < upper->first + upper->second - (jl + kl))
			deg = *lower;
		    else if (jl + kl - (lower->first + lower->second) != upper->first + upper->second - (jl + kl))
Tiago Peixoto's avatar
Tiago Peixoto committed
436
437
438
			deg = *upper;
		    else
		    {
439
440
441
			// if equal, choose randomly with equal probability
			uniform_int<size_t> sample(0, 1);
			if (sample(rng))
Tiago Peixoto's avatar
Tiago Peixoto committed
442
443
			    deg = *lower;
			else
444
			    deg = *upper;
Tiago Peixoto's avatar
Tiago Peixoto committed
445
446
		    }
		}
447
448
449
450
451
452
453
454
455
456
		target = targets.find(deg)->second;
	    }
	    else
	    {   
		// select the (j,k) which is the closest in the j,k plane.
		deg = degree_matrix.find_closest(jl, kl, rng);
		target = targets.find(deg)->second;
//		cerr << "wanted: " << jl << ", " << kl
//		     << " got: " << deg.first << ", " << deg.second << "\n";
	       
Tiago Peixoto's avatar
Tiago Peixoto committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
	    }	    
	}

	//add edge
	graph_traits<multigraph_t>::edge_descriptor e;
	e = add_edge(vertex(source.index, _mg), vertex(target.index, _mg), _mg).first;
	_edge_index[e] = i;

	// if target received all the edges it should, remove it from target
	if (in_degree(vertex(target.index, _mg), _mg) == target.in_degree)
	{
	    targets_t::iterator iter,end;
	    for(tie(iter,end) = targets.equal_range(make_pair(target.in_degree, target.out_degree)); iter != end; ++iter)
		if (iter->second == target)
		{
		    targets.erase(iter);
		    break;
		}

	    // if there are no more targets with (jl,kl), remove pair from target_degrees, etc.
	    if (targets.find(make_pair(target.in_degree, target.out_degree)) == targets.end())
	    {
		target_degrees.erase(target_degrees.find(make_pair(target.in_degree, target.out_degree)));
		if (target_degrees.bucket_count() > 2*target_degrees.size())
		{
		    target_degrees_t temp;
		    for(target_degrees_t::iterator iter = target_degrees.begin(); iter != target_degrees.end(); ++iter)
			temp.insert(*iter);
		    target_degrees = temp;
		}
		if (undirected_corr)
		{
		    for(ordered_degrees_t::iterator iter = ordered_degrees.find(make_pair(target.in_degree, target.out_degree)); 
			iter != ordered_degrees.end(); ++iter)
			if (*iter == make_pair(target.in_degree, target.out_degree))
			{
			    ordered_degrees.erase(iter);
			    break;
			}
		}
		else
		{
		    degree_matrix.erase(make_pair(target.in_degree, target.out_degree));
		}
	    }
	    
	}

	if (verbose)
	{
	    static stringstream str;	    
	    for (size_t j = 0; j < str.str().length(); ++j)
		cout << "\b";
	    for (size_t j = 0; j < str.str().length(); ++j)
		cout << " ";
	    for (size_t j = 0; j < str.str().length(); ++j)
		cout << "\b";
	    str.str("");
515
	    str << (i+1) << " of " << E << " (" << (i+1)*100/E << "%)";
Tiago Peixoto's avatar
Tiago Peixoto committed
516
517
518
519
520
521
522
523
	    cout << str.str() << flush;
	}
	
    }
    
    if (verbose)
	cout << "\n";
}