src/hugo/min_cost_flow.h
author alpar
Thu, 23 Sep 2004 15:09:55 +0000
changeset 907 df8472ab5d4a
parent 901 69a8e672acb1
child 910 5a89cacf17f1
permissions -rw-r--r--
I forgot to apply
for i in `ls *.h`; do rpl template.h $i $i; done
in src/hugo/attic
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 * src/hugo/min_cost_flow.h - Part of HUGOlib, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
marci@901
    17
#ifndef HUGO_MIN_COST_FLOW_H
marci@901
    18
#define HUGO_MIN_COST_FLOW_H
alpar@899
    19
alpar@899
    20
///\ingroup flowalgs
alpar@899
    21
///\file
alpar@899
    22
///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost 
alpar@899
    23
alpar@899
    24
alpar@899
    25
#include <hugo/dijkstra.h>
alpar@899
    26
#include <hugo/graph_wrapper.h>
alpar@899
    27
#include <hugo/maps.h>
alpar@899
    28
#include <vector>
alpar@899
    29
alpar@899
    30
namespace hugo {
alpar@899
    31
alpar@899
    32
/// \addtogroup flowalgs
alpar@899
    33
/// @{
alpar@899
    34
alpar@899
    35
  ///\brief Implementation of an algorithm for finding a flow of value \c k 
alpar@899
    36
  ///(for small values of \c k) having minimal total cost between 2 nodes 
alpar@899
    37
  /// 
alpar@899
    38
  ///
alpar@899
    39
  /// The class \ref hugo::MinCostFlow "MinCostFlow" implements
alpar@899
    40
  /// an algorithm for finding a flow of value \c k 
alpar@899
    41
  /// having minimal total cost 
alpar@899
    42
  /// from a given source node to a given target node in an
alpar@899
    43
  /// edge-weighted directed graph. To this end, 
alpar@899
    44
  /// the edge-capacities and edge-weitghs have to be nonnegative. 
alpar@899
    45
  /// The edge-capacities should be integers, but the edge-weights can be 
alpar@899
    46
  /// integers, reals or of other comparable numeric type.
alpar@899
    47
  /// This algorithm is intended to use only for small values of \c k, 
alpar@899
    48
  /// since it is only polynomial in k, 
alpar@899
    49
  /// not in the length of k (which is log k). 
alpar@899
    50
  /// In order to find the minimum cost flow of value \c k it 
alpar@899
    51
  /// finds the minimum cost flow of value \c i for every 
alpar@899
    52
  /// \c i between 0 and \c k. 
alpar@899
    53
  ///
alpar@899
    54
  ///\param Graph The directed graph type the algorithm runs on.
alpar@899
    55
  ///\param LengthMap The type of the length map.
alpar@899
    56
  ///\param CapacityMap The capacity map type.
alpar@899
    57
  ///
alpar@899
    58
  ///\author Attila Bernath
alpar@899
    59
  template <typename Graph, typename LengthMap, typename CapacityMap>
alpar@899
    60
  class MinCostFlow {
alpar@899
    61
alpar@899
    62
    typedef typename LengthMap::ValueType Length;
alpar@899
    63
alpar@899
    64
    //Warning: this should be integer type
alpar@899
    65
    typedef typename CapacityMap::ValueType Capacity;
alpar@899
    66
    
alpar@899
    67
    typedef typename Graph::Node Node;
alpar@899
    68
    typedef typename Graph::NodeIt NodeIt;
alpar@899
    69
    typedef typename Graph::Edge Edge;
alpar@899
    70
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@899
    71
    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
alpar@899
    72
alpar@899
    73
alpar@899
    74
    typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
alpar@899
    75
    typedef typename ResGraphType::Edge ResGraphEdge;
alpar@899
    76
alpar@899
    77
    class ModLengthMap {   
alpar@899
    78
      typedef typename Graph::template NodeMap<Length> NodeMap;
alpar@899
    79
      const ResGraphType& G;
alpar@899
    80
      const LengthMap &ol;
alpar@899
    81
      const NodeMap &pot;
alpar@899
    82
    public :
alpar@899
    83
      typedef typename LengthMap::KeyType KeyType;
alpar@899
    84
      typedef typename LengthMap::ValueType ValueType;
alpar@899
    85
	
alpar@899
    86
      ValueType operator[](typename ResGraphType::Edge e) const {     
alpar@899
    87
	if (G.forward(e))
alpar@899
    88
	  return  ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
alpar@899
    89
	else
alpar@899
    90
	  return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
alpar@899
    91
      }     
alpar@899
    92
	
alpar@899
    93
      ModLengthMap(const ResGraphType& _G,
alpar@899
    94
		   const LengthMap &o,  const NodeMap &p) : 
alpar@899
    95
	G(_G), /*rev(_rev),*/ ol(o), pot(p){}; 
alpar@899
    96
    };//ModLengthMap
alpar@899
    97
alpar@899
    98
alpar@899
    99
  protected:
alpar@899
   100
    
alpar@899
   101
    //Input
alpar@899
   102
    const Graph& G;
alpar@899
   103
    const LengthMap& length;
alpar@899
   104
    const CapacityMap& capacity;
alpar@899
   105
alpar@899
   106
alpar@899
   107
    //auxiliary variables
alpar@899
   108
alpar@899
   109
    //To store the flow
alpar@899
   110
    EdgeIntMap flow; 
alpar@899
   111
    //To store the potential (dual variables)
alpar@899
   112
    typedef typename Graph::template NodeMap<Length> PotentialMap;
alpar@899
   113
    PotentialMap potential;
alpar@899
   114
    
alpar@899
   115
alpar@899
   116
    Length total_length;
alpar@899
   117
alpar@899
   118
alpar@899
   119
  public :
alpar@899
   120
alpar@899
   121
    /// The constructor of the class.
alpar@899
   122
    
alpar@899
   123
    ///\param _G The directed graph the algorithm runs on. 
alpar@899
   124
    ///\param _length The length (weight or cost) of the edges. 
alpar@899
   125
    ///\param _cap The capacity of the edges. 
alpar@899
   126
    MinCostFlow(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), 
alpar@899
   127
      length(_length), capacity(_cap), flow(_G), potential(_G){ }
alpar@899
   128
alpar@899
   129
    
alpar@899
   130
    ///Runs the algorithm.
alpar@899
   131
    
alpar@899
   132
    ///Runs the algorithm.
alpar@899
   133
    ///Returns k if there is a flow of value at least k edge-disjoint 
alpar@899
   134
    ///from s to t.
alpar@899
   135
    ///Otherwise it returns the maximum value of a flow from s to t.
alpar@899
   136
    ///
alpar@899
   137
    ///\param s The source node.
alpar@899
   138
    ///\param t The target node.
alpar@899
   139
    ///\param k The value of the flow we are looking for.
alpar@899
   140
    ///
alpar@899
   141
    ///\todo May be it does make sense to be able to start with a nonzero 
alpar@899
   142
    /// feasible primal-dual solution pair as well.
alpar@899
   143
    int run(Node s, Node t, int k) {
alpar@899
   144
alpar@899
   145
      //Resetting variables from previous runs
alpar@899
   146
      total_length = 0;
alpar@899
   147
      
alpar@899
   148
      for (typename Graph::EdgeIt e(G); e!=INVALID; ++e) flow.set(e, 0);
alpar@899
   149
alpar@899
   150
      //Initialize the potential to zero
alpar@899
   151
      for (typename Graph::NodeIt n(G); n!=INVALID; ++n) potential.set(n, 0);
alpar@899
   152
      
alpar@899
   153
      
alpar@899
   154
      //We need a residual graph
alpar@899
   155
      ResGraphType res_graph(G, capacity, flow);
alpar@899
   156
alpar@899
   157
alpar@899
   158
      ModLengthMap mod_length(res_graph, length, potential);
alpar@899
   159
alpar@899
   160
      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
alpar@899
   161
alpar@899
   162
      int i;
alpar@899
   163
      for (i=0; i<k; ++i){
alpar@899
   164
	dijkstra.run(s);
alpar@899
   165
	if (!dijkstra.reached(t)){
alpar@899
   166
	  //There are no flow of value k from s to t
alpar@899
   167
	  break;
alpar@899
   168
	};
alpar@899
   169
	
alpar@899
   170
	//We have to change the potential
alpar@899
   171
        for(typename ResGraphType::NodeIt n(res_graph); n!=INVALID; ++n)
alpar@899
   172
	  potential[n] += dijkstra.distMap()[n];
alpar@899
   173
alpar@899
   174
alpar@899
   175
	//Augmenting on the sortest path
alpar@899
   176
	Node n=t;
alpar@899
   177
	ResGraphEdge e;
alpar@899
   178
	while (n!=s){
alpar@899
   179
	  e = dijkstra.pred(n);
alpar@899
   180
	  n = dijkstra.predNode(n);
alpar@899
   181
	  res_graph.augment(e,1);
alpar@899
   182
	  //Let's update the total length
alpar@899
   183
	  if (res_graph.forward(e))
alpar@899
   184
	    total_length += length[e];
alpar@899
   185
	  else 
alpar@899
   186
	    total_length -= length[e];	    
alpar@899
   187
	}
alpar@899
   188
alpar@899
   189
	  
alpar@899
   190
      }
alpar@899
   191
      
alpar@899
   192
alpar@899
   193
      return i;
alpar@899
   194
    }
alpar@899
   195
alpar@899
   196
alpar@899
   197
alpar@899
   198
    /// Gives back the total weight of the found flow.
alpar@899
   199
alpar@899
   200
    ///This function gives back the total weight of the found flow.
alpar@899
   201
    ///Assumes that \c run() has been run and nothing changed since then.
alpar@899
   202
    Length totalLength(){
alpar@899
   203
      return total_length;
alpar@899
   204
    }
alpar@899
   205
alpar@899
   206
    ///Returns a const reference to the EdgeMap \c flow. 
alpar@899
   207
alpar@899
   208
    ///Returns a const reference to the EdgeMap \c flow. 
alpar@899
   209
    ///\pre \ref run() must
alpar@899
   210
    ///be called before using this function.
alpar@899
   211
    const EdgeIntMap &getFlow() const { return flow;}
alpar@899
   212
alpar@899
   213
    ///Returns a const reference to the NodeMap \c potential (the dual solution).
alpar@899
   214
alpar@899
   215
    ///Returns a const reference to the NodeMap \c potential (the dual solution).
alpar@899
   216
    /// \pre \ref run() must be called before using this function.
alpar@899
   217
    const PotentialMap &getPotential() const { return potential;}
alpar@899
   218
alpar@899
   219
    /// Checking the complementary slackness optimality criteria
alpar@899
   220
alpar@899
   221
    ///This function checks, whether the given solution is optimal
alpar@899
   222
    ///If executed after the call of \c run() then it should return with true.
alpar@899
   223
    ///This function only checks optimality, doesn't bother with feasibility.
alpar@899
   224
    ///It is meant for testing purposes.
alpar@899
   225
    ///
alpar@899
   226
    bool checkComplementarySlackness(){
alpar@899
   227
      Length mod_pot;
alpar@899
   228
      Length fl_e;
alpar@899
   229
        for(typename Graph::EdgeIt e(G); e!=INVALID; ++e) {
alpar@899
   230
	//C^{\Pi}_{i,j}
alpar@899
   231
	mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)];
alpar@899
   232
	fl_e = flow[e];
alpar@899
   233
	if (0<fl_e && fl_e<capacity[e]) {
alpar@899
   234
	  /// \todo better comparison is needed for real types, moreover, 
alpar@899
   235
	  /// this comparison here is superfluous.
alpar@899
   236
	  if (mod_pot != 0)
alpar@899
   237
	    return false;
alpar@899
   238
	} 
alpar@899
   239
	else {
alpar@899
   240
	  if (mod_pot > 0 && fl_e != 0)
alpar@899
   241
	    return false;
alpar@899
   242
	  if (mod_pot < 0 && fl_e != capacity[e])
alpar@899
   243
	    return false;
alpar@899
   244
	}
alpar@899
   245
      }
alpar@899
   246
      return true;
alpar@899
   247
    }
alpar@899
   248
    
alpar@899
   249
alpar@899
   250
  }; //class MinCostFlow
alpar@899
   251
alpar@899
   252
  ///@}
alpar@899
   253
alpar@899
   254
} //namespace hugo
alpar@899
   255
marci@901
   256
#endif //HUGO_MIN_COST_FLOW_H