graph_betweenness.cc 7.01 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
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
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2007  Tiago de Paula Peixoto <tiago@forked.de>
//
// 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/lambda/bind.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 boost::lambda;
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)
{
    double vfactor = (n > 2) ? 1.0/((n-1)*(n-2)) : 1.0;
    double efactor = (n > 1) ? 1.0/(n*(n-1)) : 1.0;
    if (is_convertible<typename graph_traits<Graph>::directed_category,
                       undirected_tag>::value)
    {
        vfactor *= 2;
        efactor *= 2;
    }

    int i, N = num_vertices(g);
    #pragma omp parallel for default(shared) private(i)   \
        schedule(dynamic)
    for (i = 0; i < N; ++i)
    {
        typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
        if (v == graph_traits<Graph>::null_vertex())
            continue;
        put(vertex_betweenness, v, vfactor * get(vertex_betweenness, v));
    }

    typename graph_traits<Graph>::edge_iterator e, e_end;
    for (tie(e, e_end) = edges(g); e != e_end; ++e)
    {
        put(edge_betweenness, *e, efactor * get(edge_betweenness, *e));
    }
}

struct get_betweenness
{
    template <class Graph, class EdgeBetweenness, class VertexBetweenness>
69
    void operator()(Graph& g,
Tiago Peixoto's avatar
Tiago Peixoto committed
70
71
72
73
74
75
                    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> >
76
77
            incoming_map(num_vertices(g));
        vector<size_t> distance_map(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
78
        vector<typename property_traits<VertexBetweenness>::value_type>
79
80
            dependency_map(num_vertices(g));
        vector<size_t> path_count_map(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
81
        brandes_betweenness_centrality
82
            (g, vertex_betweenness, edge_betweenness,
Tiago Peixoto's avatar
Tiago Peixoto committed
83
84
85
86
87
88
             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)
89
            normalize_betweenness(g, edge_betweenness, vertex_betweenness, n);
Tiago Peixoto's avatar
Tiago Peixoto committed
90
91
92
93
94
95
96
    }
};

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

111
112
113
        typename EdgeBetweenness::checked_t weight =
            any_cast<typename EdgeBetweenness::checked_t>(weight_map);

Tiago Peixoto's avatar
Tiago Peixoto committed
114
        brandes_betweenness_centrality
115
            (g, vertex_betweenness, edge_betweenness,
Tiago Peixoto's avatar
Tiago Peixoto committed
116
117
118
119
             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),
120
             vertex_index, weight.get_unchecked(max_eindex+1));
Tiago Peixoto's avatar
Tiago Peixoto committed
121
        if (normalize)
122
            normalize_betweenness(g, edge_betweenness, vertex_betweenness, n);
Tiago Peixoto's avatar
Tiago Peixoto committed
123
124
125
126
127
128
129
130
131
    }
};

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

    if (!belongs<vertex_floating_properties>()(vertex_betweenness))
136
137
        throw GraphException("vertex property must be of floating point value"
                             " type");
Tiago Peixoto's avatar
Tiago Peixoto committed
138
139
140
141
142
143
144

    if (!weight.empty())
    {
        run_action<>()
            (g, lambda::bind<void>
             (get_weighted_betweenness(), lambda::_1, g.GetVertexIndex(),
              lambda::_2, lambda::_3, weight, normalize,
145
              g.GetNumberOfVertices(), g.GetMaxEdgeIndex()),
Tiago Peixoto's avatar
Tiago Peixoto committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
             edge_floating_properties(),
             vertex_floating_properties())
            (edge_betweenness, vertex_betweenness);
    }
    else
    {
        run_action<>()
            (g, lambda::bind<void>
             (get_betweenness(), lambda::_1, g.GetVertexIndex(), lambda::_2,
              lambda::_3, normalize, g.GetNumberOfVertices()),
             edge_floating_properties(),
             vertex_floating_properties())
            (edge_betweenness, vertex_betweenness);
    }
}

struct get_central_point_dominance
{
    template <class Graph, class VertexBetweenness>
165
    void operator()(Graph& g, VertexBetweenness vertex_betweenness, double& c)
Tiago Peixoto's avatar
Tiago Peixoto committed
166
167
        const
    {
168
        c = central_point_dominance(g, vertex_betweenness);
Tiago Peixoto's avatar
Tiago Peixoto committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    }
};

double central_point(GraphInterface& g,
                     boost::any vertex_betweenness)
{
    double c = 0.0;
    run_action<graph_tool::detail::never_reversed>()
        (g, lambda::bind<void>(get_central_point_dominance(), lambda::_1,
                               lambda::_2, var(c)),
         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);
}