stoer_wagner_min_cut.hpp 11 KB

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