Chris@16
|
1 //
|
Chris@16
|
2 //=======================================================================
|
Chris@16
|
3 // Copyright 2012 Fernando Vilas
|
Chris@16
|
4 // 2010 Daniel Trebbien
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
7 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //=======================================================================
|
Chris@16
|
10 //
|
Chris@16
|
11
|
Chris@16
|
12 // The maximum adjacency search algorithm was originally part of the
|
Chris@16
|
13 // Stoer-Wagner min cut implementation by Daniel Trebbien. It has been
|
Chris@16
|
14 // broken out into its own file to be a public search algorithm, with
|
Chris@16
|
15 // visitor concepts.
|
Chris@16
|
16 #ifndef BOOST_GRAPH_MAXIMUM_ADJACENCY_SEARCH_H
|
Chris@16
|
17 #define BOOST_GRAPH_MAXIMUM_ADJACENCY_SEARCH_H
|
Chris@16
|
18
|
Chris@16
|
19 /**
|
Chris@16
|
20 * This is an implementation of the maximum adjacency search on an
|
Chris@16
|
21 * undirected graph. It allows a visitor object to perform some
|
Chris@16
|
22 * operation on each vertex as that vertex is visited.
|
Chris@16
|
23 *
|
Chris@16
|
24 * The algorithm runs as follows:
|
Chris@16
|
25 *
|
Chris@16
|
26 * Initialize all nodes to be unvisited (reach count = 0)
|
Chris@16
|
27 * and call vis.initialize_vertex
|
Chris@16
|
28 * For i = number of nodes in graph downto 1
|
Chris@16
|
29 * Select the unvisited node with the highest reach count
|
Chris@16
|
30 * The user provides the starting node to break the first tie,
|
Chris@16
|
31 * but future ties are broken arbitrarily
|
Chris@16
|
32 * Visit the node by calling vis.start_vertex
|
Chris@16
|
33 * Increment the reach count for all unvisited neighbors
|
Chris@16
|
34 * and call vis.examine_edge for each of these edges
|
Chris@16
|
35 * Mark the node as visited and call vis.finish_vertex
|
Chris@16
|
36 *
|
Chris@16
|
37 */
|
Chris@16
|
38
|
Chris@16
|
39 #include <boost/concept_check.hpp>
|
Chris@16
|
40 #include <boost/concept/assert.hpp>
|
Chris@16
|
41 #include <boost/graph/buffer_concepts.hpp>
|
Chris@16
|
42 #include <boost/graph/exception.hpp>
|
Chris@16
|
43 #include <boost/graph/graph_concepts.hpp>
|
Chris@16
|
44 #include <boost/graph/iteration_macros.hpp>
|
Chris@16
|
45 #include <boost/graph/named_function_params.hpp>
|
Chris@16
|
46 #include <boost/graph/visitors.hpp>
|
Chris@16
|
47 #include <boost/tuple/tuple.hpp>
|
Chris@16
|
48
|
Chris@16
|
49 #include <set>
|
Chris@16
|
50
|
Chris@16
|
51 namespace boost {
|
Chris@16
|
52 template <class Visitor, class Graph>
|
Chris@16
|
53 struct MASVisitorConcept {
|
Chris@16
|
54 void constraints() {
|
Chris@16
|
55 boost::function_requires< boost::CopyConstructibleConcept<Visitor> >();
|
Chris@16
|
56 vis.initialize_vertex(u, g);
|
Chris@16
|
57 vis.start_vertex(u, g);
|
Chris@16
|
58 vis.examine_edge(e, g);
|
Chris@16
|
59 vis.finish_vertex(u, g);
|
Chris@16
|
60 }
|
Chris@16
|
61 Visitor vis;
|
Chris@16
|
62 Graph g;
|
Chris@16
|
63 typename boost::graph_traits<Graph>::vertex_descriptor u;
|
Chris@16
|
64 typename boost::graph_traits<Graph>::edge_descriptor e;
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67 template <class Visitors = null_visitor>
|
Chris@16
|
68 class mas_visitor {
|
Chris@16
|
69 public:
|
Chris@16
|
70 mas_visitor() { }
|
Chris@16
|
71 mas_visitor(Visitors vis) : m_vis(vis) { }
|
Chris@16
|
72
|
Chris@16
|
73 template <class Vertex, class Graph>
|
Chris@16
|
74 void
|
Chris@16
|
75 initialize_vertex(Vertex u, Graph& g)
|
Chris@16
|
76 {
|
Chris@16
|
77 invoke_visitors(m_vis, u, g, ::boost::on_initialize_vertex());
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 template <class Vertex, class Graph>
|
Chris@16
|
81 void
|
Chris@16
|
82 start_vertex(Vertex u, Graph& g)
|
Chris@16
|
83 {
|
Chris@16
|
84 invoke_visitors(m_vis, u, g, ::boost::on_start_vertex());
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 template <class Edge, class Graph>
|
Chris@16
|
88 void
|
Chris@16
|
89 examine_edge(Edge e, Graph& g)
|
Chris@16
|
90 {
|
Chris@16
|
91 invoke_visitors(m_vis, e, g, ::boost::on_examine_edge());
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 template <class Vertex, class Graph>
|
Chris@16
|
95 void
|
Chris@16
|
96 finish_vertex(Vertex u, Graph& g)
|
Chris@16
|
97 {
|
Chris@16
|
98 invoke_visitors(m_vis, u, g, ::boost::on_finish_vertex());
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 BOOST_GRAPH_EVENT_STUB(on_initialize_vertex,mas)
|
Chris@16
|
102 BOOST_GRAPH_EVENT_STUB(on_start_vertex,mas)
|
Chris@16
|
103 BOOST_GRAPH_EVENT_STUB(on_examine_edge,mas)
|
Chris@16
|
104 BOOST_GRAPH_EVENT_STUB(on_finish_vertex,mas)
|
Chris@16
|
105
|
Chris@16
|
106 protected:
|
Chris@16
|
107 Visitors m_vis;
|
Chris@16
|
108 };
|
Chris@16
|
109 template <class Visitors>
|
Chris@16
|
110 mas_visitor<Visitors>
|
Chris@16
|
111 make_mas_visitor(Visitors vis) {
|
Chris@16
|
112 return mas_visitor<Visitors>(vis);
|
Chris@16
|
113 }
|
Chris@16
|
114 typedef mas_visitor<> default_mas_visitor;
|
Chris@16
|
115
|
Chris@16
|
116 namespace detail {
|
Chris@16
|
117 template <class Graph, class WeightMap, class MASVisitor, class VertexAssignmentMap, class KeyedUpdatablePriorityQueue>
|
Chris@16
|
118 void
|
Chris@16
|
119 maximum_adjacency_search(const Graph& g, WeightMap weights, MASVisitor vis, const typename boost::graph_traits<Graph>::vertex_descriptor start, VertexAssignmentMap assignments, KeyedUpdatablePriorityQueue pq) {
|
Chris@16
|
120 typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
|
Chris@16
|
121 typedef typename boost::property_traits<WeightMap>::value_type weight_type;
|
Chris@16
|
122
|
Chris@16
|
123 std::set<vertex_descriptor> assignedVertices;
|
Chris@16
|
124
|
Chris@16
|
125 // initialize `assignments` (all vertices are initially
|
Chris@16
|
126 // assigned to themselves)
|
Chris@16
|
127 BGL_FORALL_VERTICES_T(v, g, Graph) {
|
Chris@16
|
128 put(assignments, v, v);
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 typename KeyedUpdatablePriorityQueue::key_map keys = pq.keys();
|
Chris@16
|
132
|
Chris@16
|
133 // set number of visited neighbors for all vertices to 0
|
Chris@16
|
134 BGL_FORALL_VERTICES_T(v, g, Graph) {
|
Chris@16
|
135 if (v == get(assignments, v)) { // foreach u \in V do
|
Chris@16
|
136 put(keys, v, weight_type(0)); vis.initialize_vertex(v, g);
|
Chris@16
|
137
|
Chris@16
|
138 pq.push(v);
|
Chris@16
|
139 }
|
Chris@16
|
140 }
|
Chris@16
|
141 BOOST_ASSERT(pq.size() >= 2);
|
Chris@16
|
142
|
Chris@16
|
143 // Give the starting vertex high priority
|
Chris@16
|
144 put(keys, start, get(keys, start) + num_vertices(g) + 1);
|
Chris@16
|
145 pq.update(start);
|
Chris@16
|
146
|
Chris@16
|
147 // start traversing the graph
|
Chris@16
|
148 //vertex_descriptor s, t;
|
Chris@16
|
149 weight_type w;
|
Chris@16
|
150 while (!pq.empty()) { // while PQ \neq {} do
|
Chris@16
|
151 const vertex_descriptor u = pq.top(); // u = extractmax(PQ)
|
Chris@16
|
152 w = get(keys, u); vis.start_vertex(u, g);
|
Chris@16
|
153 pq.pop(); // vis.start_vertex(u, g);
|
Chris@16
|
154
|
Chris@16
|
155 BGL_FORALL_OUTEDGES_T(u, e, g, Graph) { // foreach (u, v) \in E do
|
Chris@16
|
156 vis.examine_edge(e, g);
|
Chris@16
|
157
|
Chris@16
|
158 const vertex_descriptor v = get(assignments, target(e, g));
|
Chris@16
|
159
|
Chris@16
|
160 if (pq.contains(v)) { // if v \in PQ then
|
Chris@16
|
161 put(keys, v, get(keys, v) + get(weights, e)); // increasekey(PQ, v, wA(v) + w(u, v))
|
Chris@16
|
162 pq.update(v);
|
Chris@16
|
163 }
|
Chris@16
|
164 }
|
Chris@16
|
165
|
Chris@16
|
166 typename std::set<vertex_descriptor>::const_iterator assignedVertexIt, assignedVertexEnd = assignedVertices.end();
|
Chris@16
|
167 for (assignedVertexIt = assignedVertices.begin(); assignedVertexIt != assignedVertexEnd; ++assignedVertexIt) {
|
Chris@16
|
168 const vertex_descriptor uPrime = *assignedVertexIt;
|
Chris@16
|
169
|
Chris@16
|
170 if (get(assignments, uPrime) == u) {
|
Chris@16
|
171 BGL_FORALL_OUTEDGES_T(uPrime, e, g, Graph) { // foreach (u, v) \in E do
|
Chris@16
|
172 vis.examine_edge(e, g);
|
Chris@16
|
173
|
Chris@16
|
174 const vertex_descriptor v = get(assignments, target(e, g));
|
Chris@16
|
175
|
Chris@16
|
176 if (pq.contains(v)) { // if v \in PQ then
|
Chris@16
|
177 put(keys, v, get(keys, v) + get(weights, e)); // increasekey(PQ, v, wA(v) + w(u, v))
|
Chris@16
|
178 pq.update(v);
|
Chris@16
|
179 }
|
Chris@16
|
180 }
|
Chris@16
|
181 }
|
Chris@16
|
182 }
|
Chris@16
|
183 vis.finish_vertex(u, g);
|
Chris@16
|
184 }
|
Chris@16
|
185 }
|
Chris@16
|
186 } // end namespace detail
|
Chris@16
|
187
|
Chris@16
|
188 template <class Graph, class WeightMap, class MASVisitor, class VertexAssignmentMap, class KeyedUpdatablePriorityQueue>
|
Chris@16
|
189 void
|
Chris@16
|
190 maximum_adjacency_search(const Graph& g, WeightMap weights, MASVisitor vis, const typename boost::graph_traits<Graph>::vertex_descriptor start, VertexAssignmentMap assignments, KeyedUpdatablePriorityQueue pq) {
|
Chris@16
|
191 BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept<Graph>));
|
Chris@16
|
192 BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept<Graph>));
|
Chris@16
|
193 typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
|
Chris@16
|
194 typedef typename boost::graph_traits<Graph>::vertices_size_type vertices_size_type;
|
Chris@16
|
195 typedef typename boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
|
Chris@16
|
196 BOOST_CONCEPT_ASSERT((boost::Convertible<typename boost::graph_traits<Graph>::directed_category, boost::undirected_tag>));
|
Chris@16
|
197 BOOST_CONCEPT_ASSERT((boost::ReadablePropertyMapConcept<WeightMap, edge_descriptor>));
|
Chris@16
|
198 // typedef typename boost::property_traits<WeightMap>::value_type weight_type;
|
Chris@16
|
199 boost::function_requires< MASVisitorConcept<MASVisitor, Graph> >();
|
Chris@16
|
200 BOOST_CONCEPT_ASSERT((boost::ReadWritePropertyMapConcept<VertexAssignmentMap, vertex_descriptor>));
|
Chris@16
|
201 BOOST_CONCEPT_ASSERT((boost::Convertible<vertex_descriptor, typename boost::property_traits<VertexAssignmentMap>::value_type>));
|
Chris@16
|
202 BOOST_CONCEPT_ASSERT((boost::KeyedUpdatableQueueConcept<KeyedUpdatablePriorityQueue>));
|
Chris@16
|
203
|
Chris@16
|
204 vertices_size_type n = num_vertices(g);
|
Chris@16
|
205 if (n < 2)
|
Chris@16
|
206 throw boost::bad_graph("the input graph must have at least two vertices.");
|
Chris@16
|
207 else if (!pq.empty())
|
Chris@16
|
208 throw std::invalid_argument("the max-priority queue must be empty initially.");
|
Chris@16
|
209
|
Chris@16
|
210 detail::maximum_adjacency_search(g, weights,
|
Chris@16
|
211 vis, start,
|
Chris@16
|
212 assignments, pq);
|
Chris@16
|
213 }
|
Chris@16
|
214
|
Chris@16
|
215 namespace graph {
|
Chris@16
|
216 namespace detail {
|
Chris@16
|
217 template <typename WeightMap>
|
Chris@16
|
218 struct mas_dispatch {
|
Chris@16
|
219 typedef void result_type;
|
Chris@16
|
220 template <typename Graph, typename ArgPack>
|
Chris@16
|
221 static result_type apply(const Graph& g,
|
Chris@16
|
222 //const bgl_named_params<P,T,R>& params,
|
Chris@16
|
223 const ArgPack& params,
|
Chris@16
|
224 WeightMap w) {
|
Chris@16
|
225
|
Chris@16
|
226 using namespace boost::graph::keywords;
|
Chris@16
|
227 typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
|
Chris@16
|
228 typedef typename WeightMap::value_type weight_type;
|
Chris@16
|
229
|
Chris@16
|
230 typedef boost::detail::make_priority_queue_from_arg_pack_gen<boost::graph::keywords::tag::max_priority_queue, weight_type, vertex_descriptor, std::greater<weight_type> > default_pq_gen_type;
|
Chris@16
|
231
|
Chris@16
|
232 default_pq_gen_type pq_gen(choose_param(get_param(params, boost::distance_zero_t()), weight_type(0)));
|
Chris@16
|
233
|
Chris@16
|
234 typename boost::result_of<default_pq_gen_type(const Graph&, const ArgPack&)>::type pq = pq_gen(g, params);
|
Chris@16
|
235
|
Chris@16
|
236 boost::maximum_adjacency_search
|
Chris@16
|
237 (g,
|
Chris@16
|
238 w,
|
Chris@16
|
239 params [ _visitor | make_mas_visitor(null_visitor())],
|
Chris@16
|
240 params [ _root_vertex | *vertices(g).first],
|
Chris@16
|
241 params [ _vertex_assignment_map | boost::detail::make_property_map_from_arg_pack_gen<boost::graph::keywords::tag::vertex_assignment_map, vertex_descriptor>(vertex_descriptor())(g, params)],
|
Chris@16
|
242 pq
|
Chris@16
|
243 );
|
Chris@16
|
244 }
|
Chris@16
|
245 };
|
Chris@16
|
246
|
Chris@16
|
247 template <>
|
Chris@16
|
248 struct mas_dispatch<boost::param_not_found> {
|
Chris@16
|
249 typedef void result_type;
|
Chris@16
|
250
|
Chris@16
|
251 template <typename Graph, typename ArgPack>
|
Chris@16
|
252 static result_type apply(const Graph& g,
|
Chris@16
|
253 const ArgPack& params,
|
Chris@16
|
254 param_not_found) {
|
Chris@16
|
255
|
Chris@16
|
256 using namespace boost::graph::keywords;
|
Chris@16
|
257 typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
|
Chris@16
|
258
|
Chris@16
|
259 // get edge_weight_t as the weight type
|
Chris@16
|
260 typedef typename boost::property_map<Graph, edge_weight_t> WeightMap;
|
Chris@16
|
261 typedef typename WeightMap::value_type weight_type;
|
Chris@16
|
262
|
Chris@16
|
263 typedef boost::detail::make_priority_queue_from_arg_pack_gen<boost::graph::keywords::tag::max_priority_queue, weight_type, vertex_descriptor, std::greater<weight_type> > default_pq_gen_type;
|
Chris@16
|
264
|
Chris@16
|
265 default_pq_gen_type pq_gen(choose_param(get_param(params, boost::distance_zero_t()), weight_type(0)));
|
Chris@16
|
266
|
Chris@16
|
267 typename boost::result_of<default_pq_gen_type(const Graph&, const ArgPack&)>::type pq = pq_gen(g, params);
|
Chris@16
|
268
|
Chris@16
|
269 boost::maximum_adjacency_search
|
Chris@16
|
270 (g,
|
Chris@16
|
271 get(edge_weight, g),
|
Chris@16
|
272 params [ _visitor | make_mas_visitor(null_visitor())],
|
Chris@16
|
273 params [ _root_vertex | *vertices(g).first],
|
Chris@16
|
274 params [ _vertex_assignment_map | boost::detail::make_property_map_from_arg_pack_gen<boost::graph::keywords::tag::vertex_assignment_map, vertex_descriptor>(vertex_descriptor())(g, params)],
|
Chris@16
|
275 pq
|
Chris@16
|
276 );
|
Chris@16
|
277 }
|
Chris@16
|
278 };
|
Chris@16
|
279 } // end namespace detail
|
Chris@16
|
280 } // end namespace graph
|
Chris@16
|
281
|
Chris@16
|
282 // Named parameter interface
|
Chris@16
|
283 //BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(maximum_adjacency_search, 1)
|
Chris@16
|
284 template <typename Graph, typename P, typename T, typename R>
|
Chris@16
|
285 void
|
Chris@16
|
286 maximum_adjacency_search (const Graph& g,
|
Chris@16
|
287 const bgl_named_params<P,T,R>& params) {
|
Chris@16
|
288
|
Chris@16
|
289 typedef bgl_named_params<P, T, R> params_type;
|
Chris@16
|
290 BOOST_GRAPH_DECLARE_CONVERTED_PARAMETERS(params_type, params)
|
Chris@16
|
291
|
Chris@16
|
292 // do the dispatch based on WeightMap
|
Chris@16
|
293 typedef typename get_param_type<edge_weight_t, bgl_named_params<P,T,R> >::type W;
|
Chris@16
|
294 graph::detail::mas_dispatch<W>::apply(g, arg_pack, get_param(params, edge_weight));
|
Chris@16
|
295 }
|
Chris@16
|
296
|
Chris@16
|
297 namespace graph {
|
Chris@16
|
298 namespace detail {
|
Chris@16
|
299 template <typename Graph>
|
Chris@16
|
300 struct maximum_adjacency_search_impl {
|
Chris@16
|
301 typedef void result_type;
|
Chris@16
|
302
|
Chris@16
|
303 template <typename ArgPack>
|
Chris@16
|
304 void
|
Chris@16
|
305 operator() (const Graph& g, const ArgPack& arg_pack) const {
|
Chris@16
|
306 // call the function that does the dispatching
|
Chris@16
|
307 typedef typename get_param_type<edge_weight_t, ArgPack >::type W;
|
Chris@16
|
308 graph::detail::mas_dispatch<W>::apply(g, arg_pack, get_param(arg_pack, edge_weight));
|
Chris@16
|
309 }
|
Chris@16
|
310 };
|
Chris@16
|
311 } // end namespace detail
|
Chris@16
|
312 BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(maximum_adjacency_search,1,5)
|
Chris@16
|
313 } // end namespace graph
|
Chris@16
|
314
|
Chris@16
|
315 } // end namespace boost
|
Chris@16
|
316
|
Chris@16
|
317 #include <boost/graph/iteration_macros_undef.hpp>
|
Chris@16
|
318
|
Chris@16
|
319 #endif // BOOST_GRAPH_MAXIMUM_ADJACENCY_SEARCH_H
|