graph_absolute_trust.hh 12 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2007  Tiago de Paula Peixoto <tiago@forked.de>
//
// 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_ABSOLUTE_TRUST_HH
#define GRAPH_ABSOLUTE_TRUST_HH

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

Tiago Peixoto's avatar
Tiago Peixoto committed
25
#include <tr1/unordered_set>
26
27
28
29
#include <tr1/tuple>
#include <algorithm>

#include "minmax.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
30

31
32
#include <iostream>

Tiago Peixoto's avatar
Tiago Peixoto committed
33
34
35
36
namespace graph_tool
{
using namespace std;
using namespace boost;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using std::tr1::get;
using std::tr1::tuple;

template <class Path>
struct path_cmp
{
    path_cmp(vector<Path>& paths): _paths(paths) {}
    vector<Path>& _paths;

    typedef size_t first_argument_type;
    typedef size_t second_argument_type;
    typedef bool result_type;
    inline bool operator()(size_t a, size_t b)
    {
51
        if (get<0>(_paths[a]).second == get<0>(_paths[b]).second)
52
            return get<1>(_paths[a]).size() > get<1>(_paths[b]).size();
53
        return get<0>(_paths[a]).second < get<0>(_paths[b]).second;
54
55
    }
};
Tiago Peixoto's avatar
Tiago Peixoto committed
56

57
struct get_absolute_trust
Tiago Peixoto's avatar
Tiago Peixoto committed
58
{
59
    template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap,
Tiago Peixoto's avatar
Tiago Peixoto committed
60
              class InferredTrustMap>
61
    void operator()(Graph& g, VertexIndex vertex_index, EdgeIndex edge_index,
62
63
                    size_t max_edge_index, int64_t source, TrustMap c,
                    InferredTrustMap t, size_t n_paths, bool reversed) const
Tiago Peixoto's avatar
Tiago Peixoto committed
64
    {
65
        typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
66
        typedef typename graph_traits<Graph>::edge_descriptor edge_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
67
        typedef typename property_traits<TrustMap>::value_type c_type;
68
69
        typedef typename property_traits<InferredTrustMap>::value_type::
            value_type t_type;
Tiago Peixoto's avatar
Tiago Peixoto committed
70

71
72
73
        // the path type: the first value is the (trust,weight) pair, the second
        // the set of vertices in the path and the third is the list of edges,
        // in the path sequence.
74
        typedef tuple<pair<t_type, t_type>, tr1::unordered_set<vertex_t>,
75
                      vector<edge_t> > path_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
76

77
        int i, N = (source == -1) ? num_vertices(g) : source + 1;
78
        #pragma omp parallel for default(shared) private(i) schedule(dynamic)
79
        for (i= (source == -1) ? 0 : source; i < N; ++i)
Tiago Peixoto's avatar
Tiago Peixoto committed
80
        {
81
            vertex_t v = vertex(i, g);
82
            t[v].resize(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
83

84
85
86
87
88
89
90
            // path priority queue
            vector<path_t> paths(1);
            typedef double_priority_queue<size_t, path_cmp<path_t> > queue_t;
            queue_t queue = queue_t(path_cmp<path_t>(paths));
            get<0>(paths.back()).first = get<0>(paths.back()).second = 1;
            get<1>(paths.back()).insert(v);
            queue.push(0);
91

92
93
            // this is the actual queue of paths which will be used to compute
            // the trust values
94
95
            queue_t final_queue = queue_t(path_cmp<path_t>(paths));

96
97
98
99
            // store all paths which reach a given vertex
            unchecked_vector_property_map<tr1::unordered_set<size_t>,
                                          VertexIndex>
                path_map(vertex_index, num_vertices(g));
100
101

            while (!queue.empty())
Tiago Peixoto's avatar
Tiago Peixoto committed
102
            {
103
104
                size_t pi = queue.top();
                queue.pop_top();
105
106
107
108
109
110
111
                vertex_t w;

                // push queue top into final queue
                if (get<2>(paths[pi]).size() > 0)
                {
                    w = target(get<2>(paths[pi]).back(), g);
                    final_queue.push(pi);
112
113
114

                    // augment path map
                    path_map[target(get<2>(paths[pi]).back(),g)].insert(pi);
115
116
117
118
119
120
121
122
123
124
125
126
                }
                else
                {
                    w = v; // the first path
                }

                // if maximum size is reached, remove the bottom
                if ((n_paths > 0) && (final_queue.size() > n_paths))
                {
                    size_t bi = final_queue.bottom();
                    final_queue.pop_bottom();

127
128
129
130
                    // remove path from path map
                    path_map[boost::target(get<2>(paths[bi]).back(),g)].
                        erase(bi);

131
132
133
134
135
                    if (bi == pi)
                        continue;
                }

                // augment paths and put them in the queue
136
137
                typename graph_traits<Graph>::out_edge_iterator e, e_end;
                for (tie(e, e_end) = out_edges(w, g); e != e_end; ++e)
Tiago Peixoto's avatar
Tiago Peixoto committed
138
                {
139
140
141
                    vertex_t a = target(*e, g);
                    // no loops
                    if (get<1>(paths[pi]).find(a) == get<1>(paths[pi]).end())
Tiago Peixoto's avatar
Tiago Peixoto committed
142
                    {
143
144
145
146
147
148
149
                        // only follow non-zero paths
                        if (c[*e] > 0 || (reversed &&
                                          get<1>(paths[pi]).size() == 1))
                        {
                            size_t npi;
                            paths.push_back(paths[pi]); // clone last path
                            npi = paths.size()-1;
150

151
                            path_t& np = paths[npi]; // new path
152

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
                            if (!reversed)
                            {
                                // path weight
                                get<0>(np).second = get<0>(np).first;

                                // path value
                                get<0>(np).first *= c[*e];
                            }
                            else
                            {
                                if (get<1>(np).size() > 1)
                                    get<0>(np).second *= c[*e];
                                get<0>(np).first *= c[*e];
                            }
                            get<1>(np).insert(a);
                            get<2>(np).push_back(*e);
Tiago Peixoto's avatar
Tiago Peixoto committed
169

170
171
172
173
174
175
176
177
178
                            // keep following paths only if there is a chance
                            // they will make it into the final queue
                            if ((n_paths > 0 && final_queue.size() < n_paths) ||
                                (final_queue.size() == 0 ||
                                 (get<0>(np).second >=
                                  get<0>(paths[final_queue.bottom()]).second)))
                                queue.push(npi);
                            else
                                paths.pop_back();
179
                        }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
                    }
                }
            }

            unchecked_vector_property_map<t_type, VertexIndex>
                weight_sum(vertex_index, num_vertices(g));

            // paths which were already calculated and can be skipped
            tr1::unordered_set<size_t> skip;

            // calculate trust from paths in the final queue
            while (!final_queue.empty())
            {
                size_t pi = final_queue.top();
                final_queue.pop_top();
                path_t& p = paths[pi];

                if (skip.find(pi) != skip.end())
                    continue;

                tr1::unordered_set<size_t>& apaths =
                    path_map[target(get<2>(p).back(), g)]; // all paths with the
                                                           // same final target

                tr1::unordered_set<size_t> vlist; // all vertices involved

                // compute cumulative edge weight
                unchecked_vector_property_map
                    <tr1::unordered_map<vertex_t, t_type>, EdgeIndex>
                    cum_weight(edge_index, max_edge_index+1);

                for (typeof(apaths.begin()) iter = apaths.begin();
                     iter != apaths.end(); ++iter)
                {
                    size_t path_size = get<2>(paths[*iter]).size();
                    vertex_t ptarget = target(get<2>(paths[*iter]).back(),g);
                    t_type w = 1;
                    for (size_t j = 0; j < path_size; ++j)
                    {
                        edge_t e = get<2>(paths[*iter])[path_size - 1 - j];
                        if (!reversed && j < path_size - 1)
                            w *= c[e];
                        if (reversed && j > 0)
                            w *= c[e];
                        cum_weight[e][ptarget] += w;
                        vlist.insert(target(e,g));
                    }
                    vlist.insert(boost::source(get<2>(paths[*iter]).front(),g));
                }

                // compute out-weight
                unchecked_vector_property_map
                    <tr1::unordered_map<vertex_t, t_type>, VertexIndex>
                    out_weight(vertex_index, num_vertices(g));
                for (typeof(vlist.begin()) viter = vlist.begin();
                     viter != vlist.end(); ++viter)
                {
                    vertex_t u = *viter;
                    if (!reversed)
                    {
                        typename graph_traits<Graph>::out_edge_iterator e,e_end;
                        for (tie(e, e_end) = out_edges(u, g); e != e_end; ++e)
                            for (typeof(cum_weight[*e].begin()) witer =
                                     cum_weight[*e].begin();
                                 witer != cum_weight[*e].end(); ++witer)
                                out_weight[u][witer->first] += witer->second;
                    }
                    else
                    {
                        // if reversed, use "in-trust" instead
                        typename in_edge_iteratorS<Graph>::type e, e_end;
                        for (tie(e, e_end) =
                                 in_edge_iteratorS<Graph>::get_edges(v, g);
                             e != e_end; ++e)
                            for (typeof(cum_weight[*e].begin()) witer =
                                     cum_weight[*e].begin();
                                 witer != cum_weight[*e].end(); ++witer)
                                out_weight[u][witer->first] += witer->second;
                    }
                }

                for (typeof(apaths.begin()) iter = apaths.begin();
                     iter != apaths.end(); ++iter)
                {
                    size_t pi = *iter;
                    path_t& p = paths[pi];
                    vertex_t ptarget = target(get<2>(paths[*iter]).back(),g);

                    // calculate the trust value and weight of the path
                    t_type w = 1, val = 1;
                    for (size_t i = 0; i < get<2>(p).size(); ++i)
                    {
                        edge_t e = get<2>(p)[i];
                        vertex_t u = (!reversed) ?
                            boost::source(e,g) : target(e,g);
                        if (out_weight[u][ptarget] > 0)
276
                        {
277
278
279
280
                            if ((!reversed && i < get<2>(p).size()-1) ||
                                (reversed && i > 0))
                                w *= c[e]*cum_weight[e][ptarget]/
                                    out_weight[u][ptarget];
281
                        }
282
                        val *= c[e];
283
                    }
284
285
286
287
288
                    vertex_t u = target(get<2>(p).back(), g);
                    weight_sum[u] += w;
                    t[v][u] += w*val;

                    skip.insert(pi);
289
290
291
                }
            }

292
293
294
295
            int j, N = num_vertices(g);
            #pragma omp parallel for default(shared) private(j) \
                schedule(dynamic)
            for (j = 0; j < N; ++j)
296
            {
297
                vertex_t w = vertex(j, g);
298
299
300
                if (w == graph_traits<Graph>::null_vertex())
                    continue;
                if (weight_sum[w] > 0)
301
                    t[v][w] /= weight_sum[w];
302
            }
Tiago Peixoto's avatar
Tiago Peixoto committed
303
304
        }
    }
305

Tiago Peixoto's avatar
Tiago Peixoto committed
306
307
308
309
310
};

}

#endif