stoer_wagner_min_cut.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright Daniel Trebbien 2010.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or the copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_GRAPH_STOER_WAGNER_MIN_CUT_HPP
  6. #define BOOST_GRAPH_STOER_WAGNER_MIN_CUT_HPP 1
  7. #include <boost/assert.hpp>
  8. #include <set>
  9. #include <vector>
  10. #include <boost/concept_check.hpp>
  11. #include <boost/concept/assert.hpp>
  12. #include <boost/graph/adjacency_list.hpp>
  13. #include <boost/graph/buffer_concepts.hpp>
  14. #include <boost/graph/exception.hpp>
  15. #include <boost/graph/graph_traits.hpp>
  16. #include <boost/graph/maximum_adjacency_search.hpp>
  17. #include <boost/graph/named_function_params.hpp>
  18. #include <boost/graph/one_bit_color_map.hpp>
  19. #include <boost/graph/detail/d_ary_heap.hpp>
  20. #include <boost/property_map/property_map.hpp>
  21. #include <boost/tuple/tuple.hpp>
  22. #include <boost/utility/result_of.hpp>
  23. #include <boost/graph/iteration_macros.hpp>
  24. namespace boost
  25. {
  26. namespace detail
  27. {
  28. /**
  29. * \brief Performs a phase of the Stoer-Wagner min-cut algorithm
  30. *
  31. * Performs a phase of the Stoer-Wagner min-cut algorithm.
  32. *
  33. * As described by Stoer & Wagner (1997), a phase is simply a maximum
  34. * adjacency search (also called a maximum cardinality search), which
  35. * results in the selection of two vertices \em s and \em t, and, as a side
  36. * product, a minimum <em>s</em>-<em>t</em> cut of the input graph. Here,
  37. * the input graph is basically \p g, but some vertices are virtually
  38. * assigned to others as a way of viewing \p g as a graph with some sets of
  39. * vertices merged together.
  40. *
  41. * This implementation is a translation of pseudocode by Professor Uri
  42. * Zwick, School of Computer Science, Tel Aviv University.
  43. *
  44. * \pre \p g is a connected, undirected graph
  45. * \param[in] g the input graph
  46. * \param[in] assignments a read/write property map from each vertex to the
  47. * vertex that it is assigned to
  48. * \param[in] assignedVertices a list of vertices that are assigned to
  49. * others
  50. * \param[in] weights a readable property map from each edge to its
  51. * weight (a non-negative value)
  52. * \param[out] pq a keyed, updatable max-priority queue
  53. * \returns a tuple (\em s, \em t, \em w) of the "<em>s</em>" and
  54. * "<em>t</em>" of the minimum <em>s</em>-<em>t</em> cut and the
  55. * cut weight \em w of the minimum <em>s</em>-<em>t</em> cut.
  56. * \see http://www.cs.tau.ac.il/~zwick/grad-algo-08/gmc.pdf
  57. *
  58. * \author Daniel Trebbien
  59. * \date 2010-09-11
  60. */
  61. template < class UndirectedGraph, class VertexAssignmentMap,
  62. class WeightMap, class KeyedUpdatablePriorityQueue >
  63. boost::tuple<
  64. typename boost::graph_traits< UndirectedGraph >::vertex_descriptor,
  65. typename boost::graph_traits< UndirectedGraph >::vertex_descriptor,
  66. typename boost::property_traits< WeightMap >::value_type >
  67. stoer_wagner_phase(const UndirectedGraph& g,
  68. VertexAssignmentMap assignments,
  69. const std::set< typename boost::graph_traits<
  70. UndirectedGraph >::vertex_descriptor >& assignedVertices,
  71. WeightMap weights, KeyedUpdatablePriorityQueue& pq)
  72. {
  73. typedef
  74. typename boost::graph_traits< UndirectedGraph >::vertex_descriptor
  75. vertex_descriptor;
  76. typedef typename boost::property_traits< WeightMap >::value_type
  77. weight_type;
  78. BOOST_ASSERT(pq.empty());
  79. typename KeyedUpdatablePriorityQueue::key_map keys = pq.keys();
  80. BGL_FORALL_VERTICES_T(v, g, UndirectedGraph)
  81. {
  82. if (v == get(assignments, v))
  83. { // foreach u \in V do
  84. put(keys, v, weight_type(0));
  85. pq.push(v);
  86. }
  87. }
  88. BOOST_ASSERT(pq.size() >= 2);
  89. vertex_descriptor s
  90. = boost::graph_traits< UndirectedGraph >::null_vertex();
  91. vertex_descriptor t
  92. = boost::graph_traits< UndirectedGraph >::null_vertex();
  93. weight_type w;
  94. while (!pq.empty())
  95. { // while PQ \neq {} do
  96. const vertex_descriptor u = pq.top(); // u = extractmax(PQ)
  97. w = get(keys, u);
  98. pq.pop();
  99. s = t;
  100. t = u;
  101. BGL_FORALL_OUTEDGES_T(u, e, g, UndirectedGraph)
  102. { // foreach (u, v) \in E do
  103. const vertex_descriptor v = get(assignments, target(e, g));
  104. if (pq.contains(v))
  105. { // if v \in PQ then
  106. put(keys, v,
  107. get(keys, v)
  108. + get(weights,
  109. e)); // increasekey(PQ, v, wA(v) + w(u, v))
  110. pq.update(v);
  111. }
  112. }
  113. typename std::set< vertex_descriptor >::const_iterator
  114. assignedVertexIt,
  115. assignedVertexEnd = assignedVertices.end();
  116. for (assignedVertexIt = assignedVertices.begin();
  117. assignedVertexIt != assignedVertexEnd; ++assignedVertexIt)
  118. {
  119. const vertex_descriptor uPrime = *assignedVertexIt;
  120. if (get(assignments, uPrime) == u)
  121. {
  122. BGL_FORALL_OUTEDGES_T(uPrime, e, g, UndirectedGraph)
  123. { // foreach (u, v) \in E do
  124. const vertex_descriptor v
  125. = get(assignments, target(e, g));
  126. if (pq.contains(v))
  127. { // if v \in PQ then
  128. put(keys, v,
  129. get(keys, v)
  130. + get(weights, e)); // increasekey(PQ, v,
  131. // wA(v) + w(u, v))
  132. pq.update(v);
  133. }
  134. }
  135. }
  136. }
  137. }
  138. return boost::make_tuple(s, t, w);
  139. }
  140. /**
  141. * \brief Computes a min-cut of the input graph
  142. *
  143. * Computes a min-cut of the input graph using the Stoer-Wagner algorithm.
  144. *
  145. * \pre \p g is a connected, undirected graph
  146. * \pre <code>pq.empty()</code>
  147. * \param[in] g the input graph
  148. * \param[in] weights a readable property map from each edge to its weight
  149. * (a non-negative value) \param[out] parities a writable property map from
  150. * each vertex to a bool type object for distinguishing the two vertex sets
  151. * of the min-cut \param[out] assignments a read/write property map from
  152. * each vertex to a \c vertex_descriptor object. This map serves as work
  153. * space, and no particular meaning should be derived from property values
  154. * after completion of the algorithm.
  155. * \param[out] pq a keyed, updatable max-priority queue
  156. * \returns the cut weight of the min-cut
  157. * \see
  158. * http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.114.6687&rep=rep1&type=pdf
  159. * \see
  160. * http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.31.614&rep=rep1&type=pdf
  161. *
  162. * \author Daniel Trebbien
  163. * \date 2010-09-11
  164. */
  165. template < class UndirectedGraph, class WeightMap, class ParityMap,
  166. class VertexAssignmentMap, class KeyedUpdatablePriorityQueue,
  167. class IndexMap >
  168. typename boost::property_traits< WeightMap >::value_type
  169. stoer_wagner_min_cut(const UndirectedGraph& g, WeightMap weights,
  170. ParityMap parities, VertexAssignmentMap assignments,
  171. KeyedUpdatablePriorityQueue& pq, IndexMap index_map)
  172. {
  173. typedef
  174. typename boost::graph_traits< UndirectedGraph >::vertex_descriptor
  175. vertex_descriptor;
  176. typedef typename boost::property_traits< WeightMap >::value_type
  177. weight_type;
  178. typedef
  179. typename boost::graph_traits< UndirectedGraph >::vertices_size_type
  180. vertices_size_type;
  181. typedef typename boost::property_traits< ParityMap >::value_type
  182. parity_type;
  183. vertices_size_type n = num_vertices(g);
  184. std::set< vertex_descriptor > assignedVertices;
  185. // initialize `assignments` (all vertices are initially assigned to
  186. // themselves)
  187. BGL_FORALL_VERTICES_T(v, g, UndirectedGraph) { put(assignments, v, v); }
  188. vertex_descriptor s, t;
  189. weight_type bestW;
  190. boost::tie(s, t, bestW) = boost::detail::stoer_wagner_phase(
  191. g, assignments, assignedVertices, weights, pq);
  192. BOOST_ASSERT(s != t);
  193. BGL_FORALL_VERTICES_T(v, g, UndirectedGraph)
  194. {
  195. put(parities, v, parity_type(v == t ? 1 : 0));
  196. }
  197. put(assignments, t, s);
  198. assignedVertices.insert(t);
  199. --n;
  200. for (; n >= 2; --n)
  201. {
  202. weight_type w;
  203. boost::tie(s, t, w) = boost::detail::stoer_wagner_phase(
  204. g, assignments, assignedVertices, weights, pq);
  205. BOOST_ASSERT(s != t);
  206. if (w < bestW)
  207. {
  208. BGL_FORALL_VERTICES_T(v, g, UndirectedGraph)
  209. {
  210. put(parities, v,
  211. parity_type(get(assignments, v) == t ? 1 : 0));
  212. if (get(assignments, v)
  213. == t) // all vertices that were assigned to t are now
  214. // assigned to s
  215. put(assignments, v, s);
  216. }
  217. bestW = w;
  218. }
  219. else
  220. {
  221. BGL_FORALL_VERTICES_T(v, g, UndirectedGraph)
  222. {
  223. if (get(assignments, v)
  224. == t) // all vertices that were assigned to t are now
  225. // assigned to s
  226. put(assignments, v, s);
  227. }
  228. }
  229. put(assignments, t, s);
  230. assignedVertices.insert(t);
  231. }
  232. BOOST_ASSERT(pq.empty());
  233. return bestW;
  234. }
  235. } // end `namespace detail` within `namespace boost`
  236. template < class UndirectedGraph, class WeightMap, class ParityMap,
  237. class VertexAssignmentMap, class KeyedUpdatablePriorityQueue,
  238. class IndexMap >
  239. typename boost::property_traits< WeightMap >::value_type stoer_wagner_min_cut(
  240. const UndirectedGraph& g, WeightMap weights, ParityMap parities,
  241. VertexAssignmentMap assignments, KeyedUpdatablePriorityQueue& pq,
  242. IndexMap index_map)
  243. {
  244. BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept< UndirectedGraph >));
  245. BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept< UndirectedGraph >));
  246. typedef typename boost::graph_traits< UndirectedGraph >::vertex_descriptor
  247. vertex_descriptor;
  248. typedef typename boost::graph_traits< UndirectedGraph >::vertices_size_type
  249. vertices_size_type;
  250. typedef typename boost::graph_traits< UndirectedGraph >::edge_descriptor
  251. edge_descriptor;
  252. BOOST_CONCEPT_ASSERT((boost::Convertible<
  253. typename boost::graph_traits< UndirectedGraph >::directed_category,
  254. boost::undirected_tag >));
  255. BOOST_CONCEPT_ASSERT(
  256. (boost::ReadablePropertyMapConcept< WeightMap, edge_descriptor >));
  257. // typedef typename boost::property_traits<WeightMap>::value_type
  258. // weight_type;
  259. BOOST_CONCEPT_ASSERT(
  260. (boost::WritablePropertyMapConcept< ParityMap, vertex_descriptor >));
  261. // typedef typename boost::property_traits<ParityMap>::value_type
  262. // parity_type;
  263. BOOST_CONCEPT_ASSERT(
  264. (boost::ReadWritePropertyMapConcept< VertexAssignmentMap,
  265. vertex_descriptor >));
  266. BOOST_CONCEPT_ASSERT((boost::Convertible< vertex_descriptor,
  267. typename boost::property_traits< VertexAssignmentMap >::value_type >));
  268. BOOST_CONCEPT_ASSERT(
  269. (boost::KeyedUpdatableQueueConcept< KeyedUpdatablePriorityQueue >));
  270. vertices_size_type n = num_vertices(g);
  271. if (n < 2)
  272. throw boost::bad_graph(
  273. "the input graph must have at least two vertices.");
  274. else if (!pq.empty())
  275. throw std::invalid_argument(
  276. "the max-priority queue must be empty initially.");
  277. return detail::stoer_wagner_min_cut(
  278. g, weights, parities, assignments, pq, index_map);
  279. }
  280. namespace graph
  281. {
  282. namespace detail
  283. {
  284. template < class UndirectedGraph, class WeightMap >
  285. struct stoer_wagner_min_cut_impl
  286. {
  287. typedef typename boost::property_traits< WeightMap >::value_type
  288. result_type;
  289. template < typename ArgPack >
  290. result_type operator()(const UndirectedGraph& g, WeightMap weights,
  291. const ArgPack& arg_pack) const
  292. {
  293. using namespace boost::graph::keywords;
  294. typedef typename boost::graph_traits<
  295. UndirectedGraph >::vertex_descriptor vertex_descriptor;
  296. typedef typename boost::property_traits< WeightMap >::value_type
  297. weight_type;
  298. typedef boost::detail::make_priority_queue_from_arg_pack_gen<
  299. boost::graph::keywords::tag::max_priority_queue,
  300. weight_type, vertex_descriptor,
  301. std::greater< weight_type > >
  302. gen_type;
  303. gen_type gen(
  304. choose_param(get_param(arg_pack, boost::distance_zero_t()),
  305. weight_type(0)));
  306. typename boost::result_of< gen_type(
  307. const UndirectedGraph&, const ArgPack&) >::type pq
  308. = gen(g, arg_pack);
  309. boost::dummy_property_map dummy_prop;
  310. return boost::stoer_wagner_min_cut(g, weights,
  311. arg_pack[_parity_map | dummy_prop],
  312. boost::detail::make_property_map_from_arg_pack_gen<
  313. tag::vertex_assignment_map, vertex_descriptor >(
  314. vertex_descriptor())(g, arg_pack),
  315. pq,
  316. boost::detail::override_const_property(
  317. arg_pack, _vertex_index_map, g, vertex_index));
  318. }
  319. };
  320. }
  321. BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(stoer_wagner_min_cut, 2, 4)
  322. }
  323. // Named parameter interface
  324. BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(stoer_wagner_min_cut, 2)
  325. namespace graph
  326. {
  327. // version without IndexMap kept for backwards compatibility
  328. // (but requires vertex_index_t to be defined in the graph)
  329. // Place after the macro to avoid compilation errors
  330. template < class UndirectedGraph, class WeightMap, class ParityMap,
  331. class VertexAssignmentMap, class KeyedUpdatablePriorityQueue >
  332. typename boost::property_traits< WeightMap >::value_type
  333. stoer_wagner_min_cut(const UndirectedGraph& g, WeightMap weights,
  334. ParityMap parities, VertexAssignmentMap assignments,
  335. KeyedUpdatablePriorityQueue& pq)
  336. {
  337. return stoer_wagner_min_cut(
  338. g, weights, parities, assignments, pq, get(vertex_index, g));
  339. }
  340. } // end `namespace graph`
  341. } // end `namespace boost`
  342. #include <boost/graph/iteration_macros_undef.hpp>
  343. #endif // !BOOST_GRAPH_STOER_WAGNER_MIN_CUT_HPP