Commit 990b4739 authored by Tiago Peixoto's avatar Tiago Peixoto

Fix "correlated" rewiring mode

This fixes a bug in the "correlated" mode in random_rewire().
parent bad74126
......@@ -245,13 +245,18 @@ struct graph_rewire
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
vector<edge_t> edges;
typedef random_permutation_iterator<typename vector<edge_t>::iterator,
vector<size_t> edge_pos;
typedef random_permutation_iterator<typename vector<size_t>::iterator,
rng_t>
random_edge_iter;
typename graph_traits<Graph>::edge_iterator e, e_end;
for (tie(e, e_end) = boost::edges(g); e != e_end; ++e)
{
edges.push_back(*e);
edge_pos.push_back(edge_pos.size());
}
RewireStrategy<Graph, EdgeIndexMap, CorrProb, BlockDeg>
rewire(g, edge_index, edges, corr_prob, bd, rng);
......@@ -266,8 +271,8 @@ struct graph_rewire
for (size_t i = 0; i < niter; ++i)
{
random_edge_iter
ei_begin(edges.begin(), edges.end(), rng),
ei_end(edges.end(), edges.end(), rng);
ei_begin(edge_pos.begin(), edge_pos.end(), rng),
ei_end(edge_pos.end(), edge_pos.end(), rng);
// for each edge rewire its source or target
for (random_edge_iter ei = ei_begin; ei != ei_end; ++ei)
......@@ -276,7 +281,7 @@ struct graph_rewire
if (verbose)
print_progress(i, niter, e_pos, no_sweep ? 1 : edges.size(),
str);
bool success = rewire(e_pos, self_loops, parallel_edges);
bool success = rewire(*ei, self_loops, parallel_edges);
if (!success)
++pcount;
......@@ -492,11 +497,9 @@ public:
base_t;
typedef Graph graph_t;
typedef EdgeIndexMap edge_index_t;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
typedef typename EdgeIndexMap::value_type index_t;
CorrelatedRewireStrategy(Graph& g, EdgeIndexMap edge_index,
vector<edge_t>& edges, CorrProb, BlockDeg,
......@@ -506,40 +509,42 @@ public:
for (size_t ei = 0; ei < base_t::_edges.size(); ++ei)
{
// For undirected graphs, there is no difference between source and
// target, and each edge will appear _twice_ in the lists below,
// target, and each edge will appear _twice_ in the list below,
// once for each different ordering of source and target.
edge_t& e = base_t::_edges[ei];
_edges_by_target[make_pair(in_degreeS()(target(e, _g), _g),
out_degree(target(e, _g), _g))]
.push_back(make_pair(ei, false));
vertex_t t = target(e, _g);
deg_t tdeg = make_pair(in_degreeS()(t, _g), out_degree(t, _g));
_edges_by_target[tdeg].push_back(make_pair(ei, false));
if (!is_directed::apply<Graph>::type::value)
{
_edges_by_target[make_pair(in_degreeS()(source(e, _g), _g),
out_degree(source(e, _g), _g))]
.push_back(make_pair(ei, true));
t = source(e, _g);
tdeg = make_pair(in_degreeS()(t, _g), out_degree(t, _g));
_edges_by_target[tdeg].push_back(make_pair(ei, true));
}
}
}
pair<size_t,bool> get_target_edge(size_t ei)
{
pair<size_t, size_t> deg =
make_pair(in_degreeS()(target(base_t::_edges[ei], _g), _g),
out_degree(target(base_t::_edges[ei], _g), _g));
edges_by_end_deg_t& edges = _edges_by_target;
typename edges_by_end_deg_t::mapped_type& elist = edges[deg];
edge_t& e = base_t::_edges[ei];
vertex_t t = target(e, _g);
deg_t tdeg = make_pair(in_degreeS()(t, _g), out_degree(t, _g));
typename edges_by_end_deg_t::mapped_type& elist =
_edges_by_target[tdeg];
tr1::uniform_int<> sample(0, elist.size() - 1);
return elist[sample(base_t::_rng)];
}
void update_edge(size_t e, bool insert) {}
private:
typedef tr1::unordered_map<pair<size_t, size_t>,
vector<pair<index_t, bool> >,
hash<pair<size_t, size_t> > >
typedef pair<size_t, size_t> deg_t;
typedef tr1::unordered_map<deg_t,
vector<pair<size_t, bool> >,
hash<deg_t> >
edges_by_end_deg_t;
edges_by_end_deg_t _edges_by_target;
......
This diff is collapsed.
......@@ -115,7 +115,7 @@ def local_clustering(g, prop=None, undirected=True):
>>> g = gt.random_graph(1000, lambda: (5,5))
>>> clust = gt.local_clustering(g)
>>> print gt.vertex_average(g, clust)
(0.008338888888888889, 0.0004126098432127491)
(0.00908888888888889, 0.0004449824521439575)
References
----------
......@@ -172,7 +172,7 @@ def global_clustering(g):
>>> seed(42)
>>> g = gt.random_graph(1000, lambda: (5,5))
>>> print gt.global_clustering(g)
(0.008353381448810478, 0.00041351594365452094)
(0.009114059777509717, 0.0004464454368899158)
References
----------
......@@ -247,11 +247,11 @@ def extended_clustering(g, props=None, max_depth=3, undirected=False):
>>> for i in xrange(0, 5):
... print gt.vertex_average(g, clusts[i])
...
(0.0061200000000000004, 0.0004859481453817887)
(0.024368333333333332, 0.0009455588573842338)
(0.11548333333333334, 0.0020091290954595783)
(0.3999433333333333, 0.0030720255912562527)
(0.43571666666666664, 0.0031127199163718177)
(0.0058850000000000005, 0.0004726257592782405)
(0.026346666666666668, 0.0009562588213100747)
(0.11638833333333333, 0.002086419787711849)
(0.3862533333333333, 0.003020064612995335)
(0.44685499999999995, 0.003124572962377774)
References
----------
......@@ -324,7 +324,7 @@ def motifs(g, k, p=1.0, motif_list=None):
>>> print len(motifs)
11
>>> print counts
[116256, 392719, 335, 380, 2954, 850, 808, 1, 11, 4, 1]
[115104, 389090, 724, 820, 1828, 3208, 791, 4, 12, 12, 3]
References
......@@ -489,7 +489,7 @@ def motif_significance(g, k, n_shuffles=100, p=1.0, motif_list=None,
>>> print len(motifs)
11
>>> print zscores
[0.37033485499317503, 0.2502032768488251, 0.11813557960717248, -0.64062741073137097, -0.6280946022901569, -0.36863102213820809, 0.54809736376108453, 1.89, 0.87, -0.48, -0.19]
[0.014875553792545083, 0.016154998074953769, 0.002455801898331304, -1.9579019397305546, 0.83542298414538518, 0.84715258999068244, -0.93385230436820643, -0.11, -0.1, -0.31, -0.14]
"""
s_ms, counts = motifs(g, k, p, motif_list)
......
......@@ -109,7 +109,7 @@ def assortativity(g, deg):
... lambda i,k: 1.0 / (1 + abs(i - k)), directed=False,
... mix_time=100)
>>> gt.assortativity(g, "out")
(0.15024063611634234, 0.0051996387349654925)
(0.14145218664992676, 0.005077209994557802)
References
----------
......@@ -179,13 +179,13 @@ def scalar_assortativity(g, deg):
>>> g = gt.random_graph(1000, lambda: sample_k(40), lambda i,k: abs(i-k),
... directed=False, mix_time=100)
>>> gt.scalar_assortativity(g, "out")
(-0.4569707008340512, 0.010227503673605132)
(-0.46972665544654923, 0.010035656615797507)
>>> g = gt.random_graph(1000, lambda: sample_k(40),
... lambda i, k: 1.0 / (1 + abs(i - k)),
... directed=False, mix_time=100)
>>> gt.scalar_assortativity(g, "out")
(0.5921961942149022, 0.011625836226217939)
(0.6120658464996896, 0.011388445161055338)
References
----------
......
......@@ -181,7 +181,7 @@ def random_graph(N, deg_sampler, deg_corr=None, directed=True,
... lambda i, k: 1.0 / (1 + abs(i - k)), directed=False,
... mix_time=100)
>>> gt.scalar_assortativity(g, "out")
(0.6279771609121966, 0.010942827982112517)
(0.6435658697163692, 0.010420519538259333)
The following samples an in,out-degree pair from the joint distribution:
......
......@@ -84,12 +84,12 @@ def adjacency(g, sparse=True, weight=None):
>>> m = gt.adjacency(g)
>>> print m.todense()
[[ 0. 0. 0. ..., 0. 0. 0.]
[ 1. 0. 1. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
...,
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 1. 0. 0.]
[ 0. 0. 1. ..., 0. 0. 0.]]
...,
[ 0. 0. 0. ..., 0. 0. 1.]
[ 0. 0. 1. ..., 0. 0. 0.]
[ 0. 1. 0. ..., 0. 0. 0.]]
References
----------
......@@ -192,12 +192,12 @@ def laplacian(g, deg="total", normalized=True, sparse=True, weight=None):
>>> m = gt.laplacian(g)
>>> print m.todense()
[[ 1. 0. 0. ..., 0. 0. 0. ]
[ 0.05 1. 0.05 ..., 0. 0. 0. ]
[ 0. 1. 0. ..., 0. 0. 0. ]
[ 0. 0. 1. ..., 0. 0. 0. ]
...,
[ 0. 0. 0. ..., 1. 0. 0. ]
[ 0. 0. 0. ..., 0.05 1. 0. ]
[ 0. 0. 0.05 ..., 0. 0. 1. ]]
[ 0. 0. 0. ..., 1. 0. 0.05]
[ 0. 0. 0.05 ..., 0. 1. 0. ]
[ 0. 0.05 0. ..., 0. 0. 1. ]]
References
----------
......
......@@ -371,12 +371,10 @@ def distance_histogram(g, weight=None, bins=[0, 1], samples=None,
>>> g = gt.random_graph(100, lambda: (3, 3))
>>> hist = gt.distance_histogram(g)
>>> print hist
[array([ 0.00000000e+00, 3.00000000e+02, 8.44000000e+02,
2.12300000e+03, 3.76100000e+03, 2.62500000e+03,
2.46000000e+02, 1.00000000e+00]), array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=uint64)]
[array([ 0., 300., 861., 2165., 3801., 2576., 197.]), array([0, 1, 2, 3, 4, 5, 6, 7], dtype=uint64)]
>>> hist = gt.distance_histogram(g, samples=10)
>>> print hist
[array([ 0., 30., 83., 213., 377., 257., 30.]), array([0, 1, 2, 3, 4, 5, 6, 7], dtype=uint64)]
[array([ 0., 30., 87., 221., 395., 234., 23.]), array([0, 1, 2, 3, 4, 5, 6, 7], dtype=uint64)]
"""
if samples != None:
......
......@@ -173,7 +173,7 @@ def subgraph_isomorphism(sub, g, max_n=0, random=True):
>>> sub = gt.random_graph(10, lambda: (poisson(1.8), poisson(1.9)))
>>> vm, em = gt.subgraph_isomorphism(sub, g)
>>> print len(vm)
118
79
>>> for i in xrange(len(vm)):
... g.set_vertex_filter(None)
... g.set_edge_filter(None)
......@@ -386,10 +386,9 @@ def dominator_tree(g, root, dom_map=None):
>>> root = [v for v in g.vertices() if v.in_degree() == 0]
>>> dom = gt.dominator_tree(g, root[0])
>>> print dom.a
[ 0 0 33 81 0 6 0 0 0 0 0 33 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 67 0 0 5 0 0 81 0 0 0 0 87 0 0 0 0 0 0 0 0 0 0 0
0 80 0 0 0 0 0 99 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0
0 0 0 0 0 0 5 11 0 0 0 0 88 30 0 0 0 0 0 0 0 0 0 0 84]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
References
----------
......@@ -431,8 +430,8 @@ def topological_sort(g):
>>> g.set_edge_filter(tree)
>>> sort = gt.topological_sort(g)
>>> print sort
[ 0 1 2 15 3 4 25 5 6 7 28 14 29 26 12 24 8 9 10 11 13 16 17 18 19
27 20 21 22 23]
[ 3 20 9 29 15 0 10 23 1 2 21 7 4 12 11 5 26 27 6 8 13 14 22 16 17
28 18 19 24 25]
References
----------
......@@ -521,11 +520,11 @@ def label_components(g, vprop=None, directed=None):
>>> g = gt.random_graph(100, lambda: (1, 1))
>>> comp, hist = gt.label_components(g)
>>> print comp.a
[0 1 0 1 1 1 2 1 1 1 0 1 0 1 0 0 1 0 1 0 3 0 1 0 1 0 4 3 0 0 0 0 1 1 0 4 1
1 1 1 3 1 0 2 1 1 0 1 0 0 0 1 1 0 0 1 3 1 1 1 1 1 0 1 2 1 1 1 1 0 1 0 0 1
0 3 1 1 1 1 2 3 1 3 0 1 0 0 1 0 0 0 1 1 0 3 1 4 1 1]
[0 0 0 1 0 2 0 0 0 0 2 0 0 0 2 1 0 2 0 1 2 0 1 0 0 1 0 2 0 2 1 0 2 0 0 0 0
0 0 1 0 0 2 2 2 0 0 0 0 0 0 2 0 0 1 1 0 0 2 0 1 0 0 0 2 0 0 2 2 1 2 1 0 0
2 0 0 1 2 1 2 2 0 0 0 0 0 2 0 0 0 1 1 0 0 0 1 1 2 2]
>>> print hist
[35 50 4 8 3]
[58 18 24]
"""
if vprop is None:
......@@ -573,12 +572,12 @@ def label_largest_component(g, directed=None):
>>> g = gt.random_graph(100, lambda: poisson(1), directed=False)
>>> l = gt.label_largest_component(g)
>>> print l.a
[0 0 0 1 0 1 0 0 1 1 0 1 1 0 0 0 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 0 1 0 1 0 0
1 1 1 1 0 1 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 0 1 1
0 1 0 0 1 0 1 1 1 1 1 0 0 1 1 0 1 0 1 0 1 0 0 1 0 0]
[1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 1
1 1 0 0 0 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 0]
>>> u = gt.GraphView(g, vfilt=l) # extract the largest component as a graph
>>> print u.num_vertices()
50
31
"""
label = g.new_vertex_property("bool")
......@@ -648,16 +647,15 @@ def label_biconnected_components(g, eprop=None, vprop=None):
>>> g = gt.random_graph(100, lambda: 2, directed=False)
>>> comp, art, hist = gt.label_biconnected_components(g)
>>> print comp.a
[0 1 1 0 0 0 0 0 0 2 2 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0
1 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0]
>>> print art.a
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
>>> print hist
[83 13 4]
[87 13]
"""
if vprop is None:
......@@ -737,42 +735,42 @@ def shortest_distance(g, source=None, weights=None, max_dist=None,
>>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
>>> dist = gt.shortest_distance(g, source=g.vertex(0))
>>> print dist.a
[ 0 3 5 4 2147483647 4
5 2 4 5 4 4
4 3 2 4 1 5
4 4 5 3 2147483647 6
3 4 4 2147483647 6 6
2 5 4 1 4 4
4 4 6 3 2147483647 5
5 5 4 3 6 1
3 3 4 3 6 5
4 3 4 5 5 5
3 4 5 3 3 2147483647
4 2 2 4 4 2147483647
5 4 3 3 5 5
4 3 5 3 5 2147483647
6 5 5 2 3 4
7 5 5 3 2 4
4 1 4 3]
[ 0 3 6 4 2147483647 3
4 3 4 2 3 4
3 4 2 4 2 5
4 4 2147483647 4 2147483647 6
4 7 5 2147483647 3 4
2 3 5 5 4 5
1 5 6 1 2147483647 8
4 2 1 5 5 6
7 4 5 3 4 4
5 3 3 5 4 5
4 3 5 4 2 2147483647
6 5 4 5 1 2147483647
5 5 4 2 5 4
6 3 5 3 4 2147483647
4 4 7 4 3 5
5 2 7 3 4 4
4 3 4 4]
>>> dist = gt.shortest_distance(g)
>>> print dist[g.vertex(0)].a
[ 0 3 5 4 2147483647 4
5 2 4 5 4 4
4 3 2 4 1 5
4 4 5 3 2147483647 6
3 4 4 2147483647 6 6
2 5 4 1 4 4
4 4 6 3 2147483647 5
5 5 4 3 6 1
3 3 4 3 6 5
4 3 4 5 5 5
3 4 5 3 3 2147483647
4 2 2 4 4 2147483647
5 4 3 3 5 5
4 3 5 3 5 2147483647
6 5 5 2 3 4
7 5 5 3 2 4
4 1 4 3]
[ 0 3 6 4 2147483647 3
4 3 4 2 3 4
3 4 2 4 2 5
4 4 2147483647 4 2147483647 6
4 7 5 2147483647 3 4
2 3 5 5 4 5
1 5 6 1 2147483647 8
4 2 1 5 5 6
7 4 5 3 4 4
5 3 3 5 4 5
4 3 5 4 2 2147483647
6 5 4 5 1 2147483647
5 5 4 2 5 4
6 3 5 3 4 2147483647
4 4 7 4 3 5
5 2 7 3 4 4
4 3 4 4]
References
----------
......@@ -876,9 +874,9 @@ def shortest_path(g, source, target, weights=None, pred_map=None):
>>> g = gt.random_graph(300, lambda: (poisson(3), poisson(3)))
>>> vlist, elist = gt.shortest_path(g, g.vertex(10), g.vertex(11))
>>> print [str(v) for v in vlist]
['10', '243', '171', '60', '94', '116', '266', '11']
['10', '222', '246', '0', '50', '257', '12', '242', '11']
>>> print [str(e) for e in elist]
['(10,243)', '(243,171)', '(171,60)', '(60,94)', '(94,116)', '(116,266)', '(266,11)']
['(10,222)', '(222,246)', '(246,0)', '(0,50)', '(50,257)', '(257,12)', '(12,242)', '(242,11)']
References
----------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment