graph_hits.hh 3.85 KB
Newer Older
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>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//
// 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_EIGENVECTOR_HH
#define GRAPH_EIGENVECTOR_HH

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

Tiago Peixoto's avatar
Tiago Peixoto committed
25
#ifndef __clang__
26
27
#include <ext/numeric>
using __gnu_cxx::power;
Tiago Peixoto's avatar
Tiago Peixoto committed
28
29
30
31
32
33
34
#else
template <class Value>
Value power(Value value, int n)
{
    return pow(value, n);
}
#endif
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

namespace graph_tool
{
using namespace std;
using namespace boost;

struct get_hits
{
    template <class Graph, class VertexIndex, class WeightMap,
              class CentralityMap>
    void operator()(Graph& g, VertexIndex vertex_index, WeightMap w,
                    CentralityMap x, CentralityMap y, double epsilon,
                    size_t max_iter, long double& eig) const
    {
        typedef typename property_traits<CentralityMap>::value_type t_type;

        CentralityMap x_temp(vertex_index, num_vertices(g));
        CentralityMap y_temp(vertex_index, num_vertices(g));

        // init centrality
55
56
57
58
59
60
61
62
        auto V = HardNumVertices()(g);
        parallel_vertex_loop
            (g,
             [&](auto v)
             {
                 x[v] = 1.0 / V;
                 y[v] = 1.0 / V;
             });
63

64
        t_type x_norm = 0, y_norm = 0;
65
66
67
68
69

        t_type delta = epsilon + 1;
        size_t iter = 0;
        while (delta >= epsilon)
        {
70
            x_norm = 0, y_norm=0;
Tiago Peixoto's avatar
Tiago Peixoto committed
71

Tiago Peixoto's avatar
Tiago Peixoto committed
72
73
            #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
                reduction(+:x_norm, y_norm)
Tiago Peixoto's avatar
Tiago Peixoto committed
74
75
76
77
78
79
80
            parallel_vertex_loop_no_spawn
                (g,
                 [&](auto v)
                 {
                     x_temp[v] = 0;
                     for (const auto& ie : in_or_out_edges_range(v, g))
                     {
81
                         auto s = source(ie, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
                         x_temp[v] += get(w, ie) * y[s];
                     }
                     x_norm += power(x_temp[v], 2);

                     y_temp[v] = 0;

                     for (const auto& e : out_edges_range(v, g))
                     {
                         auto s = target(e, g);
                         y_temp[v] += get(w, e) * x[s];
                     }
                     y_norm += power(y_temp[v], 2);
                 });

96
            x_norm = sqrt(x_norm);
97
            y_norm = sqrt(y_norm);
98
99

            delta = 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
100
101
102
103
104
105
106
107
108
109
110
111
            #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
                reduction(+:delta)
            parallel_vertex_loop_no_spawn
                (g,
                 [&](auto v)
                 {
                     x_temp[v] /= x_norm;
                     y_temp[v] /= y_norm;
                     delta += abs(x_temp[v] - x[v]);
                     delta += abs(y_temp[v] - y[v]);
                 });

112
113
114
115
116
117
118
119
120
121
            swap(x_temp, x);
            swap(y_temp, y);

            ++iter;
            if (max_iter > 0 && iter== max_iter)
                break;
        }

        if (iter % 2 != 0)
        {
122
123
124
125
126
127
128
            parallel_vertex_loop
                (g,
                 [&](auto v)
                 {
                     x[v] = x_temp[v];
                     y[v] = y_temp[v];
                 });
129
130
131
132
133
134
135
136
137
        }

        eig = x_norm;
    }
};

}

#endif