annotate DEPENDENCIES/generic/include/boost/graph/betweenness_centrality.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 // Copyright 2004 The Trustees of Indiana University.
Chris@16 2
Chris@16 3 // Distributed under the Boost Software License, Version 1.0.
Chris@16 4 // (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 5 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6
Chris@16 7 // Authors: Douglas Gregor
Chris@16 8 // Andrew Lumsdaine
Chris@16 9 #ifndef BOOST_GRAPH_BRANDES_BETWEENNESS_CENTRALITY_HPP
Chris@16 10 #define BOOST_GRAPH_BRANDES_BETWEENNESS_CENTRALITY_HPP
Chris@16 11
Chris@16 12 #include <stack>
Chris@16 13 #include <vector>
Chris@16 14 #include <boost/graph/overloading.hpp>
Chris@16 15 #include <boost/graph/dijkstra_shortest_paths.hpp>
Chris@16 16 #include <boost/graph/breadth_first_search.hpp>
Chris@16 17 #include <boost/graph/relax.hpp>
Chris@16 18 #include <boost/graph/graph_traits.hpp>
Chris@16 19 #include <boost/tuple/tuple.hpp>
Chris@16 20 #include <boost/type_traits/is_convertible.hpp>
Chris@16 21 #include <boost/type_traits/is_same.hpp>
Chris@16 22 #include <boost/mpl/if.hpp>
Chris@16 23 #include <boost/property_map/property_map.hpp>
Chris@16 24 #include <boost/graph/named_function_params.hpp>
Chris@16 25 #include <algorithm>
Chris@16 26
Chris@16 27 namespace boost {
Chris@16 28
Chris@16 29 namespace detail { namespace graph {
Chris@16 30
Chris@16 31 /**
Chris@16 32 * Customized visitor passed to Dijkstra's algorithm by Brandes'
Chris@16 33 * betweenness centrality algorithm. This visitor is responsible for
Chris@16 34 * keeping track of the order in which vertices are discovered, the
Chris@16 35 * predecessors on the shortest path(s) to a vertex, and the number
Chris@16 36 * of shortest paths.
Chris@16 37 */
Chris@16 38 template<typename Graph, typename WeightMap, typename IncomingMap,
Chris@16 39 typename DistanceMap, typename PathCountMap>
Chris@16 40 struct brandes_dijkstra_visitor : public bfs_visitor<>
Chris@16 41 {
Chris@16 42 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
Chris@16 43 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
Chris@16 44
Chris@16 45 brandes_dijkstra_visitor(std::stack<vertex_descriptor>& ordered_vertices,
Chris@16 46 WeightMap weight,
Chris@16 47 IncomingMap incoming,
Chris@16 48 DistanceMap distance,
Chris@16 49 PathCountMap path_count)
Chris@16 50 : ordered_vertices(ordered_vertices), weight(weight),
Chris@16 51 incoming(incoming), distance(distance),
Chris@16 52 path_count(path_count)
Chris@16 53 { }
Chris@16 54
Chris@16 55 /**
Chris@16 56 * Whenever an edge e = (v, w) is relaxed, the incoming edge list
Chris@16 57 * for w is set to {(v, w)} and the shortest path count of w is set to
Chris@16 58 * the number of paths that reach {v}.
Chris@16 59 */
Chris@16 60 void edge_relaxed(edge_descriptor e, const Graph& g)
Chris@16 61 {
Chris@16 62 vertex_descriptor v = source(e, g), w = target(e, g);
Chris@16 63 incoming[w].clear();
Chris@16 64 incoming[w].push_back(e);
Chris@16 65 put(path_count, w, get(path_count, v));
Chris@16 66 }
Chris@16 67
Chris@16 68 /**
Chris@16 69 * If an edge e = (v, w) was not relaxed, it may still be the case
Chris@16 70 * that we've found more equally-short paths, so include {(v, w)} in the
Chris@16 71 * incoming edges of w and add all of the shortest paths to v to the
Chris@16 72 * shortest path count of w.
Chris@16 73 */
Chris@16 74 void edge_not_relaxed(edge_descriptor e, const Graph& g)
Chris@16 75 {
Chris@16 76 typedef typename property_traits<WeightMap>::value_type weight_type;
Chris@16 77 typedef typename property_traits<DistanceMap>::value_type distance_type;
Chris@16 78 vertex_descriptor v = source(e, g), w = target(e, g);
Chris@16 79 distance_type d_v = get(distance, v), d_w = get(distance, w);
Chris@16 80 weight_type w_e = get(weight, e);
Chris@16 81
Chris@16 82 closed_plus<distance_type> combine;
Chris@16 83 if (d_w == combine(d_v, w_e)) {
Chris@16 84 put(path_count, w, get(path_count, w) + get(path_count, v));
Chris@16 85 incoming[w].push_back(e);
Chris@16 86 }
Chris@16 87 }
Chris@16 88
Chris@16 89 /// Keep track of vertices as they are reached
Chris@16 90 void examine_vertex(vertex_descriptor w, const Graph&)
Chris@16 91 {
Chris@16 92 ordered_vertices.push(w);
Chris@16 93 }
Chris@16 94
Chris@16 95 private:
Chris@16 96 std::stack<vertex_descriptor>& ordered_vertices;
Chris@16 97 WeightMap weight;
Chris@16 98 IncomingMap incoming;
Chris@16 99 DistanceMap distance;
Chris@16 100 PathCountMap path_count;
Chris@16 101 };
Chris@16 102
Chris@16 103 /**
Chris@16 104 * Function object that calls Dijkstra's shortest paths algorithm
Chris@16 105 * using the Dijkstra visitor for the Brandes betweenness centrality
Chris@16 106 * algorithm.
Chris@16 107 */
Chris@16 108 template<typename WeightMap>
Chris@16 109 struct brandes_dijkstra_shortest_paths
Chris@16 110 {
Chris@16 111 brandes_dijkstra_shortest_paths(WeightMap weight_map)
Chris@16 112 : weight_map(weight_map) { }
Chris@16 113
Chris@16 114 template<typename Graph, typename IncomingMap, typename DistanceMap,
Chris@16 115 typename PathCountMap, typename VertexIndexMap>
Chris@16 116 void
Chris@16 117 operator()(Graph& g,
Chris@16 118 typename graph_traits<Graph>::vertex_descriptor s,
Chris@16 119 std::stack<typename graph_traits<Graph>::vertex_descriptor>& ov,
Chris@16 120 IncomingMap incoming,
Chris@16 121 DistanceMap distance,
Chris@16 122 PathCountMap path_count,
Chris@16 123 VertexIndexMap vertex_index)
Chris@16 124 {
Chris@16 125 typedef brandes_dijkstra_visitor<Graph, WeightMap, IncomingMap,
Chris@16 126 DistanceMap, PathCountMap> visitor_type;
Chris@16 127 visitor_type visitor(ov, weight_map, incoming, distance, path_count);
Chris@16 128
Chris@16 129 dijkstra_shortest_paths(g, s,
Chris@16 130 boost::weight_map(weight_map)
Chris@16 131 .vertex_index_map(vertex_index)
Chris@16 132 .distance_map(distance)
Chris@16 133 .visitor(visitor));
Chris@16 134 }
Chris@16 135
Chris@16 136 private:
Chris@16 137 WeightMap weight_map;
Chris@16 138 };
Chris@16 139
Chris@16 140 /**
Chris@16 141 * Function object that invokes breadth-first search for the
Chris@16 142 * unweighted form of the Brandes betweenness centrality algorithm.
Chris@16 143 */
Chris@16 144 struct brandes_unweighted_shortest_paths
Chris@16 145 {
Chris@16 146 /**
Chris@16 147 * Customized visitor passed to breadth-first search, which
Chris@16 148 * records predecessor and the number of shortest paths to each
Chris@16 149 * vertex.
Chris@16 150 */
Chris@16 151 template<typename Graph, typename IncomingMap, typename DistanceMap,
Chris@16 152 typename PathCountMap>
Chris@16 153 struct visitor_type : public bfs_visitor<>
Chris@16 154 {
Chris@16 155 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
Chris@16 156 typedef typename graph_traits<Graph>::vertex_descriptor
Chris@16 157 vertex_descriptor;
Chris@16 158
Chris@16 159 visitor_type(IncomingMap incoming, DistanceMap distance,
Chris@16 160 PathCountMap path_count,
Chris@16 161 std::stack<vertex_descriptor>& ordered_vertices)
Chris@16 162 : incoming(incoming), distance(distance),
Chris@16 163 path_count(path_count), ordered_vertices(ordered_vertices) { }
Chris@16 164
Chris@16 165 /// Keep track of vertices as they are reached
Chris@16 166 void examine_vertex(vertex_descriptor v, Graph&)
Chris@16 167 {
Chris@16 168 ordered_vertices.push(v);
Chris@16 169 }
Chris@16 170
Chris@16 171 /**
Chris@16 172 * Whenever an edge e = (v, w) is labelled a tree edge, the
Chris@16 173 * incoming edge list for w is set to {(v, w)} and the shortest
Chris@16 174 * path count of w is set to the number of paths that reach {v}.
Chris@16 175 */
Chris@16 176 void tree_edge(edge_descriptor e, Graph& g)
Chris@16 177 {
Chris@16 178 vertex_descriptor v = source(e, g);
Chris@16 179 vertex_descriptor w = target(e, g);
Chris@16 180 put(distance, w, get(distance, v) + 1);
Chris@16 181
Chris@16 182 put(path_count, w, get(path_count, v));
Chris@16 183 incoming[w].push_back(e);
Chris@16 184 }
Chris@16 185
Chris@16 186 /**
Chris@16 187 * If an edge e = (v, w) is not a tree edge, it may still be the
Chris@16 188 * case that we've found more equally-short paths, so include (v, w)
Chris@16 189 * in the incoming edge list of w and add all of the shortest
Chris@16 190 * paths to v to the shortest path count of w.
Chris@16 191 */
Chris@16 192 void non_tree_edge(edge_descriptor e, Graph& g)
Chris@16 193 {
Chris@16 194 vertex_descriptor v = source(e, g);
Chris@16 195 vertex_descriptor w = target(e, g);
Chris@16 196 if (get(distance, w) == get(distance, v) + 1) {
Chris@16 197 put(path_count, w, get(path_count, w) + get(path_count, v));
Chris@16 198 incoming[w].push_back(e);
Chris@16 199 }
Chris@16 200 }
Chris@16 201
Chris@16 202 private:
Chris@16 203 IncomingMap incoming;
Chris@16 204 DistanceMap distance;
Chris@16 205 PathCountMap path_count;
Chris@16 206 std::stack<vertex_descriptor>& ordered_vertices;
Chris@16 207 };
Chris@16 208
Chris@16 209 template<typename Graph, typename IncomingMap, typename DistanceMap,
Chris@16 210 typename PathCountMap, typename VertexIndexMap>
Chris@16 211 void
Chris@16 212 operator()(Graph& g,
Chris@16 213 typename graph_traits<Graph>::vertex_descriptor s,
Chris@16 214 std::stack<typename graph_traits<Graph>::vertex_descriptor>& ov,
Chris@16 215 IncomingMap incoming,
Chris@16 216 DistanceMap distance,
Chris@16 217 PathCountMap path_count,
Chris@16 218 VertexIndexMap vertex_index)
Chris@16 219 {
Chris@16 220 typedef typename graph_traits<Graph>::vertex_descriptor
Chris@16 221 vertex_descriptor;
Chris@16 222
Chris@16 223 visitor_type<Graph, IncomingMap, DistanceMap, PathCountMap>
Chris@16 224 visitor(incoming, distance, path_count, ov);
Chris@16 225
Chris@16 226 std::vector<default_color_type>
Chris@16 227 colors(num_vertices(g), color_traits<default_color_type>::white());
Chris@16 228 boost::queue<vertex_descriptor> Q;
Chris@16 229 breadth_first_visit(g, s, Q, visitor,
Chris@16 230 make_iterator_property_map(colors.begin(),
Chris@16 231 vertex_index));
Chris@16 232 }
Chris@16 233 };
Chris@16 234
Chris@16 235 // When the edge centrality map is a dummy property map, no
Chris@16 236 // initialization is needed.
Chris@16 237 template<typename Iter>
Chris@16 238 inline void
Chris@16 239 init_centrality_map(std::pair<Iter, Iter>, dummy_property_map) { }
Chris@16 240
Chris@16 241 // When we have a real edge centrality map, initialize all of the
Chris@16 242 // centralities to zero.
Chris@16 243 template<typename Iter, typename Centrality>
Chris@16 244 void
Chris@16 245 init_centrality_map(std::pair<Iter, Iter> keys, Centrality centrality_map)
Chris@16 246 {
Chris@16 247 typedef typename property_traits<Centrality>::value_type
Chris@16 248 centrality_type;
Chris@16 249 while (keys.first != keys.second) {
Chris@16 250 put(centrality_map, *keys.first, centrality_type(0));
Chris@16 251 ++keys.first;
Chris@16 252 }
Chris@16 253 }
Chris@16 254
Chris@16 255 // When the edge centrality map is a dummy property map, no update
Chris@16 256 // is performed.
Chris@16 257 template<typename Key, typename T>
Chris@16 258 inline void
Chris@16 259 update_centrality(dummy_property_map, const Key&, const T&) { }
Chris@16 260
Chris@16 261 // When we have a real edge centrality map, add the value to the map
Chris@16 262 template<typename CentralityMap, typename Key, typename T>
Chris@16 263 inline void
Chris@16 264 update_centrality(CentralityMap centrality_map, Key k, const T& x)
Chris@16 265 { put(centrality_map, k, get(centrality_map, k) + x); }
Chris@16 266
Chris@16 267 template<typename Iter>
Chris@16 268 inline void
Chris@16 269 divide_centrality_by_two(std::pair<Iter, Iter>, dummy_property_map) {}
Chris@16 270
Chris@16 271 template<typename Iter, typename CentralityMap>
Chris@16 272 inline void
Chris@16 273 divide_centrality_by_two(std::pair<Iter, Iter> keys,
Chris@16 274 CentralityMap centrality_map)
Chris@16 275 {
Chris@16 276 typename property_traits<CentralityMap>::value_type two(2);
Chris@16 277 while (keys.first != keys.second) {
Chris@16 278 put(centrality_map, *keys.first, get(centrality_map, *keys.first) / two);
Chris@16 279 ++keys.first;
Chris@16 280 }
Chris@16 281 }
Chris@16 282
Chris@16 283 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
Chris@16 284 typename IncomingMap, typename DistanceMap,
Chris@16 285 typename DependencyMap, typename PathCountMap,
Chris@16 286 typename VertexIndexMap, typename ShortestPaths>
Chris@16 287 void
Chris@16 288 brandes_betweenness_centrality_impl(const Graph& g,
Chris@16 289 CentralityMap centrality, // C_B
Chris@16 290 EdgeCentralityMap edge_centrality_map,
Chris@16 291 IncomingMap incoming, // P
Chris@16 292 DistanceMap distance, // d
Chris@16 293 DependencyMap dependency, // delta
Chris@16 294 PathCountMap path_count, // sigma
Chris@16 295 VertexIndexMap vertex_index,
Chris@16 296 ShortestPaths shortest_paths)
Chris@16 297 {
Chris@16 298 typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
Chris@16 299 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
Chris@16 300
Chris@16 301 // Initialize centrality
Chris@16 302 init_centrality_map(vertices(g), centrality);
Chris@16 303 init_centrality_map(edges(g), edge_centrality_map);
Chris@16 304
Chris@16 305 std::stack<vertex_descriptor> ordered_vertices;
Chris@16 306 vertex_iterator s, s_end;
Chris@16 307 for (boost::tie(s, s_end) = vertices(g); s != s_end; ++s) {
Chris@16 308 // Initialize for this iteration
Chris@16 309 vertex_iterator w, w_end;
Chris@16 310 for (boost::tie(w, w_end) = vertices(g); w != w_end; ++w) {
Chris@16 311 incoming[*w].clear();
Chris@16 312 put(path_count, *w, 0);
Chris@16 313 put(dependency, *w, 0);
Chris@16 314 }
Chris@16 315 put(path_count, *s, 1);
Chris@16 316
Chris@16 317 // Execute the shortest paths algorithm. This will be either
Chris@16 318 // Dijkstra's algorithm or a customized breadth-first search,
Chris@16 319 // depending on whether the graph is weighted or unweighted.
Chris@16 320 shortest_paths(g, *s, ordered_vertices, incoming, distance,
Chris@16 321 path_count, vertex_index);
Chris@16 322
Chris@16 323 while (!ordered_vertices.empty()) {
Chris@16 324 vertex_descriptor w = ordered_vertices.top();
Chris@16 325 ordered_vertices.pop();
Chris@16 326
Chris@16 327 typedef typename property_traits<IncomingMap>::value_type
Chris@16 328 incoming_type;
Chris@16 329 typedef typename incoming_type::iterator incoming_iterator;
Chris@16 330 typedef typename property_traits<DependencyMap>::value_type
Chris@16 331 dependency_type;
Chris@16 332
Chris@16 333 for (incoming_iterator vw = incoming[w].begin();
Chris@16 334 vw != incoming[w].end(); ++vw) {
Chris@16 335 vertex_descriptor v = source(*vw, g);
Chris@16 336 dependency_type factor = dependency_type(get(path_count, v))
Chris@16 337 / dependency_type(get(path_count, w));
Chris@16 338 factor *= (dependency_type(1) + get(dependency, w));
Chris@16 339 put(dependency, v, get(dependency, v) + factor);
Chris@16 340 update_centrality(edge_centrality_map, *vw, factor);
Chris@16 341 }
Chris@16 342
Chris@16 343 if (w != *s) {
Chris@16 344 update_centrality(centrality, w, get(dependency, w));
Chris@16 345 }
Chris@16 346 }
Chris@16 347 }
Chris@16 348
Chris@16 349 typedef typename graph_traits<Graph>::directed_category directed_category;
Chris@16 350 const bool is_undirected =
Chris@16 351 is_convertible<directed_category*, undirected_tag*>::value;
Chris@16 352 if (is_undirected) {
Chris@16 353 divide_centrality_by_two(vertices(g), centrality);
Chris@16 354 divide_centrality_by_two(edges(g), edge_centrality_map);
Chris@16 355 }
Chris@16 356 }
Chris@16 357
Chris@16 358 } } // end namespace detail::graph
Chris@16 359
Chris@16 360 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
Chris@16 361 typename IncomingMap, typename DistanceMap,
Chris@16 362 typename DependencyMap, typename PathCountMap,
Chris@16 363 typename VertexIndexMap>
Chris@16 364 void
Chris@16 365 brandes_betweenness_centrality(const Graph& g,
Chris@16 366 CentralityMap centrality, // C_B
Chris@16 367 EdgeCentralityMap edge_centrality_map,
Chris@16 368 IncomingMap incoming, // P
Chris@16 369 DistanceMap distance, // d
Chris@16 370 DependencyMap dependency, // delta
Chris@16 371 PathCountMap path_count, // sigma
Chris@16 372 VertexIndexMap vertex_index
Chris@16 373 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
Chris@16 374 {
Chris@16 375 detail::graph::brandes_unweighted_shortest_paths shortest_paths;
Chris@16 376
Chris@16 377 detail::graph::brandes_betweenness_centrality_impl(g, centrality,
Chris@16 378 edge_centrality_map,
Chris@16 379 incoming, distance,
Chris@16 380 dependency, path_count,
Chris@16 381 vertex_index,
Chris@16 382 shortest_paths);
Chris@16 383 }
Chris@16 384
Chris@16 385 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
Chris@16 386 typename IncomingMap, typename DistanceMap,
Chris@16 387 typename DependencyMap, typename PathCountMap,
Chris@16 388 typename VertexIndexMap, typename WeightMap>
Chris@16 389 void
Chris@16 390 brandes_betweenness_centrality(const Graph& g,
Chris@16 391 CentralityMap centrality, // C_B
Chris@16 392 EdgeCentralityMap edge_centrality_map,
Chris@16 393 IncomingMap incoming, // P
Chris@16 394 DistanceMap distance, // d
Chris@16 395 DependencyMap dependency, // delta
Chris@16 396 PathCountMap path_count, // sigma
Chris@16 397 VertexIndexMap vertex_index,
Chris@16 398 WeightMap weight_map
Chris@16 399 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
Chris@16 400 {
Chris@16 401 detail::graph::brandes_dijkstra_shortest_paths<WeightMap>
Chris@16 402 shortest_paths(weight_map);
Chris@16 403
Chris@16 404 detail::graph::brandes_betweenness_centrality_impl(g, centrality,
Chris@16 405 edge_centrality_map,
Chris@16 406 incoming, distance,
Chris@16 407 dependency, path_count,
Chris@16 408 vertex_index,
Chris@16 409 shortest_paths);
Chris@16 410 }
Chris@16 411
Chris@16 412 namespace detail { namespace graph {
Chris@16 413 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
Chris@16 414 typename WeightMap, typename VertexIndexMap>
Chris@16 415 void
Chris@16 416 brandes_betweenness_centrality_dispatch2(const Graph& g,
Chris@16 417 CentralityMap centrality,
Chris@16 418 EdgeCentralityMap edge_centrality_map,
Chris@16 419 WeightMap weight_map,
Chris@16 420 VertexIndexMap vertex_index)
Chris@16 421 {
Chris@16 422 typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
Chris@16 423 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
Chris@16 424 typedef typename mpl::if_c<(is_same<CentralityMap,
Chris@16 425 dummy_property_map>::value),
Chris@16 426 EdgeCentralityMap,
Chris@16 427 CentralityMap>::type a_centrality_map;
Chris@16 428 typedef typename property_traits<a_centrality_map>::value_type
Chris@16 429 centrality_type;
Chris@16 430
Chris@16 431 typename graph_traits<Graph>::vertices_size_type V = num_vertices(g);
Chris@16 432
Chris@16 433 std::vector<std::vector<edge_descriptor> > incoming(V);
Chris@16 434 std::vector<centrality_type> distance(V);
Chris@16 435 std::vector<centrality_type> dependency(V);
Chris@16 436 std::vector<degree_size_type> path_count(V);
Chris@16 437
Chris@16 438 brandes_betweenness_centrality(
Chris@16 439 g, centrality, edge_centrality_map,
Chris@16 440 make_iterator_property_map(incoming.begin(), vertex_index),
Chris@16 441 make_iterator_property_map(distance.begin(), vertex_index),
Chris@16 442 make_iterator_property_map(dependency.begin(), vertex_index),
Chris@16 443 make_iterator_property_map(path_count.begin(), vertex_index),
Chris@16 444 vertex_index,
Chris@16 445 weight_map);
Chris@16 446 }
Chris@16 447
Chris@16 448
Chris@16 449 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
Chris@16 450 typename VertexIndexMap>
Chris@16 451 void
Chris@16 452 brandes_betweenness_centrality_dispatch2(const Graph& g,
Chris@16 453 CentralityMap centrality,
Chris@16 454 EdgeCentralityMap edge_centrality_map,
Chris@16 455 VertexIndexMap vertex_index)
Chris@16 456 {
Chris@16 457 typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
Chris@16 458 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
Chris@16 459 typedef typename mpl::if_c<(is_same<CentralityMap,
Chris@16 460 dummy_property_map>::value),
Chris@16 461 EdgeCentralityMap,
Chris@16 462 CentralityMap>::type a_centrality_map;
Chris@16 463 typedef typename property_traits<a_centrality_map>::value_type
Chris@16 464 centrality_type;
Chris@16 465
Chris@16 466 typename graph_traits<Graph>::vertices_size_type V = num_vertices(g);
Chris@16 467
Chris@16 468 std::vector<std::vector<edge_descriptor> > incoming(V);
Chris@16 469 std::vector<centrality_type> distance(V);
Chris@16 470 std::vector<centrality_type> dependency(V);
Chris@16 471 std::vector<degree_size_type> path_count(V);
Chris@16 472
Chris@16 473 brandes_betweenness_centrality(
Chris@16 474 g, centrality, edge_centrality_map,
Chris@16 475 make_iterator_property_map(incoming.begin(), vertex_index),
Chris@16 476 make_iterator_property_map(distance.begin(), vertex_index),
Chris@16 477 make_iterator_property_map(dependency.begin(), vertex_index),
Chris@16 478 make_iterator_property_map(path_count.begin(), vertex_index),
Chris@16 479 vertex_index);
Chris@16 480 }
Chris@16 481
Chris@16 482 template<typename WeightMap>
Chris@16 483 struct brandes_betweenness_centrality_dispatch1
Chris@16 484 {
Chris@16 485 template<typename Graph, typename CentralityMap,
Chris@16 486 typename EdgeCentralityMap, typename VertexIndexMap>
Chris@16 487 static void
Chris@16 488 run(const Graph& g, CentralityMap centrality,
Chris@16 489 EdgeCentralityMap edge_centrality_map, VertexIndexMap vertex_index,
Chris@16 490 WeightMap weight_map)
Chris@16 491 {
Chris@16 492 brandes_betweenness_centrality_dispatch2(g, centrality, edge_centrality_map,
Chris@16 493 weight_map, vertex_index);
Chris@16 494 }
Chris@16 495 };
Chris@16 496
Chris@16 497 template<>
Chris@16 498 struct brandes_betweenness_centrality_dispatch1<param_not_found>
Chris@16 499 {
Chris@16 500 template<typename Graph, typename CentralityMap,
Chris@16 501 typename EdgeCentralityMap, typename VertexIndexMap>
Chris@16 502 static void
Chris@16 503 run(const Graph& g, CentralityMap centrality,
Chris@16 504 EdgeCentralityMap edge_centrality_map, VertexIndexMap vertex_index,
Chris@16 505 param_not_found)
Chris@16 506 {
Chris@16 507 brandes_betweenness_centrality_dispatch2(g, centrality, edge_centrality_map,
Chris@16 508 vertex_index);
Chris@16 509 }
Chris@16 510 };
Chris@16 511
Chris@16 512 template <typename T>
Chris@16 513 struct is_bgl_named_params {
Chris@16 514 BOOST_STATIC_CONSTANT(bool, value = false);
Chris@16 515 };
Chris@16 516
Chris@16 517 template <typename Param, typename Tag, typename Rest>
Chris@16 518 struct is_bgl_named_params<bgl_named_params<Param, Tag, Rest> > {
Chris@16 519 BOOST_STATIC_CONSTANT(bool, value = true);
Chris@16 520 };
Chris@16 521
Chris@16 522 } } // end namespace detail::graph
Chris@16 523
Chris@16 524 template<typename Graph, typename Param, typename Tag, typename Rest>
Chris@16 525 void
Chris@16 526 brandes_betweenness_centrality(const Graph& g,
Chris@16 527 const bgl_named_params<Param,Tag,Rest>& params
Chris@16 528 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
Chris@16 529 {
Chris@16 530 typedef bgl_named_params<Param,Tag,Rest> named_params;
Chris@16 531
Chris@16 532 typedef typename get_param_type<edge_weight_t, named_params>::type ew;
Chris@16 533 detail::graph::brandes_betweenness_centrality_dispatch1<ew>::run(
Chris@16 534 g,
Chris@16 535 choose_param(get_param(params, vertex_centrality),
Chris@16 536 dummy_property_map()),
Chris@16 537 choose_param(get_param(params, edge_centrality),
Chris@16 538 dummy_property_map()),
Chris@16 539 choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
Chris@16 540 get_param(params, edge_weight));
Chris@16 541 }
Chris@16 542
Chris@16 543 // disable_if is required to work around problem with MSVC 7.1 (it seems to not
Chris@16 544 // get partial ordering getween this overload and the previous one correct)
Chris@16 545 template<typename Graph, typename CentralityMap>
Chris@16 546 typename disable_if<detail::graph::is_bgl_named_params<CentralityMap>,
Chris@16 547 void>::type
Chris@16 548 brandes_betweenness_centrality(const Graph& g, CentralityMap centrality
Chris@16 549 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
Chris@16 550 {
Chris@16 551 detail::graph::brandes_betweenness_centrality_dispatch2(
Chris@16 552 g, centrality, dummy_property_map(), get(vertex_index, g));
Chris@16 553 }
Chris@16 554
Chris@16 555 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap>
Chris@16 556 void
Chris@16 557 brandes_betweenness_centrality(const Graph& g, CentralityMap centrality,
Chris@16 558 EdgeCentralityMap edge_centrality_map
Chris@16 559 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
Chris@16 560 {
Chris@16 561 detail::graph::brandes_betweenness_centrality_dispatch2(
Chris@16 562 g, centrality, edge_centrality_map, get(vertex_index, g));
Chris@16 563 }
Chris@16 564
Chris@16 565 /**
Chris@16 566 * Converts "absolute" betweenness centrality (as computed by the
Chris@16 567 * brandes_betweenness_centrality algorithm) in the centrality map
Chris@16 568 * into "relative" centrality. The result is placed back into the
Chris@16 569 * given centrality map.
Chris@16 570 */
Chris@16 571 template<typename Graph, typename CentralityMap>
Chris@16 572 void
Chris@16 573 relative_betweenness_centrality(const Graph& g, CentralityMap centrality)
Chris@16 574 {
Chris@16 575 typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
Chris@16 576 typedef typename property_traits<CentralityMap>::value_type centrality_type;
Chris@16 577
Chris@16 578 typename graph_traits<Graph>::vertices_size_type n = num_vertices(g);
Chris@16 579 centrality_type factor = centrality_type(2)/centrality_type(n*n - 3*n + 2);
Chris@16 580 vertex_iterator v, v_end;
Chris@16 581 for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
Chris@16 582 put(centrality, *v, factor * get(centrality, *v));
Chris@16 583 }
Chris@16 584 }
Chris@16 585
Chris@16 586 // Compute the central point dominance of a graph.
Chris@16 587 template<typename Graph, typename CentralityMap>
Chris@16 588 typename property_traits<CentralityMap>::value_type
Chris@16 589 central_point_dominance(const Graph& g, CentralityMap centrality
Chris@16 590 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
Chris@16 591 {
Chris@16 592 using std::max;
Chris@16 593
Chris@16 594 typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
Chris@16 595 typedef typename property_traits<CentralityMap>::value_type centrality_type;
Chris@16 596
Chris@16 597 typename graph_traits<Graph>::vertices_size_type n = num_vertices(g);
Chris@16 598
Chris@16 599 // Find max centrality
Chris@16 600 centrality_type max_centrality(0);
Chris@16 601 vertex_iterator v, v_end;
Chris@16 602 for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
Chris@16 603 max_centrality = (max)(max_centrality, get(centrality, *v));
Chris@16 604 }
Chris@16 605
Chris@16 606 // Compute central point dominance
Chris@16 607 centrality_type sum(0);
Chris@16 608 for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
Chris@16 609 sum += (max_centrality - get(centrality, *v));
Chris@16 610 }
Chris@16 611 return sum/(n-1);
Chris@16 612 }
Chris@16 613
Chris@16 614 } // end namespace boost
Chris@16 615
Chris@16 616 #endif // BOOST_GRAPH_BRANDES_BETWEENNESS_CENTRALITY_HPP