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