graph_average.hh 2.94 KB
Newer Older
1
// Copyright (C) 2006-2013 Tiago de Paula Peixoto <tiago@skewed.de>
2
3
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// 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_AVERAGE_HH
#define GRAPH_AVERAGE_HH

#include <algorithm>
#include <boost/numeric/conversion/bounds.hpp>
#include <boost/numeric/conversion/cast.hpp>

namespace graph_tool
{
using namespace std;
using namespace boost;

class VertexAverageTraverse
{
public:
    template <class Graph, class DegreeSelector, class ValueType>
    void operator()(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
                    DegreeSelector& deg, ValueType& a, ValueType& aa,
                    size_t& count)
    {
        ValueType x = deg(v, g);
        a += x;
        aa += x*x;
        count++;
    }
};

class EdgeAverageTraverse
{
public:
    template <class Graph, class EdgeProperty, class ValueType>
    void operator()(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
                    EdgeProperty& eprop, ValueType& a, ValueType& aa,
                    size_t& count)
    {
        typename graph_traits<Graph>::out_edge_iterator e, e_begin, e_end;
        tie(e_begin,e_end) = out_edges(v,g);
        for(e = e_begin; e != e_end; ++e)
        {
            ValueType x = eprop[*e];
            a += x;
            aa += x*x;
            count++;
        }
    }
};

// generalized functor to obtain average of different types of "degrees"
template <class AverageTraverse>
struct get_average
{
    get_average(long double& a, long double& dev)
        : _a(a), _dev(dev) {}

    template <class Graph, class DegreeSelector>
71
    void operator()(Graph& g, DegreeSelector deg) const
72
73
    {
        typedef typename DegreeSelector::value_type value_type;
Tiago Peixoto's avatar
Tiago Peixoto committed
74
75
        long double a = 0, aa = 0;
        size_t count = 0;
76
77
78
79

        AverageTraverse traverse;
        int i, N = num_vertices(g);
        #pragma omp parallel for default(shared) private(i) \
80
            reduction(+:a,aa,count) schedule(static, 100)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        for (i = 0; i < N; ++i)
        {
            typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
            if (v == graph_traits<Graph>::null_vertex())
                continue;
            traverse(g, v, deg, a, aa, count);
        }

        _a = a/count;
        _dev = sqrt((aa/count - _a*_a))/sqrt(count);
    }

    long double& _a;
    long double& _dev;
};

} // graph_tool namespace

#endif // GRAPH_AVERAGE_HH