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

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

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

Tiago Peixoto's avatar
Tiago Peixoto committed
112
        brandes_betweenness_centrality
113
            (g, vertex_betweenness, edge_betweenness,
Tiago Peixoto's avatar
Tiago Peixoto committed
114 115 116 117
             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),
118
             vertex_index, weight.get_unchecked(max_eindex+1));
Tiago Peixoto's avatar
Tiago Peixoto committed
119
        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
    }
};

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
130
        throw ValueException("edge property must be of floating point value"
131
                             " type");
Tiago Peixoto's avatar
Tiago Peixoto committed
132 133

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

    if (!weight.empty())
    {
        run_action<>()
140 141 142
            (g, bind<void>
             (get_weighted_betweenness(), _1, g.GetVertexIndex(),
              _2, _3, weight, normalize,
143
              g.GetNumberOfVertices(), g.GetMaxEdgeIndex()),
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<>()
151 152
            (g, bind<void>(get_betweenness(), _1, g.GetVertexIndex(), _2,
                           _3, normalize, g.GetNumberOfVertices()),
Tiago Peixoto's avatar
Tiago Peixoto committed
153 154 155 156 157 158 159 160 161
             edge_floating_properties(),
             vertex_floating_properties())
            (edge_betweenness, vertex_betweenness);
    }
}

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

double central_point(GraphInterface& g,
                     boost::any vertex_betweenness)
{
    double c = 0.0;
    run_action<graph_tool::detail::never_reversed>()
174 175
        (g, bind<void>(get_central_point_dominance(), _1,
                       _2, ref(c)),
Tiago Peixoto's avatar
Tiago Peixoto committed
176 177 178 179 180 181 182 183 184 185
         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);
}