Chris@16
|
1 // (C) Copyright 2007 Andrew Sutton
|
Chris@16
|
2 //
|
Chris@16
|
3 // Use, modification and distribution are subject to the
|
Chris@16
|
4 // Boost Software License, Version 1.0 (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 #ifndef BOOST_GRAPH_DETAIL_GEODESIC_HPP
|
Chris@16
|
8 #define BOOST_GRAPH_DETAIL_GEODESIC_HPP
|
Chris@16
|
9
|
Chris@16
|
10 #include <functional>
|
Chris@16
|
11 #include <boost/config.hpp>
|
Chris@16
|
12 #include <boost/graph/graph_concepts.hpp>
|
Chris@16
|
13 #include <boost/graph/numeric_values.hpp>
|
Chris@16
|
14 #include <boost/concept/assert.hpp>
|
Chris@16
|
15
|
Chris@16
|
16 // TODO: Should this really be in detail?
|
Chris@16
|
17
|
Chris@16
|
18 namespace boost
|
Chris@16
|
19 {
|
Chris@16
|
20 // This is a very good discussion on centrality measures. While I can't
|
Chris@16
|
21 // say that this has been the motivating factor for the design and
|
Chris@16
|
22 // implementation of ths centrality framework, it does provide a single
|
Chris@16
|
23 // point of reference for defining things like degree and closeness
|
Chris@16
|
24 // centrality. Plus, the bibliography seems fairly complete.
|
Chris@16
|
25 //
|
Chris@16
|
26 // @article{citeulike:1144245,
|
Chris@16
|
27 // author = {Borgatti, Stephen P. and Everett, Martin G.},
|
Chris@16
|
28 // citeulike-article-id = {1144245},
|
Chris@16
|
29 // doi = {10.1016/j.socnet.2005.11.005},
|
Chris@16
|
30 // journal = {Social Networks},
|
Chris@16
|
31 // month = {October},
|
Chris@16
|
32 // number = {4},
|
Chris@16
|
33 // pages = {466--484},
|
Chris@16
|
34 // priority = {0},
|
Chris@16
|
35 // title = {A Graph-theoretic perspective on centrality},
|
Chris@16
|
36 // url = {http://dx.doi.org/10.1016/j.socnet.2005.11.005},
|
Chris@16
|
37 // volume = {28},
|
Chris@16
|
38 // year = {2006}
|
Chris@16
|
39 // }
|
Chris@16
|
40 // }
|
Chris@16
|
41
|
Chris@16
|
42 namespace detail {
|
Chris@16
|
43 // Note that this assumes T == property_traits<DistanceMap>::value_type
|
Chris@16
|
44 // and that the args and return of combine are also T.
|
Chris@16
|
45 template <typename Graph,
|
Chris@16
|
46 typename DistanceMap,
|
Chris@16
|
47 typename Combinator,
|
Chris@16
|
48 typename Distance>
|
Chris@16
|
49 inline Distance
|
Chris@16
|
50 combine_distances(const Graph& g,
|
Chris@16
|
51 DistanceMap dist,
|
Chris@16
|
52 Combinator combine,
|
Chris@16
|
53 Distance init)
|
Chris@16
|
54 {
|
Chris@16
|
55 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph> ));
|
Chris@16
|
56 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
|
Chris@16
|
57 typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
|
Chris@16
|
58 BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<DistanceMap,Vertex> ));
|
Chris@16
|
59 BOOST_CONCEPT_ASSERT(( NumericValueConcept<Distance> ));
|
Chris@16
|
60 typedef numeric_values<Distance> DistanceNumbers;
|
Chris@16
|
61 BOOST_CONCEPT_ASSERT(( AdaptableBinaryFunction<Combinator,Distance,Distance,Distance> ));
|
Chris@16
|
62
|
Chris@16
|
63 // If there's ever an infinite distance, then we simply return
|
Chris@16
|
64 // infinity. Note that this /will/ include the a non-zero
|
Chris@16
|
65 // distance-to-self in the combined values. However, this is usually
|
Chris@16
|
66 // zero, so it shouldn't be too problematic.
|
Chris@16
|
67 Distance ret = init;
|
Chris@16
|
68 VertexIterator i, end;
|
Chris@16
|
69 for(boost::tie(i, end) = vertices(g); i != end; ++i) {
|
Chris@16
|
70 Vertex v = *i;
|
Chris@16
|
71 if(get(dist, v) != DistanceNumbers::infinity()) {
|
Chris@16
|
72 ret = combine(ret, get(dist, v));
|
Chris@16
|
73 }
|
Chris@16
|
74 else {
|
Chris@16
|
75 ret = DistanceNumbers::infinity();
|
Chris@16
|
76 break;
|
Chris@16
|
77 }
|
Chris@16
|
78 }
|
Chris@16
|
79 return ret;
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 // Similar to std::plus<T>, but maximizes parameters
|
Chris@16
|
83 // rather than adding them.
|
Chris@16
|
84 template <typename T>
|
Chris@16
|
85 struct maximize : public std::binary_function<T, T, T>
|
Chris@16
|
86 {
|
Chris@16
|
87 T operator ()(T x, T y) const
|
Chris@16
|
88 { BOOST_USING_STD_MAX(); return max BOOST_PREVENT_MACRO_SUBSTITUTION (x, y); }
|
Chris@16
|
89 };
|
Chris@16
|
90
|
Chris@16
|
91 // Another helper, like maximize() to help abstract functional
|
Chris@16
|
92 // concepts. This is trivially instantiated for builtin numeric
|
Chris@16
|
93 // types, but should be specialized for those types that have
|
Chris@16
|
94 // discrete notions of reciprocals.
|
Chris@16
|
95 template <typename T>
|
Chris@16
|
96 struct reciprocal : public std::unary_function<T, T>
|
Chris@16
|
97 {
|
Chris@16
|
98 typedef std::unary_function<T, T> function_type;
|
Chris@16
|
99 typedef typename function_type::result_type result_type;
|
Chris@16
|
100 typedef typename function_type::argument_type argument_type;
|
Chris@16
|
101 T operator ()(T t)
|
Chris@16
|
102 { return T(1) / t; }
|
Chris@16
|
103 };
|
Chris@16
|
104 } /* namespace detail */
|
Chris@16
|
105
|
Chris@16
|
106 // This type defines the basic facilities used for computing values
|
Chris@16
|
107 // based on the geodesic distances between vertices. Examples include
|
Chris@16
|
108 // closeness centrality and mean geodesic distance.
|
Chris@16
|
109 template <typename Graph, typename DistanceType, typename ResultType>
|
Chris@16
|
110 struct geodesic_measure
|
Chris@16
|
111 {
|
Chris@16
|
112 typedef DistanceType distance_type;
|
Chris@16
|
113 typedef ResultType result_type;
|
Chris@16
|
114 typedef typename graph_traits<Graph>::vertices_size_type size_type;
|
Chris@16
|
115
|
Chris@16
|
116 typedef numeric_values<distance_type> distance_values;
|
Chris@16
|
117 typedef numeric_values<result_type> result_values;
|
Chris@16
|
118
|
Chris@16
|
119 static inline distance_type infinite_distance()
|
Chris@16
|
120 { return distance_values::infinity(); }
|
Chris@16
|
121
|
Chris@16
|
122 static inline result_type infinite_result()
|
Chris@16
|
123 { return result_values::infinity(); }
|
Chris@16
|
124
|
Chris@16
|
125 static inline result_type zero_result()
|
Chris@16
|
126 { return result_values::zero(); }
|
Chris@16
|
127 };
|
Chris@16
|
128
|
Chris@16
|
129 } /* namespace boost */
|
Chris@16
|
130
|
Chris@16
|
131 #endif
|