stoer_wagner_min_cut.hpp 12 KB

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