graph_pagerank.hh 3.19 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-2019 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
21
22
23
24
25
26
27
28
29
30
31
//
// 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_PAGERANK_HH
#define GRAPH_PAGERANK_HH

#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_util.hh"

namespace graph_tool
{
using namespace std;
using namespace boost;

struct get_pagerank
{
32
33
    template <class Graph, class VertexIndex, class RankMap, class PerMap,
              class Weight>
34
    void operator()(Graph& g, VertexIndex vertex_index, RankMap rank,
35
36
                    PerMap pers, Weight weight, double damping, double epsilon,
                    size_t max_iter, size_t& iter) const
Tiago Peixoto's avatar
Tiago Peixoto committed
37
38
39
    {
        typedef typename property_traits<RankMap>::value_type rank_type;

40
41
        RankMap r_temp(vertex_index, num_vertices(g));
        RankMap deg(vertex_index, num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
42

43
        // init degs
44
45
46
47
48
49
50
51
        std::vector<size_t> sinks;
        for (auto v : vertices_range(g))
        {
            auto k = out_degreeS()(v, g, weight);
            put(deg, v, k);
            if (k == 0)
                sinks.push_back(v);
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
52

53
        rank_type delta = epsilon + 1;
Tiago Peixoto's avatar
Tiago Peixoto committed
54
55
        rank_type d = damping;
        iter = 0;
56
        while (delta >= epsilon)
Tiago Peixoto's avatar
Tiago Peixoto committed
57
58
        {
            delta = 0;
59
60
61
62
63
64
65
66
67

            double p_sink = 0;
            #pragma omp parallel if (sinks.size() > OPENMP_MIN_THRESH)      \
                reduction(+:p_sink)
            parallel_loop_no_spawn
                (sinks,
                 [&](auto, auto v){ p_sink += get(rank, v); });

            #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH)   \
Tiago Peixoto's avatar
Tiago Peixoto committed
68
69
70
71
72
                reduction(+:delta)
            parallel_vertex_loop_no_spawn
                (g,
                 [&](auto v)
                 {
73
                     rank_type r = p_sink * get(pers, v);
Tiago Peixoto's avatar
Tiago Peixoto committed
74
75
                     for (const auto& e : in_or_out_edges_range(v, g))
                     {
76
                         auto s = source(e, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
77
78
                         r += (get(rank, s) * get(weight, e)) / get(deg, s);
                     }
Tiago Peixoto's avatar
Tiago Peixoto committed
79

Tiago Peixoto's avatar
Tiago Peixoto committed
80
                     put(r_temp, v, (1.0 - d) * get(pers, v) + d * r);
81

Tiago Peixoto's avatar
Tiago Peixoto committed
82
83
                     delta += abs(get(r_temp, v) - get(rank, v));
                 });
Tiago Peixoto's avatar
Tiago Peixoto committed
84
85
86
87
88
89
90
91
            swap(r_temp, rank);
            ++iter;
            if (max_iter > 0 && iter == max_iter)
                break;
        }

        if (iter % 2 != 0)
        {
92
93
94
95
96
97
            parallel_vertex_loop
                (g,
                 [&](auto v)
                 {
                     put(rank, v, get(r_temp, v));
                 });
Tiago Peixoto's avatar
Tiago Peixoto committed
98
99
100
101
102
103
        }
    }
};

}
#endif // GRAPH_PAGERANK_HH