cycle_canceling.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //=======================================================================
  2. // Copyright 2013 University of Warsaw.
  3. // Authors: Piotr Wygocki
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. //
  10. //
  11. // This algorithm is described in "Network Flows: Theory, Algorithms, and
  12. // Applications"
  13. // by Ahuja, Magnanti, Orlin.
  14. #ifndef BOOST_GRAPH_CYCLE_CANCELING_HPP
  15. #define BOOST_GRAPH_CYCLE_CANCELING_HPP
  16. #include <numeric>
  17. #include <boost/property_map/property_map.hpp>
  18. #include <boost/graph/graph_traits.hpp>
  19. #include <boost/graph/graph_concepts.hpp>
  20. #include <boost/pending/indirect_cmp.hpp>
  21. #include <boost/graph/bellman_ford_shortest_paths.hpp>
  22. #include <boost/graph/iteration_macros.hpp>
  23. #include <boost/graph/detail/augment.hpp>
  24. #include <boost/graph/find_flow_cost.hpp>
  25. namespace boost
  26. {
  27. namespace detail
  28. {
  29. template < typename PredEdgeMap, typename Vertex >
  30. class RecordEdgeMapAndCycleVertex
  31. : public bellman_visitor<
  32. edge_predecessor_recorder< PredEdgeMap, on_edge_relaxed > >
  33. {
  34. typedef edge_predecessor_recorder< PredEdgeMap, on_edge_relaxed >
  35. PredRec;
  36. public:
  37. RecordEdgeMapAndCycleVertex(PredEdgeMap pred, Vertex& v)
  38. : bellman_visitor< PredRec >(PredRec(pred)), m_v(v), m_pred(pred)
  39. {
  40. }
  41. template < typename Graph, typename Edge >
  42. void edge_not_minimized(Edge e, const Graph& g) const
  43. {
  44. typename graph_traits< Graph >::vertices_size_type n
  45. = num_vertices(g) + 1;
  46. // edge e is not minimized but does not have to be on the negative
  47. // weight cycle to find vertex on negative wieight cycle we move n+1
  48. // times backword in the PredEdgeMap graph.
  49. while (n > 0)
  50. {
  51. e = get(m_pred, source(e, g));
  52. --n;
  53. }
  54. m_v = source(e, g);
  55. }
  56. private:
  57. Vertex& m_v;
  58. PredEdgeMap m_pred;
  59. };
  60. } // detail
  61. template < class Graph, class Pred, class Distance, class Reversed,
  62. class ResidualCapacity, class Weight >
  63. void cycle_canceling(const Graph& g, Weight weight, Reversed rev,
  64. ResidualCapacity residual_capacity, Pred pred, Distance distance)
  65. {
  66. typedef filtered_graph< const Graph, is_residual_edge< ResidualCapacity > >
  67. ResGraph;
  68. ResGraph gres = detail::residual_graph(g, residual_capacity);
  69. typedef graph_traits< ResGraph > ResGTraits;
  70. typedef graph_traits< Graph > GTraits;
  71. typedef typename ResGTraits::edge_descriptor edge_descriptor;
  72. typedef typename ResGTraits::vertex_descriptor vertex_descriptor;
  73. typename GTraits::vertices_size_type N = num_vertices(g);
  74. BGL_FORALL_VERTICES_T(v, g, Graph)
  75. {
  76. put(pred, v, edge_descriptor());
  77. put(distance, v, 0);
  78. }
  79. vertex_descriptor cycleStart;
  80. while (!bellman_ford_shortest_paths(gres, N,
  81. weight_map(weight).distance_map(distance).visitor(
  82. detail::RecordEdgeMapAndCycleVertex< Pred, vertex_descriptor >(
  83. pred, cycleStart))))
  84. {
  85. detail::augment(
  86. g, cycleStart, cycleStart, pred, residual_capacity, rev);
  87. BGL_FORALL_VERTICES_T(v, g, Graph)
  88. {
  89. put(pred, v, edge_descriptor());
  90. put(distance, v, 0);
  91. }
  92. }
  93. }
  94. // in this namespace argument dispatching takes place
  95. namespace detail
  96. {
  97. template < class Graph, class P, class T, class R, class ResidualCapacity,
  98. class Weight, class Reversed, class Pred, class Distance >
  99. void cycle_canceling_dispatch2(const Graph& g, Weight weight, Reversed rev,
  100. ResidualCapacity residual_capacity, Pred pred, Distance dist,
  101. const bgl_named_params< P, T, R >& params)
  102. {
  103. cycle_canceling(g, weight, rev, residual_capacity, pred, dist);
  104. }
  105. // setting default distance map
  106. template < class Graph, class P, class T, class R, class Pred,
  107. class ResidualCapacity, class Weight, class Reversed >
  108. void cycle_canceling_dispatch2(Graph& g, Weight weight, Reversed rev,
  109. ResidualCapacity residual_capacity, Pred pred, param_not_found,
  110. const bgl_named_params< P, T, R >& params)
  111. {
  112. typedef typename property_traits< Weight >::value_type D;
  113. std::vector< D > d_map(num_vertices(g));
  114. cycle_canceling(g, weight, rev, residual_capacity, pred,
  115. make_iterator_property_map(d_map.begin(),
  116. choose_const_pmap(
  117. get_param(params, vertex_index), g, vertex_index)));
  118. }
  119. template < class Graph, class P, class T, class R, class ResidualCapacity,
  120. class Weight, class Reversed, class Pred >
  121. void cycle_canceling_dispatch1(Graph& g, Weight weight, Reversed rev,
  122. ResidualCapacity residual_capacity, Pred pred,
  123. const bgl_named_params< P, T, R >& params)
  124. {
  125. cycle_canceling_dispatch2(g, weight, rev, residual_capacity, pred,
  126. get_param(params, vertex_distance), params);
  127. }
  128. // setting default predecessors map
  129. template < class Graph, class P, class T, class R, class ResidualCapacity,
  130. class Weight, class Reversed >
  131. void cycle_canceling_dispatch1(Graph& g, Weight weight, Reversed rev,
  132. ResidualCapacity residual_capacity, param_not_found,
  133. const bgl_named_params< P, T, R >& params)
  134. {
  135. typedef typename graph_traits< Graph >::edge_descriptor edge_descriptor;
  136. std::vector< edge_descriptor > p_map(num_vertices(g));
  137. cycle_canceling_dispatch2(g, weight, rev, residual_capacity,
  138. make_iterator_property_map(p_map.begin(),
  139. choose_const_pmap(
  140. get_param(params, vertex_index), g, vertex_index)),
  141. get_param(params, vertex_distance), params);
  142. }
  143. } // detail
  144. template < class Graph, class P, class T, class R >
  145. void cycle_canceling(Graph& g, const bgl_named_params< P, T, R >& params)
  146. {
  147. cycle_canceling_dispatch1(g,
  148. choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
  149. choose_const_pmap(get_param(params, edge_reverse), g, edge_reverse),
  150. choose_pmap(get_param(params, edge_residual_capacity), g,
  151. edge_residual_capacity),
  152. get_param(params, vertex_predecessor), params);
  153. }
  154. template < class Graph > void cycle_canceling(Graph& g)
  155. {
  156. bgl_named_params< int, buffer_param_t > params(0);
  157. cycle_canceling(g, params);
  158. }
  159. }
  160. #endif /* BOOST_GRAPH_CYCLE_CANCELING_HPP */