closeness_centrality.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // (C) Copyright 2007-2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #include <iostream>
  7. #include <vector>
  8. #include <boost/graph/undirected_graph.hpp>
  9. #include <boost/graph/directed_graph.hpp>
  10. #include <boost/graph/floyd_warshall_shortest.hpp>
  11. #include <boost/graph/closeness_centrality.hpp>
  12. #include <boost/graph/exterior_property.hpp>
  13. #include <boost/graph/property_maps/constant_property_map.hpp>
  14. using namespace std;
  15. using namespace boost;
  16. // number of vertices in the graph
  17. static const unsigned N = 5;
  18. template <typename Graph>
  19. struct vertex_vector
  20. {
  21. typedef graph_traits<Graph> traits;
  22. typedef vector<typename traits::vertex_descriptor> type;
  23. };
  24. template <typename Graph>
  25. void build_graph(Graph& g,
  26. typename vertex_vector<Graph>::type& v)
  27. {
  28. // add vertices
  29. for(size_t i = 0; i < N; ++i) {
  30. v[i] = add_vertex(g);
  31. }
  32. // add edges
  33. add_edge(v[0], v[1], g);
  34. add_edge(v[1], v[2], g);
  35. add_edge(v[2], v[0], g);
  36. add_edge(v[3], v[4], g);
  37. add_edge(v[4], v[0], g);
  38. }
  39. template <typename Graph>
  40. void test_undirected()
  41. {
  42. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  43. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  44. typedef exterior_vertex_property<Graph, double> CentralityProperty;
  45. typedef typename CentralityProperty::container_type CentralityContainer;
  46. typedef typename CentralityProperty::map_type CentralityMap;
  47. typedef exterior_vertex_property<Graph, int> DistanceProperty;
  48. typedef typename DistanceProperty::matrix_type DistanceMatrix;
  49. typedef typename DistanceProperty::matrix_map_type DistanceMatrixMap;
  50. typedef constant_property_map<Edge, int> WeightMap;
  51. Graph g;
  52. vector<Vertex> v(N);
  53. build_graph(g, v);
  54. CentralityContainer centralities(num_vertices(g));
  55. DistanceMatrix distances(num_vertices(g));
  56. CentralityMap cm(centralities, g);
  57. DistanceMatrixMap dm(distances, g);
  58. WeightMap wm(1);
  59. floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
  60. all_closeness_centralities(g, dm, cm);
  61. BOOST_ASSERT(cm[v[0]] == double(1)/5);
  62. BOOST_ASSERT(cm[v[1]] == double(1)/7);
  63. BOOST_ASSERT(cm[v[2]] == double(1)/7);
  64. BOOST_ASSERT(cm[v[3]] == double(1)/9);
  65. BOOST_ASSERT(cm[v[4]] == double(1)/6);
  66. }
  67. template <typename Graph>
  68. void test_directed()
  69. {
  70. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  71. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  72. typedef exterior_vertex_property<Graph, double> CentralityProperty;
  73. typedef typename CentralityProperty::container_type CentralityContainer;
  74. typedef typename CentralityProperty::map_type CentralityMap;
  75. typedef exterior_vertex_property<Graph, int> DistanceProperty;
  76. typedef typename DistanceProperty::matrix_type DistanceMatrix;
  77. typedef typename DistanceProperty::matrix_map_type DistanceMatrixMap;
  78. typedef constant_property_map<Edge, int> WeightMap;
  79. Graph g;
  80. vector<Vertex> v(N);
  81. build_graph(g, v);
  82. CentralityContainer centralities(num_vertices(g));
  83. DistanceMatrix distances(num_vertices(g));
  84. CentralityMap cm(centralities, g);
  85. DistanceMatrixMap dm(distances, g);
  86. WeightMap wm(1);
  87. floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
  88. all_closeness_centralities(g, dm, cm);
  89. BOOST_ASSERT(cm[v[0]] == double(0));
  90. BOOST_ASSERT(cm[v[1]] == double(0));
  91. BOOST_ASSERT(cm[v[2]] == double(0));
  92. BOOST_ASSERT(cm[v[3]] == double(1)/10);
  93. BOOST_ASSERT(cm[v[4]] == double(0));
  94. }
  95. int
  96. main(int, char *[])
  97. {
  98. typedef undirected_graph<> Graph;
  99. typedef directed_graph<> Digraph;
  100. test_undirected<Graph>();
  101. test_directed<Digraph>();
  102. }