eccentricity.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/floyd_warshall_shortest.hpp>
  10. #include <boost/graph/eccentricity.hpp>
  11. #include <boost/graph/exterior_property.hpp>
  12. #include <boost/graph/property_maps/constant_property_map.hpp>
  13. using namespace std;
  14. using namespace boost;
  15. // number of vertices in the graph
  16. static const unsigned N = 5;
  17. template <typename Graph>
  18. struct vertex_vector
  19. {
  20. typedef graph_traits<Graph> traits;
  21. typedef vector<typename traits::vertex_descriptor> type;
  22. };
  23. template <typename Graph>
  24. void build_graph(Graph& g,
  25. 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, int> EccentricityProperty;
  44. typedef typename EccentricityProperty::container_type EccentricityContainer;
  45. typedef typename EccentricityProperty::map_type EccentricityMap;
  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. EccentricityContainer eccs(num_vertices(g));
  54. DistanceMatrix distances(num_vertices(g));
  55. EccentricityMap em(eccs, g);
  56. DistanceMatrixMap dm(distances, g);
  57. WeightMap wm(1);
  58. floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
  59. all_eccentricities(g, dm, em);
  60. int rad = radius(g, em);
  61. int dia = diameter(g, em);
  62. BOOST_ASSERT(em[v[0]] == 2);
  63. BOOST_ASSERT(em[v[1]] == 3);
  64. BOOST_ASSERT(em[v[2]] == 3);
  65. BOOST_ASSERT(em[v[3]] == 3);
  66. BOOST_ASSERT(em[v[4]] == 2);
  67. BOOST_ASSERT(rad == 2);
  68. BOOST_ASSERT(dia == 3);
  69. }
  70. template <typename Graph>
  71. void test_directed()
  72. {
  73. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  74. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  75. typedef exterior_vertex_property<Graph, int> EccentricityProperty;
  76. typedef typename EccentricityProperty::container_type EccentricityContainer;
  77. typedef typename EccentricityProperty::map_type EccentricityMap;
  78. typedef exterior_vertex_property<Graph, int> DistanceProperty;
  79. typedef typename DistanceProperty::matrix_type DistanceMatrix;
  80. typedef typename DistanceProperty::matrix_map_type DistanceMatrixMap;
  81. typedef constant_property_map<Edge, int> WeightMap;
  82. Graph g;
  83. vector<Vertex> v(N);
  84. build_graph(g, v);
  85. EccentricityContainer eccs(num_vertices(g));
  86. DistanceMatrix distances(num_vertices(g));
  87. EccentricityMap em(eccs, g);
  88. DistanceMatrixMap dm(distances, g);
  89. WeightMap wm(1);
  90. floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
  91. all_eccentricities(g, dm, em);
  92. int rad = radius(g, em);
  93. int dia = diameter(g, em);
  94. int inf = numeric_values<int>::infinity();
  95. BOOST_ASSERT(em[v[0]] == inf);
  96. BOOST_ASSERT(em[v[1]] == inf);
  97. BOOST_ASSERT(em[v[2]] == inf);
  98. BOOST_ASSERT(em[v[3]] == 4);
  99. BOOST_ASSERT(em[v[4]] == inf);
  100. BOOST_ASSERT(rad == 4);
  101. BOOST_ASSERT(dia == inf);
  102. }
  103. int
  104. main(int, char *[])
  105. {
  106. typedef undirected_graph<> Graph;
  107. typedef directed_graph<> Digraph;
  108. test_undirected<Graph>();
  109. test_directed<Digraph>();
  110. }