graph_price.hh 3.58 KB
Newer Older
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2007-2012 Tiago de Paula Peixoto <tiago@skewed.de>
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
//
// 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, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_PRICE_HH
#define GRAPH_PRICE_HH

#include <iostream>
#include <boost/functional/hash.hpp>
#include "graph_util.hh"

#if (GCC_VERSION >= 40400)
#   include <tr1/random>
#   include <tr1/unordered_set>
#else
#   include <boost/tr1/random.hpp>
30
#   include <boost/tr1/unordered_set.hpp>
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
#endif

#include <map>
#include <iostream>

namespace graph_tool
{
using namespace std;
using namespace boost;

typedef tr1::mt19937 rng_t;

struct get_price
{
    template <class Graph>
    void operator()(Graph& g, size_t N, double gamma, double c, size_t m,
                    size_t seed) const
    {
        rng_t rng(static_cast<rng_t::result_type>(seed));

        typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
                                  in_degreeS, out_degreeS>::type DegSelector;

        map<double, typename graph_traits<Graph>::vertex_descriptor> probs;

56 57
        size_t n_possible = 0;
        double cp = 0, p = 0;
58 59 60
        typename graph_traits<Graph>::vertex_iterator vi, vi_end;
        for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
        {
61 62 63 64 65 66 67
            p = pow(DegSelector()(*vi, g) + c, gamma);
            cp += p;
            if (p > 0)
            {
                probs.insert(make_pair(cp, *vi));
                ++n_possible;
            }
68 69
        }

70
        if (probs.empty() || probs.rbegin()->first <= 0)
71 72
            throw GraphException("Cannot connect edges: probabilities are <= 0!");

73 74
        tr1::unordered_set<typename graph_traits<Graph>::vertex_descriptor>
            visited;
75 76
        for (size_t i = 0; i < N; ++i)
        {
77
            visited.clear();
78
            typename graph_traits<Graph>::vertex_descriptor v = add_vertex(g);
79
            for (size_t j = 0; j < min(m, n_possible); ++j)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
            {
                tr1::variate_generator<rng_t&, tr1::uniform_real<> >
                    sample(rng, tr1::uniform_real<>(0, probs.rbegin()->first));
                double r = sample();
                typeof(probs.begin()) iter = probs.lower_bound(r);
                typename graph_traits<Graph>::vertex_descriptor w =
                    iter->second;

                if (visited.find(w) != visited.end())
                {
                    --j;
                    continue;
                }
                visited.insert(w);
                add_edge(v, w, g);

                p = abs(pow(DegSelector()(w, g) + c, gamma)
                        - pow(DegSelector()(w, g) + c - 1, gamma));
98 99
                if (p > 0)
                    probs.insert(make_pair(probs.rbegin()->first + p, w));
100 101
            }
            p = pow(DegSelector()(v, g) + c, gamma);
102 103 104 105 106
            if (p > 0)
            {
                probs.insert(make_pair(probs.rbegin()->first + p, v));
                n_possible += 1;
            }
107 108 109 110 111 112 113
        }
    }
};

} // namespace graph_tool

#endif // GRAPH_PRICE_HH