/Src/Dependencies/Boost/boost/graph/betweenness_centrality.hpp

http://hadesmem.googlecode.com/ · C++ Header · 619 lines · 472 code · 73 blank · 74 comment · 24 complexity · 319e8cf81fe93da13e019633f5892385 MD5 · raw file

  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>::edge_iterator edge_iterator;
  267. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  268. // Initialize centrality
  269. init_centrality_map(vertices(g), centrality);
  270. init_centrality_map(edges(g), edge_centrality_map);
  271. std::stack<vertex_descriptor> ordered_vertices;
  272. vertex_iterator s, s_end;
  273. for (boost::tie(s, s_end) = vertices(g); s != s_end; ++s) {
  274. // Initialize for this iteration
  275. vertex_iterator w, w_end;
  276. for (boost::tie(w, w_end) = vertices(g); w != w_end; ++w) {
  277. incoming[*w].clear();
  278. put(path_count, *w, 0);
  279. put(dependency, *w, 0);
  280. }
  281. put(path_count, *s, 1);
  282. // Execute the shortest paths algorithm. This will be either
  283. // Dijkstra's algorithm or a customized breadth-first search,
  284. // depending on whether the graph is weighted or unweighted.
  285. shortest_paths(g, *s, ordered_vertices, incoming, distance,
  286. path_count, vertex_index);
  287. while (!ordered_vertices.empty()) {
  288. vertex_descriptor w = ordered_vertices.top();
  289. ordered_vertices.pop();
  290. typedef typename property_traits<IncomingMap>::value_type
  291. incoming_type;
  292. typedef typename incoming_type::iterator incoming_iterator;
  293. typedef typename property_traits<DependencyMap>::value_type
  294. dependency_type;
  295. for (incoming_iterator vw = incoming[w].begin();
  296. vw != incoming[w].end(); ++vw) {
  297. vertex_descriptor v = source(*vw, g);
  298. dependency_type factor = dependency_type(get(path_count, v))
  299. / dependency_type(get(path_count, w));
  300. factor *= (dependency_type(1) + get(dependency, w));
  301. put(dependency, v, get(dependency, v) + factor);
  302. update_centrality(edge_centrality_map, *vw, factor);
  303. }
  304. if (w != *s) {
  305. update_centrality(centrality, w, get(dependency, w));
  306. }
  307. }
  308. }
  309. typedef typename graph_traits<Graph>::directed_category directed_category;
  310. const bool is_undirected =
  311. is_convertible<directed_category*, undirected_tag*>::value;
  312. if (is_undirected) {
  313. divide_centrality_by_two(vertices(g), centrality);
  314. divide_centrality_by_two(edges(g), edge_centrality_map);
  315. }
  316. }
  317. } } // end namespace detail::graph
  318. template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
  319. typename IncomingMap, typename DistanceMap,
  320. typename DependencyMap, typename PathCountMap,
  321. typename VertexIndexMap>
  322. void
  323. brandes_betweenness_centrality(const Graph& g,
  324. CentralityMap centrality, // C_B
  325. EdgeCentralityMap edge_centrality_map,
  326. IncomingMap incoming, // P
  327. DistanceMap distance, // d
  328. DependencyMap dependency, // delta
  329. PathCountMap path_count, // sigma
  330. VertexIndexMap vertex_index
  331. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  332. {
  333. detail::graph::brandes_unweighted_shortest_paths shortest_paths;
  334. detail::graph::brandes_betweenness_centrality_impl(g, centrality,
  335. edge_centrality_map,
  336. incoming, distance,
  337. dependency, path_count,
  338. vertex_index,
  339. shortest_paths);
  340. }
  341. template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
  342. typename IncomingMap, typename DistanceMap,
  343. typename DependencyMap, typename PathCountMap,
  344. typename VertexIndexMap, typename WeightMap>
  345. void
  346. brandes_betweenness_centrality(const Graph& g,
  347. CentralityMap centrality, // C_B
  348. EdgeCentralityMap edge_centrality_map,
  349. IncomingMap incoming, // P
  350. DistanceMap distance, // d
  351. DependencyMap dependency, // delta
  352. PathCountMap path_count, // sigma
  353. VertexIndexMap vertex_index,
  354. WeightMap weight_map
  355. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  356. {
  357. detail::graph::brandes_dijkstra_shortest_paths<WeightMap>
  358. shortest_paths(weight_map);
  359. detail::graph::brandes_betweenness_centrality_impl(g, centrality,
  360. edge_centrality_map,
  361. incoming, distance,
  362. dependency, path_count,
  363. vertex_index,
  364. shortest_paths);
  365. }
  366. namespace detail { namespace graph {
  367. template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
  368. typename WeightMap, typename VertexIndexMap>
  369. void
  370. brandes_betweenness_centrality_dispatch2(const Graph& g,
  371. CentralityMap centrality,
  372. EdgeCentralityMap edge_centrality_map,
  373. WeightMap weight_map,
  374. VertexIndexMap vertex_index)
  375. {
  376. typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
  377. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  378. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  379. typedef typename mpl::if_c<(is_same<CentralityMap,
  380. dummy_property_map>::value),
  381. EdgeCentralityMap,
  382. CentralityMap>::type a_centrality_map;
  383. typedef typename property_traits<a_centrality_map>::value_type
  384. centrality_type;
  385. typename graph_traits<Graph>::vertices_size_type V = num_vertices(g);
  386. std::vector<std::vector<edge_descriptor> > incoming(V);
  387. std::vector<centrality_type> distance(V);
  388. std::vector<centrality_type> dependency(V);
  389. std::vector<degree_size_type> path_count(V);
  390. brandes_betweenness_centrality(
  391. g, centrality, edge_centrality_map,
  392. make_iterator_property_map(incoming.begin(), vertex_index),
  393. make_iterator_property_map(distance.begin(), vertex_index),
  394. make_iterator_property_map(dependency.begin(), vertex_index),
  395. make_iterator_property_map(path_count.begin(), vertex_index),
  396. vertex_index,
  397. weight_map);
  398. }
  399. template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
  400. typename VertexIndexMap>
  401. void
  402. brandes_betweenness_centrality_dispatch2(const Graph& g,
  403. CentralityMap centrality,
  404. EdgeCentralityMap edge_centrality_map,
  405. VertexIndexMap vertex_index)
  406. {
  407. typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
  408. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  409. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  410. typedef typename mpl::if_c<(is_same<CentralityMap,
  411. dummy_property_map>::value),
  412. EdgeCentralityMap,
  413. CentralityMap>::type a_centrality_map;
  414. typedef typename property_traits<a_centrality_map>::value_type
  415. centrality_type;
  416. typename graph_traits<Graph>::vertices_size_type V = num_vertices(g);
  417. std::vector<std::vector<edge_descriptor> > incoming(V);
  418. std::vector<centrality_type> distance(V);
  419. std::vector<centrality_type> dependency(V);
  420. std::vector<degree_size_type> path_count(V);
  421. brandes_betweenness_centrality(
  422. g, centrality, edge_centrality_map,
  423. make_iterator_property_map(incoming.begin(), vertex_index),
  424. make_iterator_property_map(distance.begin(), vertex_index),
  425. make_iterator_property_map(dependency.begin(), vertex_index),
  426. make_iterator_property_map(path_count.begin(), vertex_index),
  427. vertex_index);
  428. }
  429. template<typename WeightMap>
  430. struct brandes_betweenness_centrality_dispatch1
  431. {
  432. template<typename Graph, typename CentralityMap,
  433. typename EdgeCentralityMap, typename VertexIndexMap>
  434. static void
  435. run(const Graph& g, CentralityMap centrality,
  436. EdgeCentralityMap edge_centrality_map, VertexIndexMap vertex_index,
  437. WeightMap weight_map)
  438. {
  439. brandes_betweenness_centrality_dispatch2(g, centrality, edge_centrality_map,
  440. weight_map, vertex_index);
  441. }
  442. };
  443. template<>
  444. struct brandes_betweenness_centrality_dispatch1<error_property_not_found>
  445. {
  446. template<typename Graph, typename CentralityMap,
  447. typename EdgeCentralityMap, typename VertexIndexMap>
  448. static void
  449. run(const Graph& g, CentralityMap centrality,
  450. EdgeCentralityMap edge_centrality_map, VertexIndexMap vertex_index,
  451. error_property_not_found)
  452. {
  453. brandes_betweenness_centrality_dispatch2(g, centrality, edge_centrality_map,
  454. vertex_index);
  455. }
  456. };
  457. template <typename T>
  458. struct is_bgl_named_params {
  459. BOOST_STATIC_CONSTANT(bool, value = false);
  460. };
  461. template <typename Param, typename Tag, typename Rest>
  462. struct is_bgl_named_params<bgl_named_params<Param, Tag, Rest> > {
  463. BOOST_STATIC_CONSTANT(bool, value = true);
  464. };
  465. } } // end namespace detail::graph
  466. template<typename Graph, typename Param, typename Tag, typename Rest>
  467. void
  468. brandes_betweenness_centrality(const Graph& g,
  469. const bgl_named_params<Param,Tag,Rest>& params
  470. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  471. {
  472. typedef bgl_named_params<Param,Tag,Rest> named_params;
  473. typedef typename property_value<named_params, edge_weight_t>::type ew;
  474. detail::graph::brandes_betweenness_centrality_dispatch1<ew>::run(
  475. g,
  476. choose_param(get_param(params, vertex_centrality),
  477. dummy_property_map()),
  478. choose_param(get_param(params, edge_centrality),
  479. dummy_property_map()),
  480. choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
  481. get_param(params, edge_weight));
  482. }
  483. // disable_if is required to work around problem with MSVC 7.1 (it seems to not
  484. // get partial ordering getween this overload and the previous one correct)
  485. template<typename Graph, typename CentralityMap>
  486. typename disable_if<detail::graph::is_bgl_named_params<CentralityMap>,
  487. void>::type
  488. brandes_betweenness_centrality(const Graph& g, CentralityMap centrality
  489. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  490. {
  491. detail::graph::brandes_betweenness_centrality_dispatch2(
  492. g, centrality, dummy_property_map(), get(vertex_index, g));
  493. }
  494. template<typename Graph, typename CentralityMap, typename EdgeCentralityMap>
  495. void
  496. brandes_betweenness_centrality(const Graph& g, CentralityMap centrality,
  497. EdgeCentralityMap edge_centrality_map
  498. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  499. {
  500. detail::graph::brandes_betweenness_centrality_dispatch2(
  501. g, centrality, edge_centrality_map, get(vertex_index, g));
  502. }
  503. /**
  504. * Converts "absolute" betweenness centrality (as computed by the
  505. * brandes_betweenness_centrality algorithm) in the centrality map
  506. * into "relative" centrality. The result is placed back into the
  507. * given centrality map.
  508. */
  509. template<typename Graph, typename CentralityMap>
  510. void
  511. relative_betweenness_centrality(const Graph& g, CentralityMap centrality)
  512. {
  513. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  514. typedef typename property_traits<CentralityMap>::value_type centrality_type;
  515. typename graph_traits<Graph>::vertices_size_type n = num_vertices(g);
  516. centrality_type factor = centrality_type(2)/centrality_type(n*n - 3*n + 2);
  517. vertex_iterator v, v_end;
  518. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
  519. put(centrality, *v, factor * get(centrality, *v));
  520. }
  521. }
  522. // Compute the central point dominance of a graph.
  523. template<typename Graph, typename CentralityMap>
  524. typename property_traits<CentralityMap>::value_type
  525. central_point_dominance(const Graph& g, CentralityMap centrality
  526. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  527. {
  528. using std::max;
  529. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  530. typedef typename property_traits<CentralityMap>::value_type centrality_type;
  531. typename graph_traits<Graph>::vertices_size_type n = num_vertices(g);
  532. // Find max centrality
  533. centrality_type max_centrality(0);
  534. vertex_iterator v, v_end;
  535. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
  536. max_centrality = (max)(max_centrality, get(centrality, *v));
  537. }
  538. // Compute central point dominance
  539. centrality_type sum(0);
  540. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
  541. sum += (max_centrality - get(centrality, *v));
  542. }
  543. return sum/(n-1);
  544. }
  545. } // end namespace boost
  546. #endif // BOOST_GRAPH_BRANDES_BETWEENNESS_CENTRALITY_HPP