COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/mincostflows.h @ 524:bd8109f8e2fa

Last change on this file since 524:bd8109f8e2fa was 523:4da6fb104664, checked in by athos, 20 years ago

Started.

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