betweenness_centrality.hpp 24 KB

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