// (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_CLOSENESS_CENTRALITY_HPP #define BOOST_GRAPH_CLOSENESS_CENTRALITY_HPP #include #include #include namespace boost { template > struct closeness_measure : public geodesic_measure { typedef geodesic_measure< Graph, DistanceType, ResultType> base_type; typedef typename base_type::distance_type distance_type; typedef typename base_type::result_type result_type; result_type operator ()(distance_type d, const Graph&) { BOOST_CONCEPT_ASSERT(( NumericValueConcept )); BOOST_CONCEPT_ASSERT(( NumericValueConcept )); BOOST_CONCEPT_ASSERT(( AdaptableUnaryFunctionConcept )); return (d == base_type::infinite_distance()) ? base_type::zero_result() : rec(result_type(d)); } Reciprocal rec; }; template inline closeness_measure< Graph, typename property_traits::value_type, double, detail::reciprocal > measure_closeness(const Graph&, DistanceMap) { typedef typename property_traits::value_type Distance; return closeness_measure >(); } template inline closeness_measure< Graph, typename property_traits::value_type, T, detail::reciprocal > measure_closeness(const Graph&, DistanceMap) { typedef typename property_traits::value_type Distance; return closeness_measure >(); } template inline closeness_measure< Graph, typename property_traits::value_type, T, Reciprocal> measure_closeness(const Graph&, DistanceMap) { typedef typename property_traits::value_type Distance; return closeness_measure(); } template inline typename Measure::result_type closeness_centrality(const Graph& g, DistanceMap dist, Measure measure, Combinator combine) { BOOST_CONCEPT_ASSERT(( VertexListGraphConcept )); typedef typename graph_traits::vertex_descriptor Vertex; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type Distance; BOOST_CONCEPT_ASSERT(( NumericValueConcept )); BOOST_CONCEPT_ASSERT(( DistanceMeasureConcept )); Distance n = detail::combine_distances(g, dist, combine, Distance(0)); return measure(n, g); } template inline typename Measure::result_type closeness_centrality(const Graph& g, DistanceMap dist, Measure measure) { BOOST_CONCEPT_ASSERT(( GraphConcept )); typedef typename graph_traits::vertex_descriptor Vertex; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type Distance; return closeness_centrality(g, dist, measure, std::plus()); } template inline double closeness_centrality(const Graph& g, DistanceMap dist) { return closeness_centrality(g, dist, measure_closeness(g, dist)); } template inline T closeness_centrality(const Graph& g, DistanceMap dist) { return closeness_centrality(g, dist, measure_closeness(g, dist)); } template inline void all_closeness_centralities(const Graph& g, DistanceMatrixMap dist, CentralityMap cent, Measure measure) { BOOST_CONCEPT_ASSERT(( VertexListGraphConcept )); typedef typename graph_traits::vertex_descriptor Vertex; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type DistanceMap; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); BOOST_CONCEPT_ASSERT(( WritablePropertyMapConcept )); typedef typename property_traits::value_type Distance; typedef typename property_traits::value_type Centrality; typename graph_traits::vertex_iterator i, end; for(boost::tie(i, end) = vertices(g); i != end; ++i) { DistanceMap dm = get(dist, *i); Centrality c = closeness_centrality(g, dm, measure); put(cent, *i, c); } } template inline void all_closeness_centralities(const Graph& g, DistanceMatrixMap dist, CentralityMap cent) { BOOST_CONCEPT_ASSERT(( GraphConcept )); typedef typename graph_traits::vertex_descriptor Vertex; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type DistanceMap; BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); typedef typename property_traits::value_type Distance; typedef typename property_traits::value_type Result; all_closeness_centralities(g, dist, cent, measure_closeness(g, DistanceMap())); } } /* namespace boost */ #endif