lemon/ssp_min_cost_flow.h
author alpar
Tue, 31 Oct 2006 08:39:01 +0000
changeset 2279 2c3fee01d3ed
child 2350 eb371753e814
permissions -rw-r--r--
Update scripts
deba@2276
     1
/* -*- C++ -*-
deba@2276
     2
 *
deba@2276
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2276
     4
 *
deba@2276
     5
 * Copyright (C) 2003-2006
deba@2276
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2276
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2276
     8
 *
deba@2276
     9
 * Permission to use, modify and distribute this software is granted
deba@2276
    10
 * provided that this copyright notice appears in all copies. For
deba@2276
    11
 * precise terms see the accompanying LICENSE file.
deba@2276
    12
 *
deba@2276
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2276
    14
 * express or implied, and with no claim as to its suitability for any
deba@2276
    15
 * purpose.
deba@2276
    16
 *
deba@2276
    17
 */
deba@2276
    18
deba@2276
    19
#ifndef LEMON_MIN_COST_FLOW_H
deba@2276
    20
#define LEMON_MIN_COST_FLOW_H
deba@2276
    21
deba@2276
    22
///\ingroup flowalgs 
deba@2276
    23
///
deba@2276
    24
///\file \brief An algorithm for finding a flow of value \c k (for
deba@2276
    25
///small values of \c k) having minimal total cost
deba@2276
    26
deba@2276
    27
deba@2276
    28
#include <lemon/dijkstra.h>
deba@2276
    29
#include <lemon/graph_adaptor.h>
deba@2276
    30
#include <lemon/maps.h>
deba@2276
    31
#include <vector>
deba@2276
    32
deba@2276
    33
