[276] | 1 | // -*- c++ -*- |
---|
[523] | 2 | #ifndef HUGO_MINCOSTFLOWS_H |
---|
| 3 | #define HUGO_MINCOSTFLOWS_H |
---|
[276] | 4 | |
---|
[491] | 5 | ///\ingroup galgs |
---|
[294] | 6 | ///\file |
---|
[523] | 7 | ///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost |
---|
[294] | 8 | |
---|
[276] | 9 | #include <iostream> |
---|
| 10 | #include <dijkstra.h> |
---|
| 11 | #include <graph_wrapper.h> |
---|
[306] | 12 | #include <maps.h> |
---|
[511] | 13 | #include <vector.h> |
---|
[322] | 14 | |
---|
[306] | 15 | |
---|
[276] | 16 | namespace hugo { |
---|
| 17 | |
---|
[430] | 18 | /// \addtogroup galgs |
---|
| 19 | /// @{ |
---|
[322] | 20 | |
---|
[523] | 21 | ///\brief Implementation of an algorithm for finding a flow of value \c k |
---|
| 22 | ///(for small values of \c k) having minimal total cost between 2 nodes |
---|
| 23 | /// |
---|
[310] | 24 | /// |
---|
[523] | 25 | /// The class \ref hugo::MinCostFlows "MinCostFlows" implements |
---|
| 26 | /// an algorithm for finding a flow of value \c k |
---|
| 27 | ///(for small values of \c k) having minimal total cost |
---|
[310] | 28 | /// from a given source node to a given target node in an |
---|
[523] | 29 | /// edge-weighted directed graph having nonnegative integer capacities. |
---|
| 30 | /// The range of the length (weight) function is nonnegative reals but |
---|
| 31 | /// the range of capacity function is the set of nonnegative integers. |
---|
| 32 | /// It is not a polinomial time algorithm for counting the minimum cost |
---|
| 33 | /// maximal flow, since it counts the minimum cost flow for every value 0..M |
---|
| 34 | /// where \c M is the value of the maximal flow. |
---|
[456] | 35 | /// |
---|
| 36 | ///\author Attila Bernath |
---|
[310] | 37 | template <typename Graph, typename LengthMap> |
---|
[523] | 38 | class MinCostFlows { |
---|
[276] | 39 | |
---|
[310] | 40 | typedef typename LengthMap::ValueType Length; |
---|
[527] | 41 | |
---|
| 42 | typedef typename LengthMap::ValueType Length; |
---|
[511] | 43 | |
---|
[276] | 44 | typedef typename Graph::Node Node; |
---|
| 45 | typedef typename Graph::NodeIt NodeIt; |
---|
| 46 | typedef typename Graph::Edge Edge; |
---|
| 47 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
[511] | 48 | typedef typename Graph::template EdgeMap<int> EdgeIntMap; |
---|
[306] | 49 | |
---|
[527] | 50 | // typedef ConstMap<Edge,int> ConstMap; |
---|
[306] | 51 | |
---|
[527] | 52 | typedef ResGraphWrapper<const Graph,int,EdgeIntMap,EdgeIntMap> ResGraphType; |
---|
[276] | 53 | |
---|
[306] | 54 | class ModLengthMap { |
---|
[511] | 55 | typedef typename ResGraphType::template NodeMap<Length> NodeMap; |
---|
[306] | 56 | const ResGraphType& G; |
---|
[527] | 57 | // const EdgeIntMap& rev; |
---|
[310] | 58 | const LengthMap &ol; |
---|
| 59 | const NodeMap &pot; |
---|
[306] | 60 | public : |
---|
| 61 | typedef typename LengthMap::KeyType KeyType; |
---|
| 62 | typedef typename LengthMap::ValueType ValueType; |
---|
[511] | 63 | |
---|
[306] | 64 | ValueType operator[](typename ResGraphType::Edge e) const { |
---|
[527] | 65 | if (G.forward(e)) |
---|
| 66 | return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); |
---|
| 67 | else |
---|
| 68 | return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); |
---|
[306] | 69 | } |
---|
[511] | 70 | |
---|
[310] | 71 | ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev, |
---|
| 72 | const LengthMap &o, const NodeMap &p) : |
---|
[527] | 73 | G(_G), /*rev(_rev),*/ ol(o), pot(p){}; |
---|
[511] | 74 | };//ModLengthMap |
---|
| 75 | |
---|
| 76 | |
---|
[306] | 77 | |
---|
[527] | 78 | //Input |
---|
[276] | 79 | const Graph& G; |
---|
| 80 | const LengthMap& length; |
---|
[527] | 81 | const EdgeIntMap& capacity; |
---|
[276] | 82 | |
---|
[328] | 83 | //auxiliary variables |
---|
[322] | 84 | |
---|
[314] | 85 | //The value is 1 iff the edge is reversed. |
---|
| 86 | //If the algorithm has finished, the edges of the seeked paths are |
---|
| 87 | //exactly those that are reversed |
---|
[527] | 88 | EdgeIntMap flow; |
---|
[276] | 89 | |
---|
[322] | 90 | //Container to store found paths |
---|
| 91 | std::vector< std::vector<Edge> > paths; |
---|
[511] | 92 | //typedef DirPath<Graph> DPath; |
---|
| 93 | //DPath paths; |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | Length total_length; |
---|
[322] | 97 | |
---|
[276] | 98 | public : |
---|
[310] | 99 | |
---|
[276] | 100 | |
---|
[527] | 101 | MinLengthPaths(Graph& _G, LengthMap& _length, EdgeIntMap& _cap) : G(_G), |
---|
| 102 | length(_length), capacity(_cap), flow(_G)/*, dijkstra_dist(_G)*/{ } |
---|
[276] | 103 | |
---|
[294] | 104 | |
---|
[329] | 105 | ///Runs the algorithm. |
---|
| 106 | |
---|
| 107 | ///Runs the algorithm. |
---|
[306] | 108 | ///Returns k if there are at least k edge-disjoint paths from s to t. |
---|
[329] | 109 | ///Otherwise it returns the number of found edge-disjoint paths from s to t. |
---|
[306] | 110 | int run(Node s, Node t, int k) { |
---|
[276] | 111 | |
---|
[511] | 112 | |
---|
[527] | 113 | //We need a residual graph |
---|
| 114 | ResGraphType res_graph(G, capacity, flow); |
---|
[306] | 115 | |
---|
| 116 | //Initialize the copy of the Dijkstra potential to zero |
---|
[511] | 117 | typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph); |
---|
[527] | 118 | ModLengthMap mod_length(res_graph, length, dijkstra_dist); |
---|
[306] | 119 | |
---|
| 120 | Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length); |
---|
[322] | 121 | |
---|
| 122 | int i; |
---|
| 123 | for (i=0; i<k; ++i){ |
---|
[276] | 124 | dijkstra.run(s); |
---|
| 125 | if (!dijkstra.reached(t)){ |
---|
[314] | 126 | //There are no k paths from s to t |
---|
[322] | 127 | break; |
---|
[276] | 128 | }; |
---|
[306] | 129 | |
---|
| 130 | { |
---|
| 131 | //We have to copy the potential |
---|
| 132 | typename ResGraphType::NodeIt n; |
---|
| 133 | for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) { |
---|
| 134 | dijkstra_dist[n] += dijkstra.distMap()[n]; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
[527] | 139 | //Augmenting on the sortest path |
---|
[276] | 140 | Node n=t; |
---|
| 141 | Edge e; |
---|
| 142 | while (n!=s){ |
---|
[291] | 143 | e = dijkstra.pred(n); |
---|
| 144 | n = dijkstra.predNode(n); |
---|
[527] | 145 | G.augment(e,1); |
---|
[276] | 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | } |
---|
[322] | 150 | |
---|
[527] | 151 | /* |
---|
| 152 | ///\TODO To be implemented later |
---|
| 153 | |
---|
[322] | 154 | //Let's find the paths |
---|
[511] | 155 | //We put the paths into stl vectors (as an inner representation). |
---|
| 156 | //In the meantime we lose the information stored in 'reversed'. |
---|
[322] | 157 | //We suppose the lengths to be positive now. |
---|
[511] | 158 | |
---|
| 159 | //Meanwhile we put the total length of the found paths |
---|
| 160 | //in the member variable total_length |
---|
[322] | 161 | paths.clear(); |
---|
[511] | 162 | total_length=0; |
---|
[322] | 163 | paths.resize(k); |
---|
| 164 | for (int j=0; j<i; ++j){ |
---|
| 165 | Node n=s; |
---|
| 166 | OutEdgeIt e; |
---|
| 167 | |
---|
| 168 | while (n!=t){ |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | G.first(e,n); |
---|
| 172 | |
---|
| 173 | while (!reversed[e]){ |
---|
| 174 | G.next(e); |
---|
| 175 | } |
---|
| 176 | n = G.head(e); |
---|
| 177 | paths[j].push_back(e); |
---|
[511] | 178 | total_length += length[e]; |
---|
[322] | 179 | reversed[e] = 1-reversed[e]; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | } |
---|
[527] | 183 | */ |
---|
[322] | 184 | |
---|
| 185 | return i; |
---|
[276] | 186 | } |
---|
| 187 | |
---|
[511] | 188 | ///This function gives back the total length of the found paths. |
---|
| 189 | ///Assumes that \c run() has been run and nothing changed since then. |
---|
| 190 | Length totalLength(){ |
---|
| 191 | return total_length; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | ///This function gives back the \c j-th path in argument p. |
---|
| 195 | ///Assumes that \c run() has been run and nothing changed since then. |
---|
[519] | 196 | /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path. |
---|
[511] | 197 | template<typename DirPath> |
---|
| 198 | void getPath(DirPath& p, int j){ |
---|
| 199 | p.clear(); |
---|
| 200 | typename DirPath::Builder B(p); |
---|
| 201 | for(typename std::vector<Edge>::iterator i=paths[j].begin(); |
---|
| 202 | i!=paths[j].end(); ++i ){ |
---|
[520] | 203 | B.pushBack(*i); |
---|
[511] | 204 | } |
---|
| 205 | |
---|
| 206 | B.commit(); |
---|
| 207 | } |
---|
[276] | 208 | |
---|
[310] | 209 | }; //class MinLengthPaths |
---|
[276] | 210 | |
---|
[430] | 211 | ///@} |
---|
[276] | 212 | |
---|
| 213 | } //namespace hugo |
---|
| 214 | |
---|
[527] | 215 | #endif //HUGO_MINCOSTFLOW_H |
---|