Chris@16
|
1 // Copyright Daniel Trebbien 2010.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 // (See accompanying file LICENSE_1_0.txt or the copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_GRAPH_STOER_WAGNER_MIN_CUT_HPP
|
Chris@16
|
7 #define BOOST_GRAPH_STOER_WAGNER_MIN_CUT_HPP 1
|
Chris@16
|
8
|
Chris@16
|
9 #include <boost/assert.hpp>
|
Chris@16
|
10 #include <set>
|
Chris@16
|
11 #include <vector>
|
Chris@16
|
12 #include <boost/concept_check.hpp>
|
Chris@16
|
13 #include <boost/concept/assert.hpp>
|
Chris@16
|
14 #include <boost/graph/adjacency_list.hpp>
|
Chris@16
|
15 #include <boost/graph/buffer_concepts.hpp>
|
Chris@16
|
16 #include <boost/graph/exception.hpp>
|
Chris@16
|
17 #include <boost/graph/graph_traits.hpp>
|
Chris@16
|
18 #include <boost/graph/maximum_adjacency_search.hpp>
|
Chris@16
|
19 #include <boost/graph/named_function_params.hpp>
|
Chris@16
|
20 #include <boost/graph/one_bit_color_map.hpp>
|
Chris@16
|
21 #include <boost/graph/detail/d_ary_heap.hpp>
|
Chris@16
|
22 #include <boost/property_map/property_map.hpp>
|
Chris@16
|
23 #include <boost/tuple/tuple.hpp>
|
Chris@16
|
24 #include <boost/utility/result_of.hpp>
|
Chris@16
|
25 #include <boost/graph/iteration_macros.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28
|
Chris@16
|
29 namespace detail {
|
Chris@16
|
30 template < typename ParityMap, typename WeightMap, typename IndexMap >
|
Chris@16
|
31 class mas_min_cut_visitor : public boost::default_mas_visitor {
|
Chris@16
|
32 typedef one_bit_color_map <IndexMap> InternalParityMap;
|
Chris@16
|
33 typedef typename boost::property_traits<WeightMap>::value_type weight_type;
|
Chris@16
|
34 public:
|
Chris@16
|
35 template < typename Graph >
|
Chris@16
|
36 mas_min_cut_visitor(const Graph& g,
|
Chris@16
|
37 ParityMap parity,
|
Chris@16
|
38 weight_type& cutweight,
|
Chris@16
|
39 const WeightMap& weight_map,
|
Chris@16
|
40 IndexMap index_map)
|
Chris@16
|
41 : m_bestParity(parity),
|
Chris@16
|
42 m_parity(make_one_bit_color_map(num_vertices(g), index_map)),
|
Chris@16
|
43 m_bestWeight(cutweight),
|
Chris@16
|
44 m_cutweight(0),
|
Chris@16
|
45 m_visited(0),
|
Chris@16
|
46 m_weightMap(weight_map)
|
Chris@16
|
47 {
|
Chris@16
|
48 // set here since the init list sets the reference
|
Chris@16
|
49 m_bestWeight = (std::numeric_limits<weight_type>::max)();
|
Chris@16
|
50 }
|
Chris@16
|
51
|
Chris@16
|
52 template < typename Vertex, typename Graph >
|
Chris@16
|
53 void initialize_vertex(Vertex u, const Graph & g)
|
Chris@16
|
54 {
|
Chris@16
|
55 typedef typename boost::property_traits<ParityMap>::value_type parity_type;
|
Chris@16
|
56 typedef typename boost::property_traits<InternalParityMap>::value_type internal_parity_type;
|
Chris@16
|
57
|
Chris@16
|
58 put(m_parity, u, internal_parity_type(0));
|
Chris@16
|
59 put(m_bestParity, u, parity_type(0));
|
Chris@16
|
60 }
|
Chris@16
|
61
|
Chris@16
|
62 template < typename Edge, typename Graph >
|
Chris@16
|
63 void examine_edge(Edge e, const Graph & g)
|
Chris@16
|
64 {
|
Chris@16
|
65 weight_type w = get(m_weightMap, e);
|
Chris@16
|
66
|
Chris@16
|
67 // if the target of e is already marked then decrease cutweight
|
Chris@16
|
68 // otherwise, increase it
|
Chris@16
|
69 if (get(m_parity, boost::target(e, g))) {
|
Chris@16
|
70 m_cutweight -= w;
|
Chris@16
|
71 } else {
|
Chris@16
|
72 m_cutweight += w;
|
Chris@16
|
73 }
|
Chris@16
|
74 }
|
Chris@16
|
75
|
Chris@16
|
76 template < typename Vertex, typename Graph >
|
Chris@16
|
77 void finish_vertex(Vertex u, const Graph & g)
|
Chris@16
|
78 {
|
Chris@16
|
79 typedef typename boost::property_traits<InternalParityMap>::value_type internal_parity_type;
|
Chris@16
|
80
|
Chris@16
|
81 ++m_visited;
|
Chris@16
|
82 put(m_parity, u, internal_parity_type(1));
|
Chris@16
|
83
|
Chris@16
|
84 if (m_cutweight < m_bestWeight && m_visited < num_vertices(g)) {
|
Chris@16
|
85 m_bestWeight = m_cutweight;
|
Chris@16
|
86 BGL_FORALL_VERTICES_T(i, g, Graph) {
|
Chris@16
|
87 put(m_bestParity,i, get(m_parity,i));
|
Chris@16
|
88 }
|
Chris@16
|
89 }
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 inline void clear() {
|
Chris@16
|
93 m_bestWeight = (std::numeric_limits<weight_type>::max)();
|
Chris@16
|
94 m_visited = 0;
|
Chris@16
|
95 m_cutweight = 0;
|
Chris@16
|
96 }
|
Chris@16
|
97
|
Chris@16
|
98 private:
|
Chris@16
|
99 ParityMap m_bestParity;
|
Chris@16
|
100 InternalParityMap m_parity;
|
Chris@16
|
101 weight_type& m_bestWeight;
|
Chris@16
|
102 weight_type m_cutweight;
|
Chris@16
|
103 unsigned m_visited;
|
Chris@16
|
104 const WeightMap& m_weightMap;
|
Chris@16
|
105 };
|
Chris@16
|
106
|
Chris@16
|
107 /**
|
Chris@16
|
108 * \brief Computes a min-cut of the input graph
|
Chris@16
|
109 *
|
Chris@16
|
110 * Computes a min-cut of the input graph using the Stoer-Wagner algorithm.
|
Chris@16
|
111 *
|
Chris@16
|
112 * \pre \p g is a connected, undirected graph
|
Chris@16
|
113 * \pre <code>pq.empty()</code>
|
Chris@16
|
114 * \param[in] g the input graph
|
Chris@16
|
115 * \param[in] weights a readable property map from each edge to its weight (a non-negative value)
|
Chris@16
|
116 * \param[out] parities a writable property map from each vertex to a bool type object for
|
Chris@16
|
117 * distinguishing the two vertex sets of the min-cut
|
Chris@16
|
118 * \param[out] assignments a read/write property map from each vertex to a \c vertex_descriptor object. This
|
Chris@16
|
119 * map serves as work space, and no particular meaning should be derived from property values
|
Chris@16
|
120 * after completion of the algorithm.
|
Chris@16
|
121 * \param[out] pq a keyed, updatable max-priority queue
|
Chris@16
|
122 * \returns the cut weight of the min-cut
|
Chris@16
|
123 * \see http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.114.6687&rep=rep1&type=pdf
|
Chris@16
|
124 * \see http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.31.614&rep=rep1&type=pdf
|
Chris@16
|
125 *
|
Chris@16
|
126 * \author Daniel Trebbien
|
Chris@16
|
127 * \date 2010-09-11
|
Chris@16
|
128 */
|
Chris@16
|
129 template <class UndirectedGraph, class WeightMap, class ParityMap, class VertexAssignmentMap, class KeyedUpdatablePriorityQueue, class IndexMap>
|
Chris@16
|
130 typename boost::property_traits<WeightMap>::value_type
|
Chris@16
|
131 stoer_wagner_min_cut(const UndirectedGraph& g, WeightMap weights, ParityMap parities, VertexAssignmentMap assignments, KeyedUpdatablePriorityQueue& pq, IndexMap index_map) {
|
Chris@16
|
132 typedef typename boost::graph_traits<UndirectedGraph>::vertex_descriptor vertex_descriptor;
|
Chris@16
|
133 typedef typename boost::property_traits<WeightMap>::value_type weight_type;
|
Chris@16
|
134
|
Chris@16
|
135 typename graph_traits<UndirectedGraph>::vertex_iterator u_iter, u_end;
|
Chris@16
|
136
|
Chris@16
|
137 weight_type bestW = (std::numeric_limits<weight_type>::max)();
|
Chris@16
|
138 weight_type bestThisTime = (std::numeric_limits<weight_type>::max)();
|
Chris@16
|
139 vertex_descriptor bestStart = boost::graph_traits<UndirectedGraph>::null_vertex();
|
Chris@16
|
140
|
Chris@16
|
141 detail::mas_min_cut_visitor<ParityMap, WeightMap, IndexMap>
|
Chris@16
|
142 vis(g, parities, bestThisTime, weights, index_map);
|
Chris@16
|
143
|
Chris@16
|
144 // for each node in the graph,
|
Chris@16
|
145 for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter) {
|
Chris@16
|
146 // run the MAS and find the min cut
|
Chris@16
|
147 vis.clear();
|
Chris@16
|
148 boost::maximum_adjacency_search(g,
|
Chris@16
|
149 boost::weight_map(weights).
|
Chris@16
|
150 visitor(vis).
|
Chris@16
|
151 root_vertex(*u_iter).
|
Chris@16
|
152 vertex_assignment_map(assignments).
|
Chris@16
|
153 max_priority_queue(pq));
|
Chris@16
|
154 if (bestThisTime < bestW) {
|
Chris@16
|
155 bestW = bestThisTime;
|
Chris@16
|
156 bestStart = *u_iter;
|
Chris@16
|
157 }
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160 // Run one more time, starting from the best start location, to
|
Chris@16
|
161 // ensure the visitor has the best values.
|
Chris@16
|
162 vis.clear();
|
Chris@16
|
163 boost::maximum_adjacency_search(g,
|
Chris@16
|
164 boost::vertex_assignment_map(assignments).
|
Chris@16
|
165 weight_map(weights).
|
Chris@16
|
166 visitor(vis).
|
Chris@16
|
167 root_vertex(bestStart).
|
Chris@16
|
168 max_priority_queue(pq));
|
Chris@16
|
169
|
Chris@16
|
170 return bestW;
|
Chris@16
|
171 }
|
Chris@16
|
172 } // end `namespace detail` within `namespace boost`
|
Chris@16
|
173
|
Chris@16
|
174 template <class UndirectedGraph, class WeightMap, class ParityMap, class VertexAssignmentMap, class KeyedUpdatablePriorityQueue, class IndexMap>
|
Chris@16
|
175 typename boost::property_traits<WeightMap>::value_type
|
Chris@16
|
176 stoer_wagner_min_cut(const UndirectedGraph& g, WeightMap weights, ParityMap parities, VertexAssignmentMap assignments, KeyedUpdatablePriorityQueue& pq, IndexMap index_map) {
|
Chris@16
|
177 BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept<UndirectedGraph>));
|
Chris@16
|
178 BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept<UndirectedGraph>));
|
Chris@16
|
179 typedef typename boost::graph_traits<UndirectedGraph>::vertex_descriptor vertex_descriptor;
|
Chris@16
|
180 typedef typename boost::graph_traits<UndirectedGraph>::vertices_size_type vertices_size_type;
|
Chris@16
|
181 typedef typename boost::graph_traits<UndirectedGraph>::edge_descriptor edge_descriptor;
|
Chris@16
|
182 BOOST_CONCEPT_ASSERT((boost::Convertible<typename boost::graph_traits<UndirectedGraph>::directed_category, boost::undirected_tag>));
|
Chris@16
|
183 BOOST_CONCEPT_ASSERT((boost::ReadablePropertyMapConcept<WeightMap, edge_descriptor>));
|
Chris@16
|
184 // typedef typename boost::property_traits<WeightMap>::value_type weight_type;
|
Chris@16
|
185 BOOST_CONCEPT_ASSERT((boost::WritablePropertyMapConcept<ParityMap, vertex_descriptor>));
|
Chris@16
|
186 // typedef typename boost::property_traits<ParityMap>::value_type parity_type;
|
Chris@16
|
187 BOOST_CONCEPT_ASSERT((boost::ReadWritePropertyMapConcept<VertexAssignmentMap, vertex_descriptor>));
|
Chris@16
|
188 BOOST_CONCEPT_ASSERT((boost::Convertible<vertex_descriptor, typename boost::property_traits<VertexAssignmentMap>::value_type>));
|
Chris@16
|
189 BOOST_CONCEPT_ASSERT((boost::KeyedUpdatableQueueConcept<KeyedUpdatablePriorityQueue>));
|
Chris@16
|
190
|
Chris@16
|
191 vertices_size_type n = num_vertices(g);
|
Chris@16
|
192 if (n < 2)
|
Chris@16
|
193 throw boost::bad_graph("the input graph must have at least two vertices.");
|
Chris@16
|
194 else if (!pq.empty())
|
Chris@16
|
195 throw std::invalid_argument("the max-priority queue must be empty initially.");
|
Chris@16
|
196
|
Chris@16
|
197 return detail::stoer_wagner_min_cut(g, weights,
|
Chris@16
|
198 parities, assignments, pq, index_map);
|
Chris@16
|
199 }
|
Chris@16
|
200
|
Chris@16
|
201 namespace graph {
|
Chris@16
|
202 namespace detail {
|
Chris@16
|
203 template <class UndirectedGraph, class WeightMap>
|
Chris@16
|
204 struct stoer_wagner_min_cut_impl {
|
Chris@16
|
205 typedef typename boost::property_traits<WeightMap>::value_type result_type;
|
Chris@16
|
206 template <typename ArgPack>
|
Chris@16
|
207 result_type operator() (const UndirectedGraph& g, WeightMap weights, const ArgPack& arg_pack) const {
|
Chris@16
|
208 using namespace boost::graph::keywords;
|
Chris@16
|
209 typedef typename boost::graph_traits<UndirectedGraph>::vertex_descriptor vertex_descriptor;
|
Chris@16
|
210 typedef typename boost::property_traits<WeightMap>::value_type weight_type;
|
Chris@16
|
211
|
Chris@16
|
212 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> > gen_type;
|
Chris@16
|
213
|
Chris@16
|
214 gen_type gen(choose_param(get_param(arg_pack, boost::distance_zero_t()), weight_type(0)));
|
Chris@16
|
215
|
Chris@16
|
216 typename boost::result_of<gen_type(const UndirectedGraph&, const ArgPack&)>::type pq = gen(g, arg_pack);
|
Chris@16
|
217
|
Chris@16
|
218 return boost::stoer_wagner_min_cut(g,
|
Chris@16
|
219 weights,
|
Chris@16
|
220 arg_pack [_parity_map | boost::dummy_property_map()],
|
Chris@16
|
221 boost::detail::make_property_map_from_arg_pack_gen<tag::vertex_assignment_map, vertex_descriptor>(vertex_descriptor())(g, arg_pack),
|
Chris@16
|
222 pq,
|
Chris@16
|
223 boost::detail::override_const_property(arg_pack, _vertex_index_map, g, vertex_index)
|
Chris@16
|
224 );
|
Chris@16
|
225 }
|
Chris@16
|
226 };
|
Chris@16
|
227 }
|
Chris@16
|
228 BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(stoer_wagner_min_cut,2,4)
|
Chris@16
|
229 }
|
Chris@16
|
230
|
Chris@16
|
231 // Named parameter interface
|
Chris@16
|
232 BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(stoer_wagner_min_cut, 2)
|
Chris@16
|
233 namespace graph {
|
Chris@16
|
234 // version without IndexMap kept for backwards compatibility
|
Chris@16
|
235 // (but requires vertex_index_t to be defined in the graph)
|
Chris@16
|
236 // Place after the macro to avoid compilation errors
|
Chris@16
|
237 template <class UndirectedGraph, class WeightMap, class ParityMap, class VertexAssignmentMap, class KeyedUpdatablePriorityQueue>
|
Chris@16
|
238 typename boost::property_traits<WeightMap>::value_type
|
Chris@16
|
239 stoer_wagner_min_cut(const UndirectedGraph& g, WeightMap weights, ParityMap parities, VertexAssignmentMap assignments, KeyedUpdatablePriorityQueue& pq) {
|
Chris@16
|
240
|
Chris@16
|
241 return stoer_wagner_min_cut(g, weights,
|
Chris@16
|
242 parities, assignments, pq,
|
Chris@16
|
243 get(vertex_index, g));
|
Chris@16
|
244 }
|
Chris@16
|
245 } // end `namespace graph`
|
Chris@16
|
246 } // end `namespace boost`
|
Chris@16
|
247
|
Chris@16
|
248 #include <boost/graph/iteration_macros_undef.hpp>
|
Chris@16
|
249
|
Chris@16
|
250 #endif // !BOOST_GRAPH_STOER_WAGNER_MIN_CUT_HPP
|