graph_betweenness.cc 6.79 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
82

        brandes_betweenness_centrality
83
            (g, vertex_betweenness, edge_betweenness,
Tiago Peixoto's avatar
Tiago Peixoto committed
84
85
86
87
88
89
             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)
90
            normalize_betweenness(g, edge_betweenness, vertex_betweenness, n);
Tiago Peixoto's avatar
Tiago Peixoto committed
91
92
93
94
95
96
97
    }
};

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

        brandes_betweenness_centrality
113
            (g, vertex_betweenness, edge_betweenness,
Tiago Peixoto's avatar
Tiago Peixoto committed
114
115
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),
             vertex_index, any_cast<EdgeBetweenness>(weight_map));
        if (normalize)
120
            normalize_betweenness(g, edge_betweenness, vertex_betweenness, n);
Tiago Peixoto's avatar
Tiago Peixoto committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    }
};

void betweenness(GraphInterface& g, boost::any weight,
                 boost::any edge_betweenness,
                 boost::any vertex_betweenness,
                 bool normalize)
{
    if (!belongs<edge_floating_properties>()(edge_betweenness))
        throw GraphException("edge property must be of floating point value type");

    if (!belongs<vertex_floating_properties>()(vertex_betweenness))
        throw GraphException("vertex property must be of floating point value type");

    if (!weight.empty())
    {
        run_action<>()
            (g, lambda::bind<void>
             (get_weighted_betweenness(), lambda::_1, g.GetVertexIndex(),
              lambda::_2, lambda::_3, weight, normalize,
              g.GetNumberOfVertices()),
             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>
161
    void operator()(Graph& g, VertexBetweenness vertex_betweenness, double& c)
Tiago Peixoto's avatar
Tiago Peixoto committed
162
163
        const
    {
164
        c = central_point_dominance(g, vertex_betweenness);
Tiago Peixoto's avatar
Tiago Peixoto committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    }
};

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);
}