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