lemon/capacity_scaling.h
changeset 2620 8f41a3129746
parent 2589 1bbb28acb8c9
child 2623 90defb96ee61
equal deleted inserted replaced
12:2e49ed97fe6a 13:e1233955ff09
    50   /// - Supply values should be \e signed \e integers.
    50   /// - Supply values should be \e signed \e integers.
    51   /// - The value types of the maps should be convertible to each other.
    51   /// - The value types of the maps should be convertible to each other.
    52   /// - \c CostMap::Value must be signed type.
    52   /// - \c CostMap::Value must be signed type.
    53   ///
    53   ///
    54   /// \author Peter Kovacs
    54   /// \author Peter Kovacs
    55 
       
    56   template < typename Graph,
    55   template < typename Graph,
    57              typename LowerMap = typename Graph::template EdgeMap<int>,
    56              typename LowerMap = typename Graph::template EdgeMap<int>,
    58              typename CapacityMap = typename Graph::template EdgeMap<int>,
    57              typename CapacityMap = typename Graph::template EdgeMap<int>,
    59              typename CostMap = typename Graph::template EdgeMap<int>,
    58              typename CostMap = typename Graph::template EdgeMap<int>,
    60              typename SupplyMap = typename Graph::template NodeMap<int> >
    59              typename SupplyMap = typename Graph::template NodeMap<int> >
   123         _graph(graph), _flow(flow), _res_cap(res_cap), _cost(cost),
   122         _graph(graph), _flow(flow), _res_cap(res_cap), _cost(cost),
   124         _excess(excess), _potential(potential), _dist(graph),
   123         _excess(excess), _potential(potential), _dist(graph),
   125         _pred(pred)
   124         _pred(pred)
   126       {}
   125       {}
   127 
   126 
   128       /// Runs the algorithm from the given source node.
   127       /// Run the algorithm from the given source node.
   129       Node run(Node s, Capacity delta = 1) {
   128       Node run(Node s, Capacity delta = 1) {
   130         HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
   129         HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
   131         Heap heap(heap_cross_ref);
   130         Heap heap(heap_cross_ref);
   132         heap.push(s, 0);
   131         heap.push(s, 0);
   133         _pred[s] = INVALID;
   132         _pred[s] = INVALID;
   369       if (_local_flow) delete _flow;
   368       if (_local_flow) delete _flow;
   370       if (_local_potential) delete _potential;
   369       if (_local_potential) delete _potential;
   371       delete _dijkstra;
   370       delete _dijkstra;
   372     }
   371     }
   373 
   372 
   374     /// \brief Sets the flow map.
   373     /// \brief Set the flow map.
   375     ///
   374     ///
   376     /// Sets the flow map.
   375     /// Set the flow map.
   377     ///
   376     ///
   378     /// \return \c (*this)
   377     /// \return \c (*this)
   379     CapacityScaling& flowMap(FlowMap &map) {
   378     CapacityScaling& flowMap(FlowMap &map) {
   380       if (_local_flow) {
   379       if (_local_flow) {
   381         delete _flow;
   380         delete _flow;
   383       }
   382       }
   384       _flow = &map;
   383       _flow = &map;
   385       return *this;
   384       return *this;
   386     }
   385     }
   387 
   386 
   388     /// \brief Sets the potential map.
   387     /// \brief Set the potential map.
   389     ///
   388     ///
   390     /// Sets the potential map.
   389     /// Set the potential map.
   391     ///
   390     ///
   392     /// \return \c (*this)
   391     /// \return \c (*this)
   393     CapacityScaling& potentialMap(PotentialMap &map) {
   392     CapacityScaling& potentialMap(PotentialMap &map) {
   394       if (_local_potential) {
   393       if (_local_potential) {
   395         delete _potential;
   394         delete _potential;
   398       _potential = &map;
   397       _potential = &map;
   399       return *this;
   398       return *this;
   400     }
   399     }
   401 
   400 
   402     /// \name Execution control
   401     /// \name Execution control
   403     /// The only way to execute the algorithm is to call the run()
       
   404     /// function.
       
   405 
   402 
   406     /// @{
   403     /// @{
   407 
   404 
   408     /// \brief Runs the algorithm.
   405     /// \brief Run the algorithm.
   409     ///
   406     ///
   410     /// Runs the algorithm.
   407     /// This function runs the algorithm.
   411     ///
   408     ///
   412     /// \param scaling Enable or disable capacity scaling.
   409     /// \param scaling Enable or disable capacity scaling.
   413     /// If the maximum edge capacity and/or the amount of total supply
   410     /// If the maximum edge capacity and/or the amount of total supply
   414     /// is rather small, the algorithm could be slightly faster without
   411     /// is rather small, the algorithm could be slightly faster without
   415     /// scaling.
   412     /// scaling.
   420     }
   417     }
   421 
   418 
   422     /// @}
   419     /// @}
   423 
   420 
   424     /// \name Query Functions
   421     /// \name Query Functions
   425     /// The result of the algorithm can be obtained using these
   422     /// The results of the algorithm can be obtained using these
   426     /// functions.
   423     /// functions.\n
   427     /// \n run() must be called before using them.
   424     /// \ref lemon::CapacityScaling::run() "run()" must be called before
       
   425     /// using them.
   428 
   426 
   429     /// @{
   427     /// @{
   430 
   428 
   431     /// \brief Returns a const reference to the edge map storing the
   429     /// \brief Return a const reference to the edge map storing the
   432     /// found flow.
   430     /// found flow.
   433     ///
   431     ///
   434     /// Returns a const reference to the edge map storing the found flow.
   432     /// Return a const reference to the edge map storing the found flow.
   435     ///
   433     ///
   436     /// \pre \ref run() must be called before using this function.
   434     /// \pre \ref run() must be called before using this function.
   437     const FlowMap& flowMap() const {
   435     const FlowMap& flowMap() const {
   438       return *_flow;
   436       return *_flow;
   439     }
   437     }
   440 
   438 
   441     /// \brief Returns a const reference to the node map storing the
   439     /// \brief Return a const reference to the node map storing the
   442     /// found potentials (the dual solution).
   440     /// found potentials (the dual solution).
   443     ///
   441     ///
   444     /// Returns a const reference to the node map storing the found
   442     /// Return a const reference to the node map storing the found
   445     /// potentials (the dual solution).
   443     /// potentials (the dual solution).
   446     ///
   444     ///
   447     /// \pre \ref run() must be called before using this function.
   445     /// \pre \ref run() must be called before using this function.
   448     const PotentialMap& potentialMap() const {
   446     const PotentialMap& potentialMap() const {
   449       return *_potential;
   447       return *_potential;
   450     }
   448     }
   451 
   449 
   452     /// \brief Returns the flow on the given edge.
   450     /// \brief Return the flow on the given edge.
   453     ///
   451     ///
   454     /// Returns the flow on the given edge.
   452     /// Return the flow on the given edge.
   455     ///
   453     ///
   456     /// \pre \ref run() must be called before using this function.
   454     /// \pre \ref run() must be called before using this function.
   457     Capacity flow(const Edge& edge) const {
   455     Capacity flow(const Edge& edge) const {
   458       return (*_flow)[edge];
   456       return (*_flow)[edge];
   459     }
   457     }
   460 
   458 
   461     /// \brief Returns the potential of the given node.
   459     /// \brief Return the potential of the given node.
   462     ///
   460     ///
   463     /// Returns the potential of the given node.
   461     /// Return the potential of the given node.
   464     ///
   462     ///
   465     /// \pre \ref run() must be called before using this function.
   463     /// \pre \ref run() must be called before using this function.
   466     Cost potential(const Node& node) const {
   464     Cost potential(const Node& node) const {
   467       return (*_potential)[node];
   465       return (*_potential)[node];
   468     }
   466     }
   469 
   467 
   470     /// \brief Returns the total cost of the found flow.
   468     /// \brief Return the total cost of the found flow.
   471     ///
   469     ///
   472     /// Returns the total cost of the found flow. The complexity of the
   470     /// Return the total cost of the found flow. The complexity of the
   473     /// function is \f$ O(e) \f$.
   471     /// function is \f$ O(e) \f$.
   474     ///
   472     ///
   475     /// \pre \ref run() must be called before using this function.
   473     /// \pre \ref run() must be called before using this function.
   476     Cost totalCost() const {
   474     Cost totalCost() const {
   477       Cost c = 0;
   475       Cost c = 0;
   482 
   480 
   483     /// @}
   481     /// @}
   484 
   482 
   485   private:
   483   private:
   486 
   484 
   487     /// Initializes the algorithm.
   485     /// Initialize the algorithm.
   488     bool init(bool scaling) {
   486     bool init(bool scaling) {
   489       if (!_valid_supply) return false;
   487       if (!_valid_supply) return false;
   490 
   488 
   491       // Initializing maps
   489       // Initializing maps
   492       if (!_flow) {
   490       if (!_flow) {
   533         return startWithScaling();
   531         return startWithScaling();
   534       else
   532       else
   535         return startWithoutScaling();
   533         return startWithoutScaling();
   536     }
   534     }
   537 
   535 
   538     /// Executes the capacity scaling algorithm.
   536     /// Execute the capacity scaling algorithm.
   539     bool startWithScaling() {
   537     bool startWithScaling() {
   540       // Processing capacity scaling phases
   538       // Processing capacity scaling phases
   541       Node s, t;
   539       Node s, t;
   542       int phase_cnt = 0;
   540       int phase_cnt = 0;
   543       int factor = 4;
   541       int factor = 4;
   565         _deficit_nodes.clear();
   563         _deficit_nodes.clear();
   566         for (NodeIt n(_graph); n != INVALID; ++n) {
   564         for (NodeIt n(_graph); n != INVALID; ++n) {
   567           if (_excess[n] >=  _delta) _excess_nodes.push_back(n);
   565           if (_excess[n] >=  _delta) _excess_nodes.push_back(n);
   568           if (_excess[n] <= -_delta) _deficit_nodes.push_back(n);
   566           if (_excess[n] <= -_delta) _deficit_nodes.push_back(n);
   569         }
   567         }
   570         int next_node = 0;
   568         int next_node = 0, next_def_node = 0;
   571 
   569 
   572         // Finding augmenting shortest paths
   570         // Finding augmenting shortest paths
   573         while (next_node < int(_excess_nodes.size())) {
   571         while (next_node < int(_excess_nodes.size())) {
   574           // Checking deficit nodes
   572           // Checking deficit nodes
   575           if (_delta > 1) {
   573           if (_delta > 1) {
   576             bool delta_deficit = false;
   574             bool delta_deficit = false;
   577             for (int i = 0; i < int(_deficit_nodes.size()); ++i) {
   575             for ( ; next_def_node < int(_deficit_nodes.size());
   578               if (_excess[_deficit_nodes[i]] <= -_delta) {
   576                     ++next_def_node ) {
       
   577               if (_excess[_deficit_nodes[next_def_node]] <= -_delta) {
   579                 delta_deficit = true;
   578                 delta_deficit = true;
   580                 break;
   579                 break;
   581               }
   580               }
   582             }
   581             }
   583             if (!delta_deficit) break;
   582             if (!delta_deficit) break;
   639           (*_flow)[e] += (*_lower)[e];
   638           (*_flow)[e] += (*_lower)[e];
   640       }
   639       }
   641       return true;
   640       return true;
   642     }
   641     }
   643 
   642 
   644     /// Executes the successive shortest path algorithm.
   643     /// Execute the successive shortest path algorithm.
   645     bool startWithoutScaling() {
   644     bool startWithoutScaling() {
   646       // Finding excess nodes
   645       // Finding excess nodes
   647       for (NodeIt n(_graph); n != INVALID; ++n)
   646       for (NodeIt n(_graph); n != INVALID; ++n)
   648         if (_excess[n] > 0) _excess_nodes.push_back(n);
   647         if (_excess[n] > 0) _excess_nodes.push_back(n);
   649       if (_excess_nodes.size() == 0) return true;
   648       if (_excess_nodes.size() == 0) return true;