[610] | 1 | // -*- c++ -*- |
---|
[921] | 2 | #ifndef LEMON_MINCOSTFLOW_H |
---|
| 3 | #define LEMON_MINCOSTFLOW_H |
---|
[610] | 4 | |
---|
| 5 | ///\ingroup galgs |
---|
| 6 | ///\file |
---|
[645] | 7 | ///\brief An algorithm for finding the minimum cost flow of given value in an uncapacitated network |
---|
[611] | 8 | |
---|
[921] | 9 | #include <lemon/dijkstra.h> |
---|
| 10 | #include <lemon/graph_wrapper.h> |
---|
| 11 | #include <lemon/maps.h> |
---|
[610] | 12 | #include <vector> |
---|
[657] | 13 | #include <list> |
---|
[662] | 14 | #include <values.h> |
---|
[921] | 15 | #include <lemon/for_each_macros.h> |
---|
| 16 | #include <lemon/unionfind.h> |
---|
| 17 | #include <lemon/bin_heap.h> |
---|
[662] | 18 | #include <bfs_dfs.h> |
---|
[610] | 19 | |
---|
[921] | 20 | namespace lemon { |
---|
[610] | 21 | |
---|
| 22 | /// \addtogroup galgs |
---|
| 23 | /// @{ |
---|
| 24 | |
---|
[661] | 25 | ///\brief Implementation of an algorithm for solving the minimum cost general |
---|
| 26 | /// flow problem in an uncapacitated network |
---|
[610] | 27 | /// |
---|
| 28 | /// |
---|
[921] | 29 | /// The class \ref lemon::MinCostFlow "MinCostFlow" implements |
---|
[633] | 30 | /// an algorithm for solving the following general minimum cost flow problem> |
---|
| 31 | /// |
---|
| 32 | /// |
---|
| 33 | /// |
---|
| 34 | /// \warning It is assumed here that the problem has a feasible solution |
---|
| 35 | /// |
---|
[661] | 36 | /// The range of the cost (weight) function is nonnegative reals but |
---|
[610] | 37 | /// the range of capacity function is the set of nonnegative integers. |
---|
| 38 | /// It is not a polinomial time algorithm for counting the minimum cost |
---|
| 39 | /// maximal flow, since it counts the minimum cost flow for every value 0..M |
---|
| 40 | /// where \c M is the value of the maximal flow. |
---|
| 41 | /// |
---|
| 42 | ///\author Attila Bernath |
---|
[661] | 43 | template <typename Graph, typename CostMap, typename SupplyDemandMap> |
---|
[633] | 44 | class MinCostFlow { |
---|
[610] | 45 | |
---|
[987] | 46 | typedef typename CostMap::Value Cost; |
---|
[610] | 47 | |
---|
[633] | 48 | |
---|
[987] | 49 | typedef typename SupplyDemandMap::Value SupplyDemand; |
---|
[610] | 50 | |
---|
| 51 | typedef typename Graph::Node Node; |
---|
| 52 | typedef typename Graph::NodeIt NodeIt; |
---|
| 53 | typedef typename Graph::Edge Edge; |
---|
| 54 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
[661] | 55 | typedef typename Graph::template EdgeMap<SupplyDemand> FlowMap; |
---|
| 56 | typedef ConstMap<Edge,SupplyDemand> ConstEdgeMap; |
---|
[610] | 57 | |
---|
| 58 | // typedef ConstMap<Edge,int> ConstMap; |
---|
| 59 | |
---|
[661] | 60 | typedef ResGraphWrapper<const Graph,int,ConstEdgeMap,FlowMap> ResGraph; |
---|
| 61 | typedef typename ResGraph::Edge ResGraphEdge; |
---|
[610] | 62 | |
---|
[661] | 63 | class ModCostMap { |
---|
| 64 | //typedef typename ResGraph::template NodeMap<Cost> NodeMap; |
---|
| 65 | typedef typename Graph::template NodeMap<Cost> NodeMap; |
---|
| 66 | const ResGraph& res_graph; |
---|
[610] | 67 | // const EdgeIntMap& rev; |
---|
[661] | 68 | const CostMap &ol; |
---|
[610] | 69 | const NodeMap &pot; |
---|
| 70 | public : |
---|
[987] | 71 | typedef typename CostMap::Key Key; |
---|
| 72 | typedef typename CostMap::Value Value; |
---|
[610] | 73 | |
---|
[987] | 74 | Value operator[](typename ResGraph::Edge e) const { |
---|
[659] | 75 | if (res_graph.forward(e)) |
---|
[986] | 76 | return ol[e]-(pot[res_graph.target(e)]-pot[res_graph.source(e)]); |
---|
[610] | 77 | else |
---|
[986] | 78 | return -ol[e]-(pot[res_graph.target(e)]-pot[res_graph.source(e)]); |
---|
[610] | 79 | } |
---|
| 80 | |
---|
[661] | 81 | ModCostMap(const ResGraph& _res_graph, |
---|
| 82 | const CostMap &o, const NodeMap &p) : |
---|
[659] | 83 | res_graph(_res_graph), /*rev(_rev),*/ ol(o), pot(p){}; |
---|
[661] | 84 | };//ModCostMap |
---|
[610] | 85 | |
---|
| 86 | |
---|
| 87 | protected: |
---|
| 88 | |
---|
| 89 | //Input |
---|
[659] | 90 | const Graph& graph; |
---|
[661] | 91 | const CostMap& cost; |
---|
[635] | 92 | const SupplyDemandMap& supply_demand;//supply or demand of nodes |
---|
[610] | 93 | |
---|
| 94 | |
---|
| 95 | //auxiliary variables |
---|
| 96 | |
---|
| 97 | //To store the flow |
---|
[661] | 98 | FlowMap flow; |
---|
[662] | 99 | //To store the potential (dual variables) |
---|
| 100 | typedef typename Graph::template NodeMap<Cost> PotentialMap; |
---|
| 101 | PotentialMap potential; |
---|
[610] | 102 | |
---|
| 103 | |
---|
[661] | 104 | Cost total_cost; |
---|
[610] | 105 | |
---|
| 106 | |
---|
| 107 | public : |
---|
| 108 | |
---|
| 109 | |
---|
[662] | 110 | MinCostFlow(Graph& _graph, CostMap& _cost, SupplyDemandMap& _supply_demand): |
---|
| 111 | graph(_graph), |
---|
| 112 | cost(_cost), |
---|
| 113 | supply_demand(_supply_demand), |
---|
| 114 | flow(_graph), |
---|
[672] | 115 | potential(_graph){ } |
---|
[610] | 116 | |
---|
| 117 | |
---|
| 118 | ///Runs the algorithm. |
---|
| 119 | |
---|
| 120 | ///Runs the algorithm. |
---|
[635] | 121 | |
---|
[610] | 122 | ///\todo May be it does make sense to be able to start with a nonzero |
---|
| 123 | /// feasible primal-dual solution pair as well. |
---|
[659] | 124 | void run() { |
---|
[610] | 125 | |
---|
[672] | 126 | //To store excess-deficit values |
---|
| 127 | SupplyDemandMap excess_deficit(graph); |
---|
| 128 | |
---|
[610] | 129 | //Resetting variables from previous runs |
---|
[661] | 130 | //total_cost = 0; |
---|
[635] | 131 | |
---|
[672] | 132 | |
---|
[635] | 133 | typedef typename Graph::template NodeMap<int> HeapMap; |
---|
[662] | 134 | typedef BinHeap< Node, SupplyDemand, typename Graph::template NodeMap<int>, |
---|
[635] | 135 | std::greater<SupplyDemand> > HeapType; |
---|
| 136 | |
---|
| 137 | //A heap for the excess nodes |
---|
[659] | 138 | HeapMap excess_nodes_map(graph,-1); |
---|
[635] | 139 | HeapType excess_nodes(excess_nodes_map); |
---|
| 140 | |
---|
| 141 | //A heap for the deficit nodes |
---|
[659] | 142 | HeapMap deficit_nodes_map(graph,-1); |
---|
[635] | 143 | HeapType deficit_nodes(deficit_nodes_map); |
---|
| 144 | |
---|
[657] | 145 | //A container to store nonabundant arcs |
---|
[662] | 146 | std::list<Edge> nonabundant_arcs; |
---|
[659] | 147 | |
---|
| 148 | |
---|
| 149 | FOR_EACH_LOC(typename Graph::EdgeIt, e, graph){ |
---|
[610] | 150 | flow.set(e,0); |
---|
[657] | 151 | nonabundant_arcs.push_back(e); |
---|
[610] | 152 | } |
---|
[633] | 153 | |
---|
| 154 | //Initial value for delta |
---|
[635] | 155 | SupplyDemand delta = 0; |
---|
| 156 | |
---|
[657] | 157 | typedef UnionFindEnum<Node, Graph::template NodeMap> UFE; |
---|
| 158 | |
---|
| 159 | //A union-find structure to store the abundant components |
---|
[662] | 160 | typename UFE::MapType abund_comp_map(graph); |
---|
[657] | 161 | UFE abundant_components(abund_comp_map); |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|
[659] | 165 | FOR_EACH_LOC(typename Graph::NodeIt, n, graph){ |
---|
[635] | 166 | excess_deficit.set(n,supply_demand[n]); |
---|
| 167 | //A supply node |
---|
| 168 | if (excess_deficit[n] > 0){ |
---|
| 169 | excess_nodes.push(n,excess_deficit[n]); |
---|
[633] | 170 | } |
---|
[635] | 171 | //A demand node |
---|
| 172 | if (excess_deficit[n] < 0){ |
---|
| 173 | deficit_nodes.push(n, - excess_deficit[n]); |
---|
| 174 | } |
---|
| 175 | //Finding out starting value of delta |
---|
| 176 | if (delta < abs(excess_deficit[n])){ |
---|
| 177 | delta = abs(excess_deficit[n]); |
---|
| 178 | } |
---|
[633] | 179 | //Initialize the copy of the Dijkstra potential to zero |
---|
[610] | 180 | potential.set(n,0); |
---|
[657] | 181 | //Every single point is an abundant component initially |
---|
| 182 | abundant_components.insert(n); |
---|
[610] | 183 | } |
---|
| 184 | |
---|
[635] | 185 | //It'll be allright as an initial value, though this value |
---|
| 186 | //can be the maximum deficit here |
---|
| 187 | SupplyDemand max_excess = delta; |
---|
[610] | 188 | |
---|
[661] | 189 | ///\bug This is a serious cheat here, before we have an uncapacitated ResGraph |
---|
[662] | 190 | ConstEdgeMap const_inf_map(MAXINT); |
---|
[661] | 191 | |
---|
[633] | 192 | //We need a residual graph which is uncapacitated |
---|
[661] | 193 | ResGraph res_graph(graph, const_inf_map, flow); |
---|
[659] | 194 | |
---|
| 195 | //An EdgeMap to tell which arcs are abundant |
---|
[662] | 196 | typename Graph::template EdgeMap<bool> abundant_arcs(graph); |
---|
[610] | 197 | |
---|
[659] | 198 | //Let's construct the sugraph consisting only of the abundant edges |
---|
| 199 | typedef ConstMap< typename Graph::Node, bool > ConstNodeMap; |
---|
[672] | 200 | |
---|
[659] | 201 | ConstNodeMap const_true_map(true); |
---|
[662] | 202 | typedef SubGraphWrapper< const Graph, ConstNodeMap, |
---|
| 203 | typename Graph::template EdgeMap<bool> > |
---|
[659] | 204 | AbundantGraph; |
---|
| 205 | AbundantGraph abundant_graph(graph, const_true_map, abundant_arcs ); |
---|
| 206 | |
---|
| 207 | //Let's construct the residual graph for the abundant graph |
---|
[662] | 208 | typedef ResGraphWrapper<const AbundantGraph,int,ConstEdgeMap,FlowMap> |
---|
[659] | 209 | ResAbGraph; |
---|
| 210 | //Again uncapacitated |
---|
[661] | 211 | ResAbGraph res_ab_graph(abundant_graph, const_inf_map, flow); |
---|
[659] | 212 | |
---|
| 213 | //We need things for the bfs |
---|
[662] | 214 | typename ResAbGraph::template NodeMap<bool> bfs_reached(res_ab_graph); |
---|
| 215 | typename ResAbGraph::template NodeMap<typename ResAbGraph::Edge> |
---|
[659] | 216 | bfs_pred(res_ab_graph); |
---|
[662] | 217 | NullMap<typename ResAbGraph::Node, int> bfs_dist_dummy; |
---|
[671] | 218 | //Teszt celbol: |
---|
| 219 | //BfsIterator<ResAbGraph, typename ResAbGraph::template NodeMap<bool> > |
---|
| 220 | //izebize(res_ab_graph, bfs_reached); |
---|
[659] | 221 | //We want to run bfs-es (more) on this graph 'res_ab_graph' |
---|
[671] | 222 | Bfs < const ResAbGraph , |
---|
[662] | 223 | typename ResAbGraph::template NodeMap<bool>, |
---|
| 224 | typename ResAbGraph::template NodeMap<typename ResAbGraph::Edge>, |
---|
[659] | 225 | NullMap<typename ResAbGraph::Node, int> > |
---|
| 226 | bfs(res_ab_graph, bfs_reached, bfs_pred, bfs_dist_dummy); |
---|
[662] | 227 | /*This is what Marci wants for a bfs |
---|
| 228 | template <typename Graph, |
---|
| 229 | typename ReachedMap=typename Graph::template NodeMap<bool>, |
---|
| 230 | typename PredMap |
---|
| 231 | =typename Graph::template NodeMap<typename Graph::Edge>, |
---|
| 232 | typename DistMap=typename Graph::template NodeMap<int> > |
---|
| 233 | class Bfs : public BfsIterator<Graph, ReachedMap> { |
---|
| 234 | |
---|
| 235 | */ |
---|
[610] | 236 | |
---|
[661] | 237 | ModCostMap mod_cost(res_graph, cost, potential); |
---|
[610] | 238 | |
---|
[661] | 239 | Dijkstra<ResGraph, ModCostMap> dijkstra(res_graph, mod_cost); |
---|
[610] | 240 | |
---|
[671] | 241 | //We will use the number of the nodes of the graph often |
---|
| 242 | int number_of_nodes = graph.nodeNum(); |
---|
[633] | 243 | |
---|
[635] | 244 | while (max_excess > 0){ |
---|
| 245 | |
---|
[657] | 246 | //Reset delta if still too big |
---|
| 247 | if (8*number_of_nodes*max_excess <= delta){ |
---|
| 248 | delta = max_excess; |
---|
| 249 | |
---|
| 250 | } |
---|
| 251 | |
---|
[645] | 252 | /* |
---|
| 253 | * Beginning of the delta scaling phase |
---|
| 254 | */ |
---|
[635] | 255 | //Merge and stuff |
---|
[657] | 256 | { |
---|
| 257 | SupplyDemand buf=8*number_of_nodes*delta; |
---|
[662] | 258 | typename std::list<Edge>::iterator i = nonabundant_arcs.begin(); |
---|
[657] | 259 | while ( i != nonabundant_arcs.end() ){ |
---|
[671] | 260 | if (flow[*i]>=buf){ |
---|
[986] | 261 | Node a = abundant_components.find(res_graph.target(*i)); |
---|
| 262 | Node b = abundant_components.find(res_graph.source(*i)); |
---|
[657] | 263 | //Merge |
---|
| 264 | if (a != b){ |
---|
| 265 | abundant_components.join(a,b); |
---|
[659] | 266 | //We want to push the smaller |
---|
| 267 | //Which has greater absolut value excess/deficit |
---|
| 268 | Node root=(abs(excess_deficit[a])>abs(excess_deficit[b]))?a:b; |
---|
| 269 | //Which is the other |
---|
| 270 | Node non_root = ( a == root ) ? b : a ; |
---|
| 271 | abundant_components.makeRep(root); |
---|
| 272 | SupplyDemand qty_to_augment = abs(excess_deficit[non_root]); |
---|
| 273 | //Push the positive value |
---|
| 274 | if (excess_deficit[non_root] < 0) |
---|
| 275 | swap(root, non_root); |
---|
| 276 | //If the non_root node has excess/deficit at all |
---|
| 277 | if (qty_to_augment>0){ |
---|
| 278 | //Find path and augment |
---|
[671] | 279 | bfs.run(typename AbundantGraph::Node(non_root)); |
---|
[659] | 280 | //root should be reached |
---|
| 281 | |
---|
| 282 | //Augmenting on the found path |
---|
| 283 | Node n=root; |
---|
| 284 | ResGraphEdge e; |
---|
| 285 | while (n!=non_root){ |
---|
[671] | 286 | e = bfs_pred[n]; |
---|
[986] | 287 | n = res_graph.source(e); |
---|
[659] | 288 | res_graph.augment(e,qty_to_augment); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | //We know that non_root had positive excess |
---|
[671] | 292 | excess_nodes.set(non_root, |
---|
| 293 | excess_nodes[non_root] - qty_to_augment); |
---|
[659] | 294 | //But what about root node |
---|
| 295 | //It might have been positive and so became larger |
---|
| 296 | if (excess_deficit[root]>0){ |
---|
[671] | 297 | excess_nodes.set(root, |
---|
| 298 | excess_nodes[root] + qty_to_augment); |
---|
[659] | 299 | } |
---|
| 300 | else{ |
---|
| 301 | //Or negative but not turned into positive |
---|
[671] | 302 | deficit_nodes.set(root, |
---|
| 303 | deficit_nodes[root] - qty_to_augment); |
---|
[659] | 304 | } |
---|
| 305 | |
---|
| 306 | //Update the excess_deficit map |
---|
| 307 | excess_deficit[non_root] -= qty_to_augment; |
---|
| 308 | excess_deficit[root] += qty_to_augment; |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | } |
---|
[657] | 312 | } |
---|
| 313 | //What happens to i? |
---|
[659] | 314 | //Marci and Zsolt says I shouldn't do such things |
---|
| 315 | nonabundant_arcs.erase(i++); |
---|
[671] | 316 | abundant_arcs[*i] = true; |
---|
[657] | 317 | } |
---|
| 318 | else |
---|
| 319 | ++i; |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
[635] | 323 | |
---|
| 324 | Node s = excess_nodes.top(); |
---|
[672] | 325 | max_excess = excess_nodes[s]; |
---|
[635] | 326 | Node t = deficit_nodes.top(); |
---|
[659] | 327 | if (max_excess < deficit_nodes[t]){ |
---|
| 328 | max_excess = deficit_nodes[t]; |
---|
[635] | 329 | } |
---|
| 330 | |
---|
| 331 | |
---|
[662] | 332 | while(max_excess > (number_of_nodes-1)*delta/number_of_nodes){ |
---|
[659] | 333 | |
---|
[635] | 334 | |
---|
| 335 | //s es t valasztasa |
---|
[659] | 336 | |
---|
[635] | 337 | //Dijkstra part |
---|
| 338 | dijkstra.run(s); |
---|
[659] | 339 | |
---|
[635] | 340 | /*We know from theory that t can be reached |
---|
| 341 | if (!dijkstra.reached(t)){ |
---|
| 342 | //There are no k paths from s to t |
---|
| 343 | break; |
---|
| 344 | }; |
---|
| 345 | */ |
---|
| 346 | |
---|
| 347 | //We have to change the potential |
---|
[661] | 348 | FOR_EACH_LOC(typename ResGraph::NodeIt, n, res_graph){ |
---|
[635] | 349 | potential[n] += dijkstra.distMap()[n]; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | |
---|
| 353 | //Augmenting on the sortest path |
---|
| 354 | Node n=t; |
---|
| 355 | ResGraphEdge e; |
---|
| 356 | while (n!=s){ |
---|
| 357 | e = dijkstra.pred(n); |
---|
| 358 | n = dijkstra.predNode(n); |
---|
| 359 | res_graph.augment(e,delta); |
---|
| 360 | /* |
---|
[661] | 361 | //Let's update the total cost |
---|
[635] | 362 | if (res_graph.forward(e)) |
---|
[661] | 363 | total_cost += cost[e]; |
---|
[635] | 364 | else |
---|
[661] | 365 | total_cost -= cost[e]; |
---|
[635] | 366 | */ |
---|
| 367 | } |
---|
[659] | 368 | |
---|
| 369 | //Update the excess_deficit map |
---|
| 370 | excess_deficit[s] -= delta; |
---|
| 371 | excess_deficit[t] += delta; |
---|
| 372 | |
---|
[635] | 373 | |
---|
| 374 | //Update the excess_nodes heap |
---|
[672] | 375 | if (delta > excess_nodes[s]){ |
---|
[635] | 376 | if (delta > excess_nodes[s]) |
---|
| 377 | deficit_nodes.push(s,delta - excess_nodes[s]); |
---|
| 378 | excess_nodes.pop(); |
---|
| 379 | |
---|
| 380 | } |
---|
| 381 | else{ |
---|
[671] | 382 | excess_nodes.set(s, excess_nodes[s] - delta); |
---|
[635] | 383 | } |
---|
| 384 | //Update the deficit_nodes heap |
---|
[672] | 385 | if (delta > deficit_nodes[t]){ |
---|
[635] | 386 | if (delta > deficit_nodes[t]) |
---|
| 387 | excess_nodes.push(t,delta - deficit_nodes[t]); |
---|
| 388 | deficit_nodes.pop(); |
---|
| 389 | |
---|
| 390 | } |
---|
| 391 | else{ |
---|
[671] | 392 | deficit_nodes.set(t, deficit_nodes[t] - delta); |
---|
[635] | 393 | } |
---|
| 394 | //Dijkstra part ends here |
---|
[659] | 395 | |
---|
| 396 | //Choose s and t again |
---|
| 397 | s = excess_nodes.top(); |
---|
| 398 | max_excess = excess_nodes[s]; |
---|
| 399 | t = deficit_nodes.top(); |
---|
| 400 | if (max_excess < deficit_nodes[t]){ |
---|
| 401 | max_excess = deficit_nodes[t]; |
---|
| 402 | } |
---|
| 403 | |
---|
[633] | 404 | } |
---|
| 405 | |
---|
| 406 | /* |
---|
[635] | 407 | * End of the delta scaling phase |
---|
| 408 | */ |
---|
[633] | 409 | |
---|
[635] | 410 | //Whatever this means |
---|
| 411 | delta = delta / 2; |
---|
| 412 | |
---|
| 413 | /*This is not necessary here |
---|
| 414 | //Update the max_excess |
---|
| 415 | max_excess = 0; |
---|
[659] | 416 | FOR_EACH_LOC(typename Graph::NodeIt, n, graph){ |
---|
[635] | 417 | if (max_excess < excess_deficit[n]){ |
---|
| 418 | max_excess = excess_deficit[n]; |
---|
[610] | 419 | } |
---|
| 420 | } |
---|
[633] | 421 | */ |
---|
[657] | 422 | |
---|
[610] | 423 | |
---|
[635] | 424 | }//while(max_excess > 0) |
---|
[610] | 425 | |
---|
| 426 | |
---|
[671] | 427 | //return i; |
---|
[610] | 428 | } |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | |
---|
| 432 | |
---|
[661] | 433 | ///This function gives back the total cost of the found paths. |
---|
[610] | 434 | ///Assumes that \c run() has been run and nothing changed since then. |
---|
[661] | 435 | Cost totalCost(){ |
---|
| 436 | return total_cost; |
---|
[610] | 437 | } |
---|
| 438 | |
---|
| 439 | ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must |
---|
| 440 | ///be called before using this function. |
---|
[662] | 441 | const FlowMap &getFlow() const { return flow;} |
---|
[610] | 442 | |
---|
| 443 | ///Returns a const reference to the NodeMap \c potential (the dual solution). |
---|
| 444 | /// \pre \ref run() must be called before using this function. |
---|
[662] | 445 | const PotentialMap &getPotential() const { return potential;} |
---|
[610] | 446 | |
---|
| 447 | ///This function checks, whether the given solution is optimal |
---|
| 448 | ///Running after a \c run() should return with true |
---|
[672] | 449 | ///In this "state of the art" this only checks optimality, doesn't bother with feasibility |
---|
[610] | 450 | /// |
---|
| 451 | ///\todo Is this OK here? |
---|
| 452 | bool checkComplementarySlackness(){ |
---|
[661] | 453 | Cost mod_pot; |
---|
| 454 | Cost fl_e; |
---|
[659] | 455 | FOR_EACH_LOC(typename Graph::EdgeIt, e, graph){ |
---|
[610] | 456 | //C^{\Pi}_{i,j} |
---|
[986] | 457 | mod_pot = cost[e]-potential[graph.target(e)]+potential[graph.source(e)]; |
---|
[610] | 458 | fl_e = flow[e]; |
---|
| 459 | // std::cout << fl_e << std::endl; |
---|
[672] | 460 | if (mod_pot > 0 && fl_e != 0) |
---|
| 461 | return false; |
---|
| 462 | |
---|
[610] | 463 | } |
---|
| 464 | return true; |
---|
| 465 | } |
---|
[672] | 466 | |
---|
| 467 | /* |
---|
| 468 | //For testing purposes only |
---|
| 469 | //Lists the node_properties |
---|
| 470 | void write_property_vector(const SupplyDemandMap& a, |
---|
| 471 | char* prop_name="property"){ |
---|
| 472 | FOR_EACH_LOC(typename Graph::NodeIt, i, graph){ |
---|
| 473 | cout<<"Node id.: "<<graph.id(i)<<", "<<prop_name<<" value: "<<a[i]<<endl; |
---|
| 474 | } |
---|
| 475 | cout<<endl; |
---|
| 476 | } |
---|
| 477 | */ |
---|
| 478 | bool checkFeasibility(){ |
---|
| 479 | SupplyDemandMap supdem(graph); |
---|
| 480 | FOR_EACH_LOC(typename Graph::EdgeIt, e, graph){ |
---|
| 481 | |
---|
| 482 | if ( flow[e] < 0){ |
---|
| 483 | |
---|
| 484 | return false; |
---|
| 485 | } |
---|
[986] | 486 | supdem[graph.source(e)] += flow[e]; |
---|
| 487 | supdem[graph.target(e)] -= flow[e]; |
---|
[672] | 488 | } |
---|
| 489 | //write_property_vector(supdem, "supdem"); |
---|
| 490 | //write_property_vector(supply_demand, "supply_demand"); |
---|
| 491 | |
---|
| 492 | FOR_EACH_LOC(typename Graph::NodeIt, n, graph){ |
---|
| 493 | |
---|
| 494 | if ( supdem[n] != supply_demand[n]){ |
---|
| 495 | //cout<<"Node id.: "<<graph.id(n)<<" : "<<supdem[n]<<", should be: "<<supply_demand[n]<<endl; |
---|
| 496 | return false; |
---|
| 497 | } |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | return true; |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | bool checkOptimality(){ |
---|
| 504 | return checkFeasibility() && checkComplementarySlackness(); |
---|
| 505 | } |
---|
[610] | 506 | |
---|
[633] | 507 | }; //class MinCostFlow |
---|
[610] | 508 | |
---|
| 509 | ///@} |
---|
| 510 | |
---|
[921] | 511 | } //namespace lemon |
---|
[610] | 512 | |
---|
[921] | 513 | #endif //LEMON_MINCOSTFLOW_H |
---|