src/work/athos/mincostflows.h
author jacint
Tue, 04 May 2004 16:17:17 +0000
changeset 529 e63a1dda5c68
parent 523 4da6fb104664
child 530 d9c06ac0b3a3
permissions -rw-r--r--
Adding update() to NullMap
athos@276
     1
// -*- c++ -*-
athos@523
     2
#ifndef HUGO_MINCOSTFLOWS_H
athos@523
     3
#define HUGO_MINCOSTFLOWS_H
athos@276
     4
klao@491
     5
///\ingroup galgs
alpar@294
     6
///\file
athos@523
     7
///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost 
alpar@294
     8
athos@276
     9
#include <iostream>
athos@276
    10
#include <dijkstra.h>
athos@276
    11
#include <graph_wrapper.h>
athos@306
    12
#include <maps.h>
athos@511
    13
#include <vector.h>
athos@322
    14
athos@306
    15
athos@276
    16
namespace hugo {
athos@276
    17
alpar@430
    18
/// \addtogroup galgs
alpar@430
    19
/// @{
athos@322
    20
athos@523
    21
  ///\brief Implementation of an algorithm for finding a flow of value \c k 
athos@523
    22
  ///(for small values of \c k) having minimal total cost between 2 nodes 
athos@523
    23
  /// 
klao@310
    24
  ///
athos@523
    25
  /// The class \ref hugo::MinCostFlows "MinCostFlows" implements
athos@523
    26
  /// an algorithm for finding a flow of value \c k 
athos@523
    27
  ///(for small values of \c k) having minimal total cost  
klao@310
    28
  /// from a given source node to a given target node in an
athos@523
    29
  /// edge-weighted directed graph having nonnegative integer capacities.
athos@523
    30
  /// The range of the length (weight) function is nonnegative reals but 
athos@523
    31
  /// the range of capacity function is the set of nonnegative integers. 
athos@523
    32
  /// It is not a polinomial time algorithm for counting the minimum cost
athos@523
    33
  /// maximal flow, since it counts the minimum cost flow for every value 0..M
athos@523
    34
  /// where \c M is the value of the maximal flow.
alpar@456
    35
  ///
alpar@456
    36
  ///\author Attila Bernath
klao@310
    37
  template <typename Graph, typename LengthMap>
athos@523
    38
  class MinCostFlows {
athos@276
    39
klao@310
    40
    typedef typename LengthMap::ValueType Length;
athos@527
    41
athos@527
    42
    typedef typename LengthMap::ValueType Length;
athos@511
    43
    
athos@276
    44
    typedef typename Graph::Node Node;
athos@276
    45
    typedef typename Graph::NodeIt NodeIt;
athos@276
    46
    typedef typename Graph::Edge Edge;
athos@276
    47
    typedef typename Graph::OutEdgeIt OutEdgeIt;
athos@511
    48
    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
athos@306
    49
athos@527
    50
    //    typedef ConstMap<Edge,int> ConstMap;
athos@306
    51
athos@527
    52
    typedef ResGraphWrapper<const Graph,int,EdgeIntMap,EdgeIntMap> ResGraphType;
athos@276
    53
athos@306
    54
    class ModLengthMap {   
athos@511
    55
      typedef typename ResGraphType::template NodeMap<Length> NodeMap;
athos@306
    56
      const ResGraphType& G;
athos@527
    57
      //      const EdgeIntMap& rev;
klao@310
    58
      const LengthMap &ol;
klao@310
    59
      const NodeMap &pot;
athos@306
    60
    public :
athos@306
    61
      typedef typename LengthMap::KeyType KeyType;
athos@306
    62
      typedef typename LengthMap::ValueType ValueType;
athos@511
    63
	
athos@306
    64
      ValueType operator[](typename ResGraphType::Edge e) const {     
athos@527
    65
	if (G.forward(e))
athos@527
    66
	  return  ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
athos@527
    67
	else
athos@527
    68
	  return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
athos@306
    69
      }     
athos@511
    70
	
klao@310
    71
      ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev, 
klao@310
    72
		   const LengthMap &o,  const NodeMap &p) : 
athos@527
    73
	G(_G), /*rev(_rev),*/ ol(o), pot(p){}; 
athos@511
    74
    };//ModLengthMap
athos@511
    75
athos@511
    76
athos@306
    77
    
athos@527
    78
    //Input
athos@276
    79
    const Graph& G;
athos@276
    80
    const LengthMap& length;
athos@527
    81
    const EdgeIntMap& capacity;
athos@276
    82
alpar@328
    83
    //auxiliary variables
athos@322
    84
athos@314
    85
    //The value is 1 iff the edge is reversed. 
athos@314
    86
    //If the algorithm has finished, the edges of the seeked paths are 
athos@314
    87
    //exactly those that are reversed 
athos@527
    88
    EdgeIntMap flow; 
athos@276
    89
    
athos@322
    90
    //Container to store found paths
athos@322
    91
    std::vector< std::vector<Edge> > paths;
athos@511
    92
    //typedef DirPath<Graph> DPath;
athos@511
    93
    //DPath paths;
athos@511
    94
athos@511
    95
athos@511
    96
    Length total_length;
athos@322
    97
athos@276
    98
  public :
klao@310
    99
athos@276
   100
athos@527
   101
    MinLengthPaths(Graph& _G, LengthMap& _length, EdgeIntMap& _cap) : G(_G), 
athos@527
   102
      length(_length), capacity(_cap), flow(_G)/*, dijkstra_dist(_G)*/{ }
athos@276
   103
alpar@294
   104
    
alpar@329
   105
    ///Runs the algorithm.
alpar@329
   106
alpar@329
   107
    ///Runs the algorithm.
athos@306
   108
    ///Returns k if there are at least k edge-disjoint paths from s to t.
alpar@329
   109
    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
athos@306
   110
    int run(Node s, Node t, int k) {
athos@276
   111
athos@511
   112
athos@527
   113
      //We need a residual graph
athos@527
   114
      ResGraphType res_graph(G, capacity, flow);
athos@306
   115
athos@306
   116
      //Initialize the copy of the Dijkstra potential to zero
athos@511
   117
      typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
athos@527
   118
      ModLengthMap mod_length(res_graph, length, dijkstra_dist);
athos@306
   119
athos@306
   120
      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
athos@322
   121
athos@322
   122
      int i;
athos@322
   123
      for (i=0; i<k; ++i){
athos@276
   124
	dijkstra.run(s);
athos@276
   125
	if (!dijkstra.reached(t)){
athos@314
   126
	  //There are no k paths from s to t
athos@322
   127
	  break;
athos@276
   128
	};
athos@306
   129
	
athos@306
   130
	{
athos@306
   131
	  //We have to copy the potential
athos@306
   132
	  typename ResGraphType::NodeIt n;
athos@306
   133
	  for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
athos@306
   134
	      dijkstra_dist[n] += dijkstra.distMap()[n];
athos@306
   135
	  }
athos@306
   136
	}
athos@306
   137
athos@306
   138
athos@527
   139
	//Augmenting on the sortest path
athos@276
   140
	Node n=t;
athos@276
   141
	Edge e;
athos@276
   142
	while (n!=s){
athos@291
   143
	  e = dijkstra.pred(n);
athos@291
   144
	  n = dijkstra.predNode(n);
athos@527
   145
	  G.augment(e,1);
athos@276
   146
	}
athos@276
   147
athos@276
   148
	  
athos@276
   149
      }
athos@322
   150
      
athos@527
   151
      /*
athos@527
   152
	///\TODO To be implemented later
athos@527
   153
athos@322
   154
      //Let's find the paths
athos@511
   155
      //We put the paths into stl vectors (as an inner representation). 
athos@511
   156
      //In the meantime we lose the information stored in 'reversed'.
athos@322
   157
      //We suppose the lengths to be positive now.
athos@511
   158
athos@511
   159
      //Meanwhile we put the total length of the found paths 
athos@511
   160
      //in the member variable total_length
athos@322
   161
      paths.clear();
athos@511
   162
      total_length=0;
athos@322
   163
      paths.resize(k);
athos@322
   164
      for (int j=0; j<i; ++j){
athos@322
   165
	Node n=s;
athos@322
   166
	OutEdgeIt e;
athos@322
   167
athos@322
   168
	while (n!=t){
athos@322
   169
athos@322
   170
athos@322
   171
	  G.first(e,n);
athos@322
   172
	  
athos@322
   173
	  while (!reversed[e]){
athos@322
   174
	    G.next(e);
athos@322
   175
	  }
athos@322
   176
	  n = G.head(e);
athos@322
   177
	  paths[j].push_back(e);
athos@511
   178
	  total_length += length[e];
athos@322
   179
	  reversed[e] = 1-reversed[e];
athos@322
   180
	}
athos@322
   181
	
athos@322
   182
      }
athos@527
   183
      */
athos@322
   184
athos@322
   185
      return i;
athos@276
   186
    }
athos@276
   187
athos@511
   188
    ///This function gives back the total length of the found paths.
athos@511
   189
    ///Assumes that \c run() has been run and nothing changed since then.
athos@511
   190
    Length totalLength(){
athos@511
   191
      return total_length;
athos@511
   192
    }
athos@511
   193
athos@511
   194
    ///This function gives back the \c j-th path in argument p.
athos@511
   195
    ///Assumes that \c run() has been run and nothing changed since then.
athos@519
   196
    /// \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.
athos@511
   197
    template<typename DirPath>
athos@511
   198
    void getPath(DirPath& p, int j){
athos@511
   199
      p.clear();
athos@511
   200
      typename DirPath::Builder B(p);
athos@511
   201
      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
athos@511
   202
	  i!=paths[j].end(); ++i ){
athos@520
   203
	B.pushBack(*i);
athos@511
   204
      }
athos@511
   205
athos@511
   206
      B.commit();
athos@511
   207
    }
athos@276
   208
klao@310
   209
  }; //class MinLengthPaths
athos@276
   210
alpar@430
   211
  ///@}
athos@276
   212
athos@276
   213
} //namespace hugo
athos@276
   214
athos@527
   215
#endif //HUGO_MINCOSTFLOW_H