cycle_canceling.hpp 6.3 KB

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