alpar@1270: /* -*- mode: C++; indent-tabs-mode: nil; -*- thoneyvazul@1020: * alpar@1270: * This file is a part of LEMON, a generic C++ optimization library. thoneyvazul@1020: * alpar@1270: * Copyright (C) 2003-2013 thoneyvazul@1020: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport thoneyvazul@1020: * (Egervary Research Group on Combinatorial Optimization, EGRES). thoneyvazul@1020: * thoneyvazul@1020: * Permission to use, modify and distribute this software is granted thoneyvazul@1020: * provided that this copyright notice appears in all copies. For thoneyvazul@1020: * precise terms see the accompanying LICENSE file. thoneyvazul@1020: * thoneyvazul@1020: * This software is provided "AS IS" with no warranty of any kind, thoneyvazul@1020: * express or implied, and with no claim as to its suitability for any thoneyvazul@1020: * purpose. thoneyvazul@1020: * thoneyvazul@1020: */ thoneyvazul@1020: thoneyvazul@1020: #ifndef LEMON_MAX_CARDINALITY_SEARCH_H thoneyvazul@1020: #define LEMON_MAX_CARDINALITY_SEARCH_H thoneyvazul@1020: thoneyvazul@1020: thoneyvazul@1020: /// \ingroup search alpar@1270: /// \file thoneyvazul@1020: /// \brief Maximum cardinality search in undirected digraphs. thoneyvazul@1020: thoneyvazul@1020: #include thoneyvazul@1020: #include thoneyvazul@1020: thoneyvazul@1020: #include thoneyvazul@1020: #include thoneyvazul@1020: thoneyvazul@1020: #include thoneyvazul@1020: thoneyvazul@1020: namespace lemon { thoneyvazul@1020: thoneyvazul@1020: /// \brief Default traits class of MaxCardinalitySearch class. thoneyvazul@1020: /// thoneyvazul@1020: /// Default traits class of MaxCardinalitySearch class. thoneyvazul@1020: /// \param Digraph Digraph type. thoneyvazul@1020: /// \param CapacityMap Type of capacity map. thoneyvazul@1020: template thoneyvazul@1020: struct MaxCardinalitySearchDefaultTraits { alpar@1270: /// The digraph type the algorithm runs on. thoneyvazul@1020: typedef GR Digraph; thoneyvazul@1020: thoneyvazul@1020: template thoneyvazul@1020: struct CapMapSelector { thoneyvazul@1020: thoneyvazul@1020: typedef CM CapacityMap; thoneyvazul@1020: thoneyvazul@1020: static CapacityMap *createCapacityMap(const Digraph& g) { alpar@1270: return new CapacityMap(g); thoneyvazul@1020: } thoneyvazul@1020: }; thoneyvazul@1020: thoneyvazul@1020: template thoneyvazul@1020: struct CapMapSelector > > { thoneyvazul@1020: thoneyvazul@1020: typedef ConstMap > CapacityMap; thoneyvazul@1020: thoneyvazul@1020: static CapacityMap *createCapacityMap(const Digraph&) { alpar@1270: return new CapacityMap; thoneyvazul@1020: } thoneyvazul@1020: }; thoneyvazul@1020: thoneyvazul@1020: /// \brief The type of the map that stores the arc capacities. thoneyvazul@1020: /// thoneyvazul@1020: /// The type of the map that stores the arc capacities. thoneyvazul@1020: /// It must meet the \ref concepts::ReadMap "ReadMap" concept. thoneyvazul@1020: typedef typename CapMapSelector::CapacityMap CapacityMap; thoneyvazul@1020: thoneyvazul@1020: /// \brief The type of the capacity of the arcs. thoneyvazul@1020: typedef typename CapacityMap::Value Value; thoneyvazul@1020: thoneyvazul@1020: /// \brief Instantiates a CapacityMap. thoneyvazul@1020: /// thoneyvazul@1020: /// This function instantiates a \ref CapacityMap. thoneyvazul@1020: /// \param digraph is the digraph, to which we would like to define thoneyvazul@1020: /// the CapacityMap. thoneyvazul@1020: static CapacityMap *createCapacityMap(const Digraph& digraph) { thoneyvazul@1020: return CapMapSelector::createCapacityMap(digraph); thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief The cross reference type used by heap. thoneyvazul@1020: /// thoneyvazul@1020: /// The cross reference type used by heap. thoneyvazul@1020: /// Usually it is \c Digraph::NodeMap. thoneyvazul@1020: typedef typename Digraph::template NodeMap HeapCrossRef; thoneyvazul@1020: thoneyvazul@1020: /// \brief Instantiates a HeapCrossRef. thoneyvazul@1020: /// alpar@1270: /// This function instantiates a \ref HeapCrossRef. alpar@1270: /// \param digraph is the digraph, to which we would like to define the thoneyvazul@1020: /// HeapCrossRef. thoneyvazul@1020: static HeapCrossRef *createHeapCrossRef(const Digraph &digraph) { thoneyvazul@1020: return new HeapCrossRef(digraph); thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: template thoneyvazul@1020: struct HeapSelector { thoneyvazul@1020: template alpar@1270: struct Selector { thoneyvazul@1020: typedef BinHeap > Heap; thoneyvazul@1020: }; thoneyvazul@1020: }; thoneyvazul@1020: thoneyvazul@1020: template thoneyvazul@1020: struct HeapSelector > > { thoneyvazul@1020: template thoneyvazul@1020: struct Selector { thoneyvazul@1020: typedef BucketHeap Heap; thoneyvazul@1020: }; thoneyvazul@1020: }; thoneyvazul@1020: thoneyvazul@1020: /// \brief The heap type used by MaxCardinalitySearch algorithm. thoneyvazul@1020: /// thoneyvazul@1020: /// The heap type used by MaxCardinalitySearch algorithm. It should thoneyvazul@1020: /// maximalize the priorities. The default heap type is thoneyvazul@1020: /// the \ref BinHeap, but it is specialized when the thoneyvazul@1020: /// CapacityMap is ConstMap > thoneyvazul@1020: /// to BucketHeap. thoneyvazul@1020: /// thoneyvazul@1020: /// \sa MaxCardinalitySearch thoneyvazul@1020: typedef typename HeapSelector thoneyvazul@1020: ::template Selector thoneyvazul@1020: ::Heap Heap; thoneyvazul@1020: thoneyvazul@1020: /// \brief Instantiates a Heap. thoneyvazul@1020: /// alpar@1270: /// This function instantiates a \ref Heap. thoneyvazul@1020: /// \param crossref The cross reference of the heap. thoneyvazul@1020: static Heap *createHeap(HeapCrossRef& crossref) { thoneyvazul@1020: return new Heap(crossref); thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief The type of the map that stores whether a node is processed. thoneyvazul@1020: /// thoneyvazul@1020: /// The type of the map that stores whether a node is processed. thoneyvazul@1020: /// It must meet the \ref concepts::WriteMap "WriteMap" concept. thoneyvazul@1020: /// By default it is a NullMap. thoneyvazul@1020: typedef NullMap ProcessedMap; thoneyvazul@1020: thoneyvazul@1020: /// \brief Instantiates a ProcessedMap. thoneyvazul@1020: /// alpar@1270: /// This function instantiates a \ref ProcessedMap. thoneyvazul@1020: /// \param digraph is the digraph, to which thoneyvazul@1020: /// we would like to define the \ref ProcessedMap thoneyvazul@1020: #ifdef DOXYGEN thoneyvazul@1020: static ProcessedMap *createProcessedMap(const Digraph &digraph) thoneyvazul@1020: #else thoneyvazul@1020: static ProcessedMap *createProcessedMap(const Digraph &) thoneyvazul@1020: #endif thoneyvazul@1020: { thoneyvazul@1020: return new ProcessedMap(); thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief The type of the map that stores the cardinalities of the nodes. alpar@1270: /// thoneyvazul@1020: /// The type of the map that stores the cardinalities of the nodes. thoneyvazul@1020: /// It must meet the \ref concepts::WriteMap "WriteMap" concept. thoneyvazul@1020: typedef typename Digraph::template NodeMap CardinalityMap; thoneyvazul@1020: thoneyvazul@1020: /// \brief Instantiates a CardinalityMap. thoneyvazul@1020: /// alpar@1270: /// This function instantiates a \ref CardinalityMap. alpar@1271: /// \param digraph is the digraph, to which we would like to alpar@1271: /// define the \ref CardinalityMap thoneyvazul@1020: static CardinalityMap *createCardinalityMap(const Digraph &digraph) { thoneyvazul@1020: return new CardinalityMap(digraph); thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: thoneyvazul@1020: }; alpar@1270: thoneyvazul@1020: /// \ingroup search thoneyvazul@1020: /// thoneyvazul@1020: /// \brief Maximum Cardinality Search algorithm class. thoneyvazul@1020: /// alpar@1270: /// This class provides an efficient implementation of Maximum Cardinality alpar@1270: /// Search algorithm. The maximum cardinality search first chooses any thoneyvazul@1020: /// node of the digraph. Then every time it chooses one unprocessed node alpar@1271: /// with maximum cardinality, i.e the sum of capacities on out arcs alpar@1271: /// to the nodes thoneyvazul@1020: /// which were previusly processed. thoneyvazul@1020: /// If there is a cut in the digraph the algorithm should choose thoneyvazul@1020: /// again any unprocessed node of the digraph. thoneyvazul@1020: thoneyvazul@1020: /// The arc capacities are passed to the algorithm using a alpar@1270: /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any thoneyvazul@1020: /// kind of capacity. thoneyvazul@1020: /// alpar@1270: /// The type of the capacity is determined by the \ref thoneyvazul@1020: /// concepts::ReadMap::Value "Value" of the capacity map. thoneyvazul@1020: /// thoneyvazul@1020: /// It is also possible to change the underlying priority heap. thoneyvazul@1020: /// thoneyvazul@1020: /// thoneyvazul@1020: /// \param GR The digraph type the algorithm runs on. The value of alpar@1270: /// Digraph is not used directly by the search algorithm, it thoneyvazul@1020: /// is only passed to \ref MaxCardinalitySearchDefaultTraits. alpar@1270: /// \param CAP This read-only ArcMap determines the capacities of thoneyvazul@1020: /// the arcs. It is read once for each arc, so the map may involve in thoneyvazul@1020: /// relatively time consuming process to compute the arc capacity if thoneyvazul@1020: /// it is necessary. The default map type is \ref thoneyvazul@1020: /// ConstMap "ConstMap >". The value alpar@1270: /// of CapacityMap is not used directly by search algorithm, it is only alpar@1270: /// passed to \ref MaxCardinalitySearchDefaultTraits. alpar@1270: /// \param TR Traits class to set various data types used by the alpar@1270: /// algorithm. The default traits class is alpar@1270: /// \ref MaxCardinalitySearchDefaultTraits alpar@1270: /// "MaxCardinalitySearchDefaultTraits". alpar@1270: /// See \ref MaxCardinalitySearchDefaultTraits thoneyvazul@1020: /// for the documentation of a MaxCardinalitySearch traits class. thoneyvazul@1020: thoneyvazul@1020: #ifdef DOXYGEN thoneyvazul@1020: template thoneyvazul@1020: #else alpar@1270: template >, alpar@1270: typename TR = thoneyvazul@1020: MaxCardinalitySearchDefaultTraits > thoneyvazul@1020: #endif thoneyvazul@1020: class MaxCardinalitySearch { thoneyvazul@1020: public: thoneyvazul@1020: thoneyvazul@1020: typedef TR Traits; thoneyvazul@1020: ///The type of the underlying digraph. thoneyvazul@1020: typedef typename Traits::Digraph Digraph; alpar@1270: thoneyvazul@1020: ///The type of the capacity of the arcs. thoneyvazul@1020: typedef typename Traits::CapacityMap::Value Value; thoneyvazul@1020: ///The type of the map that stores the arc capacities. thoneyvazul@1020: typedef typename Traits::CapacityMap CapacityMap; thoneyvazul@1020: ///The type of the map indicating if a node is processed. thoneyvazul@1020: typedef typename Traits::ProcessedMap ProcessedMap; thoneyvazul@1020: ///The type of the map that stores the cardinalities of the nodes. thoneyvazul@1020: typedef typename Traits::CardinalityMap CardinalityMap; thoneyvazul@1020: ///The cross reference type used for the current heap. thoneyvazul@1020: typedef typename Traits::HeapCrossRef HeapCrossRef; thoneyvazul@1020: ///The heap type used by the algorithm. It maximizes the priorities. thoneyvazul@1020: typedef typename Traits::Heap Heap; thoneyvazul@1020: private: thoneyvazul@1020: // Pointer to the underlying digraph. thoneyvazul@1020: const Digraph *_graph; thoneyvazul@1020: // Pointer to the capacity map thoneyvazul@1020: const CapacityMap *_capacity; thoneyvazul@1020: // Indicates if \ref _capacity is locally allocated (\c true) or not. thoneyvazul@1020: bool local_capacity; thoneyvazul@1020: // Pointer to the map of cardinality. thoneyvazul@1020: CardinalityMap *_cardinality; thoneyvazul@1020: // Indicates if \ref _cardinality is locally allocated (\c true) or not. thoneyvazul@1020: bool local_cardinality; thoneyvazul@1020: // Pointer to the map of processed status of the nodes. thoneyvazul@1020: ProcessedMap *_processed; thoneyvazul@1020: // Indicates if \ref _processed is locally allocated (\c true) or not. thoneyvazul@1020: bool local_processed; thoneyvazul@1020: // Pointer to the heap cross references. thoneyvazul@1020: HeapCrossRef *_heap_cross_ref; thoneyvazul@1020: // Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not. thoneyvazul@1020: bool local_heap_cross_ref; thoneyvazul@1020: // Pointer to the heap. thoneyvazul@1020: Heap *_heap; thoneyvazul@1020: // Indicates if \ref _heap is locally allocated (\c true) or not. thoneyvazul@1020: bool local_heap; thoneyvazul@1020: thoneyvazul@1020: public : thoneyvazul@1020: thoneyvazul@1020: typedef MaxCardinalitySearch Create; alpar@1270: thoneyvazul@1020: ///\name Named template parameters thoneyvazul@1020: thoneyvazul@1020: ///@{ thoneyvazul@1020: thoneyvazul@1020: template thoneyvazul@1020: struct DefCapacityMapTraits : public Traits { thoneyvazul@1020: typedef T CapacityMap; thoneyvazul@1020: static CapacityMap *createCapacityMap(const Digraph &) { alpar@1270: LEMON_ASSERT(false,"Uninitialized parameter."); alpar@1270: return 0; thoneyvazul@1020: } thoneyvazul@1020: }; alpar@1270: /// \brief \ref named-templ-param "Named parameter" for setting thoneyvazul@1020: /// CapacityMap type thoneyvazul@1020: /// thoneyvazul@1020: /// \ref named-templ-param "Named parameter" for setting CapacityMap type thoneyvazul@1020: /// for the algorithm. thoneyvazul@1020: template alpar@1270: struct SetCapacityMap alpar@1270: : public MaxCardinalitySearch > { alpar@1270: typedef MaxCardinalitySearch > Create; thoneyvazul@1020: }; thoneyvazul@1020: thoneyvazul@1020: template thoneyvazul@1020: struct DefCardinalityMapTraits : public Traits { thoneyvazul@1020: typedef T CardinalityMap; alpar@1270: static CardinalityMap *createCardinalityMap(const Digraph &) thoneyvazul@1020: { alpar@1270: LEMON_ASSERT(false,"Uninitialized parameter."); alpar@1270: return 0; thoneyvazul@1020: } thoneyvazul@1020: }; alpar@1270: /// \brief \ref named-templ-param "Named parameter" for setting thoneyvazul@1020: /// CardinalityMap type thoneyvazul@1020: /// alpar@1270: /// \ref named-templ-param "Named parameter" for setting CardinalityMap thoneyvazul@1020: /// type for the algorithm. thoneyvazul@1020: template alpar@1270: struct SetCardinalityMap alpar@1270: : public MaxCardinalitySearch > { alpar@1270: typedef MaxCardinalitySearch > Create; thoneyvazul@1020: }; alpar@1270: thoneyvazul@1020: template thoneyvazul@1020: struct DefProcessedMapTraits : public Traits { thoneyvazul@1020: typedef T ProcessedMap; thoneyvazul@1020: static ProcessedMap *createProcessedMap(const Digraph &) { alpar@1270: LEMON_ASSERT(false,"Uninitialized parameter."); alpar@1270: return 0; thoneyvazul@1020: } thoneyvazul@1020: }; alpar@1270: /// \brief \ref named-templ-param "Named parameter" for setting thoneyvazul@1020: /// ProcessedMap type thoneyvazul@1020: /// thoneyvazul@1020: /// \ref named-templ-param "Named parameter" for setting ProcessedMap type thoneyvazul@1020: /// for the algorithm. thoneyvazul@1020: template alpar@1270: struct SetProcessedMap alpar@1270: : public MaxCardinalitySearch > { alpar@1270: typedef MaxCardinalitySearch > Create; thoneyvazul@1020: }; alpar@1270: thoneyvazul@1020: template thoneyvazul@1020: struct DefHeapTraits : public Traits { thoneyvazul@1020: typedef CR HeapCrossRef; thoneyvazul@1020: typedef H Heap; thoneyvazul@1020: static HeapCrossRef *createHeapCrossRef(const Digraph &) { alpar@1270: LEMON_ASSERT(false,"Uninitialized parameter."); alpar@1270: return 0; thoneyvazul@1020: } thoneyvazul@1020: static Heap *createHeap(HeapCrossRef &) { alpar@1270: LEMON_ASSERT(false,"Uninitialized parameter."); alpar@1270: return 0; thoneyvazul@1020: } thoneyvazul@1020: }; alpar@1270: /// \brief \ref named-templ-param "Named parameter" for setting heap thoneyvazul@1020: /// and cross reference type thoneyvazul@1020: /// alpar@1270: /// \ref named-templ-param "Named parameter" for setting heap and cross thoneyvazul@1020: /// reference type for the algorithm. thoneyvazul@1020: template > thoneyvazul@1020: struct SetHeap alpar@1270: : public MaxCardinalitySearch > { alpar@1270: typedef MaxCardinalitySearch< Digraph, CapacityMap, thoneyvazul@1020: DefHeapTraits > Create; thoneyvazul@1020: }; thoneyvazul@1020: thoneyvazul@1020: template thoneyvazul@1020: struct DefStandardHeapTraits : public Traits { thoneyvazul@1020: typedef CR HeapCrossRef; thoneyvazul@1020: typedef H Heap; thoneyvazul@1020: static HeapCrossRef *createHeapCrossRef(const Digraph &digraph) { alpar@1270: return new HeapCrossRef(digraph); thoneyvazul@1020: } thoneyvazul@1020: static Heap *createHeap(HeapCrossRef &crossref) { alpar@1270: return new Heap(crossref); thoneyvazul@1020: } thoneyvazul@1020: }; thoneyvazul@1020: alpar@1270: /// \brief \ref named-templ-param "Named parameter" for setting heap and thoneyvazul@1020: /// cross reference type with automatic allocation thoneyvazul@1020: /// alpar@1270: /// \ref named-templ-param "Named parameter" for setting heap and cross alpar@1270: /// reference type. It can allocate the heap and the cross reference alpar@1270: /// object if the cross reference's constructor waits for the digraph as thoneyvazul@1020: /// parameter and the heap's constructor waits for the cross reference. thoneyvazul@1020: template > thoneyvazul@1020: struct SetStandardHeap alpar@1270: : public MaxCardinalitySearch > { alpar@1270: typedef MaxCardinalitySearch > thoneyvazul@1020: Create; thoneyvazul@1020: }; alpar@1270: thoneyvazul@1020: ///@} thoneyvazul@1020: thoneyvazul@1020: thoneyvazul@1020: protected: thoneyvazul@1020: thoneyvazul@1020: MaxCardinalitySearch() {} thoneyvazul@1020: alpar@1270: public: alpar@1270: thoneyvazul@1020: /// \brief Constructor. thoneyvazul@1020: /// thoneyvazul@1020: ///\param digraph the digraph the algorithm will run on. thoneyvazul@1020: ///\param capacity the capacity map used by the algorithm. thoneyvazul@1020: MaxCardinalitySearch(const Digraph& digraph, alpar@1270: const CapacityMap& capacity) : thoneyvazul@1020: _graph(&digraph), thoneyvazul@1020: _capacity(&capacity), local_capacity(false), thoneyvazul@1020: _cardinality(0), local_cardinality(false), thoneyvazul@1020: _processed(0), local_processed(false), thoneyvazul@1020: _heap_cross_ref(0), local_heap_cross_ref(false), thoneyvazul@1020: _heap(0), local_heap(false) thoneyvazul@1020: { } thoneyvazul@1020: alpar@1129: /// \brief Constructor. alpar@1129: /// alpar@1129: ///\param digraph the digraph the algorithm will run on. alpar@1129: /// alpar@1129: ///A constant 1 capacity map will be allocated. alpar@1129: MaxCardinalitySearch(const Digraph& digraph) : alpar@1129: _graph(&digraph), alpar@1129: _capacity(0), local_capacity(false), alpar@1129: _cardinality(0), local_cardinality(false), alpar@1129: _processed(0), local_processed(false), alpar@1129: _heap_cross_ref(0), local_heap_cross_ref(false), alpar@1129: _heap(0), local_heap(false) alpar@1129: { } alpar@1129: thoneyvazul@1020: /// \brief Destructor. thoneyvazul@1020: ~MaxCardinalitySearch() { thoneyvazul@1020: if(local_capacity) delete _capacity; thoneyvazul@1020: if(local_cardinality) delete _cardinality; thoneyvazul@1020: if(local_processed) delete _processed; thoneyvazul@1020: if(local_heap_cross_ref) delete _heap_cross_ref; thoneyvazul@1020: if(local_heap) delete _heap; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief Sets the capacity map. thoneyvazul@1020: /// thoneyvazul@1020: /// Sets the capacity map. thoneyvazul@1020: /// \return (*this) thoneyvazul@1020: MaxCardinalitySearch &capacityMap(const CapacityMap &m) { thoneyvazul@1020: if (local_capacity) { alpar@1270: delete _capacity; alpar@1270: local_capacity=false; thoneyvazul@1020: } thoneyvazul@1020: _capacity=&m; thoneyvazul@1020: return *this; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief Returns a const reference to the capacity map. thoneyvazul@1020: /// thoneyvazul@1020: /// Returns a const reference to the capacity map used by thoneyvazul@1020: /// the algorithm. thoneyvazul@1020: const CapacityMap &capacityMap() const { thoneyvazul@1020: return *_capacity; thoneyvazul@1020: } thoneyvazul@1020: alpar@1270: /// \brief Sets the map storing the cardinalities calculated by the thoneyvazul@1020: /// algorithm. thoneyvazul@1020: /// thoneyvazul@1020: /// Sets the map storing the cardinalities calculated by the algorithm. thoneyvazul@1020: /// If you don't use this function before calling \ref run(), thoneyvazul@1020: /// it will allocate one. The destuctor deallocates this thoneyvazul@1020: /// automatically allocated map, of course. thoneyvazul@1020: /// \return (*this) thoneyvazul@1020: MaxCardinalitySearch &cardinalityMap(CardinalityMap &m) { thoneyvazul@1020: if(local_cardinality) { alpar@1270: delete _cardinality; alpar@1270: local_cardinality=false; thoneyvazul@1020: } thoneyvazul@1020: _cardinality = &m; thoneyvazul@1020: return *this; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief Sets the map storing the processed nodes. thoneyvazul@1020: /// thoneyvazul@1020: /// Sets the map storing the processed nodes. thoneyvazul@1020: /// If you don't use this function before calling \ref run(), thoneyvazul@1020: /// it will allocate one. The destuctor deallocates this thoneyvazul@1020: /// automatically allocated map, of course. thoneyvazul@1020: /// \return (*this) alpar@1270: MaxCardinalitySearch &processedMap(ProcessedMap &m) thoneyvazul@1020: { thoneyvazul@1020: if(local_processed) { alpar@1270: delete _processed; alpar@1270: local_processed=false; thoneyvazul@1020: } thoneyvazul@1020: _processed = &m; thoneyvazul@1020: return *this; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief Returns a const reference to the cardinality map. thoneyvazul@1020: /// thoneyvazul@1020: /// Returns a const reference to the cardinality map used by thoneyvazul@1020: /// the algorithm. thoneyvazul@1020: const ProcessedMap &processedMap() const { thoneyvazul@1020: return *_processed; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief Sets the heap and the cross reference used by algorithm. thoneyvazul@1020: /// thoneyvazul@1020: /// Sets the heap and the cross reference used by algorithm. thoneyvazul@1020: /// If you don't use this function before calling \ref run(), thoneyvazul@1020: /// it will allocate one. The destuctor deallocates this thoneyvazul@1020: /// automatically allocated map, of course. thoneyvazul@1020: /// \return (*this) thoneyvazul@1020: MaxCardinalitySearch &heap(Heap& hp, HeapCrossRef &cr) { thoneyvazul@1020: if(local_heap_cross_ref) { alpar@1270: delete _heap_cross_ref; alpar@1270: local_heap_cross_ref = false; thoneyvazul@1020: } thoneyvazul@1020: _heap_cross_ref = &cr; thoneyvazul@1020: if(local_heap) { alpar@1270: delete _heap; alpar@1270: local_heap = false; thoneyvazul@1020: } thoneyvazul@1020: _heap = &hp; thoneyvazul@1020: return *this; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief Returns a const reference to the heap. thoneyvazul@1020: /// thoneyvazul@1020: /// Returns a const reference to the heap used by thoneyvazul@1020: /// the algorithm. thoneyvazul@1020: const Heap &heap() const { thoneyvazul@1020: return *_heap; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief Returns a const reference to the cross reference. thoneyvazul@1020: /// thoneyvazul@1020: /// Returns a const reference to the cross reference thoneyvazul@1020: /// of the heap. thoneyvazul@1020: const HeapCrossRef &heapCrossRef() const { thoneyvazul@1020: return *_heap_cross_ref; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: private: thoneyvazul@1020: thoneyvazul@1020: typedef typename Digraph::Node Node; thoneyvazul@1020: typedef typename Digraph::NodeIt NodeIt; thoneyvazul@1020: typedef typename Digraph::Arc Arc; thoneyvazul@1020: typedef typename Digraph::InArcIt InArcIt; thoneyvazul@1020: thoneyvazul@1020: void create_maps() { thoneyvazul@1020: if(!_capacity) { alpar@1270: local_capacity = true; alpar@1270: _capacity = Traits::createCapacityMap(*_graph); thoneyvazul@1020: } thoneyvazul@1020: if(!_cardinality) { alpar@1270: local_cardinality = true; alpar@1270: _cardinality = Traits::createCardinalityMap(*_graph); thoneyvazul@1020: } thoneyvazul@1020: if(!_processed) { alpar@1270: local_processed = true; alpar@1270: _processed = Traits::createProcessedMap(*_graph); thoneyvazul@1020: } thoneyvazul@1020: if (!_heap_cross_ref) { alpar@1270: local_heap_cross_ref = true; alpar@1270: _heap_cross_ref = Traits::createHeapCrossRef(*_graph); thoneyvazul@1020: } thoneyvazul@1020: if (!_heap) { alpar@1270: local_heap = true; alpar@1270: _heap = Traits::createHeap(*_heap_cross_ref); thoneyvazul@1020: } thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: void finalizeNodeData(Node node, Value capacity) { thoneyvazul@1020: _processed->set(node, true); thoneyvazul@1020: _cardinality->set(node, capacity); thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: public: thoneyvazul@1020: /// \name Execution control thoneyvazul@1020: /// The simplest way to execute the algorithm is to use thoneyvazul@1020: /// one of the member functions called \ref run(). thoneyvazul@1020: /// \n thoneyvazul@1020: /// If you need more control on the execution, thoneyvazul@1020: /// first you must call \ref init(), then you can add several source nodes thoneyvazul@1020: /// with \ref addSource(). thoneyvazul@1020: /// Finally \ref start() will perform the computation. thoneyvazul@1020: thoneyvazul@1020: ///@{ thoneyvazul@1020: thoneyvazul@1020: /// \brief Initializes the internal data structures. thoneyvazul@1020: /// thoneyvazul@1020: /// Initializes the internal data structures, and clears the heap. thoneyvazul@1020: void init() { thoneyvazul@1020: create_maps(); thoneyvazul@1020: _heap->clear(); thoneyvazul@1020: for (NodeIt it(*_graph) ; it != INVALID ; ++it) { alpar@1270: _processed->set(it, false); alpar@1270: _heap_cross_ref->set(it, Heap::PRE_HEAP); thoneyvazul@1020: } thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: /// \brief Adds a new source node. alpar@1270: /// thoneyvazul@1020: /// Adds a new source node to the priority heap. thoneyvazul@1020: /// thoneyvazul@1020: /// It checks if the node has not yet been added to the heap. thoneyvazul@1020: void addSource(Node source, Value capacity = 0) { thoneyvazul@1020: if(_heap->state(source) == Heap::PRE_HEAP) { alpar@1270: _heap->push(source, capacity); alpar@1270: } thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: /// \brief Processes the next node in the priority heap thoneyvazul@1020: /// thoneyvazul@1020: /// Processes the next node in the priority heap. thoneyvazul@1020: /// thoneyvazul@1020: /// \return The processed node. thoneyvazul@1020: /// thoneyvazul@1020: /// \warning The priority heap must not be empty! thoneyvazul@1020: Node processNextNode() { alpar@1270: Node node = _heap->top(); thoneyvazul@1020: finalizeNodeData(node, _heap->prio()); thoneyvazul@1020: _heap->pop(); alpar@1270: thoneyvazul@1020: for (InArcIt it(*_graph, node); it != INVALID; ++it) { alpar@1270: Node source = _graph->source(it); alpar@1270: switch (_heap->state(source)) { alpar@1270: case Heap::PRE_HEAP: alpar@1270: _heap->push(source, (*_capacity)[it]); alpar@1270: break; alpar@1270: case Heap::IN_HEAP: alpar@1270: _heap->decrease(source, (*_heap)[source] + (*_capacity)[it]); alpar@1270: break; alpar@1270: case Heap::POST_HEAP: alpar@1270: break; alpar@1270: } thoneyvazul@1020: } thoneyvazul@1020: return node; thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: /// \brief Next node to be processed. thoneyvazul@1020: /// thoneyvazul@1020: /// Next node to be processed. thoneyvazul@1020: /// alpar@1270: /// \return The next node to be processed or INVALID if the thoneyvazul@1020: /// priority heap is empty. alpar@1270: Node nextNode() { thoneyvazul@1020: return !_heap->empty() ? _heap->top() : INVALID; thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: /// \brief Returns \c false if there are nodes thoneyvazul@1020: /// to be processed in the priority heap thoneyvazul@1020: /// thoneyvazul@1020: /// Returns \c false if there are nodes thoneyvazul@1020: /// to be processed in the priority heap thoneyvazul@1020: bool emptyQueue() { return _heap->empty(); } alpar@1270: /// \brief Returns the number of the nodes to be processed thoneyvazul@1020: /// in the priority heap thoneyvazul@1020: /// thoneyvazul@1020: /// Returns the number of the nodes to be processed in the priority heap thoneyvazul@1020: int emptySize() { return _heap->size(); } alpar@1270: thoneyvazul@1020: /// \brief Executes the algorithm. thoneyvazul@1020: /// thoneyvazul@1020: /// Executes the algorithm. thoneyvazul@1020: /// thoneyvazul@1020: ///\pre init() must be called and at least one node should be added thoneyvazul@1020: /// with addSource() before using this function. thoneyvazul@1020: /// alpar@1270: /// This method runs the Maximum Cardinality Search algorithm from the thoneyvazul@1020: /// source node(s). thoneyvazul@1020: void start() { thoneyvazul@1020: while ( !_heap->empty() ) processNextNode(); thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: /// \brief Executes the algorithm until \c dest is reached. thoneyvazul@1020: /// thoneyvazul@1020: /// Executes the algorithm until \c dest is reached. thoneyvazul@1020: /// thoneyvazul@1020: /// \pre init() must be called and at least one node should be added thoneyvazul@1020: /// with addSource() before using this function. thoneyvazul@1020: /// alpar@1270: /// This method runs the %MaxCardinalitySearch algorithm from the source thoneyvazul@1020: /// nodes. thoneyvazul@1020: void start(Node dest) { thoneyvazul@1020: while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); thoneyvazul@1020: if ( !_heap->empty() ) finalizeNodeData(_heap->top(), _heap->prio()); thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: /// \brief Executes the algorithm until a condition is met. thoneyvazul@1020: /// thoneyvazul@1020: /// Executes the algorithm until a condition is met. thoneyvazul@1020: /// thoneyvazul@1020: /// \pre init() must be called and at least one node should be added thoneyvazul@1020: /// with addSource() before using this function. thoneyvazul@1020: /// thoneyvazul@1020: /// \param nm must be a bool (or convertible) node map. The algorithm thoneyvazul@1020: /// will stop when it reaches a node \c v with nm[v]==true. thoneyvazul@1020: template thoneyvazul@1020: void start(const NodeBoolMap &nm) { thoneyvazul@1020: while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); thoneyvazul@1020: if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: /// \brief Runs the maximum cardinality search algorithm from node \c s. thoneyvazul@1020: /// alpar@1270: /// This method runs the %MaxCardinalitySearch algorithm from a root thoneyvazul@1020: /// node \c s. thoneyvazul@1020: /// thoneyvazul@1020: ///\note d.run(s) is just a shortcut of the following code. thoneyvazul@1020: ///\code thoneyvazul@1020: /// d.init(); thoneyvazul@1020: /// d.addSource(s); thoneyvazul@1020: /// d.start(); thoneyvazul@1020: ///\endcode thoneyvazul@1020: void run(Node s) { thoneyvazul@1020: init(); thoneyvazul@1020: addSource(s); thoneyvazul@1020: start(); thoneyvazul@1020: } thoneyvazul@1020: alpar@1270: /// \brief Runs the maximum cardinality search algorithm for the thoneyvazul@1020: /// whole digraph. thoneyvazul@1020: /// alpar@1270: /// This method runs the %MaxCardinalitySearch algorithm from all thoneyvazul@1020: /// unprocessed node of the digraph. thoneyvazul@1020: /// thoneyvazul@1020: ///\note d.run(s) is just a shortcut of the following code. thoneyvazul@1020: ///\code thoneyvazul@1020: /// d.init(); thoneyvazul@1020: /// for (NodeIt it(digraph); it != INVALID; ++it) { thoneyvazul@1020: /// if (!d.reached(it)) { thoneyvazul@1020: /// d.addSource(s); thoneyvazul@1020: /// d.start(); thoneyvazul@1020: /// } thoneyvazul@1020: /// } thoneyvazul@1020: ///\endcode thoneyvazul@1020: void run() { thoneyvazul@1020: init(); thoneyvazul@1020: for (NodeIt it(*_graph); it != INVALID; ++it) { thoneyvazul@1020: if (!reached(it)) { thoneyvazul@1020: addSource(it); thoneyvazul@1020: start(); thoneyvazul@1020: } thoneyvazul@1020: } thoneyvazul@1020: } alpar@1270: thoneyvazul@1020: ///@} thoneyvazul@1020: thoneyvazul@1020: /// \name Query Functions alpar@1270: /// The results of the maximum cardinality search algorithm can be thoneyvazul@1020: /// obtained using these functions. thoneyvazul@1020: /// \n alpar@1270: /// Before the use of these functions, either run() or start() must be thoneyvazul@1020: /// called. alpar@1270: thoneyvazul@1020: ///@{ thoneyvazul@1020: thoneyvazul@1020: /// \brief The cardinality of a node. thoneyvazul@1020: /// thoneyvazul@1020: /// Returns the cardinality of a node. thoneyvazul@1020: /// \pre \ref run() must be called before using this function. thoneyvazul@1020: /// \warning If node \c v in unreachable from the root the return value thoneyvazul@1020: /// of this funcion is undefined. thoneyvazul@1020: Value cardinality(Node node) const { return (*_cardinality)[node]; } thoneyvazul@1020: thoneyvazul@1020: /// \brief The current cardinality of a node. thoneyvazul@1020: /// thoneyvazul@1020: /// Returns the current cardinality of a node. thoneyvazul@1020: /// \pre the given node should be reached but not processed thoneyvazul@1020: Value currentCardinality(Node node) const { return (*_heap)[node]; } thoneyvazul@1020: thoneyvazul@1020: /// \brief Returns a reference to the NodeMap of cardinalities. thoneyvazul@1020: /// alpar@1270: /// Returns a reference to the NodeMap of cardinalities. \pre \ref run() thoneyvazul@1020: /// must be called before using this function. thoneyvazul@1020: const CardinalityMap &cardinalityMap() const { return *_cardinality;} alpar@1270: thoneyvazul@1020: /// \brief Checks if a node is reachable from the root. thoneyvazul@1020: /// thoneyvazul@1020: /// Returns \c true if \c v is reachable from the root. thoneyvazul@1020: /// \warning The source nodes are initated as unreached. thoneyvazul@1020: /// \pre \ref run() must be called before using this function. thoneyvazul@1020: bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; } thoneyvazul@1020: thoneyvazul@1020: /// \brief Checks if a node is processed. thoneyvazul@1020: /// thoneyvazul@1020: /// Returns \c true if \c v is processed, i.e. the shortest thoneyvazul@1020: /// path to \c v has already found. thoneyvazul@1020: /// \pre \ref run() must be called before using this function. thoneyvazul@1020: bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; } alpar@1270: thoneyvazul@1020: ///@} thoneyvazul@1020: }; thoneyvazul@1020: thoneyvazul@1020: } thoneyvazul@1020: thoneyvazul@1020: #endif