1 //=======================================================================
2 // Copyright 2013 University of Warsaw.
3 // Authors: Piotr Wygocki
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 //=======================================================================
11 //This algorithm is described in "Network Flows: Theory, Algorithms, and Applications"
12 // by Ahuja, Magnanti, Orlin.
14 #ifndef BOOST_GRAPH_CYCLE_CANCELING_HPP
15 #define BOOST_GRAPH_CYCLE_CANCELING_HPP
19 #include <boost/property_map/property_map.hpp>
20 #include <boost/graph/graph_traits.hpp>
21 #include <boost/graph/graph_concepts.hpp>
22 #include <boost/pending/indirect_cmp.hpp>
23 #include <boost/pending/relaxed_heap.hpp>
24 #include <boost/graph/bellman_ford_shortest_paths.hpp>
25 #include <boost/graph/iteration_macros.hpp>
26 #include <boost/graph/detail/augment.hpp>
27 #include <boost/graph/find_flow_cost.hpp>
34 template <typename PredEdgeMap, typename Vertex>
35 class RecordEdgeMapAndCycleVertex
36 : public bellman_visitor<edge_predecessor_recorder<PredEdgeMap, on_edge_relaxed> > {
37 typedef edge_predecessor_recorder<PredEdgeMap, on_edge_relaxed> PredRec;
39 RecordEdgeMapAndCycleVertex(PredEdgeMap pred, Vertex & v) :
40 bellman_visitor<PredRec>(PredRec(pred)), m_v(v), m_pred(pred) {}
42 template <typename Graph, typename Edge>
43 void edge_not_minimized(Edge e, const Graph & g) const {
44 typename graph_traits<Graph>::vertices_size_type n = num_vertices(g) + 1;
46 //edge e is not minimized but does not have to be on the negative weight cycle
47 //to find vertex on negative wieight cycle we move n+1 times backword in the PredEdgeMap graph.
49 e = get(m_pred, source(e, g));
62 template <class Graph, class Pred, class Distance, class Reversed, class ResidualCapacity, class Weight>
63 void cycle_canceling(const Graph &g, Weight weight, Reversed rev, ResidualCapacity residual_capacity, Pred pred, Distance distance) {
64 typedef filtered_graph<const Graph, is_residual_edge<ResidualCapacity> > ResGraph;
65 ResGraph gres = detail::residual_graph(g, residual_capacity);
67 typedef graph_traits<ResGraph> ResGTraits;
68 typedef graph_traits<Graph> GTraits;
69 typedef typename ResGTraits::edge_descriptor edge_descriptor;
70 typedef typename ResGTraits::vertex_descriptor vertex_descriptor;
72 typename GTraits::vertices_size_type N = num_vertices(g);
74 BGL_FORALL_VERTICES_T(v, g, Graph) {
75 put(pred, v, edge_descriptor());
79 vertex_descriptor cycleStart;
80 while(!bellman_ford_shortest_paths(gres, N,
82 distance_map(distance).
83 visitor(detail::RecordEdgeMapAndCycleVertex<Pred, vertex_descriptor>(pred, cycleStart)))) {
85 detail::augment(g, cycleStart, cycleStart, pred, residual_capacity, rev);
87 BGL_FORALL_VERTICES_T(v, g, Graph) {
88 put(pred, v, edge_descriptor());
95 //in this namespace argument dispatching takes place
98 template <class Graph, class P, class T, class R, class ResidualCapacity, class Weight, class Reversed, class Pred, class Distance>
99 void cycle_canceling_dispatch2(
103 ResidualCapacity residual_capacity,
106 const bgl_named_params<P, T, R>& params) {
107 cycle_canceling(g, weight, rev, residual_capacity, pred, dist);
110 //setting default distance map
111 template <class Graph, class P, class T, class R, class Pred, class ResidualCapacity, class Weight, class Reversed>
112 void cycle_canceling_dispatch2(
116 ResidualCapacity residual_capacity,
119 const bgl_named_params<P, T, R>& params) {
120 typedef typename property_traits<Weight>::value_type D;
122 std::vector<D> d_map(num_vertices(g));
124 cycle_canceling(g, weight, rev, residual_capacity, pred,
125 make_iterator_property_map(d_map.begin(), choose_const_pmap(get_param(params, vertex_index), g, vertex_index)));
128 template <class Graph, class P, class T, class R, class ResidualCapacity, class Weight, class Reversed, class Pred>
129 void cycle_canceling_dispatch1(
133 ResidualCapacity residual_capacity,
135 const bgl_named_params<P, T, R>& params) {
136 cycle_canceling_dispatch2(g, weight, rev,residual_capacity, pred,
137 get_param(params, vertex_distance), params);
140 //setting default predecessors map
141 template <class Graph, class P, class T, class R, class ResidualCapacity, class Weight, class Reversed>
142 void cycle_canceling_dispatch1(
146 ResidualCapacity residual_capacity,
148 const bgl_named_params<P, T, R>& params) {
149 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
150 std::vector<edge_descriptor> p_map(num_vertices(g));
152 cycle_canceling_dispatch2(g, weight, rev, residual_capacity,
153 make_iterator_property_map(p_map.begin(), choose_const_pmap(get_param(params, vertex_index), g, vertex_index)),
154 get_param(params, vertex_distance), params);
159 template <class Graph, class P, class T, class R>
160 void cycle_canceling(Graph &g,
161 const bgl_named_params<P, T, R>& params) {
162 cycle_canceling_dispatch1(g,
163 choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
164 choose_const_pmap(get_param(params, edge_reverse), g, edge_reverse),
165 choose_pmap(get_param(params, edge_residual_capacity),
166 g, edge_residual_capacity),
167 get_param(params, vertex_predecessor),
171 template <class Graph>
172 void cycle_canceling(Graph &g) {
173 bgl_named_params<int, buffer_param_t> params(0);
174 cycle_canceling(g, params);
180 #endif /* BOOST_GRAPH_CYCLE_CANCELING_HPP */