graph_pagerank.cc 2.49 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) 2007-2012 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    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;
    typedef mpl::push_back<vertex_scalar_properties, pers_map_t>::type
        pers_props_t;

    if(pers.empty())
        pers = pers_map_t(1.0 / g.GetNumberOfVertices());

    typedef ConstantPropertyMap<double, GraphInterface::edge_t> weight_map_t;
    typedef mpl::push_back<edge_scalar_properties, weight_map_t>::type
        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())
        weight = weight_map_t(1.0);
Tiago Peixoto's avatar
Tiago Peixoto committed
56 57 58

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


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