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