bandwidth.hpp 2.9 KB

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