COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/mincostflows.h @ 523:4da6fb104664

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

Started.

File size: 5.7 KB
Line 
1// -*- c++ -*-
2#ifndef HUGO_MINCOSTFLOWS_H
3#define HUGO_MINCOSTFLOWS_H
4
5///\ingroup galgs
6///\file
7///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost
8
9#include <iostream>
10#include <dijkstra.h>
11#include <graph_wrapper.h>
12#include <maps.h>
13#include <vector.h>
14
15
16namespace hugo {
17
18/// \addtogroup galgs
19/// @{
20
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  ///
24  ///
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 
28  /// from a given source node to a given target node in an
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.
35  ///
36  ///\author Attila Bernath
37  template <typename Graph, typename LengthMap>
38  class MinCostFlows {
39
40    typedef typename LengthMap::ValueType Length;
41   
42    typedef typename Graph::Node Node;
43    typedef typename Graph::NodeIt NodeIt;
44    typedef typename Graph::Edge Edge;
45    typedef typename Graph::OutEdgeIt OutEdgeIt;
46    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
47
48    typedef ConstMap<Edge,int> ConstMap;
49
50    typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
51
52    class ModLengthMap {   
53      typedef typename ResGraphType::template NodeMap<Length> NodeMap;
54      const ResGraphType& G;
55      const EdgeIntMap& rev;
56      const LengthMap &ol;
57      const NodeMap &pot;
58    public :
59      typedef typename LengthMap::KeyType KeyType;
60      typedef typename LengthMap::ValueType ValueType;
61       
62      ValueType operator[](typename ResGraphType::Edge e) const {     
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        //}
66        return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
67      }     
68       
69      ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
70                   const LengthMap &o,  const NodeMap &p) :
71        G(_G), rev(_rev), ol(o), pot(p){};
72    };//ModLengthMap
73
74
75   
76
77    const Graph& G;
78    const LengthMap& length;
79
80    //auxiliary variables
81
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
85    EdgeIntMap reversed;
86   
87    //Container to store found paths
88    std::vector< std::vector<Edge> > paths;
89    //typedef DirPath<Graph> DPath;
90    //DPath paths;
91
92
93    Length total_length;
94
95  public :
96
97
98    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
99      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
100
101   
102    ///Runs the algorithm.
103
104    ///Runs the algorithm.
105    ///Returns k if there are at least k edge-disjoint paths from s to t.
106    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
107    int run(Node s, Node t, int k) {
108      ConstMap const1map(1);
109
110
111      //We need a residual graph, in which some of the edges are reversed
112      ResGraphType res_graph(G, const1map, reversed);
113
114      //Initialize the copy of the Dijkstra potential to zero
115      typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
116      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
117
118      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
119
120      int i;
121      for (i=0; i<k; ++i){
122        dijkstra.run(s);
123        if (!dijkstra.reached(t)){
124          //There are no k paths from s to t
125          break;
126        };
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
137        //Reversing the sortest path
138        Node n=t;
139        Edge e;
140        while (n!=s){
141          e = dijkstra.pred(n);
142          n = dijkstra.predNode(n);
143          reversed[e] = 1-reversed[e];
144        }
145
146         
147      }
148     
149      //Let's find the paths
150      //We put the paths into stl vectors (as an inner representation).
151      //In the meantime we lose the information stored in 'reversed'.
152      //We suppose the lengths to be positive now.
153
154      //Meanwhile we put the total length of the found paths
155      //in the member variable total_length
156      paths.clear();
157      total_length=0;
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);
173          total_length += length[e];
174          reversed[e] = 1-reversed[e];
175        }
176       
177      }
178
179      return i;
180    }
181
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.
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.
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 ){
197        B.pushBack(*i);
198      }
199
200      B.commit();
201    }
202
203  }; //class MinLengthPaths
204
205  ///@}
206
207} //namespace hugo
208
209#endif //HUGO_MINLENGTHPATHS_H
Note: See TracBrowser for help on using the repository browser.