lemon/cycle_canceling.h
changeset 2622 fa2877651022
parent 2593 8eed667ea23c
child 2623 90defb96ee61
equal deleted inserted replaced
11:115a3ce0e7f9 12:e27e8ec61013
    62   /// but rather slower in practice.
    62   /// but rather slower in practice.
    63   /// To use this version of the algorithm, call \ref run() with \c true
    63   /// To use this version of the algorithm, call \ref run() with \c true
    64   /// parameter.
    64   /// parameter.
    65   ///
    65   ///
    66   /// \author Peter Kovacs
    66   /// \author Peter Kovacs
    67 
       
    68   template < typename Graph,
    67   template < typename Graph,
    69              typename LowerMap = typename Graph::template EdgeMap<int>,
    68              typename LowerMap = typename Graph::template EdgeMap<int>,
    70              typename CapacityMap = typename Graph::template EdgeMap<int>,
    69              typename CapacityMap = typename Graph::template EdgeMap<int>,
    71              typename CostMap = typename Graph::template EdgeMap<int>,
    70              typename CostMap = typename Graph::template EdgeMap<int>,
    72              typename SupplyMap = typename Graph::template NodeMap<int> >
    71              typename SupplyMap = typename Graph::template NodeMap<int> >
    96 
    95 
    97   private:
    96   private:
    98 
    97 
    99     /// \brief Map adaptor class for handling residual edge costs.
    98     /// \brief Map adaptor class for handling residual edge costs.
   100     ///
    99     ///
   101     /// \ref ResidualCostMap is a map adaptor class for handling
   100     /// Map adaptor class for handling residual edge costs.
   102     /// residual edge costs.
       
   103     class ResidualCostMap : public MapBase<ResEdge, Cost>
   101     class ResidualCostMap : public MapBase<ResEdge, Cost>
   104     {
   102     {
   105     private:
   103     private:
   106 
   104 
   107       const CostMap &_cost_map;
   105       const CostMap &_cost_map;
   276       if (_local_flow) delete _flow;
   274       if (_local_flow) delete _flow;
   277       if (_local_potential) delete _potential;
   275       if (_local_potential) delete _potential;
   278       delete _res_graph;
   276       delete _res_graph;
   279     }
   277     }
   280 
   278 
   281     /// \brief Sets the flow map.
   279     /// \brief Set the flow map.
   282     ///
   280     ///
   283     /// Sets the flow map.
   281     /// Set the flow map.
   284     ///
   282     ///
   285     /// \return \c (*this)
   283     /// \return \c (*this)
   286     CycleCanceling& flowMap(FlowMap &map) {
   284     CycleCanceling& flowMap(FlowMap &map) {
   287       if (_local_flow) {
   285       if (_local_flow) {
   288         delete _flow;
   286         delete _flow;
   290       }
   288       }
   291       _flow = &map;
   289       _flow = &map;
   292       return *this;
   290       return *this;
   293     }
   291     }
   294 
   292 
   295     /// \brief Sets the potential map.
   293     /// \brief Set the potential map.
   296     ///
   294     ///
   297     /// Sets the potential map.
   295     /// Set the potential map.
   298     ///
   296     ///
   299     /// \return \c (*this)
   297     /// \return \c (*this)
   300     CycleCanceling& potentialMap(PotentialMap &map) {
   298     CycleCanceling& potentialMap(PotentialMap &map) {
   301       if (_local_potential) {
   299       if (_local_potential) {
   302         delete _potential;
   300         delete _potential;
   305       _potential = &map;
   303       _potential = &map;
   306       return *this;
   304       return *this;
   307     }
   305     }
   308 
   306 
   309     /// \name Execution control
   307     /// \name Execution control
   310     /// The only way to execute the algorithm is to call the run()
       
   311     /// function.
       
   312 
   308 
   313     /// @{
   309     /// @{
   314 
   310 
   315     /// \brief Runs the algorithm.
   311     /// \brief Run the algorithm.
   316     ///
   312     ///
   317     /// Runs the algorithm.
   313     /// Run the algorithm.
   318     ///
   314     ///
   319     /// \param min_mean_cc Set this parameter to \c true to run the
   315     /// \param min_mean_cc Set this parameter to \c true to run the
   320     /// "Minimum Mean Cycle-Canceling" algorithm, which is strongly
   316     /// "Minimum Mean Cycle-Canceling" algorithm, which is strongly
   321     /// polynomial, but rather slower in practice.
   317     /// polynomial, but rather slower in practice.
   322     ///
   318     ///
   327 
   323 
   328     /// @}
   324     /// @}
   329 
   325 
   330     /// \name Query Functions
   326     /// \name Query Functions
   331     /// The result of the algorithm can be obtained using these
   327     /// The result of the algorithm can be obtained using these
   332     /// functions.
   328     /// functions.\n
   333     /// \n run() must be called before using them.
   329     /// \ref lemon::CycleCanceling::run() "run()" must be called before
       
   330     /// using them.
   334 
   331 
   335     /// @{
   332     /// @{
   336 
   333 
   337     /// \brief Returns a const reference to the edge map storing the
   334     /// \brief Return a const reference to the edge map storing the
   338     /// found flow.
   335     /// found flow.
   339     ///
   336     ///
   340     /// Returns a const reference to the edge map storing the found flow.
   337     /// Return a const reference to the edge map storing the found flow.
   341     ///
   338     ///
   342     /// \pre \ref run() must be called before using this function.
   339     /// \pre \ref run() must be called before using this function.
   343     const FlowMap& flowMap() const {
   340     const FlowMap& flowMap() const {
   344       return *_flow;
   341       return *_flow;
   345     }
   342     }
   346 
   343 
   347     /// \brief Returns a const reference to the node map storing the
   344     /// \brief Return a const reference to the node map storing the
   348     /// found potentials (the dual solution).
   345     /// found potentials (the dual solution).
   349     ///
   346     ///
   350     /// Returns a const reference to the node map storing the found
   347     /// Return a const reference to the node map storing the found
   351     /// potentials (the dual solution).
   348     /// potentials (the dual solution).
   352     ///
   349     ///
   353     /// \pre \ref run() must be called before using this function.
   350     /// \pre \ref run() must be called before using this function.
   354     const PotentialMap& potentialMap() const {
   351     const PotentialMap& potentialMap() const {
   355       return *_potential;
   352       return *_potential;
   356     }
   353     }
   357 
   354 
   358     /// \brief Returns the flow on the given edge.
   355     /// \brief Return the flow on the given edge.
   359     ///
   356     ///
   360     /// Returns the flow on the given edge.
   357     /// Return the flow on the given edge.
   361     ///
   358     ///
   362     /// \pre \ref run() must be called before using this function.
   359     /// \pre \ref run() must be called before using this function.
   363     Capacity flow(const Edge& edge) const {
   360     Capacity flow(const Edge& edge) const {
   364       return (*_flow)[edge];
   361       return (*_flow)[edge];
   365     }
   362     }
   366 
   363 
   367     /// \brief Returns the potential of the given node.
   364     /// \brief Return the potential of the given node.
   368     ///
   365     ///
   369     /// Returns the potential of the given node.
   366     /// Return the potential of the given node.
   370     ///
   367     ///
   371     /// \pre \ref run() must be called before using this function.
   368     /// \pre \ref run() must be called before using this function.
   372     Cost potential(const Node& node) const {
   369     Cost potential(const Node& node) const {
   373       return (*_potential)[node];
   370       return (*_potential)[node];
   374     }
   371     }
   375 
   372 
   376     /// \brief Returns the total cost of the found flow.
   373     /// \brief Return the total cost of the found flow.
   377     ///
   374     ///
   378     /// Returns the total cost of the found flow. The complexity of the
   375     /// Return the total cost of the found flow. The complexity of the
   379     /// function is \f$ O(e) \f$.
   376     /// function is \f$ O(e) \f$.
   380     ///
   377     ///
   381     /// \pre \ref run() must be called before using this function.
   378     /// \pre \ref run() must be called before using this function.
   382     Cost totalCost() const {
   379     Cost totalCost() const {
   383       Cost c = 0;
   380       Cost c = 0;
   388 
   385 
   389     /// @}
   386     /// @}
   390 
   387 
   391   private:
   388   private:
   392 
   389 
   393     /// Initializes the algorithm.
   390     /// Initialize the algorithm.
   394     bool init() {
   391     bool init() {
   395       if (!_valid_supply) return false;
   392       if (!_valid_supply) return false;
   396 
   393 
   397       // Initializing flow and potential maps
   394       // Initializing flow and potential maps
   398       if (!_flow) {
   395       if (!_flow) {
   426           (*_flow)[e] += (*_lower)[e];
   423           (*_flow)[e] += (*_lower)[e];
   427       }
   424       }
   428       return true;
   425       return true;
   429     }
   426     }
   430 
   427 
   431     /// \brief Executes the algorithm using \ref BellmanFord.
   428     /// \brief Execute the algorithm using \ref BellmanFord.
   432     ///
   429     ///
   433     /// Executes the algorithm using the \ref BellmanFord
   430     /// Execute the algorithm using the \ref BellmanFord
   434     /// "Bellman-Ford" algorithm for negative cycle detection with
   431     /// "Bellman-Ford" algorithm for negative cycle detection with
   435     /// successively larger limit for the number of iterations.
   432     /// successively larger limit for the number of iterations.
   436     void start() {
   433     void start() {
   437       typename BellmanFord<ResGraph, ResidualCostMap>::PredMap pred(*_res_graph);
   434       typename BellmanFord<ResGraph, ResidualCostMap>::PredMap pred(*_res_graph);
   438       typename ResGraph::template NodeMap<int> visited(*_res_graph);
   435       typename ResGraph::template NodeMap<int> visited(*_res_graph);
   504             length_bound = length_bound * BF_LIMIT_FACTOR / 100;
   501             length_bound = length_bound * BF_LIMIT_FACTOR / 100;
   505         }
   502         }
   506       }
   503       }
   507     }
   504     }
   508 
   505 
   509     /// \brief Executes the algorithm using \ref MinMeanCycle.
   506     /// \brief Execute the algorithm using \ref MinMeanCycle.
   510     ///
   507     ///
   511     /// Executes the algorithm using \ref MinMeanCycle for negative
   508     /// Execute the algorithm using \ref MinMeanCycle for negative
   512     /// cycle detection.
   509     /// cycle detection.
   513     void startMinMean() {
   510     void startMinMean() {
   514       typedef Path<ResGraph> ResPath;
   511       typedef Path<ResGraph> ResPath;
   515       MinMeanCycle<ResGraph, ResidualCostMap> mmc(*_res_graph, _res_cost);
   512       MinMeanCycle<ResGraph, ResidualCostMap> mmc(*_res_graph, _res_cost);
   516       ResPath cycle;
   513       ResPath cycle;