graph_rewiring.cc 19.2 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
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2007  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 3
// 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/functional/hash.hpp>

#include "graph.hh"
#include "graph_filtering.hh"
#include "shared_map.hh"

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

typedef boost::mt19937 rng_t;

struct get_in_degree
{
    template <class Graph>
    size_t operator()(typename graph_traits<Graph>::vertex_descriptor v, const Graph& g)
    {
        return get_degree(v, g, typename is_convertible<typename graph_traits<Graph>::directed_category,directed_tag>::type());
    }

    template <class Graph>
    size_t get_degree(typename graph_traits<Graph>::vertex_descriptor v, const Graph& g, true_type)
    {
        return in_degree(v,g);
    }

    template <class Graph>
    size_t get_degree(typename graph_traits<Graph>::vertex_descriptor v, const Graph& g, false_type)
    {
        return out_degree(v,g);
    }
};

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
struct source_in
{
    template <class Graph>
    typename graph_traits<Graph>::vertex_descriptor operator()(typename graph_traits<Graph>::edge_descriptor e, const Graph& g)
    {
        return get_source(e, g, typename is_convertible<typename graph_traits<Graph>::directed_category,directed_tag>::type());
    }

    template <class Graph>
    typename graph_traits<Graph>::vertex_descriptor get_source(typename graph_traits<Graph>::edge_descriptor e, const Graph& g, true_type)
    {
        return source(e, g);
    }

    template <class Graph>
    typename graph_traits<Graph>::vertex_descriptor get_source(typename graph_traits<Graph>::edge_descriptor e, const Graph& g, false_type)
    {
        return target(e, g);
    }
};

struct target_in
{
    template <class Graph>
    typename graph_traits<Graph>::vertex_descriptor operator()(typename graph_traits<Graph>::edge_descriptor e, const Graph& g)
    {
        return get_target(e, g, typename is_convertible<typename graph_traits<Graph>::directed_category,directed_tag>::type());
    }

    template <class Graph>
    typename graph_traits<Graph>::vertex_descriptor get_target(typename graph_traits<Graph>::edge_descriptor e, const Graph& g, true_type)
    {
        return target(e, g);
    }

    template <class Graph>
    typename graph_traits<Graph>::vertex_descriptor get_target(typename graph_traits<Graph>::edge_descriptor e, const Graph& g, false_type)
    {
        return source(e, g);
    }
};

Tiago Peixoto's avatar
 
Tiago Peixoto committed
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
template <class Graph>
struct get_in_edges
{
    typedef typename mpl::if_<typename is_convertible<typename graph_traits<Graph>::directed_category,directed_tag>::type,
                              typename graph_traits<Graph>::in_edge_iterator,
                              typename graph_traits<Graph>::out_edge_iterator>::type iterator;

    pair<iterator,iterator> operator()(typename graph_traits<Graph>::vertex_descriptor v, const Graph& g)
    {
        return get_edges(v, g, typename is_convertible<typename graph_traits<Graph>::directed_category,directed_tag>::type());
    }

    pair<iterator,iterator> get_edges(typename graph_traits<Graph>::vertex_descriptor v, const Graph& g, true_type)
    {
        return in_edges(v,g);
    }

    pair<iterator,iterator> get_edges(typename graph_traits<Graph>::vertex_descriptor v, const Graph& g, false_type)
    {
        return out_edges(v,g);
    }
};

template <class Graph>
bool is_adjacent(typename graph_traits<Graph>::vertex_descriptor u, typename graph_traits<Graph>::vertex_descriptor v, Graph& g )
{
    typename graph_traits<Graph>::out_edge_iterator e, e_end;
    for (tie(e, e_end) = out_edges(u, g); e != e_end; ++e)
    {
        if (target(*e,g) == v)
            return true;
    }
    return false;
}

135 136 137 138 139 140 141 142 143 144 145 146 147
template <class Graph, class EdgeIsNew>
bool is_adjacent_in_new(typename graph_traits<Graph>::vertex_descriptor u, typename graph_traits<Graph>::vertex_descriptor v, Graph& g , EdgeIsNew& edge_is_new,
                        const typename graph_traits<Graph>::edge_descriptor& e1, const typename graph_traits<Graph>::edge_descriptor& e2)
{
    typename graph_traits<Graph>::out_edge_iterator ei, ei_end;
    for (tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei)
    {
        if ( (target(*ei, g) == v) && edge_is_new[*ei] && (*ei != e1) && (*ei != e2) )
            return true;
    }
    return false;
}

