degree_centrality.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <vector>
  7. #include <boost/graph/undirected_graph.hpp>
  8. #include <boost/graph/directed_graph.hpp>
  9. #include <boost/graph/degree_centrality.hpp>
  10. #include <boost/graph/exterior_property.hpp>
  11. using namespace std;
  12. using namespace boost;
  13. // useful types
  14. // number of vertices in the graph
  15. static const unsigned N = 5;
  16. template <typename Graph>
  17. void build_graph(Graph& g,
  18. vector<typename graph_traits<Graph>::vertex_descriptor>& v)
  19. {
  20. // add vertices
  21. for(size_t i = 0; i < N; ++i) {
  22. v[i] = add_vertex(g);
  23. }
  24. // add edges
  25. add_edge(v[0], v[1], g);
  26. add_edge(v[1], v[2], g);
  27. add_edge(v[2], v[0], g);
  28. add_edge(v[3], v[4], g);
  29. add_edge(v[4], v[0], g);
  30. }
  31. template <typename Graph>
  32. void test_undirected()
  33. {
  34. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  35. typedef exterior_vertex_property<Graph, unsigned> CentralityProperty;
  36. typedef typename CentralityProperty::container_type CentralityContainer;
  37. typedef typename CentralityProperty::map_type CentralityMap;
  38. Graph g;
  39. vector<Vertex> v(N);
  40. build_graph(g, v);
  41. CentralityContainer cents(num_vertices(g));
  42. CentralityMap cm(cents, g);
  43. all_degree_centralities(g, cm);
  44. BOOST_ASSERT(cm[v[0]] == 3);
  45. BOOST_ASSERT(cm[v[1]] == 2);
  46. BOOST_ASSERT(cm[v[2]] == 2);
  47. BOOST_ASSERT(cm[v[3]] == 1);
  48. BOOST_ASSERT(cm[v[4]] == 2);
  49. }
  50. template <typename Graph>
  51. void test_influence()
  52. {
  53. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  54. typedef exterior_vertex_property<Graph, unsigned> CentralityProperty;
  55. typedef typename CentralityProperty::container_type CentralityContainer;
  56. typedef typename CentralityProperty::map_type CentralityMap;
  57. Graph g;
  58. vector<Vertex> v(N);
  59. build_graph(g, v);
  60. CentralityContainer cents(num_vertices(g));
  61. CentralityMap cm(cents, g);
  62. all_influence_values(g, cm);
  63. BOOST_ASSERT(cm[v[0]] == 1);
  64. BOOST_ASSERT(cm[v[1]] == 1);
  65. BOOST_ASSERT(cm[v[2]] == 1);
  66. BOOST_ASSERT(cm[v[3]] == 1);
  67. BOOST_ASSERT(cm[v[4]] == 1);
  68. }
  69. template <typename Graph>
  70. void test_prestige()
  71. {
  72. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  73. typedef exterior_vertex_property<Graph, unsigned> CentralityProperty;
  74. typedef typename CentralityProperty::container_type CentralityContainer;
  75. typedef typename CentralityProperty::map_type CentralityMap;
  76. Graph g;
  77. vector<Vertex> v(N);
  78. build_graph(g, v);
  79. CentralityContainer cents(num_vertices(g));
  80. CentralityMap cm(cents, g);
  81. all_prestige_values(g, cm);
  82. BOOST_ASSERT(cm[v[0]] == 2);
  83. BOOST_ASSERT(cm[v[1]] == 1);
  84. BOOST_ASSERT(cm[v[2]] == 1);
  85. BOOST_ASSERT(cm[v[3]] == 0);
  86. BOOST_ASSERT(cm[v[4]] == 1);
  87. }
  88. int
  89. main(int, char *[])
  90. {
  91. typedef undirected_graph<> Graph;
  92. typedef directed_graph<> Digraph;
  93. test_undirected<Graph>();
  94. test_influence<Digraph>();
  95. test_prestige<Digraph>();
  96. }