COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/mincostflows.h @ 548:61898ac9e9dc

Last change on this file since 548:61898ac9e9dc was 547:50184b822370, checked in by athos, 20 years ago

Modified a little bit

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