148 149 150 151 152 153 154 155 156 157 158 159 160
template <class EdgeIterator>
typename iterator_traits<EdgeIterator>::value_type sample_edge(const EdgeIterator& e_begin, const EdgeIterator& e_end, rng_t& rng)
{
    vector<typename iterator_traits<EdgeIterator>::value_type> candidates;
    EdgeIterator e;
    for (e = e_begin; e != e_end; ++e)
        candidates.push_back(*e);

    uniform_int<size_t> edge_sample(0, candidates.size() - 1);
    size_t edge = edge_sample(rng);
    return candidates[edge];
}

161 162 163
template <class Graph, class EdgeIndexMap, class EdgesType>
void swap_edge_triad( typename graph_traits<Graph>::edge_descriptor& e, typename graph_traits<Graph>::edge_descriptor& se,
                      typename graph_traits<Graph>::edge_descriptor& te, EdgesType& edges, EdgeIndexMap edge_index, Graph& g )
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
            typename graph_traits<Graph>::edge_descriptor ne, nse, nte;
//cout << "antes : e " << source(e,g) << ',' << target(e,g) << " se " << source(se,g) << ',' << target(se,g) << " te " << source_in()(te,g) << ',' << target_in()(te,g) << endl;
//cout << "edge index:" << " e " << edge_index[e] << " se " << edge_index[se] << " te " << edge_index[te] << endl;
            // split cases where different combinations of the three edges are the same
            if( se != te )
            {
                ne = add_edge(source(se, g), target_in()(te, g), g).first;
                if(e != se)
                {
                    nse = add_edge(source(e, g), target(se, g), g).first;
                    edge_index[nse] = edge_index[se];
                    remove_edge(se, g);
                    edges[edge_index[nse]] = nse;
se = nse;
                }
else { se = ne; }
                if(e != te)
                {
                    nte = add_edge(source_in()(te, g), target(e, g), g).first;
                    edge_index[nte] = edge_index[te];
                    remove_edge(te, g);
                    edges[edge_index[nte]] = nte;
te = nte;
                }
else { te = ne; }
                edge_index[ne] = edge_index[e];
                remove_edge(e, g);
                edges[edge_index[ne]] = ne;
e = ne;
            }
            else
            {
                if(e != se)
                {
                    ne = se; nse = e;
                    tie(edge_index[ne], edge_index[nse]) = make_pair(edge_index[e], edge_index[se]);
                    edges[edge_index[ne]] = ne; edges[edge_index[nse]] = nse;
e = ne; se = nse; te = nse;
                }
            }
//cout << "depois : e " << source(e,g) << ',' << target(e,g) << " se " << source(se,g) << ',' << target(se,g) << " te " << source(te,g) << ',' << target(te,g) << endl;
//cout << "edge index:" << " e " << edge_index[e] << " se " << edge_index[se] << " te " << edge_index[te] << endl;
207 208 209
}


210
template <template <class Graph, class EdgeIndexMap> class RewireStrategy>
Tiago Peixoto's avatar
 