namespace lemon {
deba@2276
    34
deba@2276
    35
  /// \addtogroup flowalgs
deba@2276
    36
  /// @{
deba@2276
    37
deba@2276
    38
  /// \brief Implementation of an algorithm for finding a flow of
deba@2276
    39
  /// value \c k (for small values of \c k) having minimal total cost
deba@2276
    40
  /// between two nodes
deba@2276
    41
  /// 
deba@2276
    42
  ///
deba@2276
    43
  /// The \ref lemon::SspMinCostFlow "Successive Shortest Path Minimum
deba@2276
    44
  /// Cost Flow" implements an algorithm for finding a flow of value
deba@2276
    45
  /// \c k having minimal total cost from a given source node to a
deba@2276
    46
  /// given target node in a directed graph with a cost function on
deba@2276
    47
  /// the edges. To this end, the edge-capacities and edge-costs have
deba@2276
    48
  /// to be nonnegative.  The edge-capacities should be integers, but
deba@2276
    49
  /// the edge-costs can be integers, reals or of other comparable
deba@2276
    50
  /// numeric type.  This algorithm is intended to be used only for
deba@2276
    51
  /// small values of \c k, since it is only polynomial in k, not in
deba@2276
    52
  /// the length of k (which is log k): in order to find the minimum
deba@2276
    53
  /// cost flow of value \c k it finds the minimum cost flow of value
deba@2276
    54
  /// \c i for every \c i between 0 and \c k.
deba@2276
    55
  ///
deba@2276
    56
  ///\param Graph The directed graph type the algorithm runs on.
deba@2276
    57
  ///\param LengthMap The type of the length map.
deba@2276
    58
  ///\param CapacityMap The capacity map type.
deba@2276
    59
  ///
deba@2276
    60
  ///\author Attila Bernath
deba@2276
    61
  template <typename Graph, typename LengthMap, typename CapacityMap>
deba@2276
    62
  class SspMinCostFlow {
deba@2276
    63
deba@2276
    64
    typedef typename LengthMap::Value Length;
deba@2276
    65
deba@2276
    66
    //Warning: this should be integer type
deba@2276
    67
    typedef typename CapacityMap::Value Capacity;
deba@2276
    68
    
deba@2276
    69
    typedef typename Graph::Node Node;
deba@2276
    70
    typedef typename Graph::NodeIt NodeIt;
deba@2276
    71
    typedef typename Graph::Edge Edge;
deba@2276
    72
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@2276
    73
    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
deba@2276
    74
deba@2276
    75
    typedef ResGraphAdaptor<const Graph,int,CapacityMap,EdgeIntMap> ResGW;
deba@2276
    76
    typedef typename ResGW::Edge ResGraphEdge;
deba@2276
    77
deba@2276
    78
  protected:
deba@2276
    79
deba@2276
    80
    const Graph& g;
deba@2276
    81
    const LengthMap& length;
deba@2276
    82
    const CapacityMap& capacity;
deba@2276
    83
deba@2276
    84
    EdgeIntMap flow; 
deba@2276
    85
    typedef typename Graph::template NodeMap<Length> PotentialMap;
deba@2276
    86
    PotentialMap potential;
deba@2276
    87
deba@2276
    88
    Node s;
deba@2276
    89
    Node t;
deba@2276
    90
    
deba@2276
    91
    Length total_length;
deba@2276
    92
deba@2276
    93
    class ModLengthMap {   
deba@2276
    94
      typedef typename Graph::template NodeMap<Length> NodeMap;
deba@2276
    95
      const ResGW& g;
deba@2276
    96
      const LengthMap &length;
deba@2276
    97
      const NodeMap &pot;
deba@2276
    98
    public :
deba@2276
    99
      typedef typename LengthMap::Key Key;
deba@2276
   100
      typedef typename LengthMap::Value Value;
deba@2276
   101
deba@2276
   102
      ModLengthMap(const ResGW& _g, 
deba@2276
   103
		   const LengthMap &_length, const NodeMap &_pot) : 
deba@2276
   104
	g(_g), /*rev(_rev),*/ length(_length), pot(_pot) { }
deba@2276
   105
	
deba@2276
   106
      Value operator[](typename ResGW::Edge e) const {     
deba@2276
   107
	if (g.forward(e))
deba@2276
   108
	  return  length[e]-(pot[g.target(e)]-pot[g.source(e)]);   
deba@2276
   109
	else
deba@2276
   110
	  return -length[e]-(pot[g.target(e)]-pot[g.source(e)]);   
deba@2276
   111
      }     
deba@2276
   112
	
deba@2276
   113
    }; //ModLengthMap
deba@2276
   114
deba@2276
   115
    ResGW res_graph;
deba@2276
   116
    ModLengthMap mod_length;
deba@2276
   117
    Dijkstra<ResGW, ModLengthMap> dijkstra;
deba@2276
   118
deba@2276
   119
  public :
deba@2276
   120
deba@2276
   121
    /// \brief The constructor of the class.
deba@2276
   122
    ///
deba@2276
   123
    /// \param _g The directed graph the algorithm runs on. 
deba@2276
   124
    /// \param _length The length (cost) of the edges. 
deba@2276
   125
    /// \param _cap The capacity of the edges. 
deba@2276
   126
    /// \param _s Source node.
deba@2276
   127
    /// \param _t Target node.
deba@2276
   128
    SspMinCostFlow(Graph& _g, LengthMap& _length, CapacityMap& _cap, 
deba@2276
   129
                   Node _s, Node _t) : 
deba@2276
   130
      g(_g), length(_length), capacity(_cap), flow(_g), potential(_g), 
deba@2276
   131
      s(_s), t(_t), 
deba@2276
   132
      res_graph(g, capacity, flow), 
deba@2276
   133
      mod_length(res_graph, length, potential),
deba@2276
   134
      dijkstra(res_graph, mod_length) { 
deba@2276
   135
      reset();
deba@2276
   136
    }
deba@2276
   137
    
deba@2276
   138
    /// \brief Tries to augment the flow between s and t by 1. The
deba@2276
   139
    /// return value shows if the augmentation is successful.
deba@2276
   140
    bool augment() {
deba@2276
   141
      dijkstra.run(s);
deba@2276
   142
      if (!dijkstra.reached(t)) {
deba@2276
   143
deba@2276
   144
	//Unsuccessful augmentation.
deba@2276
   145
	return false;
deba@2276
   146
      } else {
deba@2276
   147
deba@2276
   148
	//We have to change the potential
deba@2276
   149
	for(typename ResGW::NodeIt n(res_graph); n!=INVALID; ++n)
deba@2276
   150
	  potential.set(n, potential[n]+dijkstra.distMap()[n]);
deba@2276
   151
	
deba@2276
   152
	//Augmenting on the shortest path
deba@2276
   153
	Node n=t;
deba@2276
   154
	ResGraphEdge e;
deba@2276
   155
	while (n!=s){
deba@2276
   156
	  e = dijkstra.predEdge(n);
deba@2276
   157
	  n = dijkstra.predNode(n);
deba@2276
   158
	  res_graph.augment(e,1);
deba@2276
   159
	  //Let's update the total length
deba@2276
   160
	  if (res_graph.forward(e))
deba@2276
   161
	    total_length += length[e];
deba@2276
   162
	  else 
deba@2276
   163
	    total_length -= length[e];	    
deba@2276
   164
	}
deba@2276
   165
deba@2276
   166
	return true;
deba@2276
   167
      }
deba@2276
   168
    }
deba@2276
   169
    
deba@2276
   170
    /// \brief Runs the algorithm.
deba@2276
   171
    ///
deba@2276
   172
    /// Runs the algorithm.
deba@2276
   173
    /// Returns k if there is a flow of value at least k from s to t.
deba@2276
   174
    /// Otherwise it returns the maximum value of a flow from s to t.
deba@2276
   175
    ///
deba@2276
   176
    /// \param k The value of the flow we are looking for.
deba@2276
   177
    ///
deba@2276
   178
    /// \todo May be it does make sense to be able to start with a
deba@2276
   179
    /// nonzero feasible primal-dual solution pair as well.
deba@2276
   180
    ///
deba@2276
   181
    /// \todo If the actual flow value is bigger than k, then
deba@2276
   182
    /// everything is cleared and the algorithm starts from zero
deba@2276
   183
    /// flow. Is it a good approach?
deba@2276
   184
    int run(int k) {
deba@2276
   185
      if (flowValue()>k) reset();
deba@2276
   186
      while (flowValue()<k && augment()) { }
deba@2276
   187
      return flowValue();
deba@2276
   188
    }
deba@2276
   189
deba@2276
   190
    /// \brief The class is reset to zero flow and potential. The
deba@2276
   191
    /// class is reset to zero flow and potential.
deba@2276
   192
    void reset() {
deba@2276
   193
      total_length=0;
deba@2276
   194
      for (typename Graph::EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
deba@2276
   195
      for (typename Graph::NodeIt n(g); n!=INVALID; ++n) potential.set(n, 0);  
deba@2276
   196
    }
deba@2276
   197
deba@2276
   198
    /// \brief Returns the value of the actual flow. 
deba@2276
   199
    int flowValue() const {
deba@2276
   200
      int i=0;
deba@2276
   201
      for (typename Graph::OutEdgeIt e(g, s); e!=INVALID; ++e) i+=flow[e];
deba@2276
   202
      for (typename Graph::InEdgeIt e(g, s); e!=INVALID; ++e) i-=flow[e];
deba@2276
   203
      return i;
deba@2276
   204
    }
deba@2276
   205
deba@2276
   206
    /// \brief Total cost of the found flow.
deba@2276
   207
    ///
deba@2276
   208
    /// This function gives back the total cost of the found flow.
deba@2276
   209
    Length totalLength(){
deba@2276
   210
      return total_length;
deba@2276
   211
    }
deba@2276
   212
deba@2276
   213
    /// \brief Returns a const reference to the EdgeMap \c flow. 
deba@2276
   214
    ///
deba@2276
   215
    /// Returns a const reference to the EdgeMap \c flow. 
deba@2276
   216
    const EdgeIntMap &getFlow() const { return flow;}
deba@2276
   217
deba@2276
   218
    /// \brief Returns a const reference to the NodeMap \c potential
deba@2276
   219
    /// (the dual solution).
deba@2276
   220
    ///
deba@2276
   221
    /// Returns a const reference to the NodeMap \c potential (the
deba@2276
   222
    /// dual solution).
deba@2276
   223
    const PotentialMap &getPotential() const { return potential;}
deba@2276
   224
deba@2276
   225
    /// \brief Checking the complementary slackness optimality criteria.
deba@2276
   226
    ///
deba@2276
   227
    /// This function checks, whether the given flow and potential 
deba@2276
   228
    /// satisfy the complementary slackness conditions (i.e. these are optimal).
deba@2276
   229
    /// This function only checks optimality, doesn't bother with feasibility.
deba@2276
   230
    /// For testing purpose.
deba@2276
   231
    bool checkComplementarySlackness(){
deba@2276
   232
      Length mod_pot;
deba@2276
   233
      Length fl_e;
deba@2276
   234
        for(typename Graph::EdgeIt e(g); e!=INVALID; ++e) {
deba@2276
   235
	//C^{\Pi}_{i,j}
deba@2276
   236
	mod_pot = length[e]-potential[g.target(e)]+potential[g.source(e)];
deba@2276
   237
	fl_e = flow[e];
deba@2276
   238
	if (0<fl_e && fl_e<capacity[e]) {
deba@2276
   239
	  /// \todo better comparison is needed for real types, moreover, 
deba@2276
   240
	  /// this comparison here is superfluous.
deba@2276
   241
	  if (mod_pot != 0)
deba@2276
   242
	    return false;
deba@2276
   243
	} 
deba@2276
   244
	else {
deba@2276
   245
	  if (mod_pot > 0 && fl_e != 0)
deba@2276
   246
	    return false;
deba@2276
   247
	  if (mod_pot < 0 && fl_e != capacity[e])
deba@2276
   248
	    return false;
deba@2276
   249
	}
deba@2276
   250
      }
deba@2276
   251
      return true;
deba@2276
   252
    }
deba@2276
   253
    
deba@2276
   254
  }; //class SspMinCostFlow
deba@2276
   255
deba@2276
   256
  ///@}
deba@2276
   257
deba@2276
   258
} //namespace lemon
deba@2276
   259
deba@2276
   260
#endif //LEMON_MIN_COST_FLOW_H