deba@1967: /* -*- C++ -*- deba@1975: * lemon/min_cut.h - Part of LEMON, a generic C++ optimization library deba@1967: * deba@1967: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@1967: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@1967: * deba@1967: * Permission to use, modify and distribute this software is granted deba@1967: * provided that this copyright notice appears in all copies. For deba@1967: * precise terms see the accompanying LICENSE file. deba@1967: * deba@1967: * This software is provided "AS IS" with no warranty of any kind, deba@1967: * express or implied, and with no claim as to its suitability for any deba@1967: * purpose. deba@1967: * deba@1967: */ deba@1967: deba@1975: #ifndef LEMON_MIN_CUT_H deba@1975: #define LEMON_MIN_CUT_H deba@1967: deba@1967: deba@1967: /// \ingroup topology deba@1967: /// \file deba@1975: /// \brief Maximum cardinality search and min cut in undirected graphs. deba@1967: deba@1967: #include deba@1967: #include deba@2038: #include deba@1967: deba@1993: #include deba@1967: #include deba@1967: #include deba@1967: deba@1967: #include deba@1967: deba@1967: namespace lemon { deba@1967: deba@1975: namespace _min_cut_bits { deba@1967: deba@1967: template deba@1967: struct HeapSelector { deba@1967: template deba@1967: struct Selector { deba@1967: typedef BinHeap > Heap; deba@1967: }; deba@1967: }; deba@1967: deba@1967: template deba@1967: struct HeapSelector > > { deba@1967: template deba@1967: struct Selector { deba@2038: typedef BucketHeap Heap; deba@1967: }; deba@1967: }; deba@1967: deba@1967: } deba@1967: deba@1967: /// \brief Default traits class of MaxCardinalitySearch class. deba@1967: /// deba@1967: /// Default traits class of MaxCardinalitySearch class. deba@1967: /// \param Graph Graph type. deba@1967: /// \param CapacityMap Type of length map. deba@1967: template deba@1967: struct MaxCardinalitySearchDefaultTraits { deba@1967: /// The graph type the algorithm runs on. deba@1967: typedef _Graph Graph; deba@1967: deba@1967: /// \brief The type of the map that stores the edge capacities. deba@1967: /// deba@1967: /// The type of the map that stores the edge capacities. deba@1967: /// It must meet the \ref concept::ReadMap "ReadMap" concept. deba@1967: typedef _CapacityMap CapacityMap; deba@1967: deba@1967: /// \brief The type of the capacity of the edges. deba@1967: typedef typename CapacityMap::Value Value; deba@1967: deba@1967: /// \brief The cross reference type used by heap. deba@1967: /// deba@1967: /// The cross reference type used by heap. deba@1967: /// Usually it is \c Graph::NodeMap. deba@1967: typedef typename Graph::template NodeMap HeapCrossRef; deba@1967: deba@1967: /// \brief Instantiates a HeapCrossRef. deba@1967: /// deba@1967: /// This function instantiates a \ref HeapCrossRef. deba@1967: /// \param graph is the graph, to which we would like to define the deba@1967: /// HeapCrossRef. deba@1967: static HeapCrossRef *createHeapCrossRef(const Graph &graph) { deba@1967: return new HeapCrossRef(graph); deba@1967: } deba@1967: deba@1967: /// \brief The heap type used by MaxCardinalitySearch algorithm. deba@1967: /// deba@1967: /// The heap type used by MaxCardinalitySearch algorithm. It should deba@1967: /// maximalize the priorities. The default heap type is deba@1967: /// the \ref BinHeap, but it is specialized when the deba@1967: /// CapacityMap is ConstMap > deba@2038: /// to BucketHeap. deba@1967: /// deba@1967: /// \sa MaxCardinalitySearch deba@1975: typedef typename _min_cut_bits deba@1967: ::HeapSelector deba@1967: ::template Selector deba@1967: ::Heap Heap; deba@1967: deba@1967: /// \brief Instantiates a Heap. deba@1967: /// deba@1967: /// This function instantiates a \ref Heap. deba@1967: /// \param crossref The cross reference of the heap. deba@1967: static Heap *createHeap(HeapCrossRef& crossref) { deba@1967: return new Heap(crossref); deba@1967: } deba@1967: deba@1967: /// \brief The type of the map that stores whether a nodes is processed. deba@1967: /// deba@1967: /// The type of the map that stores whether a nodes is processed. deba@1967: /// It must meet the \ref concept::WriteMap "WriteMap" concept. deba@1967: /// By default it is a NullMap. deba@1967: typedef NullMap ProcessedMap; deba@1967: deba@1967: /// \brief Instantiates a ProcessedMap. deba@1967: /// deba@1967: /// This function instantiates a \ref ProcessedMap. deba@2037: /// \param graph is the graph, to which deba@1967: /// we would like to define the \ref ProcessedMap deba@1967: #ifdef DOXYGEN deba@1967: static ProcessedMap *createProcessedMap(const Graph &graph) deba@1967: #else deba@1967: static ProcessedMap *createProcessedMap(const Graph &) deba@1967: #endif deba@1967: { deba@1967: return new ProcessedMap(); deba@1967: } deba@1967: deba@1967: /// \brief The type of the map that stores the cardinalties of the nodes. deba@1967: /// deba@1967: /// The type of the map that stores the cardinalities of the nodes. deba@1967: /// It must meet the \ref concept::WriteMap "WriteMap" concept. deba@1967: typedef typename Graph::template NodeMap CardinalityMap; deba@1967: deba@1967: /// \brief Instantiates a CardinalityMap. deba@1967: /// deba@1967: /// This function instantiates a \ref CardinalityMap. deba@1967: /// \param graph is the graph, to which we would like to define the \ref deba@1967: /// CardinalityMap deba@1967: static CardinalityMap *createCardinalityMap(const Graph &graph) { deba@1967: return new CardinalityMap(graph); deba@1967: } deba@1967: deba@1967: }; deba@1967: deba@1967: /// \ingroup topology deba@1967: /// deba@1967: /// \brief Maximum Cardinality Search algorithm class. deba@1967: /// deba@1967: /// This class provides an efficient implementation of Maximum Cardinality deba@1967: /// Search algorithm. The maximum cardinality search chooses first time any alpar@1973: /// node of the graph. Then every time it chooses the node which is connected deba@1967: /// to the processed nodes at most in the sum of capacities on the out deba@1967: /// edges. If there is a cut in the graph the algorithm should choose alpar@1973: /// again any unprocessed node of the graph. Each node cardinality is deba@1967: /// the sum of capacities on the out edges to the nodes which are processed deba@1967: /// before the given node. deba@1967: /// deba@1967: /// The edge capacities are passed to the algorithm using a deba@1967: /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any deba@1967: /// kind of capacity. deba@1967: /// deba@1967: /// The type of the capacity is determined by the \ref deba@1967: /// concept::ReadMap::Value "Value" of the capacity map. deba@1967: /// deba@1967: /// It is also possible to change the underlying priority heap. deba@1967: /// deba@1967: /// deba@1967: /// \param _Graph The graph type the algorithm runs on. The default value deba@1967: /// is \ref ListGraph. The value of Graph is not used directly by deba@1967: /// the search algorithm, it is only passed to deba@1967: /// \ref MaxCardinalitySearchDefaultTraits. deba@1967: /// \param _CapacityMap This read-only EdgeMap determines the capacities of deba@1967: /// the edges. It is read once for each edge, so the map may involve in deba@1967: /// relatively time consuming process to compute the edge capacity if deba@1967: /// it is necessary. The default map type is \ref deba@1967: /// concept::StaticGraph::EdgeMap "Graph::EdgeMap". The value deba@1967: /// of CapacityMap is not used directly by search algorithm, it is only deba@1967: /// passed to \ref MaxCardinalitySearchDefaultTraits. deba@1967: /// \param _Traits Traits class to set various data types used by the deba@1967: /// algorithm. The default traits class is deba@1967: /// \ref MaxCardinalitySearchDefaultTraits deba@1967: /// "MaxCardinalitySearchDefaultTraits<_Graph, _CapacityMap>". deba@1967: /// See \ref MaxCardinalitySearchDefaultTraits deba@1967: /// for the documentation of a MaxCardinalitySearch traits class. deba@1967: /// deba@1967: /// \author Balazs Dezso deba@1967: deba@1967: #ifdef DOXYGEN deba@1967: template deba@1967: #else deba@1967: template , deba@1967: typename _Traits = deba@1967: MaxCardinalitySearchDefaultTraits<_Graph, _CapacityMap> > deba@1967: #endif deba@1967: class MaxCardinalitySearch { deba@1967: public: deba@1967: /// \brief \ref Exception for uninitialized parameters. deba@1967: /// deba@1967: /// This error represents problems in the initialization deba@1967: /// of the parameters of the algorithms. deba@1967: class UninitializedParameter : public lemon::UninitializedParameter { deba@1967: public: deba@1967: virtual const char* exceptionName() const { deba@1967: return "lemon::MaxCardinalitySearch::UninitializedParameter"; deba@1967: } deba@1967: }; deba@1967: deba@1967: typedef _Traits Traits; deba@1967: ///The type of the underlying graph. deba@1967: typedef typename Traits::Graph Graph; deba@1967: deba@1967: ///The type of the capacity of the edges. deba@1967: typedef typename Traits::CapacityMap::Value Value; deba@1967: ///The type of the map that stores the edge capacities. deba@1967: typedef typename Traits::CapacityMap CapacityMap; deba@1967: ///The type of the map indicating if a node is processed. deba@1967: typedef typename Traits::ProcessedMap ProcessedMap; deba@1967: ///The type of the map that stores the cardinalities of the nodes. deba@1967: typedef typename Traits::CardinalityMap CardinalityMap; deba@1967: ///The cross reference type used for the current heap. deba@1967: typedef typename Traits::HeapCrossRef HeapCrossRef; deba@1967: ///The heap type used by the algorithm. It maximize the priorities. deba@1967: typedef typename Traits::Heap Heap; deba@1967: private: deba@1967: /// Pointer to the underlying graph. deba@1967: const Graph *_graph; deba@1967: /// Pointer to the capacity map deba@1967: const CapacityMap *_capacity; deba@1967: ///Pointer to the map of cardinality. deba@1967: CardinalityMap *_cardinality; deba@1967: ///Indicates if \ref _cardinality is locally allocated (\c true) or not. deba@1967: bool local_cardinality; deba@1967: ///Pointer to the map of processed status of the nodes. deba@1967: ProcessedMap *_processed; deba@1967: ///Indicates if \ref _processed is locally allocated (\c true) or not. deba@1967: bool local_processed; deba@1967: ///Pointer to the heap cross references. deba@1967: HeapCrossRef *_heap_cross_ref; deba@1967: ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not. deba@1967: bool local_heap_cross_ref; deba@1967: ///Pointer to the heap. deba@1967: Heap *_heap; deba@1967: ///Indicates if \ref _heap is locally allocated (\c true) or not. deba@1967: bool local_heap; deba@1967: deba@1967: public : deba@1967: deba@1967: typedef MaxCardinalitySearch Create; deba@1967: deba@1967: ///\name Named template parameters deba@1967: deba@1967: ///@{ deba@1967: deba@1967: template deba@1967: struct DefCardinalityMapTraits : public Traits { deba@1967: typedef T CardinalityMap; deba@1967: static CardinalityMap *createCardinalityMap(const Graph &) deba@1967: { deba@1967: throw UninitializedParameter(); deba@1967: } deba@1967: }; deba@1967: /// \brief \ref named-templ-param "Named parameter" for setting deba@1967: /// CardinalityMap type deba@1967: /// deba@1967: /// \ref named-templ-param "Named parameter" for setting CardinalityMap deba@1967: /// type deba@1967: template deba@1967: struct DefCardinalityMap deba@1967: : public MaxCardinalitySearch > { deba@1967: typedef MaxCardinalitySearch > Create; deba@1967: }; deba@1967: deba@1967: template deba@1967: struct DefProcessedMapTraits : public Traits { deba@1967: typedef T ProcessedMap; deba@1967: static ProcessedMap *createProcessedMap(const Graph &) { deba@1967: throw UninitializedParameter(); deba@1967: } deba@1967: }; deba@1967: /// \brief \ref named-templ-param "Named parameter" for setting deba@1967: /// ProcessedMap type deba@1967: /// deba@1967: /// \ref named-templ-param "Named parameter" for setting ProcessedMap type deba@1967: /// deba@1967: template deba@1967: struct DefProcessedMap deba@1967: : public MaxCardinalitySearch > { deba@1967: typedef MaxCardinalitySearch > Create; deba@1967: }; deba@1967: deba@1967: template deba@1967: struct DefHeapTraits : public Traits { deba@1967: typedef CR HeapCrossRef; deba@1967: typedef H Heap; deba@1967: static HeapCrossRef *createHeapCrossRef(const Graph &) { deba@1967: throw UninitializedParameter(); deba@1967: } deba@1967: static Heap *createHeap(HeapCrossRef &) { deba@1967: throw UninitializedParameter(); deba@1967: } deba@1967: }; deba@1967: /// \brief \ref named-templ-param "Named parameter" for setting heap deba@1967: /// and cross reference type deba@1967: /// deba@1967: /// \ref named-templ-param "Named parameter" for setting heap and cross deba@1967: /// reference type deba@1967: template > deba@1967: struct DefHeap deba@1967: : public MaxCardinalitySearch > { deba@1967: typedef MaxCardinalitySearch< Graph, CapacityMap, deba@1967: DefHeapTraits > Create; deba@1967: }; deba@1967: deba@1967: template deba@1967: struct DefStandardHeapTraits : public Traits { deba@1967: typedef CR HeapCrossRef; deba@1967: typedef H Heap; deba@1967: static HeapCrossRef *createHeapCrossRef(const Graph &graph) { deba@1967: return new HeapCrossRef(graph); deba@1967: } deba@1967: static Heap *createHeap(HeapCrossRef &crossref) { deba@1967: return new Heap(crossref); deba@1967: } deba@1967: }; deba@1967: deba@1967: /// \brief \ref named-templ-param "Named parameter" for setting heap and deba@1967: /// cross reference type with automatic allocation deba@1967: /// deba@1967: /// \ref named-templ-param "Named parameter" for setting heap and cross deba@1967: /// reference type. It can allocate the heap and the cross reference deba@1967: /// object if the cross reference's constructor waits for the graph as deba@1967: /// parameter and the heap's constructor waits for the cross reference. deba@1967: template > deba@1967: struct DefStandardHeap deba@1967: : public MaxCardinalitySearch > { deba@1967: typedef MaxCardinalitySearch > deba@1967: Create; deba@1967: }; deba@1967: deba@1967: ///@} deba@1967: deba@1967: deba@1967: protected: deba@1967: deba@1967: MaxCardinalitySearch() {} deba@1967: deba@1967: public: deba@1967: deba@1967: /// \brief Constructor. deba@1967: /// deba@2037: ///\param graph the graph the algorithm will run on. deba@2037: ///\param capacity the capacity map used by the algorithm. deba@1967: MaxCardinalitySearch(const Graph& graph, const CapacityMap& capacity) : deba@1967: _graph(&graph), _capacity(&capacity), deba@1967: _cardinality(0), local_cardinality(false), deba@1967: _processed(0), local_processed(false), deba@1967: _heap_cross_ref(0), local_heap_cross_ref(false), deba@1967: _heap(0), local_heap(false) deba@1967: { } deba@1967: deba@1967: /// \brief Destructor. deba@1967: ~MaxCardinalitySearch() { deba@1967: if(local_cardinality) delete _cardinality; deba@1967: if(local_processed) delete _processed; deba@1967: if(local_heap_cross_ref) delete _heap_cross_ref; deba@1967: if(local_heap) delete _heap; deba@1967: } deba@1967: deba@1967: /// \brief Sets the capacity map. deba@1967: /// deba@1967: /// Sets the capacity map. deba@1967: /// \return (*this) deba@1967: MaxCardinalitySearch &capacityMap(const CapacityMap &m) { deba@1967: _capacity = &m; deba@1967: return *this; deba@1967: } deba@1967: deba@1967: /// \brief Sets the map storing the cardinalities calculated by the deba@1967: /// algorithm. deba@1967: /// deba@1967: /// Sets the map storing the cardinalities calculated by the algorithm. deba@1967: /// If you don't use this function before calling \ref run(), deba@1967: /// it will allocate one. The destuctor deallocates this deba@1967: /// automatically allocated map, of course. deba@1967: /// \return (*this) deba@1967: MaxCardinalitySearch &cardinalityMap(CardinalityMap &m) { deba@1967: if(local_cardinality) { deba@1967: delete _cardinality; deba@1967: local_cardinality=false; deba@1967: } deba@1967: _cardinality = &m; deba@1967: return *this; deba@1967: } deba@1967: deba@1967: /// \brief Sets the map storing the processed nodes. deba@1967: /// deba@1967: /// Sets the map storing the processed nodes. deba@1967: /// If you don't use this function before calling \ref run(), deba@1967: /// it will allocate one. The destuctor deallocates this deba@1967: /// automatically allocated map, of course. deba@1967: /// \return (*this) deba@1967: MaxCardinalitySearch &processedMap(ProcessedMap &m) deba@1967: { deba@1967: if(local_processed) { deba@1967: delete _processed; deba@1967: local_processed=false; deba@1967: } deba@1967: _processed = &m; deba@1967: return *this; deba@1967: } deba@1967: deba@1967: /// \brief Sets the heap and the cross reference used by algorithm. deba@1967: /// deba@1967: /// Sets the heap and the cross reference used by algorithm. deba@1967: /// If you don't use this function before calling \ref run(), deba@1967: /// it will allocate one. The destuctor deallocates this deba@1967: /// automatically allocated map, of course. deba@1967: /// \return (*this) deba@1967: MaxCardinalitySearch &heap(Heap& heap, HeapCrossRef &crossRef) { deba@1967: if(local_heap_cross_ref) { deba@1967: delete _heap_cross_ref; deba@1967: local_heap_cross_ref = false; deba@1967: } deba@1967: _heap_cross_ref = &crossRef; deba@1967: if(local_heap) { deba@1967: delete _heap; deba@1967: local_heap = false; deba@1967: } deba@1967: _heap = &heap; deba@1967: return *this; deba@1967: } deba@1967: deba@1967: private: deba@1967: deba@1967: typedef typename Graph::Node Node; deba@1967: typedef typename Graph::NodeIt NodeIt; deba@1967: typedef typename Graph::Edge Edge; deba@1967: typedef typename Graph::InEdgeIt InEdgeIt; deba@1967: deba@1967: void create_maps() { deba@1967: if(!_cardinality) { deba@1967: local_cardinality = true; deba@1967: _cardinality = Traits::createCardinalityMap(*_graph); deba@1967: } deba@1967: if(!_processed) { deba@1967: local_processed = true; deba@1967: _processed = Traits::createProcessedMap(*_graph); deba@1967: } deba@1967: if (!_heap_cross_ref) { deba@1967: local_heap_cross_ref = true; deba@1967: _heap_cross_ref = Traits::createHeapCrossRef(*_graph); deba@1967: } deba@1967: if (!_heap) { deba@1967: local_heap = true; deba@1967: _heap = Traits::createHeap(*_heap_cross_ref); deba@1967: } deba@1967: } deba@1967: deba@1967: void finalizeNodeData(Node node, Value capacity) { deba@1967: _processed->set(node, true); deba@1967: _cardinality->set(node, capacity); deba@1967: } deba@1967: deba@1967: public: deba@1967: /// \name Execution control deba@1967: /// The simplest way to execute the algorithm is to use deba@1967: /// one of the member functions called \c run(...). deba@1967: /// \n deba@1967: /// If you need more control on the execution, deba@1967: /// first you must call \ref init(), then you can add several source nodes deba@1967: /// with \ref addSource(). deba@1967: /// Finally \ref start() will perform the actual path deba@1967: /// computation. deba@1967: deba@1967: ///@{ deba@1967: deba@1967: /// \brief Initializes the internal data structures. deba@1967: /// deba@1967: /// Initializes the internal data structures. deba@1967: void init() { deba@1967: create_maps(); deba@1967: _heap->clear(); deba@1967: for (NodeIt it(*_graph) ; it != INVALID ; ++it) { deba@1967: _processed->set(it, false); deba@1967: _heap_cross_ref->set(it, Heap::PRE_HEAP); deba@1967: } deba@1967: } deba@1967: deba@1967: /// \brief Adds a new source node. deba@1967: /// deba@1967: /// Adds a new source node to the priority heap. deba@1967: /// deba@1967: /// It checks if the node has not yet been added to the heap. deba@1967: void addSource(Node source, Value capacity = 0) { deba@1967: if(_heap->state(source) == Heap::PRE_HEAP) { deba@1967: _heap->push(source, capacity); deba@1967: } deba@1967: } deba@1967: deba@1967: /// \brief Processes the next node in the priority heap deba@1967: /// deba@1967: /// Processes the next node in the priority heap. deba@1967: /// deba@1967: /// \return The processed node. deba@1967: /// deba@1967: /// \warning The priority heap must not be empty! deba@1967: Node processNextNode() { deba@1967: Node node = _heap->top(); deba@1967: finalizeNodeData(node, _heap->prio()); deba@1967: _heap->pop(); deba@1967: deba@1967: for (InEdgeIt it(*_graph, node); it != INVALID; ++it) { deba@1967: Node source = _graph->source(it); deba@1967: switch (_heap->state(source)) { deba@1967: case Heap::PRE_HEAP: deba@1967: _heap->push(source, (*_capacity)[it]); deba@1967: break; deba@1967: case Heap::IN_HEAP: deba@1967: _heap->decrease(source, (*_heap)[source] + (*_capacity)[it]); deba@1967: break; deba@1967: case Heap::POST_HEAP: deba@1967: break; deba@1967: } deba@1967: } deba@1967: return node; deba@1967: } deba@1967: deba@1967: /// \brief Next node to be processed. deba@1967: /// deba@1967: /// Next node to be processed. deba@1967: /// deba@1967: /// \return The next node to be processed or INVALID if the deba@1967: /// priority heap is empty. deba@1967: Node nextNode() { deba@1967: return _heap->empty() ? _heap->top() : INVALID; deba@1967: } deba@1967: deba@1967: /// \brief Returns \c false if there are nodes deba@1967: /// to be processed in the priority heap deba@1967: /// deba@1967: /// Returns \c false if there are nodes deba@1967: /// to be processed in the priority heap deba@1967: bool emptyQueue() { return _heap->empty(); } deba@1967: /// \brief Returns the number of the nodes to be processed deba@1967: /// in the priority heap deba@1967: /// deba@1967: /// Returns the number of the nodes to be processed in the priority heap deba@1967: int queueSize() { return _heap->size(); } deba@1967: deba@1967: /// \brief Executes the algorithm. deba@1967: /// deba@1967: /// Executes the algorithm. deba@1967: /// deba@1967: ///\pre init() must be called and at least one node should be added deba@1967: /// with addSource() before using this function. deba@1967: /// deba@1967: /// This method runs the Maximum Cardinality Search algorithm from the deba@1967: /// source node(s). deba@1967: void start() { deba@1967: while ( !_heap->empty() ) processNextNode(); deba@1967: } deba@1967: deba@1967: /// \brief Executes the algorithm until \c dest is reached. deba@1967: /// deba@1967: /// Executes the algorithm until \c dest is reached. deba@1967: /// deba@1967: /// \pre init() must be called and at least one node should be added deba@1967: /// with addSource() before using this function. deba@1967: /// deba@1967: /// This method runs the %MaxCardinalitySearch algorithm from the source deba@1967: /// nodes. deba@1967: void start(Node dest) { deba@1967: while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); deba@1967: if ( !_heap->empty() ) finalizeNodeData(_heap->top(), _heap->prio()); deba@1967: } deba@1967: deba@1967: /// \brief Executes the algorithm until a condition is met. deba@1967: /// deba@1967: /// Executes the algorithm until a condition is met. deba@1967: /// deba@1967: /// \pre init() must be called and at least one node should be added deba@1967: /// with addSource() before using this function. deba@1967: /// deba@1967: /// \param nm must be a bool (or convertible) node map. The algorithm deba@1967: /// will stop when it reaches a node \c v with nm[v]==true. deba@1967: template deba@1967: void start(const NodeBoolMap &nm) { deba@1967: while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); deba@1967: if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); deba@1967: } deba@1967: deba@1967: /// \brief Runs the maximal cardinality search algorithm from node \c s. deba@1967: /// deba@1967: /// This method runs the %MaxCardinalitySearch algorithm from a root deba@1967: /// node \c s. deba@1967: /// deba@1967: ///\note d.run(s) is just a shortcut of the following code. deba@1967: ///\code deba@1967: /// d.init(); deba@1967: /// d.addSource(s); deba@1967: /// d.start(); deba@1967: ///\endcode deba@1967: void run(Node s) { deba@1967: init(); deba@1967: addSource(s); deba@1967: start(); deba@1967: } deba@1967: deba@1967: /// \brief Runs the maximal cardinality search algorithm for the deba@1967: /// whole graph. deba@1967: /// deba@1967: /// This method runs the %MaxCardinalitySearch algorithm from all deba@1967: /// unprocessed node of the graph. deba@1967: /// deba@1967: ///\note d.run(s) is just a shortcut of the following code. deba@1967: ///\code deba@1967: /// d.init(); deba@1967: /// for (NodeIt it(graph); it != INVALID; ++it) { deba@1967: /// if (!d.reached(it)) { deba@1967: /// d.addSource(s); deba@1967: /// d.start(); deba@1967: /// } deba@1967: /// } deba@1967: ///\endcode deba@1967: void run() { deba@1967: init(); deba@1967: for (NodeIt it(*_graph); it != INVALID; ++it) { deba@1967: if (!reached(it)) { deba@1967: addSource(it); deba@1967: start(); deba@1967: } deba@1967: } deba@1967: } deba@1967: deba@1967: ///@} deba@1967: deba@1967: /// \name Query Functions deba@1967: /// The result of the maximum cardinality search algorithm can be deba@1967: /// obtained using these functions. deba@1967: /// \n deba@1967: /// Before the use of these functions, either run() or start() must be deba@1967: /// called. deba@1967: deba@1967: ///@{ deba@1967: deba@1967: /// \brief The cardinality of a node. deba@1967: /// deba@1967: /// Returns the cardinality of a node. deba@1967: /// \pre \ref run() must be called before using this function. deba@1967: /// \warning If node \c v in unreachable from the root the return value deba@1967: /// of this funcion is undefined. deba@1967: Value cardinality(Node node) const { return (*_cardinality)[node]; } deba@1967: deba@1967: /// \brief Returns a reference to the NodeMap of cardinalities. deba@1967: /// deba@1967: /// Returns a reference to the NodeMap of cardinalities. \pre \ref run() deba@1967: /// must be called before using this function. deba@1967: const CardinalityMap &cardinalityMap() const { return *_cardinality;} deba@1967: deba@1967: /// \brief Checks if a node is reachable from the root. deba@1967: /// deba@1967: /// Returns \c true if \c v is reachable from the root. deba@1967: /// \warning The source nodes are inditated as unreached. deba@1967: /// \pre \ref run() must be called before using this function. deba@1967: bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; } deba@1967: deba@1967: /// \brief Checks if a node is processed. deba@1967: /// deba@1967: /// Returns \c true if \c v is processed, i.e. the shortest deba@1967: /// path to \c v has already found. deba@1967: /// \pre \ref run() must be called before using this function. deba@1967: bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; } deba@1967: deba@1967: ///@} deba@1967: }; deba@1967: deba@1975: /// \brief Default traits class of MinCut class. deba@1967: /// deba@1975: /// Default traits class of MinCut class. deba@1967: /// \param Graph Graph type. deba@1967: /// \param CapacityMap Type of length map. deba@1967: template deba@1975: struct MinCutDefaultTraits { deba@1967: /// \brief The type of the capacity of the edges. deba@1967: typedef typename _CapacityMap::Value Value; deba@1967: deba@1967: /// The graph type the algorithm runs on. deba@1967: typedef _Graph Graph; deba@1967: deba@1975: /// The AuxGraph type which is an EraseableGraph deba@1975: typedef ListUGraph AuxGraph; deba@1967: deba@1975: /// \brief Instantiates a AuxGraph. deba@1967: /// deba@1975: /// This function instantiates a \ref AuxGraph. deba@1975: static AuxGraph *createAuxGraph() { deba@1975: return new AuxGraph(); deba@1967: } deba@1967: deba@1967: /// \brief The type of the map that stores the edge capacities. deba@1967: /// deba@1967: /// The type of the map that stores the edge capacities. deba@1967: /// It must meet the \ref concept::ReadMap "ReadMap" concept. deba@1967: typedef _CapacityMap CapacityMap; deba@1967: deba@1967: /// \brief Instantiates a CapacityMap. deba@1967: /// deba@1967: /// This function instantiates a \ref CapacityMap. deba@1967: #ifdef DOXYGEN deba@1967: static CapacityMap *createCapacityMap(const Graph& graph) deba@1967: #else deba@1967: static CapacityMap *createCapacityMap(const Graph&) deba@1967: #endif deba@1967: { deba@1967: throw UninitializedParameter(); deba@1967: } deba@1967: deba@1975: /// \brief The AuxCapacityMap type deba@1967: /// deba@1975: /// The type of the map that stores the auxing edge capacities. deba@1975: typedef AuxGraph::UEdgeMap AuxCapacityMap; deba@1967: deba@1975: /// \brief Instantiates a AuxCapacityMap. deba@1967: /// deba@1975: /// This function instantiates a \ref AuxCapacityMap. deba@1975: static AuxCapacityMap *createAuxCapacityMap(const AuxGraph& graph) { deba@1975: return new AuxCapacityMap(graph); deba@1967: } deba@1967: deba@1967: /// \brief The cross reference type used by heap. deba@1967: /// deba@1967: /// The cross reference type used by heap. deba@1967: /// Usually it is \c Graph::NodeMap. deba@1975: typedef AuxGraph::NodeMap HeapCrossRef; deba@1967: deba@1967: /// \brief Instantiates a HeapCrossRef. deba@1967: /// deba@1967: /// This function instantiates a \ref HeapCrossRef. deba@1967: /// \param graph is the graph, to which we would like to define the deba@1967: /// HeapCrossRef. deba@1975: static HeapCrossRef *createHeapCrossRef(const AuxGraph &graph) { deba@1967: return new HeapCrossRef(graph); deba@1967: } deba@1967: deba@1975: /// \brief The heap type used by MinCut algorithm. deba@1967: /// deba@1975: /// The heap type used by MinCut algorithm. It should deba@1967: /// maximalize the priorities and the heap's key type is deba@1975: /// the aux graph's node. deba@1967: /// deba@1967: /// \sa BinHeap deba@1975: /// \sa MinCut deba@1975: typedef typename _min_cut_bits deba@1967: ::HeapSelector deba@1975: ::template Selector deba@1967: ::Heap Heap; deba@1967: deba@1967: /// \brief Instantiates a Heap. deba@1967: /// deba@1967: /// This function instantiates a \ref Heap. deba@1967: /// \param crossref The cross reference of the heap. deba@1967: static Heap *createHeap(HeapCrossRef& crossref) { deba@1967: return new Heap(crossref); deba@1967: } deba@1967: deba@1975: /// \brief Map from the AuxGraph's node type to the Graph's node type. deba@1967: /// deba@1975: /// Map from the AuxGraph's node type to the Graph's node type. deba@1975: typedef typename AuxGraph deba@1967: ::template NodeMap NodeRefMap; deba@1967: deba@1967: /// \brief Instantiates a NodeRefMap. deba@1967: /// deba@1967: /// This function instantiates a \ref NodeRefMap. deba@1975: static NodeRefMap *createNodeRefMap(const AuxGraph& graph) { deba@1967: return new NodeRefMap(graph); deba@1967: } deba@1967: deba@1967: /// \brief Map from the Graph's node type to the Graph's node type. deba@1967: /// deba@1967: /// Map from the Graph's node type to the Graph's node type. deba@1967: typedef typename Graph deba@1967: ::template NodeMap ListRefMap; deba@1967: deba@1967: /// \brief Instantiates a ListRefMap. deba@1967: /// deba@1967: /// This function instantiates a \ref ListRefMap. deba@1967: static ListRefMap *createListRefMap(const Graph& graph) { deba@1967: return new ListRefMap(graph); deba@1967: } deba@1967: deba@1967: deba@1967: }; deba@1967: deba@1975: namespace _min_cut_bits { deba@1967: template deba@1967: class LastTwoMap { deba@1967: public: deba@1967: typedef _Key Key; deba@1967: typedef bool Value; deba@1967: deba@1967: LastTwoMap(int _num) : num(_num) {} deba@1967: void set(const Key& key, bool val) { deba@1967: if (!val) return; deba@1967: --num; deba@1967: if (num > 1) return; deba@1967: keys[num] = key; deba@1967: } deba@1967: deba@1967: Key operator[](int index) const { return keys[index]; } deba@1967: private: deba@1967: Key keys[2]; deba@1967: int num; deba@1967: }; deba@1967: } deba@1967: deba@1967: /// \ingroup topology deba@1967: /// deba@1975: /// \brief Calculates the min cut in an undirected graph. deba@1967: /// deba@1975: /// Calculates the min cut in an undirected graph. deba@1967: /// The algorithm separates the graph's nodes to two partitions with the deba@1975: /// min sum of edge capacities between the two partitions. The deba@1975: /// algorithm can be used to test the netaux reliability specifically deba@1975: /// to test how many links have to be destroyed in the netaux to split it deba@1975: /// at least two distinict subnetaux. deba@1967: /// deba@2042: /// The complexity of the algorithm is \f$ O(ne\log(n)) \f$ but with deba@2042: /// Fibonacci heap it can be decreased to \f$ O(ne+n^2\log(n)) \f$. When deba@2042: /// the neutral capacity map is used then it uses BucketHeap which deba@2042: /// results \f$ O(ne) \f$ time complexity. deba@1967: #ifdef DOXYGEN deba@1967: template deba@1967: #else deba@1967: template , deba@1975: typename _Traits = MinCutDefaultTraits<_Graph, _CapacityMap> > deba@1967: #endif deba@1975: class MinCut { deba@1967: public: deba@1967: /// \brief \ref Exception for uninitialized parameters. deba@1967: /// deba@1967: /// This error represents problems in the initialization deba@1967: /// of the parameters of the algorithms. deba@1967: class UninitializedParameter : public lemon::UninitializedParameter { deba@1967: public: deba@1967: virtual const char* exceptionName() const { deba@1975: return "lemon::MinCut::UninitializedParameter"; deba@1967: } deba@1967: }; deba@1967: deba@1967: deba@1967: private: deba@1967: deba@1967: typedef _Traits Traits; deba@1967: /// The type of the underlying graph. deba@1967: typedef typename Traits::Graph Graph; deba@1967: deba@1967: /// The type of the capacity of the edges. deba@1967: typedef typename Traits::CapacityMap::Value Value; deba@1967: /// The type of the map that stores the edge capacities. deba@1967: typedef typename Traits::CapacityMap CapacityMap; deba@1975: /// The type of the aux graph deba@1975: typedef typename Traits::AuxGraph AuxGraph; deba@1975: /// The type of the aux capacity map deba@1975: typedef typename Traits::AuxCapacityMap AuxCapacityMap; deba@1967: /// The cross reference type used for the current heap. deba@1967: typedef typename Traits::HeapCrossRef HeapCrossRef; deba@1967: /// The heap type used by the max cardinality algorithm. deba@1967: typedef typename Traits::Heap Heap; deba@1975: /// The node refrefernces between the original and aux graph type. deba@1967: typedef typename Traits::NodeRefMap NodeRefMap; deba@1967: /// The list node refrefernces in the original graph type. deba@1967: typedef typename Traits::ListRefMap ListRefMap; deba@1967: deba@1967: public: deba@1967: deba@1967: ///\name Named template parameters deba@1967: deba@1967: ///@{ deba@1967: deba@1967: struct DefNeutralCapacityTraits : public Traits { deba@1967: typedef ConstMap > CapacityMap; deba@1967: static CapacityMap *createCapacityMap(const Graph&) { deba@1967: return new CapacityMap(); deba@1967: } deba@1967: }; deba@1967: /// \brief \ref named-templ-param "Named parameter" for setting deba@1967: /// the capacity type to constMap() deba@1967: /// deba@1967: /// \ref named-templ-param "Named parameter" for setting deba@1967: /// the capacity type to constMap() deba@1967: struct DefNeutralCapacity deba@1975: : public MinCut { deba@1975: typedef MinCut Create; deba@1967: }; deba@1967: deba@1967: deba@1967: template deba@1967: struct DefHeapTraits : public Traits { deba@1967: typedef CR HeapCrossRef; deba@1967: typedef H Heap; deba@1975: static HeapCrossRef *createHeapCrossRef(const AuxGraph &) { deba@1967: throw UninitializedParameter(); deba@1967: } deba@1967: static Heap *createHeap(HeapCrossRef &) { deba@1967: throw UninitializedParameter(); deba@1967: } deba@1967: }; deba@1967: /// \brief \ref named-templ-param "Named parameter" for setting heap deba@1967: /// and cross reference type deba@1967: /// deba@1967: /// \ref named-templ-param "Named parameter" for setting heap and cross deba@1967: /// reference type deba@1967: template > deba@1967: struct DefHeap deba@1975: : public MinCut > { deba@1975: typedef MinCut< Graph, CapacityMap, DefHeapTraits > Create; deba@1967: }; deba@1967: deba@1967: template deba@1967: struct DefStandardHeapTraits : public Traits { deba@1967: typedef CR HeapCrossRef; deba@1967: typedef H Heap; deba@1975: static HeapCrossRef *createHeapCrossRef(const AuxGraph &graph) { deba@1967: return new HeapCrossRef(graph); deba@1967: } deba@1967: static Heap *createHeap(HeapCrossRef &crossref) { deba@1967: return new Heap(crossref); deba@1967: } deba@1967: }; deba@1967: deba@1967: /// \brief \ref named-templ-param "Named parameter" for setting heap and deba@1967: /// cross reference type with automatic allocation deba@1967: /// deba@1967: /// \ref named-templ-param "Named parameter" for setting heap and cross deba@1967: /// reference type. It can allocate the heap and the cross reference deba@1967: /// object if the cross reference's constructor waits for the graph as deba@1967: /// parameter and the heap's constructor waits for the cross reference. deba@1967: template > deba@1967: struct DefStandardHeap deba@1975: : public MinCut > { deba@1975: typedef MinCut > deba@1967: Create; deba@1967: }; deba@1967: deba@1967: ///@} deba@1967: deba@1967: deba@1967: private: deba@1967: /// Pointer to the underlying graph. deba@1967: const Graph *_graph; deba@1967: /// Pointer to the capacity map deba@1967: const CapacityMap *_capacity; deba@1967: /// \brief Indicates if \ref _capacity is locally allocated deba@1967: /// (\c true) or not. deba@1967: bool local_capacity; deba@1967: deba@1975: /// Pointer to the aux graph. deba@1975: AuxGraph *_aux_graph; deba@1975: /// \brief Indicates if \ref _aux_graph is locally allocated deba@1967: /// (\c true) or not. deba@1975: bool local_aux_graph; deba@1975: /// Pointer to the aux capacity map deba@1975: AuxCapacityMap *_aux_capacity; deba@1975: /// \brief Indicates if \ref _aux_capacity is locally allocated deba@1967: /// (\c true) or not. deba@1975: bool local_aux_capacity; deba@1967: /// Pointer to the heap cross references. deba@1967: HeapCrossRef *_heap_cross_ref; deba@1967: /// \brief Indicates if \ref _heap_cross_ref is locally allocated deba@1967: /// (\c true) or not. deba@1967: bool local_heap_cross_ref; deba@1967: /// Pointer to the heap. deba@1967: Heap *_heap; deba@1967: /// Indicates if \ref _heap is locally allocated (\c true) or not. deba@1967: bool local_heap; deba@1967: deba@1975: /// The min cut value. deba@1975: Value _min_cut; deba@1975: /// The number of the nodes of the aux graph. deba@1967: int _node_num; deba@1967: /// The first and last node of the min cut in the next list; deba@1967: typename Graph::Node _first_node, _last_node; deba@1967: deba@1967: /// \brief The first and last element in the list associated deba@1975: /// to the aux graph node. deba@1967: NodeRefMap *_first, *_last; deba@1967: /// \brief The next node in the node lists. deba@1967: ListRefMap *_next; deba@1967: deba@1967: void create_structures() { deba@1967: if (!_capacity) { deba@1967: local_capacity = true; deba@1967: _capacity = Traits::createCapacityMap(*_graph); deba@1967: } deba@1975: if(!_aux_graph) { deba@1975: local_aux_graph = true; deba@1975: _aux_graph = Traits::createAuxGraph(); deba@1967: } deba@1975: if(!_aux_capacity) { deba@1975: local_aux_capacity = true; deba@1975: _aux_capacity = Traits::createAuxCapacityMap(*_aux_graph); deba@1967: } deba@1967: deba@1975: _first = Traits::createNodeRefMap(*_aux_graph); deba@1975: _last = Traits::createNodeRefMap(*_aux_graph); deba@1967: deba@1967: _next = Traits::createListRefMap(*_graph); deba@1967: deba@1975: typename Graph::template NodeMap ref(*_graph); deba@1967: deba@1967: for (typename Graph::NodeIt it(*_graph); it != INVALID; ++it) { deba@1967: _next->set(it, INVALID); deba@1975: typename AuxGraph::Node node = _aux_graph->addNode(); deba@1967: _first->set(node, it); deba@1967: _last->set(node, it); deba@1967: ref.set(it, node); deba@1967: } deba@1967: deba@1967: for (typename Graph::UEdgeIt it(*_graph); it != INVALID; ++it) { deba@1967: if (_graph->source(it) == _graph->target(it)) continue; deba@1975: typename AuxGraph::UEdge uedge = deba@1975: _aux_graph->addEdge(ref[_graph->source(it)], deba@1967: ref[_graph->target(it)]); deba@1975: _aux_capacity->set(uedge, (*_capacity)[it]); deba@1967: deba@1967: } deba@1967: deba@1967: if (!_heap_cross_ref) { deba@1967: local_heap_cross_ref = true; deba@1975: _heap_cross_ref = Traits::createHeapCrossRef(*_aux_graph); deba@1967: } deba@1967: if (!_heap) { deba@1967: local_heap = true; deba@1967: _heap = Traits::createHeap(*_heap_cross_ref); deba@1967: } deba@1967: } deba@1967: deba@1967: public : deba@1967: deba@1975: typedef MinCut Create; deba@1967: deba@1967: deba@1967: /// \brief Constructor. deba@1967: /// deba@1967: ///\param graph the graph the algorithm will run on. deba@1967: ///\param capacity the capacity map used by the algorithm. deba@1975: MinCut(const Graph& graph, const CapacityMap& capacity) deba@1967: : _graph(&graph), deba@1967: _capacity(&capacity), local_capacity(false), deba@1975: _aux_graph(0), local_aux_graph(false), deba@1975: _aux_capacity(0), local_aux_capacity(false), deba@1967: _heap_cross_ref(0), local_heap_cross_ref(false), deba@1967: _heap(0), local_heap(false), deba@1967: _first(0), _last(0), _next(0) {} deba@1967: deba@1967: /// \brief Constructor. deba@1967: /// deba@1967: /// This constructor can be used only when the Traits class deba@1967: /// defines how can we instantiate a local capacity map. deba@1967: /// If the DefNeutralCapacity used the algorithm automatically deba@1967: /// construct the capacity map. deba@1967: /// deba@1967: ///\param graph the graph the algorithm will run on. deba@1975: MinCut(const Graph& graph) deba@1967: : _graph(&graph), deba@1967: _capacity(0), local_capacity(false), deba@1975: _aux_graph(0), local_aux_graph(false), deba@1975: _aux_capacity(0), local_aux_capacity(false), deba@1967: _heap_cross_ref(0), local_heap_cross_ref(false), deba@1967: _heap(0), local_heap(false), deba@1967: _first(0), _last(0), _next(0) {} deba@1967: deba@1967: /// \brief Destructor. deba@1967: /// deba@1967: /// Destructor. deba@1975: ~MinCut() { deba@1967: if (local_heap) delete _heap; deba@1967: if (local_heap_cross_ref) delete _heap_cross_ref; deba@1967: if (_first) delete _first; deba@1967: if (_last) delete _last; deba@1967: if (_next) delete _next; deba@1975: if (local_aux_capacity) delete _aux_capacity; deba@1975: if (local_aux_graph) delete _aux_graph; deba@1967: if (local_capacity) delete _capacity; deba@1967: } deba@1967: deba@1967: /// \brief Sets the heap and the cross reference used by algorithm. deba@1967: /// deba@1967: /// Sets the heap and the cross reference used by algorithm. deba@1967: /// If you don't use this function before calling \ref run(), deba@1967: /// it will allocate one. The destuctor deallocates this deba@1967: /// automatically allocated heap and cross reference, of course. deba@1967: /// \return (*this) deba@1975: MinCut &heap(Heap& heap, HeapCrossRef &crossRef) deba@1967: { deba@1967: if (local_heap_cross_ref) { deba@1967: delete _heap_cross_ref; deba@1967: local_heap_cross_ref=false; deba@1967: } deba@1967: _heap_cross_ref = &crossRef; deba@1967: if (local_heap) { deba@1967: delete _heap; deba@1967: local_heap=false; deba@1967: } deba@1967: _heap = &heap; deba@1967: return *this; deba@1967: } deba@1967: deba@1975: /// \brief Sets the aux graph. deba@1967: /// deba@1975: /// Sets the aux graph used by algorithm. deba@1967: /// If you don't use this function before calling \ref run(), deba@1967: /// it will allocate one. The destuctor deallocates this deba@1967: /// automatically allocated graph, of course. deba@1967: /// \return (*this) deba@1975: MinCut &auxGraph(AuxGraph& aux_graph) deba@1967: { deba@1975: if(local_aux_graph) { deba@1975: delete _aux_graph; deba@1975: local_aux_graph=false; deba@1967: } deba@1975: _aux_graph = &aux_graph; deba@1967: return *this; deba@1967: } deba@1967: deba@1975: /// \brief Sets the aux capacity map. deba@1967: /// deba@1975: /// Sets the aux capacity map used by algorithm. deba@1967: /// If you don't use this function before calling \ref run(), deba@1967: /// it will allocate one. The destuctor deallocates this deba@1967: /// automatically allocated graph, of course. deba@1967: /// \return (*this) deba@1975: MinCut &auxCapacityMap(AuxCapacityMap& aux_capacity_map) deba@1967: { deba@1975: if(local_aux_capacity) { deba@1975: delete _aux_capacity; deba@1975: local_aux_capacity=false; deba@1967: } deba@1975: _aux_capacity = &aux_capacity_map; deba@1967: return *this; deba@1967: } deba@1967: deba@1967: /// \name Execution control deba@1967: /// The simplest way to execute the algorithm is to use deba@1967: /// one of the member functions called \c run(). deba@1967: /// \n deba@1967: /// If you need more control on the execution, deba@1967: /// first you must call \ref init() and then call the start() deba@1967: /// or proper times the processNextPhase() member functions. deba@1967: deba@1967: ///@{ deba@1967: deba@1967: /// \brief Initializes the internal data structures. deba@1967: /// deba@1967: /// Initializes the internal data structures. deba@1967: void init() { deba@1967: create_structures(); deba@1967: _first_node = _last_node = INVALID; deba@1967: _node_num = countNodes(*_graph); deba@1967: } deba@1967: deba@1967: /// \brief Processes the next phase deba@1967: /// deba@1967: /// Processes the next phase in the algorithm. The function deba@1967: /// should be called countNodes(graph) - 1 times to get deba@1975: /// surely the min cut in the graph. The deba@1967: /// deba@1967: ///\return %True when the algorithm finished. deba@1967: bool processNextPhase() { deba@1967: if (_node_num <= 1) return true; deba@1975: using namespace _min_cut_bits; deba@1967: deba@1975: typedef typename AuxGraph::Node Node; deba@1975: typedef typename AuxGraph::NodeIt NodeIt; deba@1975: typedef typename AuxGraph::UEdge UEdge; deba@1975: typedef typename AuxGraph::IncEdgeIt IncEdgeIt; deba@1967: deba@1975: typedef typename MaxCardinalitySearch:: deba@1967: template DefHeap:: deba@1967: template DefCardinalityMap >:: deba@1967: template DefProcessedMap >:: deba@1967: Create MaxCardinalitySearch; deba@1967: deba@1975: MaxCardinalitySearch mcs(*_aux_graph, *_aux_capacity); deba@1975: for (NodeIt it(*_aux_graph); it != INVALID; ++it) { deba@1967: _heap_cross_ref->set(it, Heap::PRE_HEAP); deba@1967: } deba@1967: mcs.heap(*_heap, *_heap_cross_ref); deba@1967: deba@1967: LastTwoMap last_two_nodes(_node_num); deba@1967: mcs.processedMap(last_two_nodes); deba@1967: deba@1967: NullMap cardinality; deba@1967: mcs.cardinalityMap(cardinality); deba@1967: deba@1967: mcs.run(); deba@1967: deba@1975: Node new_node = _aux_graph->addNode(); deba@1967: deba@1975: typename AuxGraph::template NodeMap edges(*_aux_graph, INVALID); deba@1967: deba@1967: Node first_node = last_two_nodes[0]; deba@1967: Node second_node = last_two_nodes[1]; deba@1967: deba@1967: _next->set((*_last)[first_node], (*_first)[second_node]); deba@1967: _first->set(new_node, (*_first)[first_node]); deba@1967: _last->set(new_node, (*_last)[second_node]); deba@1967: deba@1967: Value current_cut = 0; deba@1975: for (IncEdgeIt it(*_aux_graph, first_node); it != INVALID; ++it) { deba@1975: Node node = _aux_graph->runningNode(it); deba@1975: current_cut += (*_aux_capacity)[it]; deba@1967: if (node == second_node) continue; deba@1967: if (edges[node] == INVALID) { deba@1975: edges[node] = _aux_graph->addEdge(new_node, node); deba@1975: (*_aux_capacity)[edges[node]] = (*_aux_capacity)[it]; deba@1967: } else { deba@1975: (*_aux_capacity)[edges[node]] += (*_aux_capacity)[it]; deba@1967: } deba@1967: } deba@1967: deba@1975: if (_first_node == INVALID || current_cut < _min_cut) { deba@1967: _first_node = (*_first)[first_node]; deba@1967: _last_node = (*_last)[first_node]; deba@1975: _min_cut = current_cut; deba@1967: } deba@1967: deba@1975: _aux_graph->erase(first_node); deba@1967: deba@1975: for (IncEdgeIt it(*_aux_graph, second_node); it != INVALID; ++it) { deba@1975: Node node = _aux_graph->runningNode(it); deba@1967: if (edges[node] == INVALID) { deba@1975: edges[node] = _aux_graph->addEdge(new_node, node); deba@1975: (*_aux_capacity)[edges[node]] = (*_aux_capacity)[it]; deba@1967: } else { deba@1975: (*_aux_capacity)[edges[node]] += (*_aux_capacity)[it]; deba@1967: } deba@1967: } deba@1975: _aux_graph->erase(second_node); deba@1967: deba@1967: --_node_num; deba@1967: return _node_num == 1; deba@1967: } deba@1967: deba@1967: /// \brief Executes the algorithm. deba@1967: /// deba@1967: /// Executes the algorithm. deba@1967: /// deba@1967: /// \pre init() must be called deba@1967: void start() { deba@1967: while (!processNextPhase()); deba@1967: } deba@1967: deba@1967: deba@1975: /// \brief Runs %MinCut algorithm. deba@1967: /// deba@1975: /// This method runs the %Min cut algorithm deba@1967: /// deba@1967: /// \note mc.run(s) is just a shortcut of the following code. deba@1967: ///\code deba@1967: /// mc.init(); deba@1967: /// mc.start(); deba@1967: ///\endcode deba@1967: void run() { deba@1967: init(); deba@1967: start(); deba@1967: } deba@1967: deba@1967: ///@} deba@1967: deba@1967: /// \name Query Functions deba@1975: /// The result of the %MinCut algorithm can be obtained using these deba@1967: /// functions.\n deba@1967: /// Before the use of these functions, deba@1967: /// either run() or start() must be called. deba@1967: deba@1967: ///@{ deba@1967: deba@1975: /// \brief Returns the min cut value. deba@1967: /// deba@1975: /// Returns the min cut value if the algorithm finished. deba@1967: /// After the first processNextPhase() it is a value of a deba@1967: /// valid cut in the graph. deba@1967: Value minCut() const { deba@1975: return _min_cut; deba@1967: } deba@1967: deba@1975: /// \brief Returns a min cut in a NodeMap. deba@1967: /// deba@1967: /// It sets the nodes of one of the two partitions to true in deba@1967: /// the given BoolNodeMap. The map contains a valid cut if the alpar@1973: /// map have been set false previously. deba@1967: template deba@1967: Value quickMinCut(NodeMap& nodeMap) const { deba@1967: for (typename Graph::Node it = _first_node; deba@1967: it != _last_node; it = (*_next)[it]) { deba@1967: nodeMap.set(it, true); deba@1967: } deba@1967: nodeMap.set(_last_node, true); deba@1967: return minCut(); deba@1967: } deba@1967: deba@1975: /// \brief Returns a min cut in a NodeMap. deba@1967: /// deba@1967: /// It sets the nodes of one of the two partitions to true and deba@1967: /// the other partition to false. The function first set all of the deba@1967: /// nodes to false and after it call the quickMinCut() member. deba@1967: template deba@1967: Value minCut(NodeMap& nodeMap) const { deba@1967: for (typename Graph::NodeIt it(*_graph); it != INVALID; ++it) { deba@1967: nodeMap.set(it, false); deba@1967: } deba@1967: quickMinCut(nodeMap); deba@1967: return minCut(); deba@1967: } deba@1967: deba@1975: /// \brief Returns a min cut in an EdgeMap. deba@1967: /// deba@1975: /// If an undirected edge is in a min cut then it will be alpar@1973: /// set to true and the others will be set to false in the given map. deba@1967: template deba@1967: Value cutEdges(EdgeMap& edgeMap) const { deba@1967: typename Graph::template NodeMap cut(*_graph, false); deba@1967: quickMinCut(cut); deba@1967: for (typename Graph::EdgeIt it(*_graph); it != INVALID; ++it) { deba@1967: edgeMap.set(it, cut[_graph->source(it)] ^ cut[_graph->target(it)]); deba@1967: } deba@1967: return minCut(); deba@1967: } deba@1967: deba@1967: ///@} deba@1967: deba@1967: }; deba@1967: } deba@1967: deba@1967: #endif