lemon/cycle_canceling.h
changeset 2495 e4f8367beb41
parent 2440 c9218405595b
child 2509 a8081c9cd96a
equal deleted inserted replaced
0:8eb62b97ed3e 1:3de895e36f2d
    20 #define LEMON_CYCLE_CANCELING_H
    20 #define LEMON_CYCLE_CANCELING_H
    21 
    21 
    22 /// \ingroup min_cost_flow
    22 /// \ingroup min_cost_flow
    23 ///
    23 ///
    24 /// \file
    24 /// \file
    25 /// \brief A cycle canceling algorithm for finding a minimum cost flow.
    25 /// \brief A cycle-canceling algorithm for finding a minimum cost flow.
    26 
    26 
    27 #include <vector>
    27 #include <vector>
    28 #include <lemon/circulation.h>
    28 #include <lemon/circulation.h>
    29 #include <lemon/graph_adaptor.h>
    29 #include <lemon/graph_adaptor.h>
    30 
    30 
    31 /// \brief The used cycle canceling method.
    31 /// \brief The used cycle-canceling method.
    32 #define LIMITED_CYCLE_CANCELING
    32 #define LIMITED_CYCLE_CANCELING
    33 //#define MIN_MEAN_CYCLE_CANCELING
    33 //#define MIN_MEAN_CYCLE_CANCELING
    34 
    34 
    35 #ifdef LIMITED_CYCLE_CANCELING
    35 #ifdef LIMITED_CYCLE_CANCELING
    36   #include <lemon/bellman_ford.h>
    36   #include <lemon/bellman_ford.h>
    37   /// \brief The maximum number of iterations for the first execution
    37   /// \brief The maximum number of iterations for the first execution
    38   /// of the \ref lemon::BellmanFord "Bellman-Ford" algorithm.
    38   /// of the \ref lemon::BellmanFord "Bellman-Ford" algorithm.
       
    39   /// It should be at least 2.
    39   #define STARTING_LIMIT	2
    40   #define STARTING_LIMIT	2
    40   /// \brief The iteration limit for the
    41   /// \brief The iteration limit for the
    41   /// \ref lemon::BellmanFord "Bellman-Ford" algorithm is multiplied by
    42   /// \ref lemon::BellmanFord "Bellman-Ford" algorithm is multiplied by
    42   /// <tt>ALPHA_MUL % ALPHA_DIV</tt> in every round.
    43   /// <tt>ALPHA_MUL / ALPHA_DIV</tt> in every round.
       
    44   /// <tt>ALPHA_MUL / ALPHA_DIV</tt> must be greater than 1.
    43   #define ALPHA_MUL		3
    45   #define ALPHA_MUL		3
    44   /// \brief The iteration limit for the
    46   /// \brief The iteration limit for the
    45   /// \ref lemon::BellmanFord "Bellman-Ford" algorithm is multiplied by
    47   /// \ref lemon::BellmanFord "Bellman-Ford" algorithm is multiplied by
    46   /// <tt>ALPHA_MUL % ALPHA_DIV</tt> in every round.
    48   /// <tt>ALPHA_MUL / ALPHA_DIV</tt> in every round.
       
    49   /// <tt>ALPHA_MUL / ALPHA_DIV</tt> must be greater than 1.
    47   #define ALPHA_DIV		2
    50   #define ALPHA_DIV		2
    48 
    51 
    49 //#define _ONLY_ONE_CYCLE_
    52 //#define _ONLY_ONE_CYCLE_
       
    53 //#define _NO_BACK_STEP_
    50 //#define _DEBUG_ITER_
    54 //#define _DEBUG_ITER_
    51 #endif
    55 #endif
    52 
    56 
    53 #ifdef MIN_MEAN_CYCLE_CANCELING
    57 #ifdef MIN_MEAN_CYCLE_CANCELING
    54   #include <lemon/min_mean_cycle.h>
    58   #include <lemon/min_mean_cycle.h>
    58 namespace lemon {
    62 namespace lemon {
    59 
    63 
    60   /// \addtogroup min_cost_flow
    64   /// \addtogroup min_cost_flow
    61   /// @{
    65   /// @{
    62 
    66 
    63   /// \brief Implementation of a cycle canceling algorithm for finding
    67   /// \brief Implementation of a cycle-canceling algorithm for finding
    64   /// a minimum cost flow.
    68   /// a minimum cost flow.
    65   ///
    69   ///
    66   /// \ref lemon::CycleCanceling "CycleCanceling" implements a cycle
    70   /// \ref lemon::CycleCanceling "CycleCanceling" implements a
    67   /// canceling algorithm for finding a minimum cost flow.
    71   /// cycle-canceling algorithm for finding a minimum cost flow.
    68   ///
    72   ///
    69   /// \param Graph The directed graph type the algorithm runs on.
    73   /// \param Graph The directed graph type the algorithm runs on.
    70   /// \param LowerMap The type of the lower bound map.
    74   /// \param LowerMap The type of the lower bound map.
    71   /// \param CapacityMap The type of the capacity (upper bound) map.
    75   /// \param CapacityMap The type of the capacity (upper bound) map.
    72   /// \param CostMap The type of the cost (length) map.
    76   /// \param CostMap The type of the cost (length) map.
   116     /// \brief The type of the flow map.
   120     /// \brief The type of the flow map.
   117     typedef CapacityRefMap FlowMap;
   121     typedef CapacityRefMap FlowMap;
   118 
   122 
   119   protected:
   123   protected:
   120 
   124 
   121     /// \brief Map adaptor class for demand map.
       
   122     class DemandMap : public MapBase<Node, Supply>
       
   123     {
       
   124     private:
       
   125 
       
   126       const SupplyMap *map;
       
   127 
       
   128     public:
       
   129 
       
   130       typedef typename MapBase<Node, Supply>::Value Value;
       
   131       typedef typename MapBase<Node, Supply>::Key Key;
       
   132 
       
   133       DemandMap(const SupplyMap &_map) : map(&_map) {}
       
   134 
       
   135       Value operator[](const Key &e) const {
       
   136 	return -(*map)[e];
       
   137       }
       
   138 
       
   139     }; //class DemandMap
       
   140 
       
   141     /// \brief Map adaptor class for handling residual edge costs.
   125     /// \brief Map adaptor class for handling residual edge costs.
   142     class ResCostMap : public MapBase<ResEdge, Cost>
   126     class ResCostMap : public MapBase<ResEdge, Cost>
   143     {
   127     {
   144     private:
   128     private:
   145 
   129 
   168     CapacityRefMap capacity;
   152     CapacityRefMap capacity;
   169     /// \brief The cost map.
   153     /// \brief The cost map.
   170     const CostMap &cost;
   154     const CostMap &cost;
   171     /// \brief The modified supply map.
   155     /// \brief The modified supply map.
   172     SupplyRefMap supply;
   156     SupplyRefMap supply;
   173     /// \brief The modified demand map.
       
   174     DemandMap demand;
       
   175     /// \brief The sum of supply values equals zero.
   157     /// \brief The sum of supply values equals zero.
   176     bool valid_supply;
   158     bool valid_supply;
   177 
   159 
   178     /// \brief The current flow.
   160     /// \brief The current flow.
   179     FlowMap flow;
   161     FlowMap flow;
   197 		    const LowerMap &_lower,
   179 		    const LowerMap &_lower,
   198 		    const CapacityMap &_capacity,
   180 		    const CapacityMap &_capacity,
   199 		    const CostMap &_cost,
   181 		    const CostMap &_cost,
   200 		    const SupplyMap &_supply ) :
   182 		    const SupplyMap &_supply ) :
   201       graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
   183       graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
   202       supply(_graph), demand(supply), flow(_graph, 0),
   184       supply(_graph), flow(_graph, 0),
   203       res_graph(_graph, capacity, flow), res_cost(cost)
   185       res_graph(_graph, capacity, flow), res_cost(cost)
   204     {
   186     {
   205       // Removing nonzero lower bounds
   187       // Removing nonzero lower bounds
   206       capacity = subMap(_capacity, _lower);
   188       capacity = subMap(_capacity, _lower);
   207       Supply sum = 0;
   189       Supply sum = 0;
   227     CycleCanceling( const Graph &_graph,
   209     CycleCanceling( const Graph &_graph,
   228 		    const CapacityMap &_capacity,
   210 		    const CapacityMap &_capacity,
   229 		    const CostMap &_cost,
   211 		    const CostMap &_cost,
   230 		    const SupplyMap &_supply ) :
   212 		    const SupplyMap &_supply ) :
   231       graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
   213       graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
   232       supply(_supply), demand(supply), flow(_graph, 0),
   214       supply(_supply), flow(_graph, 0),
   233       res_graph(_graph, capacity, flow), res_cost(cost)
   215       res_graph(_graph, capacity, flow), res_cost(cost)
   234     {
   216     {
   235       // Checking the sum of supply values
   217       // Checking the sum of supply values
   236       Supply sum = 0;
   218       Supply sum = 0;
   237       for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
   219       for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
   257 		    const CapacityMap &_capacity,
   239 		    const CapacityMap &_capacity,
   258 		    const CostMap &_cost,
   240 		    const CostMap &_cost,
   259 		    Node _s, Node _t,
   241 		    Node _s, Node _t,
   260 		    Supply _flow_value ) :
   242 		    Supply _flow_value ) :
   261       graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
   243       graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
   262       supply(_graph), demand(supply), flow(_graph, 0),
   244       supply(_graph), flow(_graph, 0),
   263       res_graph(_graph, capacity, flow), res_cost(cost)
   245       res_graph(_graph, capacity, flow), res_cost(cost)
   264     {
   246     {
   265       // Removing nonzero lower bounds
   247       // Removing nonzero lower bounds
   266       capacity = subMap(_capacity, _lower);
   248       capacity = subMap(_capacity, _lower);
   267       for (NodeIt n(graph); n != INVALID; ++n) {
   249       for (NodeIt n(graph); n != INVALID; ++n) {
   293 		    const CapacityMap &_capacity,
   275 		    const CapacityMap &_capacity,
   294 		    const CostMap &_cost,
   276 		    const CostMap &_cost,
   295 		    Node _s, Node _t,
   277 		    Node _s, Node _t,
   296 		    Supply _flow_value ) :
   278 		    Supply _flow_value ) :
   297       graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
   279       graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
   298       supply(_graph, 0), demand(supply), flow(_graph, 0),
   280       supply(_graph, 0), flow(_graph, 0),
   299       res_graph(_graph, capacity, flow), res_cost(cost)
   281       res_graph(_graph, capacity, flow), res_cost(cost)
   300     {
   282     {
   301       supply[_s] =  _flow_value;
   283       supply[_s] =  _flow_value;
   302       supply[_t] = -_flow_value;
   284       supply[_t] = -_flow_value;
   303       valid_supply = true;
   285       valid_supply = true;
   343       for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
   325       for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
   344       if (sum != 0) return false;
   326       if (sum != 0) return false;
   345 
   327 
   346       // Finding a feasible flow
   328       // Finding a feasible flow
   347       Circulation< Graph, Capacity, FlowMap, ConstMap<Edge, Capacity>,
   329       Circulation< Graph, Capacity, FlowMap, ConstMap<Edge, Capacity>,
   348 		   CapacityRefMap, DemandMap >
   330 		   CapacityRefMap, SupplyMap >
   349 	circulation( graph, constMap<Edge>((Capacity)0),
   331 	circulation( graph, constMap<Edge>((Capacity)0),
   350 		     capacity, demand, flow );
   332 		     capacity, supply, flow );
   351       return circulation.run() == -1;
   333       return circulation.run() == -1;
   352     }
   334     }
   353 
   335 
   354 #ifdef LIMITED_CYCLE_CANCELING
   336 #ifdef LIMITED_CYCLE_CANCELING
   355     /// \brief Executes a cycle canceling algorithm using
   337     /// \brief Executes a cycle-canceling algorithm using
   356     /// \ref lemon::BellmanFord "Bellman-Ford" algorithm with limited
   338     /// \ref lemon::BellmanFord "Bellman-Ford" algorithm with limited
   357     /// iteration count.
   339     /// iteration count.
   358     bool start() {
   340     bool start() {
   359       typename BellmanFord<ResGraph, ResCostMap>::PredMap pred(res_graph);
   341       typename BellmanFord<ResGraph, ResCostMap>::PredMap pred(res_graph);
   360       typename ResGraph::template NodeMap<int> visited(res_graph);
   342       typename ResGraph::template NodeMap<int> visited(res_graph);
   371 	bf.predMap(pred);
   353 	bf.predMap(pred);
   372 	bf.init(0);
   354 	bf.init(0);
   373 	int iter_num = 0;
   355 	int iter_num = 0;
   374 	bool cycle_found = false;
   356 	bool cycle_found = false;
   375 	while (!cycle_found) {
   357 	while (!cycle_found) {
       
   358 #ifdef _NO_BACK_STEP_
       
   359 	  int curr_iter_num = length_bound <= node_num ?
       
   360 			      length_bound - iter_num : node_num - iter_num;
       
   361 #else
   376 	  int curr_iter_num = iter_num + length_bound <= node_num ?
   362 	  int curr_iter_num = iter_num + length_bound <= node_num ?
   377 			      length_bound : node_num - iter_num;
   363 			      length_bound : node_num - iter_num;
       
   364 #endif
   378 	  iter_num += curr_iter_num;
   365 	  iter_num += curr_iter_num;
   379 	  int real_iter_num = curr_iter_num;
   366 	  int real_iter_num = curr_iter_num;
   380 	  for (int i = 0; i < curr_iter_num; ++i) {
   367 	  for (int i = 0; i < curr_iter_num; ++i) {
   381 	    if (bf.processNextWeakRound()) {
   368 	    if (bf.processNextWeakRound()) {
   382 	      real_iter_num = i;
   369 	      real_iter_num = i;
   430 	    length_bound = length_bound * ALPHA_MUL / ALPHA_DIV;
   417 	    length_bound = length_bound * ALPHA_MUL / ALPHA_DIV;
   431 	}
   418 	}
   432       }
   419       }
   433 
   420 
   434 #ifdef _DEBUG_ITER_
   421 #ifdef _DEBUG_ITER_
   435       std::cout << "Limited cycle canceling algorithm finished. "
   422       std::cout << "Limited cycle-canceling algorithm finished. "
   436 		<< "Found " << cycle_num << " negative cycles."
   423 		<< "Found " << cycle_num << " negative cycles."
   437 		<< std::endl;
   424 		<< std::endl;
   438 #endif
   425 #endif
   439 
   426 
   440       // Handling nonzero lower bounds
   427       // Handling nonzero lower bounds
   445       return true;
   432       return true;
   446     }
   433     }
   447 #endif
   434 #endif
   448 
   435 
   449 #ifdef MIN_MEAN_CYCLE_CANCELING
   436 #ifdef MIN_MEAN_CYCLE_CANCELING
   450     /// \brief Executes the minimum mean cycle canceling algorithm
   437     /// \brief Executes the minimum mean cycle-canceling algorithm
   451     /// using \ref lemon::MinMeanCycle "MinMeanCycle" class.
   438     /// using \ref lemon::MinMeanCycle "MinMeanCycle" class.
   452     bool start() {
   439     bool start() {
   453       typedef Path<ResGraph> ResPath;
   440       typedef Path<ResGraph> ResPath;
   454       MinMeanCycle<ResGraph, ResCostMap> mmc(res_graph, res_cost);
   441       MinMeanCycle<ResGraph, ResCostMap> mmc(res_graph, res_cost);
   455       ResPath cycle;
   442       ResPath cycle;
   484 	  if (!mmc.findMinMean()) break;
   471 	  if (!mmc.findMinMean()) break;
   485 	}
   472 	}
   486       }
   473       }
   487 
   474 
   488 #ifdef _DEBUG_ITER_
   475 #ifdef _DEBUG_ITER_
   489       std::cout << "Minimum mean cycle canceling algorithm finished. "
   476       std::cout << "Minimum mean cycle-canceling algorithm finished. "
   490 		<< "Found " << cycle_num << " negative cycles."
   477 		<< "Found " << cycle_num << " negative cycles."
   491 		<< std::endl;
   478 		<< std::endl;
   492 #endif
   479 #endif
   493 
   480 
   494       // Handling nonzero lower bounds
   481       // Handling nonzero lower bounds