| alpar@1270 |      1 | /* -*- mode: C++; indent-tabs-mode: nil; -*-
 | 
| thoneyvazul@1020 |      2 |  *
 | 
| alpar@1270 |      3 |  * This file is a part of LEMON, a generic C++ optimization library.
 | 
| thoneyvazul@1020 |      4 |  *
 | 
| alpar@1270 |      5 |  * Copyright (C) 2003-2013
 | 
| thoneyvazul@1020 |      6 |  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 | 
| thoneyvazul@1020 |      7 |  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 | 
| thoneyvazul@1020 |      8 |  *
 | 
| thoneyvazul@1020 |      9 |  * Permission to use, modify and distribute this software is granted
 | 
| thoneyvazul@1020 |     10 |  * provided that this copyright notice appears in all copies. For
 | 
| thoneyvazul@1020 |     11 |  * precise terms see the accompanying LICENSE file.
 | 
| thoneyvazul@1020 |     12 |  *
 | 
| thoneyvazul@1020 |     13 |  * This software is provided "AS IS" with no warranty of any kind,
 | 
| thoneyvazul@1020 |     14 |  * express or implied, and with no claim as to its suitability for any
 | 
| thoneyvazul@1020 |     15 |  * purpose.
 | 
| thoneyvazul@1020 |     16 |  *
 | 
| thoneyvazul@1020 |     17 |  */
 | 
| thoneyvazul@1020 |     18 | 
 | 
| thoneyvazul@1020 |     19 | #ifndef LEMON_MAX_CARDINALITY_SEARCH_H
 | 
| thoneyvazul@1020 |     20 | #define LEMON_MAX_CARDINALITY_SEARCH_H
 | 
| thoneyvazul@1020 |     21 | 
 | 
| thoneyvazul@1020 |     22 | 
 | 
| thoneyvazul@1020 |     23 | /// \ingroup search
 | 
| alpar@1270 |     24 | /// \file
 | 
| thoneyvazul@1020 |     25 | /// \brief Maximum cardinality search in undirected digraphs.
 | 
| thoneyvazul@1020 |     26 | 
 | 
| thoneyvazul@1020 |     27 | #include <lemon/bin_heap.h>
 | 
| thoneyvazul@1020 |     28 | #include <lemon/bucket_heap.h>
 | 
| thoneyvazul@1020 |     29 | 
 | 
| thoneyvazul@1020 |     30 | #include <lemon/error.h>
 | 
| thoneyvazul@1020 |     31 | #include <lemon/maps.h>
 | 
| thoneyvazul@1020 |     32 | 
 | 
| thoneyvazul@1020 |     33 | #include <functional>
 | 
| thoneyvazul@1020 |     34 | 
 | 
