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