// (C) Copyright 2007-2009 Andrew Sutton // // Use, modification and distribution are subject to the // Boost Software License, Version 1.0 (See accompanying file // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_GRAPH_ECCENTRICITY_HPP #define BOOST_GRAPH_ECCENTRICITY_HPP #include #include #include #include namespace boost { template inline typename property_traits::value_type eccentricity(const Graph& g, DistanceMap dist, Combinator combine) { BOOST_CONCEPT_ASSERT(( GraphConcept )); typedef typename graph_traits::vertex_descriptor Vertex; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type Distance; return detail::combine_distances(g, dist, combine, Distance(0)); } template inline typename property_traits::value_type eccentricity(const Graph& g, DistanceMap dist) { BOOST_CONCEPT_ASSERT(( GraphConcept )); typedef typename graph_traits::vertex_descriptor Vertex; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type Distance; return eccentricity(g, dist, detail::maximize()); } template inline std::pair::value_type, typename property_traits::value_type> all_eccentricities(const Graph& g, const DistanceMatrix& dist, EccentricityMap ecc) { BOOST_CONCEPT_ASSERT(( VertexListGraphConcept )); typedef typename graph_traits::vertex_descriptor Vertex; typedef typename graph_traits::vertex_iterator VertexIterator; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type DistanceMap; BOOST_CONCEPT_ASSERT(( WritablePropertyMapConcept )); typedef typename property_traits::value_type Eccentricity; BOOST_USING_STD_MIN(); BOOST_USING_STD_MAX(); Eccentricity r = numeric_values::infinity(), d = numeric_values::zero(); VertexIterator i, end; boost::tie(i, end) = vertices(g); for(boost::tie(i, end) = vertices(g); i != end; ++i) { DistanceMap dm = get(dist, *i); Eccentricity e = eccentricity(g, dm); put(ecc, *i, e); // track the radius and diameter at the same time r = min BOOST_PREVENT_MACRO_SUBSTITUTION (r, e); d = max BOOST_PREVENT_MACRO_SUBSTITUTION (d, e); } return std::make_pair(r, d); } template inline std::pair::value_type, typename property_traits::value_type> radius_and_diameter(const Graph& g, EccentricityMap ecc) { BOOST_CONCEPT_ASSERT(( VertexListGraphConcept )); typedef typename graph_traits::vertex_descriptor Vertex; typedef typename graph_traits::vertex_iterator VertexIterator; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type Eccentricity; BOOST_USING_STD_MIN(); BOOST_USING_STD_MAX(); VertexIterator i, end; boost::tie(i, end) = vertices(g); Eccentricity radius = get(ecc, *i); Eccentricity diameter = get(ecc, *i); for(i = boost::next(i); i != end; ++i) { Eccentricity cur = get(ecc, *i); radius = min BOOST_PREVENT_MACRO_SUBSTITUTION (radius, cur); diameter = max BOOST_PREVENT_MACRO_SUBSTITUTION (diameter, cur); } return std::make_pair(radius, diameter); } template inline typename property_traits::value_type radius(const Graph& g, EccentricityMap ecc) { return radius_and_diameter(g, ecc).first; } template inline typename property_traits::value_type diameter(const Graph& g, EccentricityMap ecc) { return radius_and_diameter(g, ecc).second; } } /* namespace boost */ #endif