Tiago Peixoto committed
211 212 213
struct graph_rewire
{
    template <class Graph, class EdgeIndexMap>
214
    void operator()(const Graph& og, EdgeIndexMap edge_index, size_t seed, bool self_loops, bool parallel_edges) const
Tiago Peixoto's avatar
 
Tiago Peixoto committed
215
    {
216 217
        Graph& g = const_cast<Graph&>(og);

Tiago Peixoto's avatar
 
Tiago Peixoto committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231
        rng_t rng(static_cast<rng_t::result_type>(seed));

        if (!self_loops)
        {
            // check the existence of self-loops
            bool has_self_loops = false;
            int i, N = num_vertices(g);
            #pragma omp parallel for default(shared) private(i) schedule(dynamic)
            for (i = 0; i < N; ++i)
            {
                typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
                if (v == graph_traits<Graph>::null_vertex())
                    continue;
                if (is_adjacent(v, v, g))
232
                    has_self_loops = true;
Tiago Peixoto's avatar
 
Tiago Peixoto committed
233 234 235 236 237 238 239
            }
            if (has_self_loops)
                throw GraphException("Self-loop detected. Can't rewire graph without self-loops if it already contains self-loops!");
        }

        if (!parallel_edges)
        {
240
            // check the existence of parallel edges
Tiago Peixoto's avatar
 
Tiago Peixoto committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
            bool has_parallel_edges = false;
            int i, N = num_vertices(g);
            #pragma omp parallel for default(shared) private(i) schedule(dynamic)
            for (i = 0; i < N; ++i)
            {
                typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
                if (v == graph_traits<Graph>::null_vertex())
                    continue;

                tr1::unordered_set<typename graph_traits<Graph>::vertex_descriptor> targets;
                typename graph_traits<Graph>::out_edge_iterator e, e_end;
                for (tie(e, e_end) = out_edges(v, g); e != e_end; ++e)
                {
                    if (targets.find(target(*e, g)) != targets.end())
                        has_parallel_edges = true;
                    else
                        targets.insert(target(*e, g));
                }
            }

            if (has_parallel_edges)
                throw GraphException("Parallel edge detected. Can't rewire graph without parallel edges if it already contains parallel edges!");
        }

265
        RewireStrategy<Graph, EdgeIndexMap> rewire(g, rng);
266

Tiago Peixoto's avatar
 
Tiago Peixoto committed
267 268 269 270 271 272 273 274 275 276 277 278 279
        vector<typename graph_traits<Graph>::edge_descriptor> edges(num_edges(g));
        int i, N = num_vertices(g);
        #pragma omp parallel for default(shared) private(i) schedule(dynamic)
        for (i = 0; i < N; ++i)
        {
            typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
            if (v == graph_traits<Graph>::null_vertex())
                continue;
            typename graph_traits<Graph>::out_edge_iterator e, e_end;
            for (tie(e, e_end) = out_edges(v, g); e != e_end; ++e)
                edges[edge_index[*e]] = *e;
        }

280 281
        // for each edge simultaneously rewire its source and target
        for (i = 0; i < int(edges.size()); ++i)
Tiago Peixoto's avatar
 
Tiago Peixoto committed
282
        {
283
            typename graph_traits<Graph>::edge_descriptor e = edges[i];
284 285 286
            typename graph_traits<Graph>::edge_descriptor se, te;
            tie(se, te) = rewire(e, self_loops, parallel_edges);
            swap_edge_triad(e, se, te, edges, edge_index, g);
Tiago Peixoto's avatar
 
Tiago Peixoto committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        }
    }
};

template <class Graph>
class RandomRewireStrategy
{
public:
    RandomRewireStrategy (const Graph& g, rng_t& rng): _g(g), _rng(rng)
    {
        int i, N = num_vertices(_g);
        for (i = 0; i < N; ++i)
        {
            typename graph_traits<Graph>::vertex_descriptor v = vertex(i, _g);
            if (v == graph_traits<Graph>::null_vertex())
                continue;
            size_t k = get_in_degree()(v, _g);
            if (k == 0)
                continue;
306 307 308 309
            if (!_vertices.empty())
                _vertices[_vertices.rbegin()->first + k] = v;
            else
                _vertices[k] = v;
Tiago Peixoto's avatar
 
Tiago Peixoto committed
310 311
        }
    }
312 313 314

    pair<typename graph_traits<Graph>::vertex_descriptor, typename graph_traits<Graph>::vertex_descriptor>
    operator()(const typename graph_traits<Graph>::edge_descriptor& e, bool self_loops)
Tiago Peixoto's avatar
 
Tiago Peixoto committed
315 316
    {
        uniform_int<size_t> vertex_sample(0, _vertices.rbegin()->first-1);
317 318
        size_t v1,v2;
        typename graph_traits<Graph>::vertex_descriptor s,t;
Tiago Peixoto's avatar
 
Tiago Peixoto committed
319 320
        do
        {
321 322 323 324
            v1 = vertex_sample(_rng);
            v2 = vertex_sample(_rng);
            s = _vertices.upper_bound(v1)->second;
            t = _vertices.upper_bound(v2)->second;
Tiago Peixoto's avatar
 
Tiago Peixoto committed
325
        }
326 327
        while (!self_loops && (s == t));
        return make_pair(s, t);
Tiago Peixoto's avatar
 
Tiago Peixoto committed
328 329 330 331 332 333 334 335 336
    }

private:
    const Graph& _g;
    rng_t& _rng;
    typedef map<size_t, typename graph_traits<Graph>::vertex_descriptor> vertices_t;
    vertices_t _vertices;
};