| thoneyvazul@1020 |     35 | namespace lemon {
 | 
| thoneyvazul@1020 |     36 | 
 | 
| thoneyvazul@1020 |     37 |   /// \brief Default traits class of MaxCardinalitySearch class.
 | 
| thoneyvazul@1020 |     38 |   ///
 | 
| thoneyvazul@1020 |     39 |   /// Default traits class of MaxCardinalitySearch class.
 | 
| thoneyvazul@1020 |     40 |   /// \param Digraph Digraph type.
 | 
| thoneyvazul@1020 |     41 |   /// \param CapacityMap Type of capacity map.
 | 
| thoneyvazul@1020 |     42 |   template <typename GR, typename CAP>
 | 
| thoneyvazul@1020 |     43 |   struct MaxCardinalitySearchDefaultTraits {
 | 
| alpar@1270 |     44 |     /// The digraph type the algorithm runs on.
 | 
| thoneyvazul@1020 |     45 |     typedef GR Digraph;
 | 
| thoneyvazul@1020 |     46 | 
 | 
| thoneyvazul@1020 |     47 |     template <typename CM>
 | 
| thoneyvazul@1020 |     48 |     struct CapMapSelector {
 | 
| thoneyvazul@1020 |     49 | 
 | 
| thoneyvazul@1020 |     50 |       typedef CM CapacityMap;
 | 
| thoneyvazul@1020 |     51 | 
 | 
| thoneyvazul@1020 |     52 |       static CapacityMap *createCapacityMap(const Digraph& g) {
 | 
| alpar@1270 |     53 |         return new CapacityMap(g);
 | 
| thoneyvazul@1020 |     54 |       }
 | 
| thoneyvazul@1020 |     55 |     };
 | 
| thoneyvazul@1020 |     56 | 
 | 
| thoneyvazul@1020 |     57 |     template <typename CM>
 | 
| thoneyvazul@1020 |     58 |     struct CapMapSelector<ConstMap<CM, Const<int, 1> > > {
 | 
| thoneyvazul@1020 |     59 | 
 | 
| thoneyvazul@1020 |     60 |       typedef ConstMap<CM, Const<int, 1> > CapacityMap;
 | 
| thoneyvazul@1020 |     61 | 
 | 
| thoneyvazul@1020 |     62 |       static CapacityMap *createCapacityMap(const Digraph&) {
 | 
| alpar@1270 |     63 |         return new CapacityMap;
 | 
| thoneyvazul@1020 |     64 |       }
 | 
| thoneyvazul@1020 |     65 |     };
 | 
| thoneyvazul@1020 |     66 | 
 | 
| thoneyvazul@1020 |     67 |     /// \brief The type of the map that stores the arc capacities.
 | 
| thoneyvazul@1020 |     68 |     ///
 | 
| thoneyvazul@1020 |     69 |     /// The type of the map that stores the arc capacities.
 | 
| thoneyvazul@1020 |     70 |     /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
 | 
| thoneyvazul@1020 |     71 |     typedef typename CapMapSelector<CAP>::CapacityMap CapacityMap;
 | 
| thoneyvazul@1020 |     72 | 
 | 
| thoneyvazul@1020 |     73 |     /// \brief The type of the capacity of the arcs.
 | 
| thoneyvazul@1020 |     74 |     typedef typename CapacityMap::Value Value;
 | 
| thoneyvazul@1020 |     75 | 
 | 
| thoneyvazul@1020 |     76 |     /// \brief Instantiates a CapacityMap.
 | 
| thoneyvazul@1020 |     77 |     ///
 | 
| thoneyvazul@1020 |     78 |     /// This function instantiates a \ref CapacityMap.
 | 
| thoneyvazul@1020 |     79 |     /// \param digraph is the digraph, to which we would like to define
 | 
| thoneyvazul@1020 |     80 |     /// the CapacityMap.
 | 
| thoneyvazul@1020 |     81 |     static CapacityMap *createCapacityMap(const Digraph& digraph) {
 | 
| thoneyvazul@1020 |     82 |       return CapMapSelector<CapacityMap>::createCapacityMap(digraph);
 | 
| thoneyvazul@1020 |     83 |     }
 | 
| thoneyvazul@1020 |     84 | 
 | 
| thoneyvazul@1020 |     85 |     /// \brief The cross reference type used by heap.
 | 
| thoneyvazul@1020 |     86 |     ///
 | 
| thoneyvazul@1020 |     87 |     /// The cross reference type used by heap.
 | 
| thoneyvazul@1020 |     88 |     /// Usually it is \c Digraph::NodeMap<int>.
 | 
| thoneyvazul@1020 |     89 |     typedef typename Digraph::template NodeMap<int> HeapCrossRef;
 | 
| thoneyvazul@1020 |     90 | 
 | 
| thoneyvazul@1020 |     91 |     /// \brief Instantiates a HeapCrossRef.
 | 
| thoneyvazul@1020 |     92 |     ///
 | 
| alpar@1270 |     93 |     /// This function instantiates a \ref HeapCrossRef.
 | 
| alpar@1270 |     94 |     /// \param digraph is the digraph, to which we would like to define the
 | 
| thoneyvazul@1020 |     95 |     /// HeapCrossRef.
 | 
| thoneyvazul@1020 |     96 |     static HeapCrossRef *createHeapCrossRef(const Digraph &digraph) {
 | 
| thoneyvazul@1020 |     97 |       return new HeapCrossRef(digraph);
 | 
| thoneyvazul@1020 |     98 |     }
 | 
| alpar@1270 |     99 | 
 | 
| thoneyvazul@1020 |    100 |     template <typename CapacityMap>
 | 
| thoneyvazul@1020 |    101 |     struct HeapSelector {
 | 
| thoneyvazul@1020 |    102 |       template <typename Value, typename Ref>
 | 
| alpar@1270 |    103 |       struct Selector {
 | 
| thoneyvazul@1020 |    104 |         typedef BinHeap<Value, Ref, std::greater<Value> > Heap;
 | 
| thoneyvazul@1020 |    105 |       };
 | 
| thoneyvazul@1020 |    106 |     };
 | 
| thoneyvazul@1020 |    107 | 
 | 
| thoneyvazul@1020 |    108 |     template <typename CapacityKey>
 | 
| thoneyvazul@1020 |    109 |     struct HeapSelector<ConstMap<CapacityKey, Const<int, 1> > > {
 | 
| thoneyvazul@1020 |    110 |       template <typename Value, typename Ref>
 | 
| thoneyvazul@1020 |    111 |       struct Selector {
 | 
| thoneyvazul@1020 |    112 |         typedef BucketHeap<Ref, false > Heap;
 | 
| thoneyvazul@1020 |    113 |       };
 | 
| thoneyvazul@1020 |    114 |     };
 | 
| thoneyvazul@1020 |    115 | 
 | 
| thoneyvazul@1020 |    116 |     /// \brief The heap type used by MaxCardinalitySearch algorithm.
 | 
| thoneyvazul@1020 |    117 |     ///
 | 
| thoneyvazul@1020 |    118 |     /// The heap type used by MaxCardinalitySearch algorithm. It should
 | 
| thoneyvazul@1020 |    119 |     /// maximalize the priorities. The default heap type is
 | 
| thoneyvazul@1020 |    120 |     /// the \ref BinHeap, but it is specialized when the
 | 
| thoneyvazul@1020 |    121 |     /// CapacityMap is ConstMap<Digraph::Node, Const<int, 1> >
 | 
| thoneyvazul@1020 |    122 |     /// to BucketHeap.
 | 
| thoneyvazul@1020 |    123 |     ///
 | 
| thoneyvazul@1020 |    124 |     /// \sa MaxCardinalitySearch
 | 
| thoneyvazul@1020 |    125 |     typedef typename HeapSelector<CapacityMap>
 | 
| thoneyvazul@1020 |    126 |     ::template Selector<Value, HeapCrossRef>
 | 
| thoneyvazul@1020 |    127 |     ::Heap Heap;
 | 
| thoneyvazul@1020 |    128 | 
 | 
| thoneyvazul@1020 |    129 |     /// \brief Instantiates a Heap.
 | 
| thoneyvazul@1020 |    130 |     ///
 | 
| alpar@1270 |    131 |     /// This function instantiates a \ref Heap.
 | 
| thoneyvazul@1020 |    132 |     /// \param crossref The cross reference of the heap.
 | 
| thoneyvazul@1020 |    133 |     static Heap *createHeap(HeapCrossRef& crossref) {
 | 
| thoneyvazul@1020 |    134 |       return new Heap(crossref);
 | 
| thoneyvazul@1020 |    135 |     }
 | 
| thoneyvazul@1020 |    136 | 
 | 
| thoneyvazul@1020 |    137 |     /// \brief The type of the map that stores whether a node is processed.
 | 
| thoneyvazul@1020 |    138 |     ///
 | 
| thoneyvazul@1020 |    139 |     /// The type of the map that stores whether a node is processed.
 | 
| thoneyvazul@1020 |    140 |     /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
 | 
| thoneyvazul@1020 |    141 |     /// By default it is a NullMap.
 | 
| thoneyvazul@1020 |    142 |     typedef NullMap<typename Digraph::Node, bool> ProcessedMap;
 | 
| thoneyvazul@1020 |    143 | 
 | 
| thoneyvazul@1020 |    144 |     /// \brief Instantiates a ProcessedMap.
 | 
| thoneyvazul@1020 |    145 |     ///
 | 
| alpar@1270 |    146 |     /// This function instantiates a \ref ProcessedMap.
 | 
| thoneyvazul@1020 |    147 |     /// \param digraph is the digraph, to which
 | 
| thoneyvazul@1020 |    148 |     /// we would like to define the \ref ProcessedMap
 | 
| thoneyvazul@1020 |    149 | #ifdef DOXYGEN
 | 
| thoneyvazul@1020 |    150 |     static ProcessedMap *createProcessedMap(const Digraph &digraph)
 | 
| thoneyvazul@1020 |    151 | #else
 | 
| thoneyvazul@1020 |    152 |     static ProcessedMap *createProcessedMap(const Digraph &)
 | 
| thoneyvazul@1020 |    153 | #endif
 | 
| thoneyvazul@1020 |    154 |     {
 | 
| thoneyvazul@1020 |    155 |       return new ProcessedMap();
 | 
| thoneyvazul@1020 |    156 |     }
 | 
| thoneyvazul@1020 |    157 | 
 | 
| thoneyvazul@1020 |    158 |     /// \brief The type of the map that stores the cardinalities of the nodes.
 | 
| alpar@1270 |    159 |     ///
 | 
| thoneyvazul@1020 |    160 |     /// The type of the map that stores the cardinalities of the nodes.
 | 
| thoneyvazul@1020 |    161 |     /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
 | 
| thoneyvazul@1020 |    162 |     typedef typename Digraph::template NodeMap<Value> CardinalityMap;
 | 
| thoneyvazul@1020 |    163 | 
 | 
| thoneyvazul@1020 |    164 |     /// \brief Instantiates a CardinalityMap.
 | 
| thoneyvazul@1020 |    165 |     ///
 | 
| alpar@1270 |    166 |     /// This function instantiates a \ref CardinalityMap.
 | 
| alpar@1271 |    167 |     /// \param digraph is the digraph, to which we would like to
 | 
| alpar@1271 |    168 |     /// define the \ref CardinalityMap
 | 
| thoneyvazul@1020 |    169 |     static CardinalityMap *createCardinalityMap(const Digraph &digraph) {
 | 
| thoneyvazul@1020 |    170 |       return new CardinalityMap(digraph);
 | 
| thoneyvazul@1020 |    171 |     }
 | 
| thoneyvazul@1020 |    172 | 
 | 
| thoneyvazul@1020 |    173 | 
 | 
| thoneyvazul@1020 |    174 |   };
 | 
| alpar@1270 |    175 | 
 | 
| thoneyvazul@1020 |    176 |   /// \ingroup search
 | 
| thoneyvazul@1020 |    177 |   ///
 | 
| thoneyvazul@1020 |    178 |   /// \brief Maximum Cardinality Search algorithm class.
 | 
| thoneyvazul@1020 |    179 |   ///
 | 
| alpar@1270 |    180 |   /// This class provides an efficient implementation of Maximum Cardinality
 | 
| alpar@1270 |    181 |   /// Search algorithm. The maximum cardinality search first chooses any
 | 
| thoneyvazul@1020 |    182 |   /// node of the digraph. Then every time it chooses one unprocessed node
 | 
| alpar@1271 |    183 |   /// with maximum cardinality, i.e the sum of capacities on out arcs
 | 
| alpar@1271 |    184 |   /// to the nodes
 | 
| thoneyvazul@1020 |    185 |   /// which were previusly processed.
 | 
| thoneyvazul@1020 |    186 |   /// If there is a cut in the digraph the algorithm should choose
 | 
| thoneyvazul@1020 |    187 |   /// again any unprocessed node of the digraph.
 | 
| thoneyvazul@1020 |    188 | 
 | 
| thoneyvazul@1020 |    189 |   /// The arc capacities are passed to the algorithm using a
 | 
| alpar@1270 |    190 |   /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any
 | 
| thoneyvazul@1020 |    191 |   /// kind of capacity.
 | 
| thoneyvazul@1020 |    192 |   ///
 | 
| alpar@1270 |    193 |   /// The type of the capacity is determined by the \ref
 | 
| thoneyvazul@1020 |    194 |   /// concepts::ReadMap::Value "Value" of the capacity map.
 | 
| thoneyvazul@1020 |    195 |   ///
 | 
| thoneyvazul@1020 |    196 |   /// It is also possible to change the underlying priority heap.
 | 
| thoneyvazul@1020 |    197 |   ///
 | 
| thoneyvazul@1020 |    198 |   ///
 | 
| thoneyvazul@1020 |    199 |   /// \param GR The digraph type the algorithm runs on. The value of
 | 
| alpar@1270 |    200 |   /// Digraph is not used directly by the search algorithm, it
 | 
| thoneyvazul@1020 |    201 |   /// is only passed to \ref MaxCardinalitySearchDefaultTraits.
 | 
| alpar@1270 |    202 |   /// \param CAP This read-only ArcMap determines the capacities of
 | 
| thoneyvazul@1020 |    203 |   /// the arcs. It is read once for each arc, so the map may involve in
 | 
| thoneyvazul@1020 |    204 |   /// relatively time consuming process to compute the arc capacity if
 | 
| thoneyvazul@1020 |    205 |   /// it is necessary. The default map type is \ref
 | 
| thoneyvazul@1020 |    206 |   /// ConstMap "ConstMap<concepts::Digraph::Arc, Const<int,1> >". The value
 | 
| alpar@1270 |    207 |   /// of CapacityMap is not used directly by search algorithm, it is only
 | 
| alpar@1270 |    208 |   /// passed to \ref MaxCardinalitySearchDefaultTraits.
 | 
| alpar@1270 |    209 |   /// \param TR Traits class to set various data types used by the
 | 
| alpar@1270 |    210 |   /// algorithm.  The default traits class is
 | 
| alpar@1270 |    211 |   /// \ref MaxCardinalitySearchDefaultTraits
 | 
| alpar@1270 |    212 |   /// "MaxCardinalitySearchDefaultTraits<GR, CAP>".
 | 
| alpar@1270 |    213 |   /// See \ref MaxCardinalitySearchDefaultTraits
 | 
| thoneyvazul@1020 |    214 |   /// for the documentation of a MaxCardinalitySearch traits class.
 | 
| thoneyvazul@1020 |    215 | 
 | 
| thoneyvazul@1020 |    216 | #ifdef DOXYGEN
 | 
| thoneyvazul@1020 |    217 |   template <typename GR, typename CAP, typename TR>
 | 
| thoneyvazul@1020 |    218 | #else
 | 
| alpar@1270 |    219 |   template <typename GR, typename CAP =
 | 
| alpar@1270 |    220 |             ConstMap<typename GR::Arc, Const<int,1> >,
 | 
| alpar@1270 |    221 |             typename TR =
 | 
| thoneyvazul@1020 |    222 |             MaxCardinalitySearchDefaultTraits<GR, CAP> >
 | 
| thoneyvazul@1020 |    223 | #endif
 | 
| thoneyvazul@1020 |    224 |   class MaxCardinalitySearch {
 | 
| thoneyvazul@1020 |    225 |   public:
 | 
| thoneyvazul@1020 |    226 | 
 | 
| thoneyvazul@1020 |    227 |     typedef TR Traits;
 | 
| thoneyvazul@1020 |    228 |     ///The type of the underlying digraph.
 | 
| thoneyvazul@1020 |    229 |     typedef typename Traits::Digraph Digraph;
 | 
| alpar@1270 |    230 | 
 | 
| thoneyvazul@1020 |    231 |     ///The type of the capacity of the arcs.
 | 
| thoneyvazul@1020 |    232 |     typedef typename Traits::CapacityMap::Value Value;
 | 
| thoneyvazul@1020 |    233 |     ///The type of the map that stores the arc capacities.
 | 
| thoneyvazul@1020 |    234 |     typedef typename Traits::CapacityMap CapacityMap;
 | 
| thoneyvazul@1020 |    235 |     ///The type of the map indicating if a node is processed.
 | 
| thoneyvazul@1020 |    236 |     typedef typename Traits::ProcessedMap ProcessedMap;
 | 
| thoneyvazul@1020 |    237 |     ///The type of the map that stores the cardinalities of the nodes.
 | 
| thoneyvazul@1020 |    238 |     typedef typename Traits::CardinalityMap CardinalityMap;
 | 
| thoneyvazul@1020 |    239 |     ///The cross reference type used for the current heap.
 | 
| thoneyvazul@1020 |    240 |     typedef typename Traits::HeapCrossRef HeapCrossRef;
 | 
| thoneyvazul@1020 |    241 |     ///The heap type used by the algorithm. It maximizes the priorities.
 | 
| thoneyvazul@1020 |    242 |     typedef typename Traits::Heap Heap;
 | 
| thoneyvazul@1020 |    243 |   private:
 | 
| thoneyvazul@1020 |    244 |     // Pointer to the underlying digraph.
 | 
| thoneyvazul@1020 |    245 |     const Digraph *_graph;
 | 
| thoneyvazul@1020 |    246 |     // Pointer to the capacity map
 | 
| thoneyvazul@1020 |    247 |     const CapacityMap *_capacity;
 | 
| thoneyvazul@1020 |    248 |     // Indicates if \ref _capacity is locally allocated (\c true) or not.
 | 
| thoneyvazul@1020 |    249 |     bool local_capacity;
 | 
| thoneyvazul@1020 |    250 |     // Pointer to the map of cardinality.
 | 
| thoneyvazul@1020 |    251 |     CardinalityMap *_cardinality;
 | 
| thoneyvazul@1020 |    252 |     // Indicates if \ref _cardinality is locally allocated (\c true) or not.
 | 
| thoneyvazul@1020 |    253 |     bool local_cardinality;
 | 
| thoneyvazul@1020 |    254 |     // Pointer to the map of processed status of the nodes.
 | 
| thoneyvazul@1020 |    255 |     ProcessedMap *_processed;
 | 
| thoneyvazul@1020 |    256 |     // Indicates if \ref _processed is locally allocated (\c true) or not.
 | 
| thoneyvazul@1020 |    257 |     bool local_processed;
 | 
| thoneyvazul@1020 |    258 |     // Pointer to the heap cross references.
 | 
| thoneyvazul@1020 |    259 |     HeapCrossRef *_heap_cross_ref;
 | 
| thoneyvazul@1020 |    260 |     // Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
 | 
| thoneyvazul@1020 |    261 |     bool local_heap_cross_ref;
 | 
| thoneyvazul@1020 |    262 |     // Pointer to the heap.
 | 
| thoneyvazul@1020 |    263 |     Heap *_heap;
 | 
| thoneyvazul@1020 |    264 |     // Indicates if \ref _heap is locally allocated (\c true) or not.
 | 
| thoneyvazul@1020 |    265 |     bool local_heap;
 | 
| thoneyvazul@1020 |    266 | 
 | 
| thoneyvazul@1020 |    267 |   public :
 | 
| thoneyvazul@1020 |    268 | 
 | 
| thoneyvazul@1020 |    269 |     typedef MaxCardinalitySearch Create;
 | 
| alpar@1270 |    270 | 
 | 
| thoneyvazul@1020 |    271 |     ///\name Named template parameters
 | 
| thoneyvazul@1020 |    272 | 
 | 
| thoneyvazul@1020 |    273 |     ///@{
 | 
| thoneyvazul@1020 |    274 | 
 | 
| thoneyvazul@1020 |    275 |     template <class T>
 | 
| thoneyvazul@1020 |    276 |     struct DefCapacityMapTraits : public Traits {
 | 
| thoneyvazul@1020 |    277 |       typedef T CapacityMap;
 | 
| thoneyvazul@1020 |    278 |       static CapacityMap *createCapacityMap(const Digraph &) {
 | 
| alpar@1270 |    279 |                LEMON_ASSERT(false,"Uninitialized parameter.");
 | 
| alpar@1270 |    280 |         return 0;
 | 
| thoneyvazul@1020 |    281 |       }
 | 
| thoneyvazul@1020 |    282 |     };
 | 
| alpar@1270 |    283 |     /// \brief \ref named-templ-param "Named parameter" for setting
 | 
| thoneyvazul@1020 |    284 |     /// CapacityMap type
 | 
| thoneyvazul@1020 |    285 |     ///
 | 
| thoneyvazul@1020 |    286 |     /// \ref named-templ-param "Named parameter" for setting CapacityMap type
 | 
| thoneyvazul@1020 |    287 |     /// for the algorithm.
 | 
| thoneyvazul@1020 |    288 |     template <class T>
 | 
| alpar@1270 |    289 |     struct SetCapacityMap
 | 
| alpar@1270 |    290 |       : public MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| alpar@1270 |    291 |                                     DefCapacityMapTraits<T> > {
 | 
| alpar@1270 |    292 |       typedef MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| thoneyvazul@1020 |    293 |                                    DefCapacityMapTraits<T> > Create;
 | 
| thoneyvazul@1020 |    294 |     };
 | 
| thoneyvazul@1020 |    295 | 
 | 
| thoneyvazul@1020 |    296 |     template <class T>
 | 
| thoneyvazul@1020 |    297 |     struct DefCardinalityMapTraits : public Traits {
 | 
| thoneyvazul@1020 |    298 |       typedef T CardinalityMap;
 | 
| alpar@1270 |    299 |       static CardinalityMap *createCardinalityMap(const Digraph &)
 | 
| thoneyvazul@1020 |    300 |       {
 | 
| alpar@1270 |    301 |         LEMON_ASSERT(false,"Uninitialized parameter.");
 | 
| alpar@1270 |    302 |         return 0;
 | 
| thoneyvazul@1020 |    303 |       }
 | 
| thoneyvazul@1020 |    304 |     };
 | 
| alpar@1270 |    305 |     /// \brief \ref named-templ-param "Named parameter" for setting
 | 
| thoneyvazul@1020 |    306 |     /// CardinalityMap type
 | 
| thoneyvazul@1020 |    307 |     ///
 | 
| alpar@1270 |    308 |     /// \ref named-templ-param "Named parameter" for setting CardinalityMap
 | 
| thoneyvazul@1020 |    309 |     /// type for the algorithm.
 | 
| thoneyvazul@1020 |    310 |     template <class T>
 | 
| alpar@1270 |    311 |     struct SetCardinalityMap
 | 
| alpar@1270 |    312 |       : public MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| alpar@1270 |    313 |                                     DefCardinalityMapTraits<T> > {
 | 
| alpar@1270 |    314 |       typedef MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| thoneyvazul@1020 |    315 |                                    DefCardinalityMapTraits<T> > Create;
 | 
| thoneyvazul@1020 |    316 |     };
 | 
| alpar@1270 |    317 | 
 | 
| thoneyvazul@1020 |    318 |     template <class T>
 | 
| thoneyvazul@1020 |    319 |     struct DefProcessedMapTraits : public Traits {
 | 
| thoneyvazul@1020 |    320 |       typedef T ProcessedMap;
 | 
| thoneyvazul@1020 |    321 |       static ProcessedMap *createProcessedMap(const Digraph &) {
 | 
| alpar@1270 |    322 |                LEMON_ASSERT(false,"Uninitialized parameter.");
 | 
| alpar@1270 |    323 |         return 0;
 | 
| thoneyvazul@1020 |    324 |       }
 | 
| thoneyvazul@1020 |    325 |     };
 | 
| alpar@1270 |    326 |     /// \brief \ref named-templ-param "Named parameter" for setting
 | 
| thoneyvazul@1020 |    327 |     /// ProcessedMap type
 | 
| thoneyvazul@1020 |    328 |     ///
 | 
| thoneyvazul@1020 |    329 |     /// \ref named-templ-param "Named parameter" for setting ProcessedMap type
 | 
| thoneyvazul@1020 |    330 |     /// for the algorithm.
 | 
| thoneyvazul@1020 |    331 |     template <class T>
 | 
| alpar@1270 |    332 |     struct SetProcessedMap
 | 
| alpar@1270 |    333 |       : public MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| alpar@1270 |    334 |                                     DefProcessedMapTraits<T> > {
 | 
| alpar@1270 |    335 |       typedef MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| thoneyvazul@1020 |    336 |                                    DefProcessedMapTraits<T> > Create;
 | 
| thoneyvazul@1020 |    337 |     };
 | 
| alpar@1270 |    338 | 
 | 
| thoneyvazul@1020 |    339 |     template <class H, class CR>
 | 
| thoneyvazul@1020 |    340 |     struct DefHeapTraits : public Traits {
 | 
| thoneyvazul@1020 |    341 |       typedef CR HeapCrossRef;
 | 
| thoneyvazul@1020 |    342 |       typedef H Heap;
 | 
| thoneyvazul@1020 |    343 |       static HeapCrossRef *createHeapCrossRef(const Digraph &) {
 | 
| alpar@1270 |    344 |              LEMON_ASSERT(false,"Uninitialized parameter.");
 | 
| alpar@1270 |    345 |         return 0;
 | 
| thoneyvazul@1020 |    346 |       }
 | 
| thoneyvazul@1020 |    347 |       static Heap *createHeap(HeapCrossRef &) {
 | 
| alpar@1270 |    348 |                LEMON_ASSERT(false,"Uninitialized parameter.");
 | 
| alpar@1270 |    349 |         return 0;
 | 
| thoneyvazul@1020 |    350 |       }
 | 
| thoneyvazul@1020 |    351 |     };
 | 
| alpar@1270 |    352 |     /// \brief \ref named-templ-param "Named parameter" for setting heap
 | 
| thoneyvazul@1020 |    353 |     /// and cross reference type
 | 
| thoneyvazul@1020 |    354 |     ///
 | 
| alpar@1270 |    355 |     /// \ref named-templ-param "Named parameter" for setting heap and cross
 | 
| thoneyvazul@1020 |    356 |     /// reference type for the algorithm.
 | 
| thoneyvazul@1020 |    357 |     template <class H, class CR = typename Digraph::template NodeMap<int> >
 | 
| thoneyvazul@1020 |    358 |     struct SetHeap
 | 
| alpar@1270 |    359 |       : public MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| alpar@1270 |    360 |                                     DefHeapTraits<H, CR> > {
 | 
| alpar@1270 |    361 |       typedef MaxCardinalitySearch< Digraph, CapacityMap,
 | 
| thoneyvazul@1020 |    362 |                                     DefHeapTraits<H, CR> > Create;
 | 
| thoneyvazul@1020 |    363 |     };
 | 
| thoneyvazul@1020 |    364 | 
 | 
| thoneyvazul@1020 |    365 |     template <class H, class CR>
 | 
| thoneyvazul@1020 |    366 |     struct DefStandardHeapTraits : public Traits {
 | 
| thoneyvazul@1020 |    367 |       typedef CR HeapCrossRef;
 | 
| thoneyvazul@1020 |    368 |       typedef H Heap;
 | 
| thoneyvazul@1020 |    369 |       static HeapCrossRef *createHeapCrossRef(const Digraph &digraph) {
 | 
| alpar@1270 |    370 |         return new HeapCrossRef(digraph);
 | 
| thoneyvazul@1020 |    371 |       }
 | 
| thoneyvazul@1020 |    372 |       static Heap *createHeap(HeapCrossRef &crossref) {
 | 
| alpar@1270 |    373 |         return new Heap(crossref);
 | 
| thoneyvazul@1020 |    374 |       }
 | 
| thoneyvazul@1020 |    375 |     };
 | 
| thoneyvazul@1020 |    376 | 
 | 
| alpar@1270 |    377 |     /// \brief \ref named-templ-param "Named parameter" for setting heap and
 | 
| thoneyvazul@1020 |    378 |     /// cross reference type with automatic allocation
 | 
| thoneyvazul@1020 |    379 |     ///
 | 
| alpar@1270 |    380 |     /// \ref named-templ-param "Named parameter" for setting heap and cross
 | 
| alpar@1270 |    381 |     /// reference type. It can allocate the heap and the cross reference
 | 
| alpar@1270 |    382 |     /// object if the cross reference's constructor waits for the digraph as
 | 
| thoneyvazul@1020 |    383 |     /// parameter and the heap's constructor waits for the cross reference.
 | 
| thoneyvazul@1020 |    384 |     template <class H, class CR = typename Digraph::template NodeMap<int> >
 | 
| thoneyvazul@1020 |    385 |     struct SetStandardHeap
 | 
| alpar@1270 |    386 |       : public MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| alpar@1270 |    387 |                                     DefStandardHeapTraits<H, CR> > {
 | 
| alpar@1270 |    388 |       typedef MaxCardinalitySearch<Digraph, CapacityMap,
 | 
| alpar@1270 |    389 |                                    DefStandardHeapTraits<H, CR> >
 | 
| thoneyvazul@1020 |    390 |       Create;
 | 
| thoneyvazul@1020 |    391 |     };
 | 
| alpar@1270 |    392 | 
 | 
| thoneyvazul@1020 |    393 |     ///@}
 | 
| thoneyvazul@1020 |    394 | 
 | 
| thoneyvazul@1020 |    395 | 
 | 
| thoneyvazul@1020 |    396 |   protected:
 | 
| thoneyvazul@1020 |    397 | 
 | 
| thoneyvazul@1020 |    398 |     MaxCardinalitySearch() {}
 | 
| thoneyvazul@1020 |    399 | 
 | 
| alpar@1270 |    400 |   public:
 | 
| alpar@1270 |    401 | 
 | 
| thoneyvazul@1020 |    402 |     /// \brief Constructor.
 | 
| thoneyvazul@1020 |    403 |     ///
 | 
| thoneyvazul@1020 |    404 |     ///\param digraph the digraph the algorithm will run on.
 | 
| thoneyvazul@1020 |    405 |     ///\param capacity the capacity map used by the algorithm.
 | 
| thoneyvazul@1020 |    406 |     MaxCardinalitySearch(const Digraph& digraph,
 | 
| alpar@1270 |    407 |                          const CapacityMap& capacity) :
 | 
| thoneyvazul@1020 |    408 |       _graph(&digraph),
 | 
| thoneyvazul@1020 |    409 |       _capacity(&capacity), local_capacity(false),
 | 
| thoneyvazul@1020 |    410 |       _cardinality(0), local_cardinality(false),
 | 
| thoneyvazul@1020 |    411 |       _processed(0), local_processed(false),
 | 
| thoneyvazul@1020 |    412 |       _heap_cross_ref(0), local_heap_cross_ref(false),
 | 
| thoneyvazul@1020 |    413 |       _heap(0), local_heap(false)
 | 
| thoneyvazul@1020 |    414 |     { }
 | 
| thoneyvazul@1020 |    415 | 
 | 
| alpar@1129 |    416 |     /// \brief Constructor.
 | 
| alpar@1129 |    417 |     ///
 | 
| alpar@1129 |    418 |     ///\param digraph the digraph the algorithm will run on.
 | 
| alpar@1129 |    419 |     ///
 | 
| alpar@1129 |    420 |     ///A constant 1 capacity map will be allocated.
 | 
| alpar@1129 |    421 |     MaxCardinalitySearch(const Digraph& digraph) :
 | 
| alpar@1129 |    422 |       _graph(&digraph),
 | 
| alpar@1129 |    423 |       _capacity(0), local_capacity(false),
 | 
| alpar@1129 |    424 |       _cardinality(0), local_cardinality(false),
 | 
| alpar@1129 |    425 |       _processed(0), local_processed(false),
 | 
| alpar@1129 |    426 |       _heap_cross_ref(0), local_heap_cross_ref(false),
 | 
| alpar@1129 |    427 |       _heap(0), local_heap(false)
 | 
| alpar@1129 |    428 |     { }
 | 
| alpar@1129 |    429 | 
 | 
| thoneyvazul@1020 |    430 |     /// \brief Destructor.
 | 
| thoneyvazul@1020 |    431 |     ~MaxCardinalitySearch() {
 | 
| thoneyvazul@1020 |    432 |       if(local_capacity) delete _capacity;
 | 
| thoneyvazul@1020 |    433 |       if(local_cardinality) delete _cardinality;
 | 
| thoneyvazul@1020 |    434 |       if(local_processed) delete _processed;
 | 
| thoneyvazul@1020 |    435 |       if(local_heap_cross_ref) delete _heap_cross_ref;
 | 
| thoneyvazul@1020 |    436 |       if(local_heap) delete _heap;
 | 
| thoneyvazul@1020 |    437 |     }
 | 
| thoneyvazul@1020 |    438 | 
 | 
| thoneyvazul@1020 |    439 |     /// \brief Sets the capacity map.
 | 
| thoneyvazul@1020 |    440 |     ///
 | 
| thoneyvazul@1020 |    441 |     /// Sets the capacity map.
 | 
| thoneyvazul@1020 |    442 |     /// \return <tt> (*this) </tt>
 | 
| thoneyvazul@1020 |    443 |     MaxCardinalitySearch &capacityMap(const CapacityMap &m) {
 | 
| thoneyvazul@1020 |    444 |       if (local_capacity) {
 | 
| alpar@1270 |    445 |         delete _capacity;
 | 
| alpar@1270 |    446 |         local_capacity=false;
 | 
| thoneyvazul@1020 |    447 |       }
 | 
| thoneyvazul@1020 |    448 |       _capacity=&m;
 | 
| thoneyvazul@1020 |    449 |       return *this;
 | 
| thoneyvazul@1020 |    450 |     }
 | 
| thoneyvazul@1020 |    451 | 
 | 
| thoneyvazul@1020 |    452 |     /// \brief Returns a const reference to the capacity map.
 | 
| thoneyvazul@1020 |    453 |     ///
 | 
| thoneyvazul@1020 |    454 |     /// Returns a const reference to the capacity map used by
 | 
| thoneyvazul@1020 |    455 |     /// the algorithm.
 | 
| thoneyvazul@1020 |    456 |     const CapacityMap &capacityMap() const {
 | 
| thoneyvazul@1020 |    457 |       return *_capacity;
 | 
| thoneyvazul@1020 |    458 |     }
 | 
| thoneyvazul@1020 |    459 | 
 | 
| alpar@1270 |    460 |     /// \brief Sets the map storing the cardinalities calculated by the
 | 
| thoneyvazul@1020 |    461 |     /// algorithm.
 | 
| thoneyvazul@1020 |    462 |     ///
 | 
| thoneyvazul@1020 |    463 |     /// Sets the map storing the cardinalities calculated by the algorithm.
 | 
| thoneyvazul@1020 |    464 |     /// If you don't use this function before calling \ref run(),
 | 
| thoneyvazul@1020 |    465 |     /// it will allocate one. The destuctor deallocates this
 | 
| thoneyvazul@1020 |    466 |     /// automatically allocated map, of course.
 | 
| thoneyvazul@1020 |    467 |     /// \return <tt> (*this) </tt>
 | 
| thoneyvazul@1020 |    468 |     MaxCardinalitySearch &cardinalityMap(CardinalityMap &m) {
 | 
| thoneyvazul@1020 |    469 |       if(local_cardinality) {
 | 
| alpar@1270 |    470 |         delete _cardinality;
 | 
| alpar@1270 |    471 |         local_cardinality=false;
 | 
| thoneyvazul@1020 |    472 |       }
 | 
| thoneyvazul@1020 |    473 |       _cardinality = &m;
 | 
| thoneyvazul@1020 |    474 |       return *this;
 | 
| thoneyvazul@1020 |    475 |     }
 | 
| thoneyvazul@1020 |    476 | 
 | 
| thoneyvazul@1020 |    477 |     /// \brief Sets the map storing the processed nodes.
 | 
| thoneyvazul@1020 |    478 |     ///
 | 
| thoneyvazul@1020 |    479 |     /// Sets the map storing the processed nodes.
 | 
| thoneyvazul@1020 |    480 |     /// If you don't use this function before calling \ref run(),
 | 
| thoneyvazul@1020 |    481 |     /// it will allocate one. The destuctor deallocates this
 | 
| thoneyvazul@1020 |    482 |     /// automatically allocated map, of course.
 | 
| thoneyvazul@1020 |    483 |     /// \return <tt> (*this) </tt>
 | 
| alpar@1270 |    484 |     MaxCardinalitySearch &processedMap(ProcessedMap &m)
 | 
| thoneyvazul@1020 |    485 |     {
 | 
| thoneyvazul@1020 |    486 |       if(local_processed) {
 | 
| alpar@1270 |    487 |         delete _processed;
 | 
| alpar@1270 |    488 |         local_processed=false;
 | 
| thoneyvazul@1020 |    489 |       }
 | 
| thoneyvazul@1020 |    490 |       _processed = &m;
 | 
| thoneyvazul@1020 |    491 |       return *this;
 | 
| thoneyvazul@1020 |    492 |     }
 | 
| thoneyvazul@1020 |    493 | 
 | 
| thoneyvazul@1020 |    494 |     /// \brief Returns a const reference to the cardinality map.
 | 
| thoneyvazul@1020 |    495 |     ///
 | 
| thoneyvazul@1020 |    496 |     /// Returns a const reference to the cardinality map used by
 | 
| thoneyvazul@1020 |    497 |     /// the algorithm.
 | 
| thoneyvazul@1020 |    498 |     const ProcessedMap &processedMap() const {
 | 
| thoneyvazul@1020 |    499 |       return *_processed;
 | 
| thoneyvazul@1020 |    500 |     }
 | 
| thoneyvazul@1020 |    501 | 
 | 
| thoneyvazul@1020 |    502 |     /// \brief Sets the heap and the cross reference used by algorithm.
 | 
| thoneyvazul@1020 |    503 |     ///
 | 
| thoneyvazul@1020 |    504 |     /// Sets the heap and the cross reference used by algorithm.
 | 
| thoneyvazul@1020 |    505 |     /// If you don't use this function before calling \ref run(),
 | 
| thoneyvazul@1020 |    506 |     /// it will allocate one. The destuctor deallocates this
 | 
| thoneyvazul@1020 |    507 |     /// automatically allocated map, of course.
 | 
| thoneyvazul@1020 |    508 |     /// \return <tt> (*this) </tt>
 | 
| thoneyvazul@1020 |    509 |     MaxCardinalitySearch &heap(Heap& hp, HeapCrossRef &cr) {
 | 
| thoneyvazul@1020 |    510 |       if(local_heap_cross_ref) {
 | 
| alpar@1270 |    511 |         delete _heap_cross_ref;
 | 
| alpar@1270 |    512 |         local_heap_cross_ref = false;
 | 
| thoneyvazul@1020 |    513 |       }
 | 
| thoneyvazul@1020 |    514 |       _heap_cross_ref = &cr;
 | 
| thoneyvazul@1020 |    515 |       if(local_heap) {
 | 
| alpar@1270 |    516 |         delete _heap;
 | 
| alpar@1270 |    517 |         local_heap = false;
 | 
| thoneyvazul@1020 |    518 |       }
 | 
| thoneyvazul@1020 |    519 |       _heap = &hp;
 | 
| thoneyvazul@1020 |    520 |       return *this;
 | 
| thoneyvazul@1020 |    521 |     }
 | 
| thoneyvazul@1020 |    522 | 
 | 
| thoneyvazul@1020 |    523 |     /// \brief Returns a const reference to the heap.
 | 
| thoneyvazul@1020 |    524 |     ///
 | 
| thoneyvazul@1020 |    525 |     /// Returns a const reference to the heap used by
 | 
| thoneyvazul@1020 |    526 |     /// the algorithm.
 | 
| thoneyvazul@1020 |    527 |     const Heap &heap() const {
 | 
| thoneyvazul@1020 |    528 |       return *_heap;
 | 
| thoneyvazul@1020 |    529 |     }
 | 
| thoneyvazul@1020 |    530 | 
 | 
| thoneyvazul@1020 |    531 |     /// \brief Returns a const reference to the cross reference.
 | 
| thoneyvazul@1020 |    532 |     ///
 | 
| thoneyvazul@1020 |    533 |     /// Returns a const reference to the cross reference
 | 
| thoneyvazul@1020 |    534 |     /// of the heap.
 | 
| thoneyvazul@1020 |    535 |     const HeapCrossRef &heapCrossRef() const {
 | 
| thoneyvazul@1020 |    536 |       return *_heap_cross_ref;
 | 
| thoneyvazul@1020 |    537 |     }
 | 
| thoneyvazul@1020 |    538 | 
 | 
| thoneyvazul@1020 |    539 |   private:
 | 
| thoneyvazul@1020 |    540 | 
 | 
| thoneyvazul@1020 |    541 |     typedef typename Digraph::Node Node;
 | 
| thoneyvazul@1020 |    542 |     typedef typename Digraph::NodeIt NodeIt;
 | 
| thoneyvazul@1020 |    543 |     typedef typename Digraph::Arc Arc;
 | 
| thoneyvazul@1020 |    544 |     typedef typename Digraph::InArcIt InArcIt;
 | 
| thoneyvazul@1020 |    545 | 
 | 
| thoneyvazul@1020 |    546 |     void create_maps() {
 | 
| thoneyvazul@1020 |    547 |       if(!_capacity) {
 | 
| alpar@1270 |    548 |         local_capacity = true;
 | 
| alpar@1270 |    549 |         _capacity = Traits::createCapacityMap(*_graph);
 | 
| thoneyvazul@1020 |    550 |       }
 | 
| thoneyvazul@1020 |    551 |       if(!_cardinality) {
 | 
| alpar@1270 |    552 |         local_cardinality = true;
 | 
| alpar@1270 |    553 |         _cardinality = Traits::createCardinalityMap(*_graph);
 | 
| thoneyvazul@1020 |    554 |       }
 | 
| thoneyvazul@1020 |    555 |       if(!_processed) {
 | 
| alpar@1270 |    556 |         local_processed = true;
 | 
| alpar@1270 |    557 |         _processed = Traits::createProcessedMap(*_graph);
 | 
| thoneyvazul@1020 |    558 |       }
 | 
| thoneyvazul@1020 |    559 |       if (!_heap_cross_ref) {
 | 
| alpar@1270 |    560 |         local_heap_cross_ref = true;
 | 
| alpar@1270 |    561 |         _heap_cross_ref = Traits::createHeapCrossRef(*_graph);
 | 
| thoneyvazul@1020 |    562 |       }
 | 
| thoneyvazul@1020 |    563 |       if (!_heap) {
 | 
| alpar@1270 |    564 |         local_heap = true;
 | 
| alpar@1270 |    565 |         _heap = Traits::createHeap(*_heap_cross_ref);
 | 
| thoneyvazul@1020 |    566 |       }
 | 
| thoneyvazul@1020 |    567 |     }
 | 
| alpar@1270 |    568 | 
 | 
| thoneyvazul@1020 |    569 |     void finalizeNodeData(Node node, Value capacity) {
 | 
| thoneyvazul@1020 |    570 |       _processed->set(node, true);
 | 
| thoneyvazul@1020 |    571 |       _cardinality->set(node, capacity);
 | 
| thoneyvazul@1020 |    572 |     }
 | 
| thoneyvazul@1020 |    573 | 
 | 
| thoneyvazul@1020 |    574 |   public:
 | 
| thoneyvazul@1020 |    575 |     /// \name Execution control
 | 
| thoneyvazul@1020 |    576 |     /// The simplest way to execute the algorithm is to use
 | 
| thoneyvazul@1020 |    577 |     /// one of the member functions called \ref run().
 | 
| thoneyvazul@1020 |    578 |     /// \n
 | 
| thoneyvazul@1020 |    579 |     /// If you need more control on the execution,
 | 
| thoneyvazul@1020 |    580 |     /// first you must call \ref init(), then you can add several source nodes
 | 
| thoneyvazul@1020 |    581 |     /// with \ref addSource().
 | 
| thoneyvazul@1020 |    582 |     /// Finally \ref start() will perform the computation.
 | 
| thoneyvazul@1020 |    583 | 
 | 
| thoneyvazul@1020 |    584 |     ///@{
 | 
| thoneyvazul@1020 |    585 | 
 | 
| thoneyvazul@1020 |    586 |     /// \brief Initializes the internal data structures.
 | 
| thoneyvazul@1020 |    587 |     ///
 | 
| thoneyvazul@1020 |    588 |     /// Initializes the internal data structures, and clears the heap.
 | 
| thoneyvazul@1020 |    589 |     void init() {
 | 
| thoneyvazul@1020 |    590 |       create_maps();
 | 
| thoneyvazul@1020 |    591 |       _heap->clear();
 | 
| thoneyvazul@1020 |    592 |       for (NodeIt it(*_graph) ; it != INVALID ; ++it) {
 | 
| alpar@1270 |    593 |         _processed->set(it, false);
 | 
| alpar@1270 |    594 |         _heap_cross_ref->set(it, Heap::PRE_HEAP);
 | 
| thoneyvazul@1020 |    595 |       }
 | 
| thoneyvazul@1020 |    596 |     }
 | 
| alpar@1270 |    597 | 
 | 
| thoneyvazul@1020 |    598 |     /// \brief Adds a new source node.
 | 
| alpar@1270 |    599 |     ///
 | 
| thoneyvazul@1020 |    600 |     /// Adds a new source node to the priority heap.
 | 
| thoneyvazul@1020 |    601 |     ///
 | 
| thoneyvazul@1020 |    602 |     /// It checks if the node has not yet been added to the heap.
 | 
| thoneyvazul@1020 |    603 |     void addSource(Node source, Value capacity = 0) {
 | 
| thoneyvazul@1020 |    604 |       if(_heap->state(source) == Heap::PRE_HEAP) {
 | 
| alpar@1270 |    605 |         _heap->push(source, capacity);
 | 
| alpar@1270 |    606 |       }
 | 
| thoneyvazul@1020 |    607 |     }
 | 
| alpar@1270 |    608 | 
 | 
| thoneyvazul@1020 |    609 |     /// \brief Processes the next node in the priority heap
 | 
| thoneyvazul@1020 |    610 |     ///
 | 
| thoneyvazul@1020 |    611 |     /// Processes the next node in the priority heap.
 | 
| thoneyvazul@1020 |    612 |     ///
 | 
| thoneyvazul@1020 |    613 |     /// \return The processed node.
 | 
| thoneyvazul@1020 |    614 |     ///
 | 
| thoneyvazul@1020 |    615 |     /// \warning The priority heap must not be empty!
 | 
| thoneyvazul@1020 |    616 |     Node processNextNode() {
 | 
| alpar@1270 |    617 |       Node node = _heap->top();
 | 
| thoneyvazul@1020 |    618 |       finalizeNodeData(node, _heap->prio());
 | 
| thoneyvazul@1020 |    619 |       _heap->pop();
 | 
| alpar@1270 |    620 | 
 | 
| thoneyvazul@1020 |    621 |       for (InArcIt it(*_graph, node); it != INVALID; ++it) {
 | 
| alpar@1270 |    622 |         Node source = _graph->source(it);
 | 
| alpar@1270 |    623 |         switch (_heap->state(source)) {
 | 
| alpar@1270 |    624 |         case Heap::PRE_HEAP:
 | 
| alpar@1270 |    625 |           _heap->push(source, (*_capacity)[it]);
 | 
| alpar@1270 |    626 |           break;
 | 
| alpar@1270 |    627 |         case Heap::IN_HEAP:
 | 
| alpar@1270 |    628 |           _heap->decrease(source, (*_heap)[source] + (*_capacity)[it]);
 | 
| alpar@1270 |    629 |           break;
 | 
| alpar@1270 |    630 |         case Heap::POST_HEAP:
 | 
| alpar@1270 |    631 |           break;
 | 
| alpar@1270 |    632 |         }
 | 
| thoneyvazul@1020 |    633 |       }
 | 
| thoneyvazul@1020 |    634 |       return node;
 | 
| thoneyvazul@1020 |    635 |     }
 | 
| thoneyvazul@1020 |    636 | 
 | 
| thoneyvazul@1020 |    637 |     /// \brief Next node to be processed.
 | 
| thoneyvazul@1020 |    638 |     ///
 | 
| thoneyvazul@1020 |    639 |     /// Next node to be processed.
 | 
| thoneyvazul@1020 |    640 |     ///
 | 
| alpar@1270 |    641 |     /// \return The next node to be processed or INVALID if the
 | 
| thoneyvazul@1020 |    642 |     /// priority heap is empty.
 | 
| alpar@1270 |    643 |     Node nextNode() {
 | 
| thoneyvazul@1020 |    644 |       return !_heap->empty() ? _heap->top() : INVALID;
 | 
| thoneyvazul@1020 |    645 |     }
 | 
| alpar@1270 |    646 | 
 | 
| thoneyvazul@1020 |    647 |     /// \brief Returns \c false if there are nodes
 | 
| thoneyvazul@1020 |    648 |     /// to be processed in the priority heap
 | 
| thoneyvazul@1020 |    649 |     ///
 | 
| thoneyvazul@1020 |    650 |     /// Returns \c false if there are nodes
 | 
| thoneyvazul@1020 |    651 |     /// to be processed in the priority heap
 | 
| thoneyvazul@1020 |    652 |     bool emptyQueue() { return _heap->empty(); }
 | 
| alpar@1270 |    653 |     /// \brief Returns the number of the nodes to be processed
 | 
| thoneyvazul@1020 |    654 |     /// in the priority heap
 | 
| thoneyvazul@1020 |    655 |     ///
 | 
| thoneyvazul@1020 |    656 |     /// Returns the number of the nodes to be processed in the priority heap
 | 
| thoneyvazul@1020 |    657 |     int emptySize() { return _heap->size(); }
 | 
| alpar@1270 |    658 | 
 | 
| thoneyvazul@1020 |    659 |     /// \brief Executes the algorithm.
 | 
| thoneyvazul@1020 |    660 |     ///
 | 
| thoneyvazul@1020 |    661 |     /// Executes the algorithm.
 | 
| thoneyvazul@1020 |    662 |     ///
 | 
| thoneyvazul@1020 |    663 |     ///\pre init() must be called and at least one node should be added
 | 
| thoneyvazul@1020 |    664 |     /// with addSource() before using this function.
 | 
| thoneyvazul@1020 |    665 |     ///
 | 
| alpar@1270 |    666 |     /// This method runs the Maximum Cardinality Search algorithm from the
 | 
| thoneyvazul@1020 |    667 |     /// source node(s).
 | 
| thoneyvazul@1020 |    668 |     void start() {
 | 
| thoneyvazul@1020 |    669 |       while ( !_heap->empty() ) processNextNode();
 | 
| thoneyvazul@1020 |    670 |     }
 | 
| alpar@1270 |    671 | 
 | 
| thoneyvazul@1020 |    672 |     /// \brief Executes the algorithm until \c dest is reached.
 | 
| thoneyvazul@1020 |    673 |     ///
 | 
| thoneyvazul@1020 |    674 |     /// Executes the algorithm until \c dest is reached.
 | 
| thoneyvazul@1020 |    675 |     ///
 | 
| thoneyvazul@1020 |    676 |     /// \pre init() must be called and at least one node should be added
 | 
| thoneyvazul@1020 |    677 |     /// with addSource() before using this function.
 | 
| thoneyvazul@1020 |    678 |     ///
 | 
| alpar@1270 |    679 |     /// This method runs the %MaxCardinalitySearch algorithm from the source
 | 
| thoneyvazul@1020 |    680 |     /// nodes.
 | 
| thoneyvazul@1020 |    681 |     void start(Node dest) {
 | 
| thoneyvazul@1020 |    682 |       while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
 | 
| thoneyvazul@1020 |    683 |       if ( !_heap->empty() ) finalizeNodeData(_heap->top(), _heap->prio());
 | 
| thoneyvazul@1020 |    684 |     }
 | 
| alpar@1270 |    685 | 
 | 
| thoneyvazul@1020 |    686 |     /// \brief Executes the algorithm until a condition is met.
 | 
| thoneyvazul@1020 |    687 |     ///
 | 
| thoneyvazul@1020 |    688 |     /// Executes the algorithm until a condition is met.
 | 
| thoneyvazul@1020 |    689 |     ///
 | 
| thoneyvazul@1020 |    690 |     /// \pre init() must be called and at least one node should be added
 | 
| thoneyvazul@1020 |    691 |     /// with addSource() before using this function.
 | 
| thoneyvazul@1020 |    692 |     ///
 | 
| thoneyvazul@1020 |    693 |     /// \param nm must be a bool (or convertible) node map. The algorithm
 | 
| thoneyvazul@1020 |    694 |     /// will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
 | 
| thoneyvazul@1020 |    695 |     template <typename NodeBoolMap>
 | 
| thoneyvazul@1020 |    696 |     void start(const NodeBoolMap &nm) {
 | 
| thoneyvazul@1020 |    697 |       while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
 | 
| thoneyvazul@1020 |    698 |       if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
 | 
| thoneyvazul@1020 |    699 |     }
 | 
| alpar@1270 |    700 | 
 | 
| thoneyvazul@1020 |    701 |     /// \brief Runs the maximum cardinality search algorithm from node \c s.
 | 
| thoneyvazul@1020 |    702 |     ///
 | 
| alpar@1270 |    703 |     /// This method runs the %MaxCardinalitySearch algorithm from a root
 | 
| thoneyvazul@1020 |    704 |     /// node \c s.
 | 
| thoneyvazul@1020 |    705 |     ///
 | 
| thoneyvazul@1020 |    706 |     ///\note d.run(s) is just a shortcut of the following code.
 | 
| thoneyvazul@1020 |    707 |     ///\code
 | 
| thoneyvazul@1020 |    708 |     ///  d.init();
 | 
| thoneyvazul@1020 |    709 |     ///  d.addSource(s);
 | 
| thoneyvazul@1020 |    710 |     ///  d.start();
 | 
| thoneyvazul@1020 |    711 |     ///\endcode
 | 
| thoneyvazul@1020 |    712 |     void run(Node s) {
 | 
| thoneyvazul@1020 |    713 |       init();
 | 
| thoneyvazul@1020 |    714 |       addSource(s);
 | 
| thoneyvazul@1020 |    715 |       start();
 | 
| thoneyvazul@1020 |    716 |     }
 | 
| thoneyvazul@1020 |    717 | 
 | 
| alpar@1270 |    718 |     /// \brief Runs the maximum cardinality search algorithm for the
 | 
| thoneyvazul@1020 |    719 |     /// whole digraph.
 | 
| thoneyvazul@1020 |    720 |     ///
 | 
| alpar@1270 |    721 |     /// This method runs the %MaxCardinalitySearch algorithm from all
 | 
| thoneyvazul@1020 |    722 |     /// unprocessed node of the digraph.
 | 
| thoneyvazul@1020 |    723 |     ///
 | 
| thoneyvazul@1020 |    724 |     ///\note d.run(s) is just a shortcut of the following code.
 | 
| thoneyvazul@1020 |    725 |     ///\code
 | 
| thoneyvazul@1020 |    726 |     ///  d.init();
 | 
| thoneyvazul@1020 |    727 |     ///  for (NodeIt it(digraph); it != INVALID; ++it) {
 | 
| thoneyvazul@1020 |    728 |     ///    if (!d.reached(it)) {
 | 
| thoneyvazul@1020 |    729 |     ///      d.addSource(s);
 | 
| thoneyvazul@1020 |    730 |     ///      d.start();
 | 
| thoneyvazul@1020 |    731 |     ///    }
 | 
| thoneyvazul@1020 |    732 |     ///  }
 | 
| thoneyvazul@1020 |    733 |     ///\endcode
 | 
| thoneyvazul@1020 |    734 |     void run() {
 | 
| thoneyvazul@1020 |    735 |       init();
 | 
| thoneyvazul@1020 |    736 |       for (NodeIt it(*_graph); it != INVALID; ++it) {
 | 
| thoneyvazul@1020 |    737 |         if (!reached(it)) {
 | 
| thoneyvazul@1020 |    738 |           addSource(it);
 | 
| thoneyvazul@1020 |    739 |           start();
 | 
| thoneyvazul@1020 |    740 |         }
 | 
| thoneyvazul@1020 |    741 |       }
 | 
| thoneyvazul@1020 |    742 |     }
 | 
| alpar@1270 |    743 | 
 | 
| thoneyvazul@1020 |    744 |     ///@}
 | 
| thoneyvazul@1020 |    745 | 
 | 
| thoneyvazul@1020 |    746 |     /// \name Query Functions
 | 
| alpar@1270 |    747 |     /// The results of the maximum cardinality search algorithm can be
 | 
| thoneyvazul@1020 |    748 |     /// obtained using these functions.
 | 
| thoneyvazul@1020 |    749 |     /// \n
 | 
| alpar@1270 |    750 |     /// Before the use of these functions, either run() or start() must be
 | 
| thoneyvazul@1020 |    751 |     /// called.
 | 
| alpar@1270 |    752 | 
 | 
| thoneyvazul@1020 |    753 |     ///@{
 | 
| thoneyvazul@1020 |    754 | 
 | 
| thoneyvazul@1020 |    755 |     /// \brief The cardinality of a node.
 | 
| thoneyvazul@1020 |    756 |     ///
 | 
| thoneyvazul@1020 |    757 |     /// Returns the cardinality of a node.
 | 
| thoneyvazul@1020 |    758 |     /// \pre \ref run() must be called before using this function.
 | 
| thoneyvazul@1020 |    759 |     /// \warning If node \c v in unreachable from the root the return value
 | 
| thoneyvazul@1020 |    760 |     /// of this funcion is undefined.
 | 
| thoneyvazul@1020 |    761 |     Value cardinality(Node node) const { return (*_cardinality)[node]; }
 | 
| thoneyvazul@1020 |    762 | 
 | 
| thoneyvazul@1020 |    763 |     /// \brief The current cardinality of a node.
 | 
| thoneyvazul@1020 |    764 |     ///
 | 
| thoneyvazul@1020 |    765 |     /// Returns the current cardinality of a node.
 | 
| thoneyvazul@1020 |    766 |     /// \pre the given node should be reached but not processed
 | 
| thoneyvazul@1020 |    767 |     Value currentCardinality(Node node) const { return (*_heap)[node]; }
 | 
| thoneyvazul@1020 |    768 | 
 | 
| thoneyvazul@1020 |    769 |     /// \brief Returns a reference to the NodeMap of cardinalities.
 | 
| thoneyvazul@1020 |    770 |     ///
 | 
| alpar@1270 |    771 |     /// Returns a reference to the NodeMap of cardinalities. \pre \ref run()
 | 
| thoneyvazul@1020 |    772 |     /// must be called before using this function.
 | 
| thoneyvazul@1020 |    773 |     const CardinalityMap &cardinalityMap() const { return *_cardinality;}
 | 
| alpar@1270 |    774 | 
 | 
| thoneyvazul@1020 |    775 |     /// \brief Checks if a node is reachable from the root.
 | 
| thoneyvazul@1020 |    776 |     ///
 | 
| thoneyvazul@1020 |    777 |     /// Returns \c true if \c v is reachable from the root.
 | 
| thoneyvazul@1020 |    778 |     /// \warning The source nodes are initated as unreached.
 | 
| thoneyvazul@1020 |    779 |     /// \pre \ref run() must be called before using this function.
 | 
| thoneyvazul@1020 |    780 |     bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
 | 
| thoneyvazul@1020 |    781 | 
 | 
| thoneyvazul@1020 |    782 |     /// \brief Checks if a node is processed.
 | 
| thoneyvazul@1020 |    783 |     ///
 | 
| thoneyvazul@1020 |    784 |     /// Returns \c true if \c v is processed, i.e. the shortest
 | 
| thoneyvazul@1020 |    785 |     /// path to \c v has already found.
 | 
| thoneyvazul@1020 |    786 |     /// \pre \ref run() must be called before using this function.
 | 
| thoneyvazul@1020 |    787 |     bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
 | 
| alpar@1270 |    788 | 
 | 
| thoneyvazul@1020 |    789 |     ///@}
 | 
| thoneyvazul@1020 |    790 |   };
 | 
| thoneyvazul@1020 |    791 | 
 | 
| thoneyvazul@1020 |    792 | }
 | 
| thoneyvazul@1020 |    793 | 
 | 
| thoneyvazul@1020 |    794 | #endif
 |