1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/nagamochi_ibaraki.h Tue Oct 31 14:30:54 2006 +0000
1.3 @@ -0,0 +1,1361 @@
1.4 +/* -*- C++ -*-
1.5 + * lemon/min_cut.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_MIN_CUT_H
1.21 +#define LEMON_MIN_CUT_H
1.22 +
1.23 +
1.24 +/// \ingroup topology
1.25 +/// \file
1.26 +/// \brief Maximum cardinality search and min cut in undirected graphs.
1.27 +
1.28 +#include <lemon/list_graph.h>
1.29 +#include <lemon/bin_heap.h>
1.30 +#include <lemon/bucket_heap.h>
1.31 +
1.32 +#include <lemon/bits/invalid.h>
1.33 +#include <lemon/error.h>
1.34 +#include <lemon/maps.h>
1.35 +
1.36 +#include <functional>
1.37 +
1.38 +namespace lemon {
1.39 +
1.40 + namespace _min_cut_bits {
1.41 +
1.42 + template <typename CapacityMap>
1.43 + struct HeapSelector {
1.44 + template <typename Value, typename Ref>
1.45 + struct Selector {
1.46 + typedef BinHeap<Value, Ref, std::greater<Value> > Heap;
1.47 + };
1.48 + };
1.49 +
1.50 + template <typename CapacityKey>
1.51 + struct HeapSelector<ConstMap<CapacityKey, Const<int, 1> > > {
1.52 + template <typename Value, typename Ref>
1.53 + struct Selector {
1.54 + typedef BucketHeap<Ref, false > Heap;
1.55 + };
1.56 + };
1.57 +
1.58 + }
1.59 +
1.60 + /// \brief Default traits class of MaxCardinalitySearch class.
1.61 + ///
1.62 + /// Default traits class of MaxCardinalitySearch class.
1.63 + /// \param Graph Graph type.
1.64 + /// \param CapacityMap Type of length map.
1.65 + template <typename _Graph, typename _CapacityMap>
1.66 + struct MaxCardinalitySearchDefaultTraits {
1.67 + /// The graph type the algorithm runs on.
1.68 + typedef _Graph Graph;
1.69 +
1.70 + /// \brief The type of the map that stores the edge capacities.
1.71 + ///
1.72 + /// The type of the map that stores the edge capacities.
1.73 + /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
1.74 + typedef _CapacityMap CapacityMap;
1.75 +
1.76 + /// \brief The type of the capacity of the edges.
1.77 + typedef typename CapacityMap::Value Value;
1.78 +
1.79 + /// \brief The cross reference type used by heap.
1.80 + ///
1.81 + /// The cross reference type used by heap.
1.82 + /// Usually it is \c Graph::NodeMap<int>.
1.83 + typedef typename Graph::template NodeMap<int> HeapCrossRef;
1.84 +
1.85 + /// \brief Instantiates a HeapCrossRef.
1.86 + ///
1.87 + /// This function instantiates a \ref HeapCrossRef.
1.88 + /// \param graph is the graph, to which we would like to define the
1.89 + /// HeapCrossRef.
1.90 + static HeapCrossRef *createHeapCrossRef(const Graph &graph) {
1.91 + return new HeapCrossRef(graph);
1.92 + }
1.93 +
1.94 + /// \brief The heap type used by MaxCardinalitySearch algorithm.
1.95 + ///
1.96 + /// The heap type used by MaxCardinalitySearch algorithm. It should
1.97 + /// maximalize the priorities. The default heap type is
1.98 + /// the \ref BinHeap, but it is specialized when the
1.99 + /// CapacityMap is ConstMap<Graph::Node, Const<int, 1> >
1.100 + /// to BucketHeap.
1.101 + ///
1.102 + /// \sa MaxCardinalitySearch
1.103 + typedef typename _min_cut_bits
1.104 + ::HeapSelector<CapacityMap>
1.105 + ::template Selector<Value, HeapCrossRef>
1.106 + ::Heap Heap;
1.107 +
1.108 + /// \brief Instantiates a Heap.
1.109 + ///
1.110 + /// This function instantiates a \ref Heap.
1.111 + /// \param crossref The cross reference of the heap.
1.112 + static Heap *createHeap(HeapCrossRef& crossref) {
1.113 + return new Heap(crossref);
1.114 + }
1.115 +
1.116 + /// \brief The type of the map that stores whether a nodes is processed.
1.117 + ///
1.118 + /// The type of the map that stores whether a nodes is processed.
1.119 + /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
1.120 + /// By default it is a NullMap.
1.121 + typedef NullMap<typename Graph::Node, bool> ProcessedMap;
1.122 +
1.123 + /// \brief Instantiates a ProcessedMap.
1.124 + ///
1.125 + /// This function instantiates a \ref ProcessedMap.
1.126 + /// \param graph is the graph, to which
1.127 + /// we would like to define the \ref ProcessedMap
1.128 +#ifdef DOXYGEN
1.129 + static ProcessedMap *createProcessedMap(const Graph &graph)
1.130 +#else
1.131 + static ProcessedMap *createProcessedMap(const Graph &)
1.132 +#endif
1.133 + {
1.134 + return new ProcessedMap();
1.135 + }
1.136 +
1.137 + /// \brief The type of the map that stores the cardinalties of the nodes.
1.138 + ///
1.139 + /// The type of the map that stores the cardinalities of the nodes.
1.140 + /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
1.141 + typedef typename Graph::template NodeMap<Value> CardinalityMap;
1.142 +
1.143 + /// \brief Instantiates a CardinalityMap.
1.144 + ///
1.145 + /// This function instantiates a \ref CardinalityMap.
1.146 + /// \param graph is the graph, to which we would like to define the \ref
1.147 + /// CardinalityMap
1.148 + static CardinalityMap *createCardinalityMap(const Graph &graph) {
1.149 + return new CardinalityMap(graph);
1.150 + }
1.151 +
1.152 + };
1.153 +
1.154 + /// \ingroup topology
1.155 + ///
1.156 + /// \brief Maximum Cardinality Search algorithm class.
1.157 + ///
1.158 + /// This class provides an efficient implementation of Maximum Cardinality
1.159 + /// Search algorithm. The maximum cardinality search chooses first time any
1.160 + /// node of the graph. Then every time it chooses the node which is connected
1.161 + /// to the processed nodes at most in the sum of capacities on the out
1.162 + /// edges. If there is a cut in the graph the algorithm should choose
1.163 + /// again any unprocessed node of the graph. Each node cardinality is
1.164 + /// the sum of capacities on the out edges to the nodes which are processed
1.165 + /// before the given node.
1.166 + ///
1.167 + /// The edge capacities are passed to the algorithm using a
1.168 + /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any
1.169 + /// kind of capacity.
1.170 + ///
1.171 + /// The type of the capacity is determined by the \ref
1.172 + /// concepts::ReadMap::Value "Value" of the capacity map.
1.173 + ///
1.174 + /// It is also possible to change the underlying priority heap.
1.175 + ///
1.176 + ///
1.177 + /// \param _Graph The graph type the algorithm runs on. The default value
1.178 + /// is \ref ListGraph. The value of Graph is not used directly by
1.179 + /// the search algorithm, it is only passed to
1.180 + /// \ref MaxCardinalitySearchDefaultTraits.
1.181 + /// \param _CapacityMap This read-only EdgeMap determines the capacities of
1.182 + /// the edges. It is read once for each edge, so the map may involve in
1.183 + /// relatively time consuming process to compute the edge capacity if
1.184 + /// it is necessary. The default map type is \ref
1.185 + /// concepts::Graph::EdgeMap "Graph::EdgeMap<int>". The value
1.186 + /// of CapacityMap is not used directly by search algorithm, it is only
1.187 + /// passed to \ref MaxCardinalitySearchDefaultTraits.
1.188 + /// \param _Traits Traits class to set various data types used by the
1.189 + /// algorithm. The default traits class is
1.190 + /// \ref MaxCardinalitySearchDefaultTraits
1.191 + /// "MaxCardinalitySearchDefaultTraits<_Graph, _CapacityMap>".
1.192 + /// See \ref MaxCardinalitySearchDefaultTraits
1.193 + /// for the documentation of a MaxCardinalitySearch traits class.
1.194 + ///
1.195 + /// \author Balazs Dezso
1.196 +
1.197 +#ifdef DOXYGEN
1.198 + template <typename _Graph, typename _CapacityMap, typename _Traits>
1.199 +#else
1.200 + template <typename _Graph = ListUGraph,
1.201 + typename _CapacityMap = typename _Graph::template EdgeMap<int>,
1.202 + typename _Traits =
1.203 + MaxCardinalitySearchDefaultTraits<_Graph, _CapacityMap> >
1.204 +#endif
1.205 + class MaxCardinalitySearch {
1.206 + public:
1.207 + /// \brief \ref Exception for uninitialized parameters.
1.208 + ///
1.209 + /// This error represents problems in the initialization
1.210 + /// of the parameters of the algorithms.
1.211 + class UninitializedParameter : public lemon::UninitializedParameter {
1.212 + public:
1.213 + virtual const char* what() const throw() {
1.214 + return "lemon::MaxCardinalitySearch::UninitializedParameter";
1.215 + }
1.216 + };
1.217 +
1.218 + typedef _Traits Traits;
1.219 + ///The type of the underlying graph.
1.220 + typedef typename Traits::Graph Graph;
1.221 +
1.222 + ///The type of the capacity of the edges.
1.223 + typedef typename Traits::CapacityMap::Value Value;
1.224 + ///The type of the map that stores the edge capacities.
1.225 + typedef typename Traits::CapacityMap CapacityMap;
1.226 + ///The type of the map indicating if a node is processed.
1.227 + typedef typename Traits::ProcessedMap ProcessedMap;
1.228 + ///The type of the map that stores the cardinalities of the nodes.
1.229 + typedef typename Traits::CardinalityMap CardinalityMap;
1.230 + ///The cross reference type used for the current heap.
1.231 + typedef typename Traits::HeapCrossRef HeapCrossRef;
1.232 + ///The heap type used by the algorithm. It maximize the priorities.
1.233 + typedef typename Traits::Heap Heap;
1.234 + private:
1.235 + /// Pointer to the underlying graph.
1.236 + const Graph *_graph;
1.237 + /// Pointer to the capacity map
1.238 + const CapacityMap *_capacity;
1.239 + ///Pointer to the map of cardinality.
1.240 + CardinalityMap *_cardinality;
1.241 + ///Indicates if \ref _cardinality is locally allocated (\c true) or not.
1.242 + bool local_cardinality;
1.243 + ///Pointer to the map of processed status of the nodes.
1.244 + ProcessedMap *_processed;
1.245 + ///Indicates if \ref _processed is locally allocated (\c true) or not.
1.246 + bool local_processed;
1.247 + ///Pointer to the heap cross references.
1.248 + HeapCrossRef *_heap_cross_ref;
1.249 + ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
1.250 + bool local_heap_cross_ref;
1.251 + ///Pointer to the heap.
1.252 + Heap *_heap;
1.253 + ///Indicates if \ref _heap is locally allocated (\c true) or not.
1.254 + bool local_heap;
1.255 +
1.256 + public :
1.257 +
1.258 + typedef MaxCardinalitySearch Create;
1.259 +
1.260 + ///\name Named template parameters
1.261 +
1.262 + ///@{
1.263 +
1.264 + template <class T>
1.265 + struct DefCardinalityMapTraits : public Traits {
1.266 + typedef T CardinalityMap;
1.267 + static CardinalityMap *createCardinalityMap(const Graph &)
1.268 + {
1.269 + throw UninitializedParameter();
1.270 + }
1.271 + };
1.272 + /// \brief \ref named-templ-param "Named parameter" for setting
1.273 + /// CardinalityMap type
1.274 + ///
1.275 + /// \ref named-templ-param "Named parameter" for setting CardinalityMap
1.276 + /// type
1.277 + template <class T>
1.278 + struct DefCardinalityMap
1.279 + : public MaxCardinalitySearch<Graph, CapacityMap,
1.280 + DefCardinalityMapTraits<T> > {
1.281 + typedef MaxCardinalitySearch<Graph, CapacityMap,
1.282 + DefCardinalityMapTraits<T> > Create;
1.283 + };
1.284 +
1.285 + template <class T>
1.286 + struct DefProcessedMapTraits : public Traits {
1.287 + typedef T ProcessedMap;
1.288 + static ProcessedMap *createProcessedMap(const Graph &) {
1.289 + throw UninitializedParameter();
1.290 + }
1.291 + };
1.292 + /// \brief \ref named-templ-param "Named parameter" for setting
1.293 + /// ProcessedMap type
1.294 + ///
1.295 + /// \ref named-templ-param "Named parameter" for setting ProcessedMap type
1.296 + ///
1.297 + template <class T>
1.298 + struct DefProcessedMap
1.299 + : public MaxCardinalitySearch<Graph, CapacityMap,
1.300 + DefProcessedMapTraits<T> > {
1.301 + typedef MaxCardinalitySearch<Graph, CapacityMap,
1.302 + DefProcessedMapTraits<T> > Create;
1.303 + };
1.304 +
1.305 + template <class H, class CR>
1.306 + struct DefHeapTraits : public Traits {
1.307 + typedef CR HeapCrossRef;
1.308 + typedef H Heap;
1.309 + static HeapCrossRef *createHeapCrossRef(const Graph &) {
1.310 + throw UninitializedParameter();
1.311 + }
1.312 + static Heap *createHeap(HeapCrossRef &) {
1.313 + throw UninitializedParameter();
1.314 + }
1.315 + };
1.316 + /// \brief \ref named-templ-param "Named parameter" for setting heap
1.317 + /// and cross reference type
1.318 + ///
1.319 + /// \ref named-templ-param "Named parameter" for setting heap and cross
1.320 + /// reference type
1.321 + template <class H, class CR = typename Graph::template NodeMap<int> >
1.322 + struct DefHeap
1.323 + : public MaxCardinalitySearch<Graph, CapacityMap,
1.324 + DefHeapTraits<H, CR> > {
1.325 + typedef MaxCardinalitySearch< Graph, CapacityMap,
1.326 + DefHeapTraits<H, CR> > Create;
1.327 + };
1.328 +
1.329 + template <class H, class CR>
1.330 + struct DefStandardHeapTraits : public Traits {
1.331 + typedef CR HeapCrossRef;
1.332 + typedef H Heap;
1.333 + static HeapCrossRef *createHeapCrossRef(const Graph &graph) {
1.334 + return new HeapCrossRef(graph);
1.335 + }
1.336 + static Heap *createHeap(HeapCrossRef &crossref) {
1.337 + return new Heap(crossref);
1.338 + }
1.339 + };
1.340 +
1.341 + /// \brief \ref named-templ-param "Named parameter" for setting heap and
1.342 + /// cross reference type with automatic allocation
1.343 + ///
1.344 + /// \ref named-templ-param "Named parameter" for setting heap and cross
1.345 + /// reference type. It can allocate the heap and the cross reference
1.346 + /// object if the cross reference's constructor waits for the graph as
1.347 + /// parameter and the heap's constructor waits for the cross reference.
1.348 + template <class H, class CR = typename Graph::template NodeMap<int> >
1.349 + struct DefStandardHeap
1.350 + : public MaxCardinalitySearch<Graph, CapacityMap,
1.351 + DefStandardHeapTraits<H, CR> > {
1.352 + typedef MaxCardinalitySearch<Graph, CapacityMap,
1.353 + DefStandardHeapTraits<H, CR> >
1.354 + Create;
1.355 + };
1.356 +
1.357 + ///@}
1.358 +
1.359 +
1.360 + protected:
1.361 +
1.362 + MaxCardinalitySearch() {}
1.363 +
1.364 + public:
1.365 +
1.366 + /// \brief Constructor.
1.367 + ///
1.368 + ///\param graph the graph the algorithm will run on.
1.369 + ///\param capacity the capacity map used by the algorithm.
1.370 + MaxCardinalitySearch(const Graph& graph, const CapacityMap& capacity) :
1.371 + _graph(&graph), _capacity(&capacity),
1.372 + _cardinality(0), local_cardinality(false),
1.373 + _processed(0), local_processed(false),
1.374 + _heap_cross_ref(0), local_heap_cross_ref(false),
1.375 + _heap(0), local_heap(false)
1.376 + { }
1.377 +
1.378 + /// \brief Destructor.
1.379 + ~MaxCardinalitySearch() {
1.380 + if(local_cardinality) delete _cardinality;
1.381 + if(local_processed) delete _processed;
1.382 + if(local_heap_cross_ref) delete _heap_cross_ref;
1.383 + if(local_heap) delete _heap;
1.384 + }
1.385 +
1.386 + /// \brief Sets the capacity map.
1.387 + ///
1.388 + /// Sets the capacity map.
1.389 + /// \return <tt> (*this) </tt>
1.390 + MaxCardinalitySearch &capacityMap(const CapacityMap &m) {
1.391 + _capacity = &m;
1.392 + return *this;
1.393 + }
1.394 +
1.395 + /// \brief Sets the map storing the cardinalities calculated by the
1.396 + /// algorithm.
1.397 + ///
1.398 + /// Sets the map storing the cardinalities calculated by the algorithm.
1.399 + /// If you don't use this function before calling \ref run(),
1.400 + /// it will allocate one. The destuctor deallocates this
1.401 + /// automatically allocated map, of course.
1.402 + /// \return <tt> (*this) </tt>
1.403 + MaxCardinalitySearch &cardinalityMap(CardinalityMap &m) {
1.404 + if(local_cardinality) {
1.405 + delete _cardinality;
1.406 + local_cardinality=false;
1.407 + }
1.408 + _cardinality = &m;
1.409 + return *this;
1.410 + }
1.411 +
1.412 + /// \brief Sets the map storing the processed nodes.
1.413 + ///
1.414 + /// Sets the map storing the processed nodes.
1.415 + /// If you don't use this function before calling \ref run(),
1.416 + /// it will allocate one. The destuctor deallocates this
1.417 + /// automatically allocated map, of course.
1.418 + /// \return <tt> (*this) </tt>
1.419 + MaxCardinalitySearch &processedMap(ProcessedMap &m)
1.420 + {
1.421 + if(local_processed) {
1.422 + delete _processed;
1.423 + local_processed=false;
1.424 + }
1.425 + _processed = &m;
1.426 + return *this;
1.427 + }
1.428 +
1.429 + /// \brief Sets the heap and the cross reference used by algorithm.
1.430 + ///
1.431 + /// Sets the heap and the cross reference used by algorithm.
1.432 + /// If you don't use this function before calling \ref run(),
1.433 + /// it will allocate one. The destuctor deallocates this
1.434 + /// automatically allocated map, of course.
1.435 + /// \return <tt> (*this) </tt>
1.436 + MaxCardinalitySearch &heap(Heap& heap, HeapCrossRef &crossRef) {
1.437 + if(local_heap_cross_ref) {
1.438 + delete _heap_cross_ref;
1.439 + local_heap_cross_ref = false;
1.440 + }
1.441 + _heap_cross_ref = &crossRef;
1.442 + if(local_heap) {
1.443 + delete _heap;
1.444 + local_heap = false;
1.445 + }
1.446 + _heap = &heap;
1.447 + return *this;
1.448 + }
1.449 +
1.450 + private:
1.451 +
1.452 + typedef typename Graph::Node Node;
1.453 + typedef typename Graph::NodeIt NodeIt;
1.454 + typedef typename Graph::Edge Edge;
1.455 + typedef typename Graph::InEdgeIt InEdgeIt;
1.456 +
1.457 + void create_maps() {
1.458 + if(!_cardinality) {
1.459 + local_cardinality = true;
1.460 + _cardinality = Traits::createCardinalityMap(*_graph);
1.461 + }
1.462 + if(!_processed) {
1.463 + local_processed = true;
1.464 + _processed = Traits::createProcessedMap(*_graph);
1.465 + }
1.466 + if (!_heap_cross_ref) {
1.467 + local_heap_cross_ref = true;
1.468 + _heap_cross_ref = Traits::createHeapCrossRef(*_graph);
1.469 + }
1.470 + if (!_heap) {
1.471 + local_heap = true;
1.472 + _heap = Traits::createHeap(*_heap_cross_ref);
1.473 + }
1.474 + }
1.475 +
1.476 + void finalizeNodeData(Node node, Value capacity) {
1.477 + _processed->set(node, true);
1.478 + _cardinality->set(node, capacity);
1.479 + }
1.480 +
1.481 + public:
1.482 + /// \name Execution control
1.483 + /// The simplest way to execute the algorithm is to use
1.484 + /// one of the member functions called \c run(...).
1.485 + /// \n
1.486 + /// If you need more control on the execution,
1.487 + /// first you must call \ref init(), then you can add several source nodes
1.488 + /// with \ref addSource().
1.489 + /// Finally \ref start() will perform the actual path
1.490 + /// computation.
1.491 +
1.492 + ///@{
1.493 +
1.494 + /// \brief Initializes the internal data structures.
1.495 + ///
1.496 + /// Initializes the internal data structures.
1.497 + void init() {
1.498 + create_maps();
1.499 + _heap->clear();
1.500 + for (NodeIt it(*_graph) ; it != INVALID ; ++it) {
1.501 + _processed->set(it, false);
1.502 + _heap_cross_ref->set(it, Heap::PRE_HEAP);
1.503 + }
1.504 + }
1.505 +
1.506 + /// \brief Adds a new source node.
1.507 + ///
1.508 + /// Adds a new source node to the priority heap.
1.509 + ///
1.510 + /// It checks if the node has not yet been added to the heap.
1.511 + void addSource(Node source, Value capacity = 0) {
1.512 + if(_heap->state(source) == Heap::PRE_HEAP) {
1.513 + _heap->push(source, capacity);
1.514 + }
1.515 + }
1.516 +
1.517 + /// \brief Processes the next node in the priority heap
1.518 + ///
1.519 + /// Processes the next node in the priority heap.
1.520 + ///
1.521 + /// \return The processed node.
1.522 + ///
1.523 + /// \warning The priority heap must not be empty!
1.524 + Node processNextNode() {
1.525 + Node node = _heap->top();
1.526 + finalizeNodeData(node, _heap->prio());
1.527 + _heap->pop();
1.528 +
1.529 + for (InEdgeIt it(*_graph, node); it != INVALID; ++it) {
1.530 + Node source = _graph->source(it);
1.531 + switch (_heap->state(source)) {
1.532 + case Heap::PRE_HEAP:
1.533 + _heap->push(source, (*_capacity)[it]);
1.534 + break;
1.535 + case Heap::IN_HEAP:
1.536 + _heap->decrease(source, (*_heap)[source] + (*_capacity)[it]);
1.537 + break;
1.538 + case Heap::POST_HEAP:
1.539 + break;
1.540 + }
1.541 + }
1.542 + return node;
1.543 + }
1.544 +
1.545 + /// \brief Next node to be processed.
1.546 + ///
1.547 + /// Next node to be processed.
1.548 + ///
1.549 + /// \return The next node to be processed or INVALID if the
1.550 + /// priority heap is empty.
1.551 + Node nextNode() {
1.552 + return _heap->empty() ? _heap->top() : INVALID;
1.553 + }
1.554 +
1.555 + /// \brief Returns \c false if there are nodes
1.556 + /// to be processed in the priority heap
1.557 + ///
1.558 + /// Returns \c false if there are nodes
1.559 + /// to be processed in the priority heap
1.560 + bool emptyQueue() { return _heap->empty(); }
1.561 + /// \brief Returns the number of the nodes to be processed
1.562 + /// in the priority heap
1.563 + ///
1.564 + /// Returns the number of the nodes to be processed in the priority heap
1.565 + int queueSize() { return _heap->size(); }
1.566 +
1.567 + /// \brief Executes the algorithm.
1.568 + ///
1.569 + /// Executes the algorithm.
1.570 + ///
1.571 + ///\pre init() must be called and at least one node should be added
1.572 + /// with addSource() before using this function.
1.573 + ///
1.574 + /// This method runs the Maximum Cardinality Search algorithm from the
1.575 + /// source node(s).
1.576 + void start() {
1.577 + while ( !_heap->empty() ) processNextNode();
1.578 + }
1.579 +
1.580 + /// \brief Executes the algorithm until \c dest is reached.
1.581 + ///
1.582 + /// Executes the algorithm until \c dest is reached.
1.583 + ///
1.584 + /// \pre init() must be called and at least one node should be added
1.585 + /// with addSource() before using this function.
1.586 + ///
1.587 + /// This method runs the %MaxCardinalitySearch algorithm from the source
1.588 + /// nodes.
1.589 + void start(Node dest) {
1.590 + while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
1.591 + if ( !_heap->empty() ) finalizeNodeData(_heap->top(), _heap->prio());
1.592 + }
1.593 +
1.594 + /// \brief Executes the algorithm until a condition is met.
1.595 + ///
1.596 + /// Executes the algorithm until a condition is met.
1.597 + ///
1.598 + /// \pre init() must be called and at least one node should be added
1.599 + /// with addSource() before using this function.
1.600 + ///
1.601 + /// \param nm must be a bool (or convertible) node map. The algorithm
1.602 + /// will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
1.603 + template <typename NodeBoolMap>
1.604 + void start(const NodeBoolMap &nm) {
1.605 + while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
1.606 + if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
1.607 + }
1.608 +
1.609 + /// \brief Runs the maximal cardinality search algorithm from node \c s.
1.610 + ///
1.611 + /// This method runs the %MaxCardinalitySearch algorithm from a root
1.612 + /// node \c s.
1.613 + ///
1.614 + ///\note d.run(s) is just a shortcut of the following code.
1.615 + ///\code
1.616 + /// d.init();
1.617 + /// d.addSource(s);
1.618 + /// d.start();
1.619 + ///\endcode
1.620 + void run(Node s) {
1.621 + init();
1.622 + addSource(s);
1.623 + start();
1.624 + }
1.625 +
1.626 + /// \brief Runs the maximal cardinality search algorithm for the
1.627 + /// whole graph.
1.628 + ///
1.629 + /// This method runs the %MaxCardinalitySearch algorithm from all
1.630 + /// unprocessed node of the graph.
1.631 + ///
1.632 + ///\note d.run(s) is just a shortcut of the following code.
1.633 + ///\code
1.634 + /// d.init();
1.635 + /// for (NodeIt it(graph); it != INVALID; ++it) {
1.636 + /// if (!d.reached(it)) {
1.637 + /// d.addSource(s);
1.638 + /// d.start();
1.639 + /// }
1.640 + /// }
1.641 + ///\endcode
1.642 + void run() {
1.643 + init();
1.644 + for (NodeIt it(*_graph); it != INVALID; ++it) {
1.645 + if (!reached(it)) {
1.646 + addSource(it);
1.647 + start();
1.648 + }
1.649 + }
1.650 + }
1.651 +
1.652 + ///@}
1.653 +
1.654 + /// \name Query Functions
1.655 + /// The result of the maximum cardinality search algorithm can be
1.656 + /// obtained using these functions.
1.657 + /// \n
1.658 + /// Before the use of these functions, either run() or start() must be
1.659 + /// called.
1.660 +
1.661 + ///@{
1.662 +
1.663 + /// \brief The cardinality of a node.
1.664 + ///
1.665 + /// Returns the cardinality of a node.
1.666 + /// \pre \ref run() must be called before using this function.
1.667 + /// \warning If node \c v in unreachable from the root the return value
1.668 + /// of this funcion is undefined.
1.669 + Value cardinality(Node node) const { return (*_cardinality)[node]; }
1.670 +
1.671 + /// \brief Returns a reference to the NodeMap of cardinalities.
1.672 + ///
1.673 + /// Returns a reference to the NodeMap of cardinalities. \pre \ref run()
1.674 + /// must be called before using this function.
1.675 + const CardinalityMap &cardinalityMap() const { return *_cardinality;}
1.676 +
1.677 + /// \brief Checks if a node is reachable from the root.
1.678 + ///
1.679 + /// Returns \c true if \c v is reachable from the root.
1.680 + /// \warning The source nodes are inditated as unreached.
1.681 + /// \pre \ref run() must be called before using this function.
1.682 + bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
1.683 +
1.684 + /// \brief Checks if a node is processed.
1.685 + ///
1.686 + /// Returns \c true if \c v is processed, i.e. the shortest
1.687 + /// path to \c v has already found.
1.688 + /// \pre \ref run() must be called before using this function.
1.689 + bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
1.690 +
1.691 + ///@}
1.692 + };
1.693 +
1.694 + /// \brief Default traits class of NagamochiIbaraki class.
1.695 + ///
1.696 + /// Default traits class of NagamochiIbaraki class.
1.697 + /// \param Graph Graph type.
1.698 + /// \param CapacityMap Type of length map.
1.699 + template <typename _Graph, typename _CapacityMap>
1.700 + struct NagamochiIbarakiDefaultTraits {
1.701 + /// \brief The type of the capacity of the edges.
1.702 + typedef typename _CapacityMap::Value Value;
1.703 +
1.704 + /// The graph type the algorithm runs on.
1.705 + typedef _Graph Graph;
1.706 +
1.707 + /// The AuxGraph type which is an Graph
1.708 + typedef ListUGraph AuxGraph;
1.709 +
1.710 + /// \brief Instantiates a AuxGraph.
1.711 + ///
1.712 + /// This function instantiates a \ref AuxGraph.
1.713 + static AuxGraph *createAuxGraph() {
1.714 + return new AuxGraph();
1.715 + }
1.716 +
1.717 + /// \brief The type of the map that stores the edge capacities.
1.718 + ///
1.719 + /// The type of the map that stores the edge capacities.
1.720 + /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
1.721 + typedef _CapacityMap CapacityMap;
1.722 +
1.723 + /// \brief Instantiates a CapacityMap.
1.724 + ///
1.725 + /// This function instantiates a \ref CapacityMap.
1.726 +#ifdef DOXYGEN
1.727 + static CapacityMap *createCapacityMap(const Graph& graph)
1.728 +#else
1.729 + static CapacityMap *createCapacityMap(const Graph&)
1.730 +#endif
1.731 + {
1.732 + throw UninitializedParameter();
1.733 + }
1.734 +
1.735 + /// \brief The AuxCapacityMap type
1.736 + ///
1.737 + /// The type of the map that stores the auxing edge capacities.
1.738 + typedef AuxGraph::UEdgeMap<Value> AuxCapacityMap;
1.739 +
1.740 + /// \brief Instantiates a AuxCapacityMap.
1.741 + ///
1.742 + /// This function instantiates a \ref AuxCapacityMap.
1.743 + static AuxCapacityMap *createAuxCapacityMap(const AuxGraph& graph) {
1.744 + return new AuxCapacityMap(graph);
1.745 + }
1.746 +
1.747 + /// \brief The cross reference type used by heap.
1.748 + ///
1.749 + /// The cross reference type used by heap.
1.750 + /// Usually it is \c Graph::NodeMap<int>.
1.751 + typedef AuxGraph::NodeMap<int> HeapCrossRef;
1.752 +
1.753 + /// \brief Instantiates a HeapCrossRef.
1.754 + ///
1.755 + /// This function instantiates a \ref HeapCrossRef.
1.756 + /// \param graph is the graph, to which we would like to define the
1.757 + /// HeapCrossRef.
1.758 + static HeapCrossRef *createHeapCrossRef(const AuxGraph &graph) {
1.759 + return new HeapCrossRef(graph);
1.760 + }
1.761 +
1.762 + /// \brief The heap type used by NagamochiIbaraki algorithm.
1.763 + ///
1.764 + /// The heap type used by NagamochiIbaraki algorithm. It should
1.765 + /// maximalize the priorities and the heap's key type is
1.766 + /// the aux graph's node.
1.767 + ///
1.768 + /// \sa BinHeap
1.769 + /// \sa NagamochiIbaraki
1.770 + typedef typename _min_cut_bits
1.771 + ::HeapSelector<CapacityMap>
1.772 + ::template Selector<Value, HeapCrossRef>
1.773 + ::Heap Heap;
1.774 +
1.775 + /// \brief Instantiates a Heap.
1.776 + ///
1.777 + /// This function instantiates a \ref Heap.
1.778 + /// \param crossref The cross reference of the heap.
1.779 + static Heap *createHeap(HeapCrossRef& crossref) {
1.780 + return new Heap(crossref);
1.781 + }
1.782 +
1.783 + /// \brief Map from the AuxGraph's node type to the Graph's node type.
1.784 + ///
1.785 + /// Map from the AuxGraph's node type to the Graph's node type.
1.786 + typedef typename AuxGraph
1.787 + ::template NodeMap<typename Graph::Node> NodeRefMap;
1.788 +
1.789 + /// \brief Instantiates a NodeRefMap.
1.790 + ///
1.791 + /// This function instantiates a \ref NodeRefMap.
1.792 + static NodeRefMap *createNodeRefMap(const AuxGraph& graph) {
1.793 + return new NodeRefMap(graph);
1.794 + }
1.795 +
1.796 + /// \brief Map from the Graph's node type to the Graph's node type.
1.797 + ///
1.798 + /// Map from the Graph's node type to the Graph's node type.
1.799 + typedef typename Graph
1.800 + ::template NodeMap<typename Graph::Node> ListRefMap;
1.801 +
1.802 + /// \brief Instantiates a ListRefMap.
1.803 + ///
1.804 + /// This function instantiates a \ref ListRefMap.
1.805 + static ListRefMap *createListRefMap(const Graph& graph) {
1.806 + return new ListRefMap(graph);
1.807 + }
1.808 +
1.809 +
1.810 + };
1.811 +
1.812 + namespace _min_cut_bits {
1.813 + template <typename _Key>
1.814 + class LastTwoMap {
1.815 + public:
1.816 + typedef _Key Key;
1.817 + typedef bool Value;
1.818 +
1.819 + LastTwoMap(int _num) : num(_num) {}
1.820 + void set(const Key& key, bool val) {
1.821 + if (!val) return;
1.822 + --num;
1.823 + if (num > 1) return;
1.824 + keys[num] = key;
1.825 + }
1.826 +
1.827 + Key operator[](int index) const { return keys[index]; }
1.828 + private:
1.829 + Key keys[2];
1.830 + int num;
1.831 + };
1.832 + }
1.833 +
1.834 + /// \ingroup topology
1.835 + ///
1.836 + /// \brief Calculates the minimum cut in an undirected graph.
1.837 + ///
1.838 + /// Calculates the minimum cut in an undirected graph with the
1.839 + /// Nagamochi-Ibaraki algorithm. The algorithm separates the graph's
1.840 + /// nodes into two partitions with the minimum sum of edge capacities
1.841 + /// between the two partitions. The algorithm can be used to test
1.842 + /// the network reliability specifically to test how many links have
1.843 + /// to be destroyed in the network to split it at least two
1.844 + /// distinict subnetwork.
1.845 + ///
1.846 + /// The complexity of the algorithm is \f$ O(ne\log(n)) \f$ but with
1.847 + /// Fibonacci heap it can be decreased to \f$ O(ne+n^2\log(n))
1.848 + /// \f$. When capacity map is neutral then it uses BucketHeap which
1.849 + /// results \f$ O(ne) \f$ time complexity.
1.850 +#ifdef DOXYGEN
1.851 + template <typename _Graph, typename _CapacityMap, typename _Traits>
1.852 +#else
1.853 + template <typename _Graph = ListUGraph,
1.854 + typename _CapacityMap = typename _Graph::template UEdgeMap<int>,
1.855 + typename _Traits
1.856 + = NagamochiIbarakiDefaultTraits<_Graph, _CapacityMap> >
1.857 +#endif
1.858 + class NagamochiIbaraki {
1.859 + public:
1.860 + /// \brief \ref Exception for uninitialized parameters.
1.861 + ///
1.862 + /// This error represents problems in the initialization
1.863 + /// of the parameters of the algorithms.
1.864 + class UninitializedParameter : public lemon::UninitializedParameter {
1.865 + public:
1.866 + virtual const char* what() const throw() {
1.867 + return "lemon::NagamochiIbaraki::UninitializedParameter";
1.868 + }
1.869 + };
1.870 +
1.871 +
1.872 + private:
1.873 +
1.874 + typedef _Traits Traits;
1.875 + /// The type of the underlying graph.
1.876 + typedef typename Traits::Graph Graph;
1.877 +
1.878 + /// The type of the capacity of the edges.
1.879 + typedef typename Traits::CapacityMap::Value Value;
1.880 + /// The type of the map that stores the edge capacities.
1.881 + typedef typename Traits::CapacityMap CapacityMap;
1.882 + /// The type of the aux graph
1.883 + typedef typename Traits::AuxGraph AuxGraph;
1.884 + /// The type of the aux capacity map
1.885 + typedef typename Traits::AuxCapacityMap AuxCapacityMap;
1.886 + /// The cross reference type used for the current heap.
1.887 + typedef typename Traits::HeapCrossRef HeapCrossRef;
1.888 + /// The heap type used by the max cardinality algorithm.
1.889 + typedef typename Traits::Heap Heap;
1.890 + /// The node refrefernces between the original and aux graph type.
1.891 + typedef typename Traits::NodeRefMap NodeRefMap;
1.892 + /// The list node refrefernces in the original graph type.
1.893 + typedef typename Traits::ListRefMap ListRefMap;
1.894 +
1.895 + public:
1.896 +
1.897 + ///\name Named template parameters
1.898 +
1.899 + ///@{
1.900 +
1.901 + struct DefNeutralCapacityTraits : public Traits {
1.902 + typedef ConstMap<typename Graph::UEdge, Const<int, 1> > CapacityMap;
1.903 + static CapacityMap *createCapacityMap(const Graph&) {
1.904 + return new CapacityMap();
1.905 + }
1.906 + };
1.907 + /// \brief \ref named-templ-param "Named parameter" for setting
1.908 + /// the capacity type to constMap<UEdge, int, 1>()
1.909 + ///
1.910 + /// \ref named-templ-param "Named parameter" for setting
1.911 + /// the capacity type to constMap<UEdge, int, 1>()
1.912 + struct DefNeutralCapacity
1.913 + : public NagamochiIbaraki<Graph, CapacityMap,
1.914 + DefNeutralCapacityTraits> {
1.915 + typedef NagamochiIbaraki<Graph, CapacityMap,
1.916 + DefNeutralCapacityTraits> Create;
1.917 + };
1.918 +
1.919 +
1.920 + template <class H, class CR>
1.921 + struct DefHeapTraits : public Traits {
1.922 + typedef CR HeapCrossRef;
1.923 + typedef H Heap;
1.924 + static HeapCrossRef *createHeapCrossRef(const AuxGraph &) {
1.925 + throw UninitializedParameter();
1.926 + }
1.927 + static Heap *createHeap(HeapCrossRef &) {
1.928 + throw UninitializedParameter();
1.929 + }
1.930 + };
1.931 + /// \brief \ref named-templ-param "Named parameter" for setting heap
1.932 + /// and cross reference type
1.933 + ///
1.934 + /// \ref named-templ-param "Named parameter" for setting heap and cross
1.935 + /// reference type
1.936 + template <class H, class CR = typename Graph::template NodeMap<int> >
1.937 + struct DefHeap
1.938 + : public NagamochiIbaraki<Graph, CapacityMap,
1.939 + DefHeapTraits<H, CR> > {
1.940 + typedef NagamochiIbaraki< Graph, CapacityMap,
1.941 + DefHeapTraits<H, CR> > Create;
1.942 + };
1.943 +
1.944 + template <class H, class CR>
1.945 + struct DefStandardHeapTraits : public Traits {
1.946 + typedef CR HeapCrossRef;
1.947 + typedef H Heap;
1.948 + static HeapCrossRef *createHeapCrossRef(const AuxGraph &graph) {
1.949 + return new HeapCrossRef(graph);
1.950 + }
1.951 + static Heap *createHeap(HeapCrossRef &crossref) {
1.952 + return new Heap(crossref);
1.953 + }
1.954 + };
1.955 +
1.956 + /// \brief \ref named-templ-param "Named parameter" for setting heap and
1.957 + /// cross reference type with automatic allocation
1.958 + ///
1.959 + /// \ref named-templ-param "Named parameter" for setting heap and cross
1.960 + /// reference type. It can allocate the heap and the cross reference
1.961 + /// object if the cross reference's constructor waits for the graph as
1.962 + /// parameter and the heap's constructor waits for the cross reference.
1.963 + template <class H, class CR = typename Graph::template NodeMap<int> >
1.964 + struct DefStandardHeap
1.965 + : public NagamochiIbaraki<Graph, CapacityMap,
1.966 + DefStandardHeapTraits<H, CR> > {
1.967 + typedef NagamochiIbaraki<Graph, CapacityMap,
1.968 + DefStandardHeapTraits<H, CR> >
1.969 + Create;
1.970 + };
1.971 +
1.972 + ///@}
1.973 +
1.974 +
1.975 + private:
1.976 + /// Pointer to the underlying graph.
1.977 + const Graph *_graph;
1.978 + /// Pointer to the capacity map
1.979 + const CapacityMap *_capacity;
1.980 + /// \brief Indicates if \ref _capacity is locally allocated
1.981 + /// (\c true) or not.
1.982 + bool local_capacity;
1.983 +
1.984 + /// Pointer to the aux graph.
1.985 + AuxGraph *_aux_graph;
1.986 + /// \brief Indicates if \ref _aux_graph is locally allocated
1.987 + /// (\c true) or not.
1.988 + bool local_aux_graph;
1.989 + /// Pointer to the aux capacity map
1.990 + AuxCapacityMap *_aux_capacity;
1.991 + /// \brief Indicates if \ref _aux_capacity is locally allocated
1.992 + /// (\c true) or not.
1.993 + bool local_aux_capacity;
1.994 + /// Pointer to the heap cross references.
1.995 + HeapCrossRef *_heap_cross_ref;
1.996 + /// \brief Indicates if \ref _heap_cross_ref is locally allocated
1.997 + /// (\c true) or not.
1.998 + bool local_heap_cross_ref;
1.999 + /// Pointer to the heap.
1.1000 + Heap *_heap;
1.1001 + /// Indicates if \ref _heap is locally allocated (\c true) or not.
1.1002 + bool local_heap;
1.1003 +
1.1004 + /// The min cut value.
1.1005 + Value _min_cut;
1.1006 + /// The number of the nodes of the aux graph.
1.1007 + int _node_num;
1.1008 + /// The first and last node of the min cut in the next list;
1.1009 + typename Graph::Node _first_node, _last_node;
1.1010 +
1.1011 + /// \brief The first and last element in the list associated
1.1012 + /// to the aux graph node.
1.1013 + NodeRefMap *_first, *_last;
1.1014 + /// \brief The next node in the node lists.
1.1015 + ListRefMap *_next;
1.1016 +
1.1017 + void create_structures() {
1.1018 + if (!_capacity) {
1.1019 + local_capacity = true;
1.1020 + _capacity = Traits::createCapacityMap(*_graph);
1.1021 + }
1.1022 + if(!_aux_graph) {
1.1023 + local_aux_graph = true;
1.1024 + _aux_graph = Traits::createAuxGraph();
1.1025 + }
1.1026 + if(!_aux_capacity) {
1.1027 + local_aux_capacity = true;
1.1028 + _aux_capacity = Traits::createAuxCapacityMap(*_aux_graph);
1.1029 + }
1.1030 +
1.1031 + _first = Traits::createNodeRefMap(*_aux_graph);
1.1032 + _last = Traits::createNodeRefMap(*_aux_graph);
1.1033 +
1.1034 + _next = Traits::createListRefMap(*_graph);
1.1035 +
1.1036 + typename Graph::template NodeMap<typename AuxGraph::Node> ref(*_graph);
1.1037 +
1.1038 + for (typename Graph::NodeIt it(*_graph); it != INVALID; ++it) {
1.1039 + _next->set(it, INVALID);
1.1040 + typename AuxGraph::Node node = _aux_graph->addNode();
1.1041 + _first->set(node, it);
1.1042 + _last->set(node, it);
1.1043 + ref.set(it, node);
1.1044 + }
1.1045 +
1.1046 + for (typename Graph::UEdgeIt it(*_graph); it != INVALID; ++it) {
1.1047 + if (_graph->source(it) == _graph->target(it)) continue;
1.1048 + typename AuxGraph::UEdge uedge =
1.1049 + _aux_graph->addEdge(ref[_graph->source(it)],
1.1050 + ref[_graph->target(it)]);
1.1051 + _aux_capacity->set(uedge, (*_capacity)[it]);
1.1052 +
1.1053 + }
1.1054 +
1.1055 + if (!_heap_cross_ref) {
1.1056 + local_heap_cross_ref = true;
1.1057 + _heap_cross_ref = Traits::createHeapCrossRef(*_aux_graph);
1.1058 + }
1.1059 + if (!_heap) {
1.1060 + local_heap = true;
1.1061 + _heap = Traits::createHeap(*_heap_cross_ref);
1.1062 + }
1.1063 + }
1.1064 +
1.1065 + public :
1.1066 +
1.1067 + typedef NagamochiIbaraki Create;
1.1068 +
1.1069 +
1.1070 + /// \brief Constructor.
1.1071 + ///
1.1072 + ///\param graph the graph the algorithm will run on.
1.1073 + ///\param capacity the capacity map used by the algorithm.
1.1074 + NagamochiIbaraki(const Graph& graph, const CapacityMap& capacity)
1.1075 + : _graph(&graph),
1.1076 + _capacity(&capacity), local_capacity(false),
1.1077 + _aux_graph(0), local_aux_graph(false),
1.1078 + _aux_capacity(0), local_aux_capacity(false),
1.1079 + _heap_cross_ref(0), local_heap_cross_ref(false),
1.1080 + _heap(0), local_heap(false),
1.1081 + _first(0), _last(0), _next(0) {}
1.1082 +
1.1083 + /// \brief Constructor.
1.1084 + ///
1.1085 + /// This constructor can be used only when the Traits class
1.1086 + /// defines how can we instantiate a local capacity map.
1.1087 + /// If the DefNeutralCapacity used the algorithm automatically
1.1088 + /// construct the capacity map.
1.1089 + ///
1.1090 + ///\param graph the graph the algorithm will run on.
1.1091 + NagamochiIbaraki(const Graph& graph)
1.1092 + : _graph(&graph),
1.1093 + _capacity(0), local_capacity(false),
1.1094 + _aux_graph(0), local_aux_graph(false),
1.1095 + _aux_capacity(0), local_aux_capacity(false),
1.1096 + _heap_cross_ref(0), local_heap_cross_ref(false),
1.1097 + _heap(0), local_heap(false),
1.1098 + _first(0), _last(0), _next(0) {}
1.1099 +
1.1100 + /// \brief Destructor.
1.1101 + ///
1.1102 + /// Destructor.
1.1103 + ~NagamochiIbaraki() {
1.1104 + if (local_heap) delete _heap;
1.1105 + if (local_heap_cross_ref) delete _heap_cross_ref;
1.1106 + if (_first) delete _first;
1.1107 + if (_last) delete _last;
1.1108 + if (_next) delete _next;
1.1109 + if (local_aux_capacity) delete _aux_capacity;
1.1110 + if (local_aux_graph) delete _aux_graph;
1.1111 + if (local_capacity) delete _capacity;
1.1112 + }
1.1113 +
1.1114 + /// \brief Sets the heap and the cross reference used by algorithm.
1.1115 + ///
1.1116 + /// Sets the heap and the cross reference used by algorithm.
1.1117 + /// If you don't use this function before calling \ref run(),
1.1118 + /// it will allocate one. The destuctor deallocates this
1.1119 + /// automatically allocated heap and cross reference, of course.
1.1120 + /// \return <tt> (*this) </tt>
1.1121 + NagamochiIbaraki &heap(Heap& heap, HeapCrossRef &crossRef)
1.1122 + {
1.1123 + if (local_heap_cross_ref) {
1.1124 + delete _heap_cross_ref;
1.1125 + local_heap_cross_ref=false;
1.1126 + }
1.1127 + _heap_cross_ref = &crossRef;
1.1128 + if (local_heap) {
1.1129 + delete _heap;
1.1130 + local_heap=false;
1.1131 + }
1.1132 + _heap = &heap;
1.1133 + return *this;
1.1134 + }
1.1135 +
1.1136 + /// \brief Sets the aux graph.
1.1137 + ///
1.1138 + /// Sets the aux graph used by algorithm.
1.1139 + /// If you don't use this function before calling \ref run(),
1.1140 + /// it will allocate one. The destuctor deallocates this
1.1141 + /// automatically allocated graph, of course.
1.1142 + /// \return <tt> (*this) </tt>
1.1143 + NagamochiIbaraki &auxGraph(AuxGraph& aux_graph)
1.1144 + {
1.1145 + if(local_aux_graph) {
1.1146 + delete _aux_graph;
1.1147 + local_aux_graph=false;
1.1148 + }
1.1149 + _aux_graph = &aux_graph;
1.1150 + return *this;
1.1151 + }
1.1152 +
1.1153 + /// \brief Sets the aux capacity map.
1.1154 + ///
1.1155 + /// Sets the aux capacity map used by algorithm.
1.1156 + /// If you don't use this function before calling \ref run(),
1.1157 + /// it will allocate one. The destuctor deallocates this
1.1158 + /// automatically allocated graph, of course.
1.1159 + /// \return <tt> (*this) </tt>
1.1160 + NagamochiIbaraki &auxCapacityMap(AuxCapacityMap& aux_capacity_map)
1.1161 + {
1.1162 + if(local_aux_capacity) {
1.1163 + delete _aux_capacity;
1.1164 + local_aux_capacity=false;
1.1165 + }
1.1166 + _aux_capacity = &aux_capacity_map;
1.1167 + return *this;
1.1168 + }
1.1169 +
1.1170 + /// \name Execution control
1.1171 + /// The simplest way to execute the algorithm is to use
1.1172 + /// one of the member functions called \c run().
1.1173 + /// \n
1.1174 + /// If you need more control on the execution,
1.1175 + /// first you must call \ref init() and then call the start()
1.1176 + /// or proper times the processNextPhase() member functions.
1.1177 +
1.1178 + ///@{
1.1179 +
1.1180 + /// \brief Initializes the internal data structures.
1.1181 + ///
1.1182 + /// Initializes the internal data structures.
1.1183 + void init() {
1.1184 + create_structures();
1.1185 + _first_node = _last_node = INVALID;
1.1186 + _node_num = countNodes(*_graph);
1.1187 + }
1.1188 +
1.1189 + /// \brief Processes the next phase
1.1190 + ///
1.1191 + /// Processes the next phase in the algorithm. The function
1.1192 + /// should be called countNodes(graph) - 1 times to get
1.1193 + /// surely the min cut in the graph. The
1.1194 + ///
1.1195 + ///\return %True when the algorithm finished.
1.1196 + bool processNextPhase() {
1.1197 + if (_node_num <= 1) return true;
1.1198 + using namespace _min_cut_bits;
1.1199 +
1.1200 + typedef typename AuxGraph::Node Node;
1.1201 + typedef typename AuxGraph::NodeIt NodeIt;
1.1202 + typedef typename AuxGraph::UEdge UEdge;
1.1203 + typedef typename AuxGraph::IncEdgeIt IncEdgeIt;
1.1204 +
1.1205 + typedef typename MaxCardinalitySearch<AuxGraph, AuxCapacityMap>::
1.1206 + template DefHeap<Heap, HeapCrossRef>::
1.1207 + template DefCardinalityMap<NullMap<Node, Value> >::
1.1208 + template DefProcessedMap<LastTwoMap<Node> >::
1.1209 + Create MaxCardinalitySearch;
1.1210 +
1.1211 + MaxCardinalitySearch mcs(*_aux_graph, *_aux_capacity);
1.1212 + for (NodeIt it(*_aux_graph); it != INVALID; ++it) {
1.1213 + _heap_cross_ref->set(it, Heap::PRE_HEAP);
1.1214 + }
1.1215 + mcs.heap(*_heap, *_heap_cross_ref);
1.1216 +
1.1217 + LastTwoMap<Node> last_two_nodes(_node_num);
1.1218 + mcs.processedMap(last_two_nodes);
1.1219 +
1.1220 + NullMap<Node, Value> cardinality;
1.1221 + mcs.cardinalityMap(cardinality);
1.1222 +
1.1223 + mcs.run();
1.1224 +
1.1225 + Node new_node = _aux_graph->addNode();
1.1226 +
1.1227 + typename AuxGraph::template NodeMap<UEdge> edges(*_aux_graph, INVALID);
1.1228 +
1.1229 + Node first_node = last_two_nodes[0];
1.1230 + Node second_node = last_two_nodes[1];
1.1231 +
1.1232 + _next->set((*_last)[first_node], (*_first)[second_node]);
1.1233 + _first->set(new_node, (*_first)[first_node]);
1.1234 + _last->set(new_node, (*_last)[second_node]);
1.1235 +
1.1236 + Value current_cut = 0;
1.1237 + for (IncEdgeIt it(*_aux_graph, first_node); it != INVALID; ++it) {
1.1238 + Node node = _aux_graph->runningNode(it);
1.1239 + current_cut += (*_aux_capacity)[it];
1.1240 + if (node == second_node) continue;
1.1241 + if (edges[node] == INVALID) {
1.1242 + edges[node] = _aux_graph->addEdge(new_node, node);
1.1243 + (*_aux_capacity)[edges[node]] = (*_aux_capacity)[it];
1.1244 + } else {
1.1245 + (*_aux_capacity)[edges[node]] += (*_aux_capacity)[it];
1.1246 + }
1.1247 + }
1.1248 +
1.1249 + if (_first_node == INVALID || current_cut < _min_cut) {
1.1250 + _first_node = (*_first)[first_node];
1.1251 + _last_node = (*_last)[first_node];
1.1252 + _min_cut = current_cut;
1.1253 + }
1.1254 +
1.1255 + _aux_graph->erase(first_node);
1.1256 +
1.1257 + for (IncEdgeIt it(*_aux_graph, second_node); it != INVALID; ++it) {
1.1258 + Node node = _aux_graph->runningNode(it);
1.1259 + if (edges[node] == INVALID) {
1.1260 + edges[node] = _aux_graph->addEdge(new_node, node);
1.1261 + (*_aux_capacity)[edges[node]] = (*_aux_capacity)[it];
1.1262 + } else {
1.1263 + (*_aux_capacity)[edges[node]] += (*_aux_capacity)[it];
1.1264 + }
1.1265 + }
1.1266 + _aux_graph->erase(second_node);
1.1267 +
1.1268 + --_node_num;
1.1269 + return _node_num == 1;
1.1270 + }
1.1271 +
1.1272 + /// \brief Executes the algorithm.
1.1273 + ///
1.1274 + /// Executes the algorithm.
1.1275 + ///
1.1276 + /// \pre init() must be called
1.1277 + void start() {
1.1278 + while (!processNextPhase());
1.1279 + }
1.1280 +
1.1281 +
1.1282 + /// \brief Runs %NagamochiIbaraki algorithm.
1.1283 + ///
1.1284 + /// This method runs the %Min cut algorithm
1.1285 + ///
1.1286 + /// \note mc.run(s) is just a shortcut of the following code.
1.1287 + ///\code
1.1288 + /// mc.init();
1.1289 + /// mc.start();
1.1290 + ///\endcode
1.1291 + void run() {
1.1292 + init();
1.1293 + start();
1.1294 + }
1.1295 +
1.1296 + ///@}
1.1297 +
1.1298 + /// \name Query Functions
1.1299 + ///
1.1300 + /// The result of the %NagamochiIbaraki
1.1301 + /// algorithm can be obtained using these functions.\n
1.1302 + /// Before the use of these functions, either run() or start()
1.1303 + /// must be called.
1.1304 +
1.1305 + ///@{
1.1306 +
1.1307 + /// \brief Returns the min cut value.
1.1308 + ///
1.1309 + /// Returns the min cut value if the algorithm finished.
1.1310 + /// After the first processNextPhase() it is a value of a
1.1311 + /// valid cut in the graph.
1.1312 + Value minCut() const {
1.1313 + return _min_cut;
1.1314 + }
1.1315 +
1.1316 + /// \brief Returns a min cut in a NodeMap.
1.1317 + ///
1.1318 + /// It sets the nodes of one of the two partitions to true in
1.1319 + /// the given BoolNodeMap. The map contains a valid cut if the
1.1320 + /// map have been set false previously.
1.1321 + template <typename NodeMap>
1.1322 + Value quickMinCut(NodeMap& nodeMap) const {
1.1323 + for (typename Graph::Node it = _first_node;
1.1324 + it != _last_node; it = (*_next)[it]) {
1.1325 + nodeMap.set(it, true);
1.1326 + }
1.1327 + nodeMap.set(_last_node, true);
1.1328 + return minCut();
1.1329 + }
1.1330 +
1.1331 + /// \brief Returns a min cut in a NodeMap.
1.1332 + ///
1.1333 + /// It sets the nodes of one of the two partitions to true and
1.1334 + /// the other partition to false. The function first set all of the
1.1335 + /// nodes to false and after it call the quickMinCut() member.
1.1336 + template <typename NodeMap>
1.1337 + Value minCut(NodeMap& nodeMap) const {
1.1338 + for (typename Graph::NodeIt it(*_graph); it != INVALID; ++it) {
1.1339 + nodeMap.set(it, false);
1.1340 + }
1.1341 + quickMinCut(nodeMap);
1.1342 + return minCut();
1.1343 + }
1.1344 +
1.1345 + /// \brief Returns a min cut in an EdgeMap.
1.1346 + ///
1.1347 + /// If an undirected edge is in a min cut then it will be
1.1348 + /// set to true and the others will be set to false in the given map.
1.1349 + template <typename EdgeMap>
1.1350 + Value cutEdges(EdgeMap& edgeMap) const {
1.1351 + typename Graph::template NodeMap<bool> cut(*_graph, false);
1.1352 + quickMinCut(cut);
1.1353 + for (typename Graph::EdgeIt it(*_graph); it != INVALID; ++it) {
1.1354 + edgeMap.set(it, cut[_graph->source(it)] ^ cut[_graph->target(it)]);
1.1355 + }
1.1356 + return minCut();
1.1357 + }
1.1358 +
1.1359 + ///@}
1.1360 +
1.1361 + };
1.1362 +}
1.1363 +
1.1364 +#endif