bandwidth.hpp 2.7 KB

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