bandwidth.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. template <typename Graph, typename VertexIndexMap>
  14. typename graph_traits<Graph>::vertices_size_type
  15. ith_bandwidth(typename graph_traits<Graph>::vertex_descriptor i,
  16. const Graph& g,
  17. VertexIndexMap index)
  18. {
  19. BOOST_USING_STD_MAX();
  20. using std::abs;
  21. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  22. vertices_size_type b = 0;
  23. typename graph_traits<Graph>::out_edge_iterator e, end;
  24. for (boost::tie(e, end) = out_edges(i, g); e != end; ++e) {
  25. int f_i = get(index, i);
  26. int f_j = get(index, target(*e, g));
  27. b = max BOOST_PREVENT_MACRO_SUBSTITUTION (b, vertices_size_type(abs(f_i - f_j)));
  28. }
  29. return b;
  30. }
  31. template <typename Graph>
  32. typename graph_traits<Graph>::vertices_size_type
  33. ith_bandwidth(typename graph_traits<Graph>::vertex_descriptor i,
  34. const Graph& g)
  35. {
  36. return ith_bandwidth(i, g, get(vertex_index, g));
  37. }
  38. template <typename Graph, typename VertexIndexMap>
  39. typename graph_traits<Graph>::vertices_size_type
  40. bandwidth(const Graph& g, VertexIndexMap index)
  41. {
  42. BOOST_USING_STD_MAX();
  43. using std::abs;
  44. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  45. vertices_size_type b = 0;
  46. typename graph_traits<Graph>::edge_iterator i, end;
  47. for (boost::tie(i, end) = edges(g); i != end; ++i) {
  48. int f_i = get(index, source(*i, g));
  49. int f_j = get(index, target(*i, g));
  50. b = max BOOST_PREVENT_MACRO_SUBSTITUTION (b, vertices_size_type(abs(f_i - f_j)));
  51. }
  52. return b;
  53. }
  54. template <typename Graph>
  55. typename graph_traits<Graph>::vertices_size_type
  56. bandwidth(const Graph& g)
  57. {
  58. return bandwidth(g, get(vertex_index, g));
  59. }
  60. template <typename Graph, typename VertexIndexMap>
  61. typename graph_traits<Graph>::vertices_size_type
  62. edgesum(const Graph& g, VertexIndexMap index_map)
  63. {
  64. typedef typename graph_traits<Graph>::vertices_size_type size_type;
  65. typedef typename detail::numeric_traits<size_type>::difference_type diff_t;
  66. size_type sum = 0;
  67. typename graph_traits<Graph>::edge_iterator i, end;
  68. for (boost::tie(i, end) = edges(g); i != end; ++i) {
  69. diff_t f_u = get(index_map, source(*i, g));
  70. diff_t f_v = get(index_map, target(*i, g));
  71. using namespace std; // to call abs() unqualified
  72. sum += abs(f_u - f_v);
  73. }
  74. return sum;
  75. }
  76. } // namespace boost
  77. #endif // BOOST_GRAPH_BANDWIDTH_HPP