graph_eigentrust.hh 3.78 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-2020 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
//
5
6
7
8
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
Tiago Peixoto's avatar
Tiago Peixoto committed
9
//
10
11
12
13
// 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 Lesser General Public License for more
// details.
Tiago Peixoto's avatar
Tiago Peixoto committed
14
//
15
// You should have received a copy of the GNU Lesser General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_TRUST_HH
#define GRAPH_TRUST_HH

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

namespace graph_tool
{
using namespace std;

struct get_eigentrust
{
Tiago Peixoto's avatar
Tiago Peixoto committed
31
    typedef void result_type;
Tiago Peixoto's avatar
Tiago Peixoto committed
32
33
    template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap,
              class InferredTrustMap>
34
    void operator()(Graph& g, VertexIndex vertex_index,
Tiago Peixoto's avatar
Tiago Peixoto committed
35
                    EdgeIndex edge_index, TrustMap c, InferredTrustMap t,
36
                    double epslon, size_t max_iter, size_t& iter) const
Tiago Peixoto's avatar
Tiago Peixoto committed
37
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
38
        using namespace boost;
Tiago Peixoto's avatar
Tiago Peixoto committed
39
40
41
42
43
44
45
        typedef typename property_traits<TrustMap>::value_type c_type;
        typedef typename property_traits<InferredTrustMap>::value_type t_type;

        InferredTrustMap t_temp(vertex_index, num_vertices(g));

        // Norm c values
        InferredTrustMap c_sum(vertex_index);
46
        if (graph_tool::is_directed(g))
Tiago Peixoto's avatar
Tiago Peixoto committed
47
48
        {
            TrustMap c_temp(edge_index, c.get_storage().size());
49
50
51
52
53
54
55
56
57
58
59
60
61
            parallel_vertex_loop
                (g,
                 [&](auto v)
                 {
                     c_type sum = 0;
                     for (const auto& e : out_edges_range(v, g))
                         sum += get(c, e);
                     if (sum > 0)
                     {
                         for (const auto& e : out_edges_range(v, g))
                             put(c_temp, e, get(c, e) / sum);
                     }
                 });
Tiago Peixoto's avatar
Tiago Peixoto committed
62
63
64
65
66
            c = c_temp;
        }
        else
        {
            c_sum.reserve(num_vertices(g));
67
68
69
70
71
72
73
74
            parallel_vertex_loop
                (g,
                 [&](auto v)
                 {
                     c_sum[v] = 0;
                     for (const auto& e : out_edges_range(v, g))
                         c_sum[v] += c[e];
                 });
Tiago Peixoto's avatar
Tiago Peixoto committed
75
76
77
        }

        // init inferred trust t
78
79
        auto V = HardNumVertices()(g);
        parallel_vertex_loop(g, [&](auto v) { t[v] = 1.0/V; });
Tiago Peixoto's avatar
Tiago Peixoto committed
80

81
82
        t_type delta = epslon + 1;
        iter = 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
83
84
85
        while (delta >= epslon)
        {
            delta = 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
86
87
88
89
90
91
92
93
94
            #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
                reduction(+:delta)
            parallel_vertex_loop_no_spawn
                (g,
                 [&](auto v)
                 {
                     t_temp[v] = 0;
                     for (const auto& e : in_or_out_edges_range(v, g))
                     {
95
                         auto s = source(e, g);
96
                         if (!graph_tool::is_directed(g))
Tiago Peixoto's avatar
Tiago Peixoto committed
97
98
99
100
101
102
                             t_temp[v] += get(c, e) * t[s] / abs(c_sum[s]);
                         else
                             t_temp[v] += get(c, e) * t[s];
                     }
                     delta += abs(t_temp[v] - t[v]);
                 });
Tiago Peixoto's avatar
Tiago Peixoto committed
103
            swap(t_temp, t);
104

Tiago Peixoto's avatar
Tiago Peixoto committed
105
106
107
108
109
110
            ++iter;
            if (max_iter > 0 && iter== max_iter)
                break;
        }

        if (iter % 2 != 0)
111
            parallel_vertex_loop(g, [&](auto v) {t[v] = t_temp[v];});
Tiago Peixoto's avatar
Tiago Peixoto committed
112
113
114
115
116
117
    }
};

}

#endif