// (C) Copyright Andrew Sutton 2007 // // 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_GEODESIC_DISTANCE_HPP #define BOOST_GRAPH_GEODESIC_DISTANCE_HPP #include #include namespace boost { template > struct mean_geodesic_measure : public geodesic_measure { typedef geodesic_measure 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& g) { function_requires< VertexListGraphConcept >(); function_requires< NumericValueConcept >(); function_requires< NumericValueConcept >(); function_requires< AdaptableBinaryFunctionConcept >(); return (d == base_type::infinite_distance()) ? base_type::infinite_result() : div(result_type(d), result_type(num_vertices(g) - 1)); } Divides div; }; template inline mean_geodesic_measure::value_type, double> measure_mean_geodesic(const Graph&, DistanceMap) { return mean_geodesic_measure::value_type, double>(); } template inline mean_geodesic_measure::value_type, T> measure_mean_geodesic(const Graph&, DistanceMap) { return mean_geodesic_measure::value_type, T>(); } // This is a little different because it's expected that the result type // should (must?) be the same as the distance type. There's a type of // transitivity in this thinking... If the average of distances has type // X then the average of x's should also be type X. Is there a case where this // is not true? // // This type is a little under-genericized... It needs generic parameters // for addition and division. template struct mean_graph_distance_measure : public geodesic_measure { typedef geodesic_measure base_type; typedef typename base_type::distance_type distance_type; typedef typename base_type::result_type result_type; inline result_type operator ()(distance_type d, const Graph& g) { function_requires< VertexListGraphConcept >(); function_requires< NumericValueConcept >(); if(d == base_type::infinite_distance()) { return base_type::infinite_result(); } else { return d / result_type(num_vertices(g)); } } }; template inline mean_graph_distance_measure::value_type> measure_graph_mean_geodesic(const Graph&, DistanceMap) { typedef typename property_traits::value_type T; return mean_graph_distance_measure(); } template inline typename Measure::result_type mean_geodesic(const Graph& g, DistanceMap dist, Measure measure, Combinator combine) { function_requires< DistanceMeasureConcept >(); typedef typename Measure::distance_type Distance; Distance n = detail::combine_distances(g, dist, combine, Distance(0)); return measure(n, g); } template inline typename Measure::result_type mean_geodesic(const Graph& g, DistanceMap dist, Measure measure) { function_requires< DistanceMeasureConcept >(); typedef typename Measure::distance_type Distance; return mean_geodesic(g, dist, measure, std::plus()); } template inline double mean_geodesic(const Graph& g, DistanceMap dist) { return mean_geodesic(g, dist, measure_mean_geodesic(g, dist)); } template inline T mean_geodesic(const Graph& g, DistanceMap dist) { return mean_geodesic(g, dist, measure_mean_geodesic(g, dist)); } template inline typename property_traits::value_type all_mean_geodesics(const Graph& g, DistanceMatrixMap dist, GeodesicMap geo, Measure measure) { function_requires< VertexListGraphConcept >(); typedef typename graph_traits::vertex_descriptor Vertex; typedef typename graph_traits::vertex_iterator VertexIterator; function_requires< ReadablePropertyMapConcept >(); typedef typename property_traits::value_type DistanceMap; function_requires< DistanceMeasureConcept >(); typedef typename Measure::result_type Result; function_requires< WritablePropertyMapConcept >(); function_requires< NumericValueConcept >(); // NOTE: We could compute the mean geodesic here by performing additional // computations (i.e., adding and dividing). However, I don't really feel // like fully genericizing the entire operation yet so I'm not going to. Result inf = numeric_values::infinity(); Result sum = numeric_values::zero(); VertexIterator i, end; for(boost::tie(i, end) = vertices(g); i != end; ++i) { DistanceMap dm = get(dist, *i); Result r = mean_geodesic(g, dm, measure); put(geo, *i, r); // compute the sum along with geodesics if(r == inf) { sum = inf; } else if(sum != inf) { sum += r; } } // return the average of averages. return sum / Result(num_vertices(g)); } template inline typename property_traits::value_type all_mean_geodesics(const Graph& g, DistanceMatrixMap dist, GeodesicMap geo) { function_requires< GraphConcept >(); typedef typename graph_traits::vertex_descriptor Vertex; function_requires< ReadablePropertyMapConcept >(); typedef typename property_traits::value_type DistanceMap; function_requires< WritablePropertyMapConcept >(); typedef typename property_traits::value_type Result; return all_mean_geodesics(g, dist, geo, measure_mean_geodesic(g, DistanceMap())); } template inline typename Measure::result_type small_world_distance(const Graph& g, GeodesicMap geo, Measure measure) { function_requires< DistanceMeasureConcept >(); typedef typename Measure::result_type Result; Result sum = detail::combine_distances(g, geo, std::plus(), Result(0)); return measure(sum, g); } template inline typename property_traits::value_type small_world_distance(const Graph& g, GeodesicMap geo) { return small_world_distance(g, geo, measure_graph_mean_geodesic(g, geo)); } } #endif