betweenness_centrality.hpp 26 KB

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