graph_betweenness.cc 7 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-2017 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
25
26
27
28
29
30
31
32
33
34
35
36
//
// 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/>.

#include "graph_filtering.hh"

#include <boost/python.hpp>
#include <boost/graph/betweenness_centrality.hpp>

#include "graph.hh"
#include "graph_selectors.hh"
#include "graph_util.hh"

using namespace std;
using namespace boost;
using namespace graph_tool;

template <class Graph, class EdgeBetweenness, class VertexBetweenness>
void normalize_betweenness(const Graph& g,
                           EdgeBetweenness edge_betweenness,
                           VertexBetweenness vertex_betweenness,
                           size_t n)
{
Tiago Peixoto's avatar
Tiago Peixoto committed
37
38
    double vfactor = (n > 2) ? 1.0 / ((n - 1) * (n - 2)) : 1.0;
    double efactor = (n > 1) ? 1.0 / (n * (n - 1)) : 1.0;
Tiago Peixoto's avatar
Tiago Peixoto committed
39
40
    if (std::is_convertible<typename graph_traits<Graph>::directed_category,
                            undirected_tag>::value)
Tiago Peixoto's avatar
Tiago Peixoto committed
41
42
43
44
45
    {
        vfactor *= 2;
        efactor *= 2;
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
46
47
48
49
50
51
    parallel_vertex_loop
        (g,
         [&](auto v)
         {
             put(vertex_betweenness, v, vfactor * get(vertex_betweenness, v));
         });
Tiago Peixoto's avatar
Tiago Peixoto committed
52

Tiago Peixoto's avatar
Tiago Peixoto committed
53
54
55
56
57
58
    parallel_edge_loop
        (g,
         [&](const auto& e)
         {
             put(edge_betweenness, e, efactor * get(edge_betweenness, e));
         });
Tiago Peixoto's avatar
Tiago Peixoto committed
59
60
61
62
}

struct get_betweenness
{
Tiago Peixoto's avatar
Tiago Peixoto committed
63
    typedef void result_type;
Tiago Peixoto's avatar
Tiago Peixoto committed
64
    template <class Graph, class EdgeBetweenness, class VertexBetweenness>
65
    void operator()(Graph& g,
Tiago Peixoto's avatar
Tiago Peixoto committed
66
67
68
69
70
71
                    GraphInterface::vertex_index_map_t index_map,
                    EdgeBetweenness edge_betweenness,
                    VertexBetweenness vertex_betweenness,
                    bool normalize, size_t n) const
    {
        vector<vector<typename graph_traits<Graph>::edge_descriptor> >
72
73
            incoming_map(num_vertices(g));
        vector<size_t> distance_map(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
74
        vector<typename property_traits<VertexBetweenness>::value_type>
75
76
            dependency_map(num_vertices(g));
        vector<size_t> path_count_map(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
77
        brandes_betweenness_centrality
78
            (g, vertex_betweenness, edge_betweenness,
Tiago Peixoto's avatar
Tiago Peixoto committed
79
80
81
82
83
84
             make_iterator_property_map(incoming_map.begin(), index_map),
             make_iterator_property_map(distance_map.begin(), index_map),
             make_iterator_property_map(dependency_map.begin(), index_map),
             make_iterator_property_map(path_count_map.begin(), index_map),
             index_map);
        if (normalize)
85
            normalize_betweenness(g, edge_betweenness, vertex_betweenness, n);
Tiago Peixoto's avatar
Tiago Peixoto committed
86
87
88
89
90
    }
};

struct get_weighted_betweenness
{
Tiago Peixoto's avatar
Tiago Peixoto committed
91
    typedef void result_type;
Tiago Peixoto's avatar
Tiago Peixoto committed
92
93
    template <class Graph, class EdgeBetweenness, class VertexBetweenness,
              class VertexIndexMap>
94
        void operator()(Graph& g, VertexIndexMap vertex_index,
Tiago Peixoto's avatar
Tiago Peixoto committed
95
96
97
                        EdgeBetweenness edge_betweenness,
                        VertexBetweenness vertex_betweenness,
                        boost::any weight_map, bool normalize,
98
                        size_t n, size_t max_eindex) const
Tiago Peixoto's avatar
Tiago Peixoto committed
99
100
    {
        vector<vector<typename graph_traits<Graph>::edge_descriptor> >
101
            incoming_map(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
102
        vector<typename property_traits<EdgeBetweenness>::value_type>
103
            distance_map(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
104
        vector<typename property_traits<VertexBetweenness>::value_type>
105
106
            dependency_map(num_vertices(g));
        vector<size_t> path_count_map(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
107

108
109
110
        typename EdgeBetweenness::checked_t weight =
            any_cast<typename EdgeBetweenness::checked_t>(weight_map);

Tiago Peixoto's avatar
Tiago Peixoto committed
111
        brandes_betweenness_centrality
112
            (g, vertex_betweenness, edge_betweenness,
Tiago Peixoto's avatar
Tiago Peixoto committed
113
114
115
116
             make_iterator_property_map(incoming_map.begin(), vertex_index),
             make_iterator_property_map(distance_map.begin(), vertex_index),
             make_iterator_property_map(dependency_map.begin(), vertex_index),
             make_iterator_property_map(path_count_map.begin(), vertex_index),
117
             vertex_index, weight.get_unchecked(max_eindex+1));
Tiago Peixoto's avatar
Tiago Peixoto committed
118
        if (normalize)
119
            normalize_betweenness(g, edge_betweenness, vertex_betweenness, n);
Tiago Peixoto's avatar
Tiago Peixoto committed
120
121
122
123
124
125
126
127
128
    }
};

void betweenness(GraphInterface& g, boost::any weight,
                 boost::any edge_betweenness,
                 boost::any vertex_betweenness,
                 bool normalize)
{
    if (!belongs<edge_floating_properties>()(edge_betweenness))
Tiago Peixoto's avatar
Tiago Peixoto committed
129
        throw ValueException("edge property must be of floating point value"
130
                             " type");
Tiago Peixoto's avatar
Tiago Peixoto committed
131
132

    if (!belongs<vertex_floating_properties>()(vertex_betweenness))
Tiago Peixoto's avatar
Tiago Peixoto committed
133
        throw ValueException("vertex property must be of floating point value"
134
                             " type");
Tiago Peixoto's avatar
Tiago Peixoto committed
135
136
137
138

    if (!weight.empty())
    {
        run_action<>()
Tiago Peixoto's avatar
Tiago Peixoto committed
139
            (g, std::bind<>(get_weighted_betweenness(),
140
                            std::placeholders::_1, g.get_vertex_index(),
Tiago Peixoto's avatar
Tiago Peixoto committed
141
142
                            std::placeholders::_2,
                            std::placeholders::_3, weight, normalize,
143
                            g.get_num_vertices(), g.get_edge_index_range()),
Tiago Peixoto's avatar
Tiago Peixoto committed
144
145
146
147
148
149
150
             edge_floating_properties(),
             vertex_floating_properties())
            (edge_betweenness, vertex_betweenness);
    }
    else
    {
        run_action<>()
Tiago Peixoto's avatar
Tiago Peixoto committed
151
            (g, std::bind<void>(get_betweenness(), std::placeholders::_1,
152
                                g.get_vertex_index(), std::placeholders::_2,
Tiago Peixoto's avatar
Tiago Peixoto committed
153
                                std::placeholders::_3, normalize,
154
                                g.get_num_vertices()),
Tiago Peixoto's avatar
Tiago Peixoto committed
155
156
157
158
159
160
161
162
163
             edge_floating_properties(),
             vertex_floating_properties())
            (edge_betweenness, vertex_betweenness);
    }
}

struct get_central_point_dominance
{
    template <class Graph, class VertexBetweenness>
164
    void operator()(Graph& g, VertexBetweenness vertex_betweenness, double& c)
Tiago Peixoto's avatar
Tiago Peixoto committed
165
166
        const
    {
167
        c = double(central_point_dominance(g, vertex_betweenness));
Tiago Peixoto's avatar
Tiago Peixoto committed
168
169
170
171
172
173
174
175
    }
};

double central_point(GraphInterface& g,
                     boost::any vertex_betweenness)
{
    double c = 0.0;
    run_action<graph_tool::detail::never_reversed>()
Tiago Peixoto's avatar
Tiago Peixoto committed
176
177
        (g, std::bind<>(get_central_point_dominance(), std::placeholders::_1,
                        std::placeholders::_2, std::ref(c)),
Tiago Peixoto's avatar
Tiago Peixoto committed
178
179
180
181
182
183
184
185
186
187
         vertex_scalar_properties()) (vertex_betweenness);
    return c;
}

void export_betweenness()
{
    using namespace boost::python;
    def("get_betweenness", &betweenness);
    def("get_central_point_dominance", &central_point);
}