annotate DEPENDENCIES/generic/include/boost/graph/cycle_canceling.hpp @ 133:4acb5d8d80b6 tip

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