COIN-OR::LEMON - Graph Library

Ticket #176: preflow_impr_9b8778f1d992.patch

File preflow_impr_9b8778f1d992.patch, 17.3 KB (added by Peter Kovacs, 16 years ago)
  • lemon/preflow.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1227863811 -3600
    # Node ID 9b8778f1d9922a15dcf159d34c815df3c83d0a82
    # Parent  624e673efa760f16a74fd941b320cc11c1890f5e
    Many doc improvements for Preflow (#176)
    
     - More precise doc for members.
     - Add doc for public types.
     - Removing \author comments.
     - Use \tparam for template parameters.
    
    diff --git a/lemon/preflow.h b/lemon/preflow.h
    a b  
    3131  /// \brief Default traits class of Preflow class.
    3232  ///
    3333  /// Default traits class of Preflow class.
    34   /// \param _Graph Digraph type.
    35   /// \param _CapacityMap Type of capacity map.
    36   template <typename _Graph, typename _CapacityMap>
     34  /// \tparam _Digraph Digraph type.
     35  /// \tparam _CapacityMap Capacity map type.
     36  template <typename _Digraph, typename _CapacityMap>
    3737  struct PreflowDefaultTraits {
    3838
    39     /// \brief The digraph type the algorithm runs on.
    40     typedef _Graph Digraph;
     39    /// \brief The type of the digraph the algorithm runs on.
     40    typedef _Digraph Digraph;
    4141
    4242    /// \brief The type of the map that stores the arc capacities.
    4343    ///
     
    4545    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
    4646    typedef _CapacityMap CapacityMap;
    4747
    48     /// \brief The type of the length of the arcs.
     48    /// \brief The type of the flow values.
    4949    typedef typename CapacityMap::Value Value;
    5050
    51     /// \brief The map type that stores the flow values.
     51    /// \brief The type of the map that stores the flow values.
    5252    ///
    53     /// The map type that stores the flow values.
     53    /// The type of the map that stores the flow values.
    5454    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
    5555    typedef typename Digraph::template ArcMap<Value> FlowMap;
    5656
     
    6363      return new FlowMap(digraph);
    6464    }
    6565
    66     /// \brief The eleavator type used by Preflow algorithm.
     66    /// \brief The elevator type used by Preflow algorithm.
    6767    ///
    6868    /// The elevator type used by Preflow algorithm.
    6969    ///
     
    7373
    7474    /// \brief Instantiates an Elevator.
    7575    ///
    76     /// This function instantiates a \ref Elevator.
     76    /// This function instantiates an \ref Elevator.
    7777    /// \param digraph The digraph, to which we would like to define
    7878    /// the elevator.
    7979    /// \param max_level The maximum level of the elevator.
     
    9191
    9292  /// \ingroup max_flow
    9393  ///
    94   /// \brief %Preflow algorithms class.
     94  /// \brief %Preflow algorithm class.
    9595  ///
    96   /// This class provides an implementation of the Goldberg's \e
    97   /// preflow \e algorithm producing a flow of maximum value in a
    98   /// digraph. The preflow algorithms are the fastest known max
     96  /// This class provides an implementation of Goldberg's \e preflow
     97  /// \e push-relabel \e algorithm producing a flow of maximum value in a
     98  /// digraph. The preflow algorithms are the fastest known maximum
    9999  /// flow algorithms. The current implementation use a mixture of the
    100100  /// \e "highest label" and the \e "bound decrease" heuristics.
    101101  /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
    102102  ///
    103   /// The algorithm consists from two phases. After the first phase
    104   /// the maximal flow value and the minimum cut can be obtained. The
    105   /// second phase constructs the feasible maximum flow on each arc.
     103  /// The algorithm consists of two phases. After the first phase
     104  /// the maximum flow value and the minimum cut is obtained. The
     105  /// second phase constructs a feasible maximum flow on each arc.
    106106  ///
    107   /// \param _Graph The digraph type the algorithm runs on.
    108   /// \param _CapacityMap The flow map type.
    109   /// \param _Traits Traits class to set various data types used by
     107  /// \tparam _Digraph The type of the digraph the algorithm runs on.
     108  /// \tparam _CapacityMap The type of the capacity map. The default map
     109  /// type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>".
     110  /// \tparam _Traits Traits class to set various data types used by
    110111  /// the algorithm.  The default traits class is \ref
    111112  /// PreflowDefaultTraits.  See \ref PreflowDefaultTraits for the
    112113  /// documentation of a %Preflow traits class.
    113   ///
    114   ///\author Jacint Szabo and Balazs Dezso
    115114#ifdef DOXYGEN
    116   template <typename _Graph, typename _CapacityMap, typename _Traits>
     115  template <typename _Digraph, typename _CapacityMap, typename _Traits>
    117116#else
    118   template <typename _Graph,
    119             typename _CapacityMap = typename _Graph::template ArcMap<int>,
    120             typename _Traits = PreflowDefaultTraits<_Graph, _CapacityMap> >
     117  template <typename _Digraph,
     118            typename _CapacityMap = typename _Digraph::template ArcMap<int>,
     119            typename _Traits = PreflowDefaultTraits<_Digraph, _CapacityMap> >
    121120#endif
    122121  class Preflow {
    123122  public:
    124123
     124    ///The traits class.
    125125    typedef _Traits Traits;
     126    ///The type of the digraph the algorithm runs on.
    126127    typedef typename Traits::Digraph Digraph;
     128    ///The type of the capacity map.
    127129    typedef typename Traits::CapacityMap CapacityMap;
     130    ///The type of the flow values.
    128131    typedef typename Traits::Value Value;
    129132
     133    ///The type of the flow map.
    130134    typedef typename Traits::FlowMap FlowMap;
     135    ///The type of the elevator.
    131136    typedef typename Traits::Elevator Elevator;
     137    ///The type of the tolerance.
    132138    typedef typename Traits::Tolerance Tolerance;
    133139
    134140  private:
     
    188194
    189195    typedef Preflow Create;
    190196
    191     ///\name Named template parameters
     197    ///\name Named Template Parameters
    192198
    193199    ///@{
    194200
     
    205211    /// FlowMap type
    206212    ///
    207213    /// \ref named-templ-param "Named parameter" for setting FlowMap
    208     /// type
     214    /// type.
    209215    template <typename _FlowMap>
    210216    struct SetFlowMap
    211217      : public Preflow<Digraph, CapacityMap, SetFlowMapTraits<_FlowMap> > {
     
    226232    /// Elevator type
    227233    ///
    228234    /// \ref named-templ-param "Named parameter" for setting Elevator
    229     /// type
     235    /// type. If this named parameter is used, then an external
     236    /// elevator object must be passed to the algorithm using the
     237    /// \ref elevator(Elevator&) "elevator()" function before calling
     238    /// \ref run() or \ref init().
     239    /// \sa SetStandardElevator
    230240    template <typename _Elevator>
    231241    struct SetElevator
    232242      : public Preflow<Digraph, CapacityMap, SetElevatorTraits<_Elevator> > {
     
    243253    };
    244254
    245255    /// \brief \ref named-templ-param "Named parameter" for setting
    246     /// Elevator type
     256    /// Elevator type with automatic allocation
    247257    ///
    248258    /// \ref named-templ-param "Named parameter" for setting Elevator
    249     /// type. The Elevator should be standard constructor interface, ie.
    250     /// the digraph and the maximum level should be passed to it.
     259    /// type with automatic allocation.
     260    /// The Elevator should have standard constructor interface to be
     261    /// able to automatically created by the algorithm (i.e. the
     262    /// digraph and the maximum level should be passed to it).
     263    /// However an external elevator object could also be passed to the
     264    /// algorithm with the \ref elevator(Elevator&) "elevator()" function
     265    /// before calling \ref run() or \ref init().
     266    /// \sa SetElevator
    251267    template <typename _Elevator>
    252268    struct SetStandardElevator
    253269      : public Preflow<Digraph, CapacityMap,
     
    273289    /// \param source The source node.
    274290    /// \param target The target node.
    275291    Preflow(const Digraph& digraph, const CapacityMap& capacity,
    276                Node source, Node target)
     292            Node source, Node target)
    277293      : _graph(digraph), _capacity(&capacity),
    278294        _node_num(0), _source(source), _target(target),
    279295        _flow(0), _local_flow(false),
     
    290306    /// \brief Sets the capacity map.
    291307    ///
    292308    /// Sets the capacity map.
    293     /// \return \c (*this)
     309    /// \return <tt>(*this)</tt>
    294310    Preflow& capacityMap(const CapacityMap& map) {
    295311      _capacity = &map;
    296312      return *this;
     
    299315    /// \brief Sets the flow map.
    300316    ///
    301317    /// Sets the flow map.
    302     /// \return \c (*this)
     318    /// If you don't use this function before calling \ref run() or
     319    /// \ref init(), an instance will be allocated automatically.
     320    /// The destructor deallocates this automatically allocated map,
     321    /// of course.
     322    /// \return <tt>(*this)</tt>
    303323    Preflow& flowMap(FlowMap& map) {
    304324      if (_local_flow) {
    305325        delete _flow;
     
    309329      return *this;
    310330    }
    311331
    312     /// \brief Returns the flow map.
     332    /// \brief Sets the source node.
    313333    ///
    314     /// \return The flow map.
    315     const FlowMap& flowMap() {
    316       return *_flow;
     334    /// Sets the source node.
     335    /// \return <tt>(*this)</tt>
     336    Preflow& source(const Node& node) {
     337      _source = node;
     338      return *this;
     339    }
     340
     341    /// \brief Sets the target node.
     342    ///
     343    /// Sets the target node.
     344    /// \return <tt>(*this)</tt>
     345    Preflow& target(const Node& node) {
     346      _target = node;
     347      return *this;
    317348    }
    318349
    319350    /// \brief Sets the elevator.
    320351    ///
    321352    /// Sets the elevator.
    322     /// \return \c (*this)
     353    /// If you don't use this function before calling \ref run() or
     354    /// \ref init(), an instance will be allocated automatically.
     355    /// The destructor deallocates this automatically allocated elevator,
     356    /// of course.
     357    /// \return <tt>(*this)</tt>
    323358    Preflow& elevator(Elevator& elevator) {
    324359      if (_local_level) {
    325360        delete _level;
     
    329364      return *this;
    330365    }
    331366
    332     /// \brief Returns the elevator.
     367    /// \brief Returns a const reference to the elevator.
    333368    ///
    334     /// \return The elevator.
     369    /// Returns a const reference to the elevator.
     370    ///
     371    /// \pre Either \ref run() or \ref init() must be called before
     372    /// using this function.
    335373    const Elevator& elevator() {
    336374      return *_level;
    337     }
    338 
    339     /// \brief Sets the source node.
    340     ///
    341     /// Sets the source node.
    342     /// \return \c (*this)
    343     Preflow& source(const Node& node) {
    344       _source = node;
    345       return *this;
    346     }
    347 
    348     /// \brief Sets the target node.
    349     ///
    350     /// Sets the target node.
    351     /// \return \c (*this)
    352     Preflow& target(const Node& node) {
    353       _target = node;
    354       return *this;
    355375    }
    356376
    357377    /// \brief Sets the tolerance used by algorithm.
     
    369389      return tolerance;
    370390    }
    371391
    372     /// \name Execution control The simplest way to execute the
    373     /// algorithm is to use one of the member functions called \c
    374     /// run().
    375     /// \n
    376     /// If you need more control on initial solution or
    377     /// execution then you have to call one \ref init() function and then
    378     /// the startFirstPhase() and if you need the startSecondPhase().
     392    /// \name Execution Control
     393    /// The simplest way to execute the preflow algorithm is to use
     394    /// \ref run() or \ref runMinCut().\n
     395    /// If you need more control on the initial solution or the execution,
     396    /// first you have to call one of the \ref init() functions, then
     397    /// \ref startFirstPhase() and if you need it \ref startSecondPhase().
    379398
    380399    ///@{
    381400
    382401    /// \brief Initializes the internal data structures.
    383402    ///
    384     /// Initializes the internal data structures.
    385     ///
     403    /// Initializes the internal data structures and sets the initial
     404    /// flow to zero on each arc.
    386405    void init() {
    387406      createStructures();
    388407
     
    440459    ///
    441460    /// Initializes the internal data structures and sets the initial
    442461    /// flow to the given \c flowMap. The \c flowMap should contain a
    443     /// flow or at least a preflow, ie. in each node excluding the
    444     /// target the incoming flow should greater or equal to the
     462    /// flow or at least a preflow, i.e. at each node excluding the
     463    /// source node the incoming flow should greater or equal to the
    445464    /// outgoing flow.
    446     /// \return %False when the given \c flowMap is not a preflow.
     465    /// \return \c false if the given \c flowMap is not a preflow.
    447466    template <typename FlowMap>
    448467    bool flowInit(const FlowMap& flowMap) {
    449468      createStructures();
     
    536555    /// maximum flow is not yet obtained. So after calling this method
    537556    /// \ref flowValue() returns the value of a maximum flow and \ref
    538557    /// minCut() returns a minimum cut.
    539     /// \pre One of the \ref init() functions should be called.
     558    /// \pre One of the \ref init() functions must be called before
     559    /// using this function.
    540560    void startFirstPhase() {
    541561      _phase = true;
    542562
     
    702722    /// \brief Starts the second phase of the preflow algorithm.
    703723    ///
    704724    /// The preflow algorithm consists of two phases, this method runs
    705     /// the second phase. After calling \ref init() and \ref
    706     /// startFirstPhase() and then \ref startSecondPhase(), \ref
    707     /// flowMap() return a maximum flow, \ref flowValue() returns the
     725    /// the second phase. After calling one of the \ref init() functions
     726    /// and \ref startFirstPhase() and then \ref startSecondPhase(),
     727    /// \ref flowMap() returns a maximum flow, \ref flowValue() returns the
    708728    /// value of a maximum flow, \ref minCut() returns a minimum cut
    709     /// \pre The \ref init() and startFirstPhase() functions should be
    710     /// called before.
     729    /// \pre One of the \ref init() functions and \ref startFirstPhase()
     730    /// must be called before using this function.
    711731    void startSecondPhase() {
    712732      _phase = false;
    713733
     
    863883    /// @}
    864884
    865885    /// \name Query Functions
    866     /// The result of the %Preflow algorithm can be obtained using these
     886    /// The results of the preflow algorithm can be obtained using these
    867887    /// functions.\n
    868     /// Before the use of these functions,
    869     /// either run() or start() must be called.
     888    /// Either one of the \ref run() "run*()" functions or one of the
     889    /// \ref startFirstPhase() "start*()" functions should be called
     890    /// before using them.
    870891
    871892    ///@{
    872893
    873894    /// \brief Returns the value of the maximum flow.
    874895    ///
    875896    /// Returns the value of the maximum flow by returning the excess
    876     /// of the target node \c t. This value equals to the value of
    877     /// the maximum flow already after the first phase.
     897    /// of the target node. This value equals to the value of
     898    /// the maximum flow already after the first phase of the algorithm.
     899    ///
     900    /// \pre Either \ref run() or \ref init() must be called before
     901    /// using this function.
    878902    Value flowValue() const {
    879903      return (*_excess)[_target];
    880904    }
    881905
    882     /// \brief Returns true when the node is on the source side of minimum cut.
     906    /// \brief Returns the flow on the given arc.
    883907    ///
    884     /// Returns true when the node is on the source side of minimum
    885     /// cut. This method can be called both after running \ref
     908    /// Returns the flow on the given arc. This method can
     909    /// be called after the second phase of the algorithm.
     910    ///
     911    /// \pre Either \ref run() or \ref init() must be called before
     912    /// using this function.
     913    Value flow(const Arc& arc) const {
     914      return (*_flow)[arc];
     915    }
     916
     917    /// \brief Returns a const reference to the flow map.
     918    ///
     919    /// Returns a const reference to the arc map storing the found flow.
     920    ///
     921    /// \pre Either \ref run() or \ref init() must be called before
     922    /// using this function.
     923    const FlowMap& flowMap() {
     924      return *_flow;
     925    }
     926
     927    /// \brief Returns \c true when the node is on the source side of the
     928    /// minimum cut.
     929    ///
     930    /// Returns true when the node is on the source side of the found
     931    /// minimum cut. This method can be called both after running \ref
    886932    /// startFirstPhase() and \ref startSecondPhase().
     933    ///
     934    /// \pre Either \ref run() or \ref init() must be called before
     935    /// using this function.
    887936    bool minCut(const Node& node) const {
    888937      return ((*_level)[node] == _level->maxLevel()) == _phase;
    889938    }
    890939
    891     /// \brief Returns a minimum value cut.
     940    /// \brief Gives back a minimum value cut.
    892941    ///
    893     /// Sets the \c cutMap to the characteristic vector of a minimum value
    894     /// cut. This method can be called both after running \ref
    895     /// startFirstPhase() and \ref startSecondPhase(). The result after second
    896     /// phase could be changed slightly if inexact computation is used.
    897     /// \pre The \c cutMap should be a bool-valued node-map.
     942    /// Sets \c cutMap to the characteristic vector of a minimum value
     943    /// cut. \c cutMap should be a \ref concepts::WriteMap "writable"
     944    /// node map with \c bool (or convertible) value type.
     945    ///
     946    /// This method can be called both after running \ref startFirstPhase()
     947    /// and \ref startSecondPhase(). The result after the second phase
     948    /// could be slightly different if inexact computation is used.
     949    ///
     950    /// This function is just a shortcut of calling \ref minCut() for
     951    /// each node, so it runs in \f$O(n)\f$ time.
     952    ///
     953    /// \pre Either \ref run() or \ref init() must be called before
     954    /// using this function.
    898955    template <typename CutMap>
    899956    void minCutMap(CutMap& cutMap) const {
    900957      for (NodeIt n(_graph); n != INVALID; ++n) {
     
    902959      }
    903960    }
    904961
    905     /// \brief Returns the flow on the arc.
    906     ///
    907     /// Sets the \c flowMap to the flow on the arcs. This method can
    908     /// be called after the second phase of algorithm.
    909     Value flow(const Arc& arc) const {
    910       return (*_flow)[arc];
    911     }
    912 
    913962    /// @}
    914963  };
    915964}