337
template <class Graph, class EdgeIndexMap>
Tiago Peixoto's avatar
 
Tiago Peixoto committed
338 339 340
class CorrelatedRewireStrategy
{
public:
341
    CorrelatedRewireStrategy (const Graph& g, rng_t& rng): _g(g), _rng(rng)
Tiago Peixoto's avatar
 
Tiago Peixoto committed
342 343 344
    {
        int i, N = num_vertices(_g);
        for (i = 0; i < N; ++i)
345
        {
Tiago Peixoto's avatar
 
Tiago Peixoto committed
346 347 348 349 350 351 352
            typename graph_traits<Graph>::vertex_descriptor v = vertex(i, _g);
            if (v == graph_traits<Graph>::null_vertex())
                continue;

            size_t j = get_in_degree()(v, _g);
            size_t k = out_degree(v, _g);

353
            if (j > 0)
354
                _deg_target_vertices[make_pair(j,k)].push_back(v);
355 356
            
            if (k > 0)
357
                _deg_source_vertices[make_pair(j,k)].push_back(v);
Tiago Peixoto's avatar
 
Tiago Peixoto committed
358 359 360
        }
    }

361 362
    pair<typename graph_traits<Graph>::edge_descriptor, typename graph_traits<Graph>::edge_descriptor>
    operator()(const typename graph_traits<Graph>::edge_descriptor& e, bool self_loops, bool parallel_edges)
Tiago Peixoto's avatar
 
Tiago Peixoto committed
363
    {
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        typename deg_vertices_t::value_type::second_type& source_vertices = _deg_source_vertices[make_pair(get_in_degree()(source(e, _g), _g),
                                                                                                 out_degree(source(e, _g), _g))];
        typename deg_vertices_t::value_type::second_type& target_vertices = _deg_target_vertices[make_pair(get_in_degree()(target(e, _g), _g),
                                                                                                 out_degree(target(e, _g), _g))];

        //shuffle the sources
        random_number_generator<rng_t, size_t> generator(_rng);
        random_shuffle(source_vertices.begin(), source_vertices.end(), generator);
        random_shuffle(target_vertices.begin(), target_vertices.end(), generator);

        //try new targets until one passes all the consistency checks
        typename graph_traits<Graph>::edge_descriptor se, te;
        typename deg_vertices_t::value_type::second_type::iterator vs, vs_begin = source_vertices.begin(), vs_end = source_vertices.end();
        typename deg_vertices_t::value_type::second_type::iterator vt, vt_begin = target_vertices.begin(), vt_end = target_vertices.end();
        bool pass=false;
        for(vs = vs_begin ; vs != vs_end; ++vs)
Tiago Peixoto's avatar
 
Tiago Peixoto committed
380
        {
381
            for(vt = vt_begin; vt != vt_end; ++vt)
Tiago Peixoto's avatar
 
Tiago Peixoto committed
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
                if( (!self_loops && (*vs == *vt)) || (!parallel_edges && is_adjacent_in_new(*vs, *vt, _g, _edge_is_new, e, e)) )
                    continue;

                // set up the shuffled edges
                vector<typename graph_traits<Graph>::edge_descriptor> in_edges_vt, out_edges_vs;
                typename get_in_edges<Graph>::iterator ie, ie_end;
                typename graph_traits<Graph>::out_edge_iterator oe, oe_end;
                for( tie(ie, ie_end) = get_in_edges<Graph>()(*vt, _g); ie != ie_end ; ++ie)
                    in_edges_vt.push_back(*ie);
                for( tie(oe, oe_end) = out_edges(*vs, _g); oe != oe_end ; ++oe)
                    out_edges_vs.push_back(*oe);
                random_shuffle(in_edges_vt.begin(), in_edges_vt.end(), generator);
                random_shuffle(out_edges_vs.begin(), out_edges_vs.end(), generator);

                // for combinations of in_vt and out_vs...
                typename vector<typename graph_traits<Graph>::edge_descriptor>::iterator out_vs_i, out_vs_i_begin=out_edges_vs.begin(), out_vs_i_end=out_edges_vs.end();
                typename vector<typename graph_traits<Graph>::edge_descriptor>::iterator in_vt_i, in_vt_i_begin=in_edges_vt.begin(), in_vt_i_end=in_edges_vt.end();
                for(out_vs_i = out_vs_i_begin; out_vs_i != out_vs_i_end; ++out_vs_i)
                {
                    for(in_vt_i = in_vt_i_begin; in_vt_i != in_vt_i_end; ++in_vt_i)
                    {
                        se = *out_vs_i;
                        te = *in_vt_i;
                            if( ( !self_loops && (source_in()(*in_vt_i, _g) == target(e, _g))) ||
                                ( !parallel_edges &&
                                  is_adjacent_in_new(source_in()(*in_vt_i, _g), target(e, _g), _g, _edge_is_new, *out_vs_i, *in_vt_i) ) )
                                continue;
                            if( ( !self_loops && (source(e, _g) == target(*out_vs_i, _g))) ||
                                ( !parallel_edges &&
                                  is_adjacent_in_new(source(e, _g), target(*out_vs_i, _g), _g, _edge_is_new, *out_vs_i, *in_vt_i) ) )
                                break;
                            if( !parallel_edges &&
                                _edge_is_new[*in_vt_i] && ( (source_in()(*in_vt_i, _g)==*vs) && (target(e, _g)==*vt) ) )
                                continue;
                            if( !parallel_edges &&
                                _edge_is_new[*out_vs_i] && ( (source(e, _g)==*vs) && (target(*out_vs_i, _g)==*vt) ) )
                                break;
                            if( !parallel_edges && (_edge_is_new[*in_vt_i] && _edge_is_new[*out_vs_i]) &&
                                (source(e, _g)==source_in()(*in_vt_i, _g)) && (target(e, _g)==target(*out_vs_i, _g)) &&
                                (*in_vt_i != *out_vs_i) )
                                continue;
                        pass = true;
                        break;
                    }
                    if(pass) break;
                }
                if(pass) break;
Tiago Peixoto's avatar
 
Tiago Peixoto committed
430
            }
431
            if(pass) break;
Tiago Peixoto's avatar
 
Tiago Peixoto committed
432
        }
433 434 435 436 437
//cout << "pass " << pass << endl;
        _edge_is_new[e]=true;
        if(pass==false)
            throw GraphException("Bad things happen when you're not one with your inner self.");
        return make_pair(se, te);
Tiago Peixoto's avatar
 
Tiago Peixoto committed
438 439 440 441 442
    }

