bandwidth.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) Jeremy Siek 2001, Marc Wintermantel 2002
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_BANDWIDTH_HPP
  7. #define BOOST_GRAPH_BANDWIDTH_HPP
  8. #include <algorithm> // for std::min and std::max
  9. #include <boost/config.hpp>
  10. #include <boost/graph/graph_traits.hpp>
  11. #include <boost/detail/numeric_traits.hpp>
  12. namespace boost
  13. {
  14. template < typename Graph, typename VertexIndexMap >
  15. typename graph_traits< Graph >::vertices_size_type ith_bandwidth(
  16. typename graph_traits< Graph >::vertex_descriptor i, const Graph& g,
  17. VertexIndexMap index)
  18. {
  19. BOOST_USING_STD_MAX();
  20. using std::abs;
  21. typedef
  22. typename graph_traits< Graph >::vertices_size_type vertices_size_type;
  23. vertices_size_type b = 0;
  24. typename graph_traits< Graph >::out_edge_iterator e, end;
  25. for (boost::tie(e, end) = out_edges(i, g); e != end; ++e)
  26. {
  27. int f_i = get(index, i);
  28. int f_j = get(index, target(*e, g));
  29. b = max BOOST_PREVENT_MACRO_SUBSTITUTION(
  30. b, vertices_size_type(abs(f_i - f_j)));
  31. }
  32. return b;
  33. }
  34. template < typename Graph >
  35. typename graph_traits< Graph >::vertices_size_type ith_bandwidth(
  36. typename graph_traits< Graph >::vertex_descriptor i, const Graph& g)
  37. {
  38. return ith_bandwidth(i, g, get(vertex_index, g));
  39. }
  40. template < typename Graph, typename VertexIndexMap >
  41. typename graph_traits< Graph >::vertices_size_type bandwidth(
  42. const Graph& g, VertexIndexMap index)
  43. {
  44. BOOST_USING_STD_MAX();
  45. using std::abs;
  46. typedef
  47. typename graph_traits< Graph >::vertices_size_type vertices_size_type;
  48. vertices_size_type b = 0;
  49. typename graph_traits< Graph >::edge_iterator i, end;
  50. for (boost::tie(i, end) = edges(g); i != end; ++i)
  51. {
  52. int f_i = get(index, source(*i, g));
  53. int f_j = get(index, target(*i, g));
  54. b = max BOOST_PREVENT_MACRO_SUBSTITUTION(
  55. b, vertices_size_type(abs(f_i - f_j)));
  56. }
  57. return b;
  58. }
  59. template < typename Graph >
  60. typename graph_traits< Graph >::vertices_size_type bandwidth(const Graph& g)
  61. {
  62. return bandwidth(g, get(vertex_index, g));
  63. }
  64. template < typename Graph, typename VertexIndexMap >
  65. typename graph_traits< Graph >::vertices_size_type edgesum(
  66. const Graph& g, VertexIndexMap index_map)
  67. {
  68. typedef typename graph_traits< Graph >::vertices_size_type size_type;
  69. typedef
  70. typename detail::numeric_traits< size_type >::difference_type diff_t;
  71. size_type sum = 0;
  72. typename graph_traits< Graph >::edge_iterator i, end;
  73. for (boost::tie(i, end) = edges(g); i != end; ++i)
  74. {
  75. diff_t f_u = get(index_map, source(*i, g));
  76. diff_t f_v = get(index_map, target(*i, g));
  77. using namespace std; // to call abs() unqualified
  78. sum += abs(f_u - f_v);
  79. }
  80. return sum;
  81. }
  82. } // namespace boost
  83. #endif // BOOST_GRAPH_BANDWIDTH_HPP