graph_pagerank.cc 2.59 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2017 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
// 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/>.

#include "graph_filtering.hh"

#include <boost/python.hpp>
21
#include <boost/bind.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
22
23
24
25
26
27
28
29
30

#include "graph.hh"
#include "graph_selectors.hh"
#include "graph_pagerank.hh"

using namespace std;
using namespace boost;
using namespace graph_tool;

31
32
size_t pagerank(GraphInterface& g, boost::any rank, boost::any pers,
                boost::any weight, double d, double epsilon, size_t max_iter)
Tiago Peixoto's avatar
Tiago Peixoto committed
33
{
34
35
36
37
38
39
40
    if (!belongs<vertex_floating_properties>()(rank))
        throw ValueException("rank vertex property must have a floating-point value type");

    if (!pers.empty() && !belongs<vertex_scalar_properties>()(pers))
        throw ValueException("personalization vertex property must have a scalar value type");

    typedef ConstantPropertyMap<double, GraphInterface::vertex_t> pers_map_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
41
    typedef boost::mpl::push_back<vertex_scalar_properties, pers_map_t>::type
42
43
44
        pers_props_t;

    if(pers.empty())
45
        pers = pers_map_t(1.0 / g.get_num_vertices());
46

47
    typedef UnityPropertyMap<int,GraphInterface::edge_t> weight_map_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
48
    typedef boost::mpl::push_back<edge_scalar_properties, weight_map_t>::type
49
50
51
52
53
54
        weight_props_t;

    if (!weight.empty() && !belongs<edge_scalar_properties>()(weight))
        throw ValueException("weight edge property must have a scalar value type");

    if(weight.empty())
55
        weight = weight_map_t();
Tiago Peixoto's avatar
Tiago Peixoto committed
56
57
58

    size_t iter;
    run_action<>()
Tiago Peixoto's avatar
Tiago Peixoto committed
59
        (g, std::bind(get_pagerank(),
60
61
                      std::placeholders::_1, g.get_vertex_index(), std::placeholders::_2,
                      std::placeholders::_3, std::placeholders::_4, d,
Tiago Peixoto's avatar
Tiago Peixoto committed
62
                      epsilon, max_iter, std::ref(iter)),
63
64
         vertex_floating_properties(),
         pers_props_t(), weight_props_t())(rank, pers, weight);
Tiago Peixoto's avatar
Tiago Peixoto committed
65
66
67
68
69
70
71
72
73
    return iter;
}


void export_pagerank()
{
    using namespace boost::python;
    def("get_pagerank", &pagerank);
}