Chris@16
|
1 // Copyright 2004 The Trustees of Indiana University.
|
Chris@16
|
2
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // Authors: Douglas Gregor
|
Chris@16
|
8 // Andrew Lumsdaine
|
Chris@16
|
9 #ifndef BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
|
Chris@16
|
10 #define BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/graph/betweenness_centrality.hpp>
|
Chris@16
|
13 #include <boost/graph/graph_traits.hpp>
|
Chris@16
|
14 #include <boost/graph/graph_utility.hpp>
|
Chris@16
|
15 #include <boost/pending/indirect_cmp.hpp>
|
Chris@16
|
16 #include <algorithm>
|
Chris@16
|
17 #include <vector>
|
Chris@16
|
18 #include <boost/property_map/property_map.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost {
|
Chris@16
|
21
|
Chris@16
|
22 /** Threshold termination function for the betweenness centrality
|
Chris@16
|
23 * clustering algorithm.
|
Chris@16
|
24 */
|
Chris@16
|
25 template<typename T>
|
Chris@16
|
26 struct bc_clustering_threshold
|
Chris@16
|
27 {
|
Chris@16
|
28 typedef T centrality_type;
|
Chris@16
|
29
|
Chris@16
|
30 /// Terminate clustering when maximum absolute edge centrality is
|
Chris@16
|
31 /// below the given threshold.
|
Chris@16
|
32 explicit bc_clustering_threshold(T threshold)
|
Chris@16
|
33 : threshold(threshold), dividend(1.0) {}
|
Chris@16
|
34
|
Chris@16
|
35 /**
|
Chris@16
|
36 * Terminate clustering when the maximum edge centrality is below
|
Chris@16
|
37 * the given threshold.
|
Chris@16
|
38 *
|
Chris@16
|
39 * @param threshold the threshold value
|
Chris@16
|
40 *
|
Chris@16
|
41 * @param g the graph on which the threshold will be calculated
|
Chris@16
|
42 *
|
Chris@16
|
43 * @param normalize when true, the threshold is compared against the
|
Chris@16
|
44 * normalized edge centrality based on the input graph; otherwise,
|
Chris@16
|
45 * the threshold is compared against the absolute edge centrality.
|
Chris@16
|
46 */
|
Chris@16
|
47 template<typename Graph>
|
Chris@16
|
48 bc_clustering_threshold(T threshold, const Graph& g, bool normalize = true)
|
Chris@16
|
49 : threshold(threshold), dividend(1.0)
|
Chris@16
|
50 {
|
Chris@16
|
51 if (normalize) {
|
Chris@16
|
52 typename graph_traits<Graph>::vertices_size_type n = num_vertices(g);
|
Chris@16
|
53 dividend = T((n - 1) * (n - 2)) / T(2);
|
Chris@16
|
54 }
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57 /** Returns true when the given maximum edge centrality (potentially
|
Chris@16
|
58 * normalized) falls below the threshold.
|
Chris@16
|
59 */
|
Chris@16
|
60 template<typename Graph, typename Edge>
|
Chris@16
|
61 bool operator()(T max_centrality, Edge, const Graph&)
|
Chris@16
|
62 {
|
Chris@16
|
63 return (max_centrality / dividend) < threshold;
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 protected:
|
Chris@16
|
67 T threshold;
|
Chris@16
|
68 T dividend;
|
Chris@16
|
69 };
|
Chris@16
|
70
|
Chris@16
|
71 /** Graph clustering based on edge betweenness centrality.
|
Chris@16
|
72 *
|
Chris@16
|
73 * This algorithm implements graph clustering based on edge
|
Chris@16
|
74 * betweenness centrality. It is an iterative algorithm, where in each
|
Chris@16
|
75 * step it compute the edge betweenness centrality (via @ref
|
Chris@16
|
76 * brandes_betweenness_centrality) and removes the edge with the
|
Chris@16
|
77 * maximum betweenness centrality. The @p done function object
|
Chris@16
|
78 * determines when the algorithm terminates (the edge found when the
|
Chris@16
|
79 * algorithm terminates will not be removed).
|
Chris@16
|
80 *
|
Chris@16
|
81 * @param g The graph on which clustering will be performed. The type
|
Chris@16
|
82 * of this parameter (@c MutableGraph) must be a model of the
|
Chris@16
|
83 * VertexListGraph, IncidenceGraph, EdgeListGraph, and Mutable Graph
|
Chris@16
|
84 * concepts.
|
Chris@16
|
85 *
|
Chris@16
|
86 * @param done The function object that indicates termination of the
|
Chris@16
|
87 * algorithm. It must be a ternary function object thats accepts the
|
Chris@16
|
88 * maximum centrality, the descriptor of the edge that will be
|
Chris@16
|
89 * removed, and the graph @p g.
|
Chris@16
|
90 *
|
Chris@16
|
91 * @param edge_centrality (UTIL/OUT) The property map that will store
|
Chris@16
|
92 * the betweenness centrality for each edge. When the algorithm
|
Chris@16
|
93 * terminates, it will contain the edge centralities for the
|
Chris@16
|
94 * graph. The type of this property map must model the
|
Chris@16
|
95 * ReadWritePropertyMap concept. Defaults to an @c
|
Chris@16
|
96 * iterator_property_map whose value type is
|
Chris@16
|
97 * @c Done::centrality_type and using @c get(edge_index, g) for the
|
Chris@16
|
98 * index map.
|
Chris@16
|
99 *
|
Chris@16
|
100 * @param vertex_index (IN) The property map that maps vertices to
|
Chris@16
|
101 * indices in the range @c [0, num_vertices(g)). This type of this
|
Chris@16
|
102 * property map must model the ReadablePropertyMap concept and its
|
Chris@16
|
103 * value type must be an integral type. Defaults to
|
Chris@16
|
104 * @c get(vertex_index, g).
|
Chris@16
|
105 */
|
Chris@16
|
106 template<typename MutableGraph, typename Done, typename EdgeCentralityMap,
|
Chris@16
|
107 typename VertexIndexMap>
|
Chris@16
|
108 void
|
Chris@16
|
109 betweenness_centrality_clustering(MutableGraph& g, Done done,
|
Chris@16
|
110 EdgeCentralityMap edge_centrality,
|
Chris@16
|
111 VertexIndexMap vertex_index)
|
Chris@16
|
112 {
|
Chris@16
|
113 typedef typename property_traits<EdgeCentralityMap>::value_type
|
Chris@16
|
114 centrality_type;
|
Chris@16
|
115 typedef typename graph_traits<MutableGraph>::edge_iterator edge_iterator;
|
Chris@16
|
116 typedef typename graph_traits<MutableGraph>::edge_descriptor edge_descriptor;
|
Chris@16
|
117 typedef typename graph_traits<MutableGraph>::vertices_size_type
|
Chris@16
|
118 vertices_size_type;
|
Chris@16
|
119
|
Chris@16
|
120 if (has_no_edges(g)) return;
|
Chris@16
|
121
|
Chris@16
|
122 // Function object that compares the centrality of edges
|
Chris@16
|
123 indirect_cmp<EdgeCentralityMap, std::less<centrality_type> >
|
Chris@16
|
124 cmp(edge_centrality);
|
Chris@16
|
125
|
Chris@16
|
126 bool is_done;
|
Chris@16
|
127 do {
|
Chris@16
|
128 brandes_betweenness_centrality(g,
|
Chris@16
|
129 edge_centrality_map(edge_centrality)
|
Chris@16
|
130 .vertex_index_map(vertex_index));
|
Chris@16
|
131 std::pair<edge_iterator, edge_iterator> edges_iters = edges(g);
|
Chris@16
|
132 edge_descriptor e = *max_element(edges_iters.first, edges_iters.second, cmp);
|
Chris@16
|
133 is_done = done(get(edge_centrality, e), e, g);
|
Chris@16
|
134 if (!is_done) remove_edge(e, g);
|
Chris@16
|
135 } while (!is_done && !has_no_edges(g));
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 /**
|
Chris@16
|
139 * \overload
|
Chris@16
|
140 */
|
Chris@16
|
141 template<typename MutableGraph, typename Done, typename EdgeCentralityMap>
|
Chris@16
|
142 void
|
Chris@16
|
143 betweenness_centrality_clustering(MutableGraph& g, Done done,
|
Chris@16
|
144 EdgeCentralityMap edge_centrality)
|
Chris@16
|
145 {
|
Chris@16
|
146 betweenness_centrality_clustering(g, done, edge_centrality,
|
Chris@16
|
147 get(vertex_index, g));
|
Chris@16
|
148 }
|
Chris@16
|
149
|
Chris@16
|
150 /**
|
Chris@16
|
151 * \overload
|
Chris@16
|
152 */
|
Chris@16
|
153 template<typename MutableGraph, typename Done>
|
Chris@16
|
154 void
|
Chris@16
|
155 betweenness_centrality_clustering(MutableGraph& g, Done done)
|
Chris@16
|
156 {
|
Chris@16
|
157 typedef typename Done::centrality_type centrality_type;
|
Chris@16
|
158 std::vector<centrality_type> edge_centrality(num_edges(g));
|
Chris@16
|
159 betweenness_centrality_clustering(g, done,
|
Chris@16
|
160 make_iterator_property_map(edge_centrality.begin(), get(edge_index, g)),
|
Chris@16
|
161 get(vertex_index, g));
|
Chris@16
|
162 }
|
Chris@16
|
163
|
Chris@16
|
164 } // end namespace boost
|
Chris@16
|
165
|
Chris@16
|
166 #endif // BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
|