sl@0: // Copyright (c) Jeremy Siek 2001, Marc Wintermantel 2002
sl@0: //
sl@0: // Distributed under the Boost Software License, Version 1.0. (See
sl@0: // accompanying file LICENSE_1_0.txt or copy at
sl@0: // http://www.boost.org/LICENSE_1_0.txt)
sl@0: 
sl@0: #ifndef BOOST_GRAPH_BANDWIDTH_HPP
sl@0: #define BOOST_GRAPH_BANDWIDTH_HPP
sl@0: 
sl@0: #include <algorithm> // for std::min and std::max
sl@0: #include <boost/config.hpp>
sl@0: #include <boost/graph/graph_traits.hpp>
sl@0: #include <boost/detail/numeric_traits.hpp>
sl@0: 
sl@0: namespace boost {
sl@0: 
sl@0:   template <typename Graph, typename VertexIndexMap>
sl@0:   typename graph_traits<Graph>::vertices_size_type
sl@0:   ith_bandwidth(typename graph_traits<Graph>::vertex_descriptor i,
sl@0:                 const Graph& g,
sl@0:                 VertexIndexMap index)
sl@0:   {
sl@0:     BOOST_USING_STD_MAX();
sl@0:     typedef typename graph_traits<Graph>::vertices_size_type size_type;
sl@0:     size_type b = 0;
sl@0:     typename graph_traits<Graph>::out_edge_iterator e, end;
sl@0:     for (tie(e, end) = out_edges(i, g); e != end; ++e) {
sl@0:       int f_i = get(index, i);
sl@0:       int f_j = get(index, target(*e, g));
sl@0:       using namespace std; // to call abs() unqualified
sl@0:       if(f_i > f_j)
sl@0:           b = max BOOST_PREVENT_MACRO_SUBSTITUTION (b, size_type(f_i - f_j));
sl@0:     }
sl@0:     return b;
sl@0:   }
sl@0: 
sl@0:   template <typename Graph>
sl@0:   typename graph_traits<Graph>::vertices_size_type
sl@0:   ith_bandwidth(typename graph_traits<Graph>::vertex_descriptor i,
sl@0:                 const Graph& g)
sl@0:   {
sl@0:     return ith_bandwidth(i, g, get(vertex_index, g));
sl@0:   }
sl@0: 
sl@0:   template <typename Graph, typename VertexIndexMap>
sl@0:   typename graph_traits<Graph>::vertices_size_type
sl@0:   bandwidth(const Graph& g, VertexIndexMap index)
sl@0:   {
sl@0:     BOOST_USING_STD_MAX();
sl@0:     typename graph_traits<Graph>::vertices_size_type b = 0;
sl@0:     typename graph_traits<Graph>::vertex_iterator i, end;
sl@0:     for (tie(i, end) = vertices(g); i != end; ++i)
sl@0:         b = max BOOST_PREVENT_MACRO_SUBSTITUTION (b, ith_bandwidth(*i, g, index));
sl@0:     return b;
sl@0:   }
sl@0: 
sl@0:   template <typename Graph>
sl@0:   typename graph_traits<Graph>::vertices_size_type
sl@0:   bandwidth(const Graph& g)
sl@0:   {
sl@0:     return bandwidth(g, get(vertex_index, g));
sl@0:   }
sl@0: 
sl@0:   template <typename Graph, typename VertexIndexMap>
sl@0:   typename graph_traits<Graph>::vertices_size_type
sl@0:   edgesum(const Graph& g, VertexIndexMap index_map)
sl@0:   {
sl@0:     typedef typename graph_traits<Graph>::vertices_size_type size_type;
sl@0:     typedef typename detail::numeric_traits<size_type>::difference_type diff_t;
sl@0:     size_type sum = 0;
sl@0:     typename graph_traits<Graph>::edge_iterator i, end;
sl@0:     for (tie(i, end) = edges(g); i != end; ++i) {
sl@0:       diff_t f_u = get(index_map, source(*i, g));
sl@0:       diff_t f_v = get(index_map, target(*i, g));
sl@0:       using namespace std; // to call abs() unqualified
sl@0:       sum += abs(f_u - f_v);
sl@0:     }
sl@0:     return sum;
sl@0:   }
sl@0:   
sl@0: } // namespace boost
sl@0: 
sl@0: #endif // BOOST_GRAPH_BANDWIDTH_HPP