private:
    const Graph& _g;
    rng_t& _rng;
443
    typedef tr1::unordered_map<pair<size_t, size_t>, vector<typename graph_traits<Graph>::vertex_descriptor>, hash<pair<size_t, size_t> > > deg_vertices_t;
444 445
    deg_vertices_t _deg_source_vertices;
    deg_vertices_t _deg_target_vertices;
446
    vector_property_map<bool, EdgeIndexMap> _edge_is_new;
Tiago Peixoto's avatar
 
Tiago Peixoto committed
447 448 449 450 451
};

//==============================================================================
// RandomRewire
//==============================================================================
452
void GraphInterface::RandomRewire(rewire_strat_t strat, bool self_loops, bool parallel_edges, size_t seed)
Tiago Peixoto's avatar
 
Tiago Peixoto committed
453 454 455 456
{
    bool reversed = GetReversed();
    SetReversed(false);

457
/*
Tiago Peixoto's avatar
 
Tiago Peixoto committed
458 459 460 461
    if (strat == UNCORRELATED_STRAT)
        check_filter(*this, bind<void>(graph_rewire<RandomRewireStrategy>(), _1, _edge_index, seed, self_loops, parallel_edges),
                     never_reversed(), directed_check());
    else
462
*/
Tiago Peixoto's avatar
 
Tiago Peixoto committed
463 464 465 466 467
        check_filter(*this, bind<void>(graph_rewire<CorrelatedRewireStrategy>(), _1, _edge_index, seed, self_loops, parallel_edges),
                     never_reversed(), directed_check());

    SetReversed(reversed);
}