graph_katz.hh 3.73 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-2015 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
//
// 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__
Tiago Peixoto's avatar
Tiago Peixoto committed
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
Tiago Peixoto's avatar
Tiago Peixoto committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

namespace graph_tool
{
using namespace std;
using namespace boost;

struct get_katz
{
    template <class Graph, class VertexIndex, class WeightMap,
              class CentralityMap, class PersonalizationMap>
    void operator()(Graph& g, VertexIndex vertex_index, WeightMap w,
                    CentralityMap c, PersonalizationMap beta, long double alpha,
                    long double epsilon, size_t max_iter) const
    {
        typedef typename property_traits<CentralityMap>::value_type t_type;

        CentralityMap c_temp(vertex_index, num_vertices(g));

        t_type delta = epsilon + 1;
        size_t iter = 0;
        int i, N = num_vertices(g);
        while (delta >= epsilon)
        {
            #pragma omp parallel for default(shared) private(i) \
59
                schedule(runtime) if (N > 100)
Tiago Peixoto's avatar
Tiago Peixoto committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
            for (i = 0; i < N; ++i)
            {
                typename graph_traits<Graph>::vertex_descriptor v =
                    vertex(i, g);
                if (v == graph_traits<Graph>::null_vertex())
                    continue;

                c_temp[v] = get(beta, v);
                typename in_or_out_edge_iteratorS<Graph>::type e, e_end;
                for (tie(e, e_end) = in_or_out_edge_iteratorS<Graph>::get_edges(v, g);
                     e != e_end; ++e)
                {
                    typename graph_traits<Graph>::vertex_descriptor s;
                    if (is_directed::apply<Graph>::type::value)
74
                        s = source(*e, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
75
                    else
76
                        s = target(*e, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
77 78 79 80 81 82
                    c_temp[v] += alpha * get(w, *e) * c[s];
                }
            }

            delta = 0;
            #pragma omp parallel for default(shared) private(i) \
83
                schedule(runtime) if (N > 100) reduction(+:delta)
Tiago Peixoto's avatar
Tiago Peixoto committed
84 85 86 87 88 89 90 91 92 93 94
            for (i = 0; i < N; ++i)
            {
                typename graph_traits<Graph>::vertex_descriptor v =
                    vertex(i, g);
                if (v == graph_traits<Graph>::null_vertex())
                    continue;
                delta += abs(c_temp[v] - c[v]);
            }
            swap(c_temp, c);

            ++iter;
95
            if (max_iter > 0 && iter == max_iter)
Tiago Peixoto's avatar
Tiago Peixoto committed
96 97 98 99 100 101
                break;
        }

        if (iter % 2 != 0)
        {
            #pragma omp parallel for default(shared) private(i)     \
102
                schedule(runtime) if (N > 100)
Tiago Peixoto's avatar
Tiago Peixoto committed
103 104 105 106 107 108
            for (i = 0; i < N; ++i)
            {
                typename graph_traits<Graph>::vertex_descriptor v =
                    vertex(i, g);
                if (v == graph_traits<Graph>::null_vertex())
                    continue;
109
                c_temp[v] = c[v];
Tiago Peixoto's avatar
Tiago Peixoto committed
110 111 112 113 114 115 116 117
            }
        }
    }
};

}

#endif