PageRenderTime 40ms CodeModel.GetById 14ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/graph/closeness_centrality.hpp

http://hadesmem.googlecode.com/
C++ Header | 157 lines | 133 code | 19 blank | 5 comment | 3 complexity | 6eb264fa29f039df4f443b66e614a6c9 MD5 | raw file
  1// (C) Copyright 2007-2009 Andrew Sutton
  2//
  3// Use, modification and distribution are subject to the
  4// Boost Software License, Version 1.0 (See accompanying file
  5// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6
  7#ifndef BOOST_GRAPH_CLOSENESS_CENTRALITY_HPP
  8#define BOOST_GRAPH_CLOSENESS_CENTRALITY_HPP
  9
 10#include <boost/graph/detail/geodesic.hpp>
 11#include <boost/graph/exterior_property.hpp>
 12
 13namespace boost
 14{
 15template <typename Graph,
 16          typename DistanceType,
 17          typename ResultType,
 18          typename Reciprocal = detail::reciprocal<ResultType> >
 19struct closeness_measure
 20    : public geodesic_measure<Graph, DistanceType, ResultType>
 21{
 22    typedef geodesic_measure< Graph, DistanceType, ResultType> base_type;
 23    typedef typename base_type::distance_type distance_type;
 24    typedef typename base_type::result_type result_type;
 25
 26    result_type operator ()(distance_type d, const Graph&)
 27    {
 28        function_requires< NumericValueConcept<DistanceType> >();
 29        function_requires< NumericValueConcept<ResultType> >();
 30        function_requires< AdaptableUnaryFunctionConcept<Reciprocal,ResultType,ResultType> >();
 31        return (d == base_type::infinite_distance())
 32            ? base_type::zero_result()
 33            : rec(result_type(d));
 34    }
 35    Reciprocal rec;
 36};
 37
 38template <typename Graph, typename DistanceMap>
 39inline closeness_measure<
 40        Graph, typename property_traits<DistanceMap>::value_type, double,
 41        detail::reciprocal<double> >
 42measure_closeness(const Graph&, DistanceMap)
 43{
 44    typedef typename property_traits<DistanceMap>::value_type Distance;
 45    return closeness_measure<Graph, Distance, double, detail::reciprocal<double> >();
 46}
 47
 48template <typename T, typename Graph, typename DistanceMap>
 49inline closeness_measure<
 50        Graph, typename property_traits<DistanceMap>::value_type, T,
 51        detail::reciprocal<T> >
 52measure_closeness(const Graph&, DistanceMap)
 53{
 54    typedef typename property_traits<DistanceMap>::value_type Distance;
 55    return closeness_measure<Graph, Distance, T, detail::reciprocal<T> >();
 56}
 57
 58template <typename T, typename Graph, typename DistanceMap, typename Reciprocal>
 59inline closeness_measure<
 60        Graph, typename property_traits<DistanceMap>::value_type, T,
 61        Reciprocal>
 62measure_closeness(const Graph&, DistanceMap)
 63{
 64    typedef typename property_traits<DistanceMap>::value_type Distance;
 65    return closeness_measure<Graph, Distance, T, Reciprocal>();
 66}
 67
 68template <typename Graph,
 69          typename DistanceMap,
 70          typename Measure,
 71          typename Combinator>
 72inline typename Measure::result_type
 73closeness_centrality(const Graph& g,
 74                     DistanceMap dist,
 75                     Measure measure,
 76                     Combinator combine)
 77{
 78    function_requires< VertexListGraphConcept<Graph> >();
 79    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
 80    function_requires< ReadablePropertyMapConcept<DistanceMap,Vertex> >();
 81    typedef typename property_traits<DistanceMap>::value_type Distance;
 82    function_requires< NumericValueConcept<Distance> >();
 83    function_requires< DistanceMeasureConcept<Measure,Graph> >();
 84
 85    Distance n = detail::combine_distances(g, dist, combine, Distance(0));
 86    return measure(n, g);
 87}
 88
 89template <typename Graph, typename DistanceMap, typename Measure>
 90inline typename Measure::result_type
 91closeness_centrality(const Graph& g, DistanceMap dist, Measure measure)
 92{
 93    function_requires< GraphConcept<Graph> >();
 94    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
 95    function_requires< ReadablePropertyMapConcept<DistanceMap,Vertex> >();
 96    typedef typename property_traits<DistanceMap>::value_type Distance;
 97
 98    return closeness_centrality(g, dist, measure, std::plus<Distance>());
 99}
100
101template <typename Graph, typename DistanceMap>
102inline double closeness_centrality(const Graph& g, DistanceMap dist)
103{ return closeness_centrality(g, dist, measure_closeness(g, dist)); }
104
105template <typename T, typename Graph, typename DistanceMap>
106inline T closeness_centrality(const Graph& g, DistanceMap dist)
107{ return closeness_centrality(g, dist, measure_closeness<T>(g, dist)); }
108
109template <typename Graph,
110          typename DistanceMatrixMap,
111          typename CentralityMap,
112          typename Measure>
113inline void
114all_closeness_centralities(const Graph& g,
115                           DistanceMatrixMap dist,
116                           CentralityMap cent,
117                           Measure measure)
118{
119    function_requires< VertexListGraphConcept<Graph> >();
120    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
121    function_requires< ReadablePropertyMapConcept<DistanceMatrixMap,Vertex> >();
122    typedef typename property_traits<DistanceMatrixMap>::value_type DistanceMap;
123    function_requires< ReadablePropertyMapConcept<DistanceMap,Vertex> >();
124    function_requires< WritablePropertyMapConcept<CentralityMap,Vertex> >();
125    typedef typename property_traits<DistanceMap>::value_type Distance;
126    typedef typename property_traits<CentralityMap>::value_type Centrality;
127
128    typename graph_traits<Graph>::vertex_iterator i, end;
129    for(tie(i, end) = vertices(g); i != end; ++i) {
130        DistanceMap dm = get(dist, *i);
131        Centrality c = closeness_centrality(g, dm, measure);
132        put(cent, *i, c);
133    }
134}
135
136template <typename Graph,
137          typename DistanceMatrixMap,
138          typename CentralityMap>
139inline void
140all_closeness_centralities(const Graph& g,
141                            DistanceMatrixMap dist,
142                            CentralityMap cent)
143{
144    function_requires< GraphConcept<Graph> >();
145    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
146    function_requires< ReadablePropertyMapConcept<DistanceMatrixMap,Vertex> >();
147    typedef typename property_traits<DistanceMatrixMap>::value_type DistanceMap;
148    function_requires< ReadablePropertyMapConcept<DistanceMap,Vertex> >();
149    typedef typename property_traits<DistanceMap>::value_type Distance;
150    typedef typename property_traits<CentralityMap>::value_type Result;
151
152    all_closeness_centralities(g, dist, cent, measure_closeness<Result>(g, DistanceMap()));
153}
154
155} /* namespace boost */
156
157#endif