mean_geodesic.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <boost/graph/undirected_graph.hpp>
  8. #include <boost/graph/directed_graph.hpp>
  9. #include <boost/graph/exterior_property.hpp>
  10. #include <boost/graph/property_maps/constant_property_map.hpp>
  11. #include <boost/graph/floyd_warshall_shortest.hpp>
  12. #include <boost/graph/geodesic_distance.hpp>
  13. using namespace std;
  14. using namespace boost;
  15. // useful types
  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, typename vertex_vector<Graph>::type& v)
  26. {
  27. // add vertices
  28. for(size_t i = 0; i < N; ++i) {
  29. v[i] = add_vertex(g);
  30. }
  31. // add edges
  32. add_edge(v[0], v[1], g);
  33. add_edge(v[1], v[2], g);
  34. add_edge(v[2], v[0], g);
  35. add_edge(v[3], v[4], g);
  36. add_edge(v[4], v[0], g);
  37. }
  38. template <typename Graph>
  39. void test_undirected()
  40. {
  41. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  42. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  43. typedef exterior_vertex_property<Graph, double> CentralityProperty;
  44. typedef typename CentralityProperty::container_type CentralityContainer;
  45. typedef typename CentralityProperty::map_type CentralityMap;
  46. typedef exterior_vertex_property<Graph, int> DistanceProperty;
  47. typedef typename DistanceProperty::matrix_type DistanceMatrix;
  48. typedef typename DistanceProperty::matrix_map_type DistanceMatrixMap;
  49. typedef constant_property_map<Edge, int> WeightMap;
  50. Graph g;
  51. vector<Vertex> v(N);
  52. build_graph(g, v);
  53. CentralityContainer centralities(num_vertices(g));
  54. DistanceMatrix distances(num_vertices(g));
  55. CentralityMap cm(centralities, g);
  56. DistanceMatrixMap dm(distances, g);
  57. WeightMap wm(1);
  58. floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
  59. double geo1 = all_mean_geodesics(g, dm, cm);
  60. double geo2 = small_world_distance(g, cm);
  61. BOOST_ASSERT(cm[v[0]] == double(5)/4);
  62. BOOST_ASSERT(cm[v[1]] == double(7)/4);
  63. BOOST_ASSERT(cm[v[2]] == double(7)/4);
  64. BOOST_ASSERT(cm[v[3]] == double(9)/4);
  65. BOOST_ASSERT(cm[v[4]] == double(6)/4);
  66. BOOST_ASSERT(geo1 == double(34)/20);
  67. BOOST_ASSERT(geo1 == geo2);
  68. }
  69. template <typename Graph>
  70. void test_directed()
  71. {
  72. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  73. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  74. typedef exterior_vertex_property<Graph, double> CentralityProperty;
  75. typedef typename CentralityProperty::container_type CentralityContainer;
  76. typedef typename CentralityProperty::map_type CentralityMap;
  77. typedef exterior_vertex_property<Graph, int> DistanceProperty;
  78. typedef typename DistanceProperty::matrix_type DistanceMatrix;
  79. typedef typename DistanceProperty::matrix_map_type DistanceMatrixMap;
  80. typedef constant_property_map<Edge, int> WeightMap;
  81. Graph g;
  82. vector<Vertex> v(N);
  83. build_graph(g, v);
  84. CentralityContainer centralities(num_vertices(g));
  85. DistanceMatrix distances(num_vertices(g));
  86. CentralityMap cm(centralities, g);
  87. DistanceMatrixMap dm(distances, g);
  88. WeightMap wm(1);
  89. floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
  90. double geo1 = all_mean_geodesics(g, dm, cm);
  91. double geo2 = small_world_distance(g, cm);
  92. double inf = numeric_values<double>::infinity();
  93. BOOST_ASSERT(cm[v[0]] == inf);
  94. BOOST_ASSERT(cm[v[1]] == inf);
  95. BOOST_ASSERT(cm[v[2]] == inf);
  96. BOOST_ASSERT(cm[v[3]] == double(10)/4);
  97. BOOST_ASSERT(cm[v[4]] == inf);
  98. BOOST_ASSERT(geo1 == inf);
  99. BOOST_ASSERT(geo1 == geo2);
  100. }
  101. int
  102. main(int, char *[])
  103. {
  104. typedef undirected_graph<> Graph;
  105. typedef directed_graph<> Digraph;
  106. test_undirected<Graph>();
  107. test_directed<Digraph>();
  108. }