COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/mincostflows.h @ 539:fb261e3a9a0f

Last change on this file since 539:fb261e3a9a0f was 530:d9c06ac0b3a3, checked in by athos, 21 years ago

Minimum cost flows of small values: algorithm from Andras Frank's lecture notes (approximately)

File size: 5.5 KB
RevLine 
[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>
[530]14#include <for_each_macros.h>
[306]15
[276]16namespace 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
[530]37  template <typename Graph, typename LengthMap, typename CapacityMap>
[523]38  class MinCostFlows {
[276]39
[310]40    typedef typename LengthMap::ValueType Length;
[527]41
[530]42    //Warning: this should be integer type
43    typedef typename CapacityMap::ValueType Capacity;
[511]44   
[276]45    typedef typename Graph::Node Node;
46    typedef typename Graph::NodeIt NodeIt;
47    typedef typename Graph::Edge Edge;
48    typedef typename Graph::OutEdgeIt OutEdgeIt;
[511]49    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
[306]50
[527]51    //    typedef ConstMap<Edge,int> ConstMap;
[306]52
[530]53    typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
54    typedef typename ResGraphType::Edge ResGraphEdge;
[306]55    class ModLengthMap {   
[511]56      typedef typename ResGraphType::template NodeMap<Length> NodeMap;
[306]57      const ResGraphType& G;
[527]58      //      const EdgeIntMap& rev;
[310]59      const LengthMap &ol;
60      const NodeMap &pot;
[306]61    public :
62      typedef typename LengthMap::KeyType KeyType;
63      typedef typename LengthMap::ValueType ValueType;
[511]64       
[306]65      ValueType operator[](typename ResGraphType::Edge e) const {     
[527]66        if (G.forward(e))
67          return  ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
68        else
69          return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
[306]70      }     
[511]71       
[530]72      ModLengthMap(const ResGraphType& _G,
[310]73                   const LengthMap &o,  const NodeMap &p) :
[527]74        G(_G), /*rev(_rev),*/ ol(o), pot(p){};
[511]75    };//ModLengthMap
76
77
[306]78   
[527]79    //Input
[276]80    const Graph& G;
81    const LengthMap& length;
[530]82    const CapacityMap& capacity;
[276]83
[328]84    //auxiliary variables
[322]85
[314]86    //The value is 1 iff the edge is reversed.
87    //If the algorithm has finished, the edges of the seeked paths are
88    //exactly those that are reversed
[527]89    EdgeIntMap flow;
[276]90   
[322]91    //Container to store found paths
92    std::vector< std::vector<Edge> > paths;
[511]93    //typedef DirPath<Graph> DPath;
94    //DPath paths;
95
96
97    Length total_length;
[322]98
[276]99  public :
[310]100
[276]101
[530]102    MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G),
[527]103      length(_length), capacity(_cap), flow(_G)/*, dijkstra_dist(_G)*/{ }
[276]104
[294]105   
[329]106    ///Runs the algorithm.
107
108    ///Runs the algorithm.
[306]109    ///Returns k if there are at least k edge-disjoint paths from s to t.
[329]110    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
[306]111    int run(Node s, Node t, int k) {
[276]112
[530]113      //Resetting variables from previous runs
114      total_length = 0;
115      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
116        flow.set(e,0);
117      }
[511]118
[530]119     
[527]120      //We need a residual graph
121      ResGraphType res_graph(G, capacity, flow);
[306]122
123      //Initialize the copy of the Dijkstra potential to zero
[511]124      typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
[527]125      ModLengthMap mod_length(res_graph, length, dijkstra_dist);
[306]126
127      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
[322]128
129      int i;
130      for (i=0; i<k; ++i){
[276]131        dijkstra.run(s);
132        if (!dijkstra.reached(t)){
[314]133          //There are no k paths from s to t
[322]134          break;
[276]135        };
[306]136       
137        {
138          //We have to copy the potential
139          typename ResGraphType::NodeIt n;
140          for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
141              dijkstra_dist[n] += dijkstra.distMap()[n];
142          }
143        }
144
145
[527]146        //Augmenting on the sortest path
[276]147        Node n=t;
[530]148        ResGraphEdge e;
[276]149        while (n!=s){
[291]150          e = dijkstra.pred(n);
151          n = dijkstra.predNode(n);
[530]152          res_graph.augment(e,1);
153          //Let's update the total length
154          if (res_graph.forward(e))
155            total_length += length[e];
156          else
157            total_length -= length[e];     
[276]158        }
159
160         
161      }
[322]162     
163
164      return i;
[276]165    }
166
[530]167
168
[511]169    ///This function gives back the total length of the found paths.
170    ///Assumes that \c run() has been run and nothing changed since then.
171    Length totalLength(){
172      return total_length;
173    }
174
[530]175    /*
176      ///\todo To be implemented later
177
[511]178    ///This function gives back the \c j-th path in argument p.
179    ///Assumes that \c run() has been run and nothing changed since then.
[519]180    /// \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]181    template<typename DirPath>
182    void getPath(DirPath& p, int j){
183      p.clear();
184      typename DirPath::Builder B(p);
185      for(typename std::vector<Edge>::iterator i=paths[j].begin();
186          i!=paths[j].end(); ++i ){
[520]187        B.pushBack(*i);
[511]188      }
189
190      B.commit();
191    }
[276]192
[530]193    */
194
195  }; //class MinCostFlows
[276]196
[430]197  ///@}
[276]198
199} //namespace hugo
200
[527]201#endif //HUGO_MINCOSTFLOW_H
Note: See TracBrowser for help on using the repository browser.