graph_clustering.hh 4.9 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
//
// 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.
//
15
// you should have received a copy of the GNU General Public License
16
17
18
19
20
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_CLUSTERING_HH
#define GRAPH_CLUSTERING_HH

21
22
#include "config.h"

23
#include "hash_map_wrap.hh"
24
25
#include <boost/mpl/if.hpp>

26
#ifdef _OPENMP
27
28
29
#include "omp.h"
#endif

Tiago Peixoto's avatar
Tiago Peixoto committed
30
#ifndef __clang__
31
32
#include <ext/numeric>
using __gnu_cxx::power;
Tiago Peixoto's avatar
Tiago Peixoto committed
33
34
35
36
37
38
39
#else
template <class Value>
Value power(Value value, int n)
{
    return pow(value, n);
}
#endif
40

41
42
43
namespace graph_tool
{
using namespace boost;
44
using namespace std;
45
46

// calculates the number of triangles to which v belongs
47
48
49
template <class Graph, class EWeight, class VProp>
auto get_triangles(typename graph_traits<Graph>::vertex_descriptor v,
                   EWeight& eweight, VProp& mark, const Graph& g)
50
{
51
52
    typedef typename property_traits<EWeight>::value_type val_t;
    val_t triangles = 0, w1 = 0, w2 = 0, k = 0;
53

54
    for (auto e : out_edges_range(v, g))
55
    {
56
        auto n = target(e, g);
57
        if (n == v)
58
            continue;
59
60
61
        auto w = eweight[e];
        mark[n] = w;
        k += w;
62
    }
63

64
    for (auto e : out_edges_range(v, g))
65
    {
66
        auto n = target(e, g);
67
68
        if (n == v)
            continue;
69
70
        w1 = eweight[e];
        for (auto e2 : out_edges_range(n, g))
71
        {
72
            auto n2 = target(e2, g);
73
            if (n2 == n)
74
                continue;
75
76
            w2 = eweight[e2];
            triangles += mark[n2] * w1 * w2;
77
78
        }
    }
79

80
    for (auto n : adjacent_vertices_range(v, g))
81
        mark[n] = 0;
82

83
    if (graph_tool::is_directed(g))
84
        return make_pair(val_t(triangles), val_t((k * (k - 1))));
85
    else
86
        return make_pair(val_t(triangles / 2), val_t((k * (k - 1)) / 2));
87
88
89
90
91
92
}


// retrieves the global clustering coefficient
struct get_global_clustering
{
93
94
    template <class Graph, class EWeight>
    void operator()(const Graph& g, EWeight eweight, double& c, double& c_err) const
95
    {
96
97
98
        typedef typename property_traits<EWeight>::value_type val_t;
        val_t triangles = 0, n = 0;
        vector<val_t> mask(num_vertices(g), 0);
99

Tiago Peixoto's avatar
Tiago Peixoto committed
100
101
102
103
104
105
        #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
            firstprivate(mask) reduction(+:triangles, n)
        parallel_vertex_loop_no_spawn
                (g,
                 [&](auto v)
                 {
106
                     auto temp = get_triangles(v, eweight, mask, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
107
108
109
                     triangles += temp.first;
                     n += temp.second;
                 });
110
        c = double(triangles) / n;
111
112
113

        // "jackknife" variance
        c_err = 0.0;
114
        double cerr = 0.0;
Tiago Peixoto's avatar
Tiago Peixoto committed
115
116
117
118
119
120
        #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
            firstprivate(mask) reduction(+:cerr)
        parallel_vertex_loop_no_spawn
                (g,
                 [&](auto v)
                 {
121
                     auto temp = get_triangles(v, eweight, mask, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
122
123
124
125
                     double cl = double(triangles - temp.first) /
                         (n - temp.second);
                     cerr += power(c - cl, 2);
                 });
126
        c_err = sqrt(cerr);
127
128
129
130
131
132
    }
};

// sets the local clustering coefficient to a property
struct set_clustering_to_property
{
133
134
    template <class Graph, class EWeight, class ClustMap>
    void operator()(const Graph& g, EWeight eweight, ClustMap clust_map) const
135
    {
136
137
        typedef typename property_traits<EWeight>::value_type val_t;
        vector<val_t> mask(num_vertices(g), false);
138

Tiago Peixoto's avatar
Tiago Peixoto committed
139
140
141
142
143
144
        #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
            firstprivate(mask)
        parallel_vertex_loop_no_spawn
            (g,
             [&](auto v)
             {
145
                 auto triangles = get_triangles(v, eweight, mask, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
146
147
148
                 double clustering = (triangles.second > 0) ?
                     double(triangles.first)/triangles.second :
                     0.0;
149
                 clust_map[v] = clustering;
Tiago Peixoto's avatar
Tiago Peixoto committed
150
             });
151
    }
152

153
154
155
156
    template <class Graph>
    struct get_undirected_graph
    {
        typedef typename mpl::if_
Tiago Peixoto's avatar
Tiago Peixoto committed
157
158
           <std::is_convertible<typename graph_traits<Graph>::directed_category,
                                directed_tag>,
159
            const undirected_adaptor<Graph>,
Tiago Peixoto's avatar
Tiago Peixoto committed
160
            const Graph& >::type type;
161
162
163
    };
};

164
} //graph-tool namespace
165
166

#endif // GRAPH_CLUSTERING_HH