lemon/nagamochi_ibaraki.h
author alpar
Tue, 20 Feb 2007 15:53:33 +0000
changeset 2375 e30a0fdad0d7
parent 2284 05ff57dc401d
child 2376 0ed45a6c74b1
permissions -rw-r--r--
A preflow based general network circulation algorithm and a simple demo
deba@2284
     1
/* -*- C++ -*-
deba@2284
     2
 * lemon/min_cut.h - Part of LEMON, a generic C++ optimization library
deba@2284
     3
 *
deba@2284
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2284
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2284
     6
 *
deba@2284
     7
 * Permission to use, modify and distribute this software is granted
deba@2284
     8
 * provided that this copyright notice appears in all copies. For
deba@2284
     9
 * precise terms see the accompanying LICENSE file.
deba@2284
    10
 *
deba@2284
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@2284
    12
 * express or implied, and with no claim as to its suitability for any
deba@2284
    13
 * purpose.
deba@2284
    14
 *
deba@2284
    15
 */
deba@2284
    16
deba@2337
    17
#ifndef LEMON_NAGAMOCHI_IBARAKI_H
deba@2337
    18
#define LEMON_NAGAMOCHI_IBARAKI_H
deba@2284
    19
deba@2284
    20
deba@2337
    21
/// \ingroup topology 
deba@2337
    22
/// \file 
deba@2337
    23
/// \brief Maximum cardinality search and minimum cut in undirected
deba@2337
    24
/// graphs.
deba@2284
    25
deba@2284
    26
#include <lemon/list_graph.h>
deba@2284
    27
#include <lemon/bin_heap.h>
deba@2284
    28
#include <lemon/bucket_heap.h>
deba@2284
    29
deba@2337
    30
#include <lemon/unionfind.h>
deba@2337
    31
#include <lemon/topology.h>
deba@2337
    32
deba@2284
    33
#include <lemon/bits/invalid.h>
deba@2284
    34
#include <lemon/error.h>
deba@2284
    35
#include <lemon/maps.h>
deba@2284
    36
deba@2284
    37
#include <functional>
deba@2284
    38
deba@2337
    39
#include <lemon/graph_writer.h>
deba@2337
    40
#include <lemon/time_measure.h>
deba@2337
    41
deba@2284
    42
namespace lemon {
deba@2284
    43
deba@2284
    44
  namespace _min_cut_bits {
deba@2284
    45
deba@2284
    46
    template <typename CapacityMap>
deba@2284
    47
    struct HeapSelector {
deba@2284
    48
      template <typename Value, typename Ref>
deba@2284
    49
      struct Selector {
deba@2284
    50
        typedef BinHeap<Value, Ref, std::greater<Value> > Heap;
deba@2284
    51
      };
deba@2284
    52
    };
deba@2284
    53
deba@2284
    54
    template <typename CapacityKey>
deba@2284
    55
    struct HeapSelector<ConstMap<CapacityKey, Const<int, 1> > > {
deba@2284
    56
      template <typename Value, typename Ref>
deba@2284
    57
      struct Selector {
deba@2284
    58
        typedef BucketHeap<Ref, false > Heap;
deba@2284
    59
      };
deba@2284
    60
    };
deba@2284
    61
deba@2284
    62
  }
deba@2284
    63
deba@2284
    64
  /// \brief Default traits class of MaxCardinalitySearch class.
deba@2284
    65
  ///
deba@2284
    66
  /// Default traits class of MaxCardinalitySearch class.
deba@2284
    67
  /// \param Graph Graph type.
deba@2284
    68
  /// \param CapacityMap Type of length map.
deba@2284
    69
  template <typename _Graph, typename _CapacityMap>
deba@2284
    70
  struct MaxCardinalitySearchDefaultTraits {
deba@2284
    71
    /// The graph type the algorithm runs on. 
deba@2284
    72
    typedef _Graph Graph;
deba@2284
    73
deba@2284
    74
    /// \brief The type of the map that stores the edge capacities.
deba@2284
    75
    ///
deba@2284
    76
    /// The type of the map that stores the edge capacities.
deba@2284
    77
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
deba@2284
    78
    typedef _CapacityMap CapacityMap;
deba@2284
    79
deba@2284
    80
    /// \brief The type of the capacity of the edges.
deba@2284
    81
    typedef typename CapacityMap::Value Value;
deba@2284
    82
deba@2284
    83
    /// \brief The cross reference type used by heap.
deba@2284
    84
    ///
deba@2284
    85
    /// The cross reference type used by heap.
deba@2284
    86
    /// Usually it is \c Graph::NodeMap<int>.
deba@2284
    87
    typedef typename Graph::template NodeMap<int> HeapCrossRef;
deba@2284
    88
deba@2284
    89
    /// \brief Instantiates a HeapCrossRef.
deba@2284
    90
    ///
deba@2284
    91
    /// This function instantiates a \ref HeapCrossRef. 
deba@2284
    92
    /// \param graph is the graph, to which we would like to define the 
deba@2284
    93
    /// HeapCrossRef.
deba@2284
    94
    static HeapCrossRef *createHeapCrossRef(const Graph &graph) {
deba@2284
    95
      return new HeapCrossRef(graph);
deba@2284
    96
    }
deba@2284
    97
    
deba@2284
    98
    /// \brief The heap type used by MaxCardinalitySearch algorithm.
deba@2284
    99
    ///
deba@2284
   100
    /// The heap type used by MaxCardinalitySearch algorithm. It should
deba@2284
   101
    /// maximalize the priorities. The default heap type is
deba@2284
   102
    /// the \ref BinHeap, but it is specialized when the
deba@2284
   103
    /// CapacityMap is ConstMap<Graph::Node, Const<int, 1> >
deba@2284
   104
    /// to BucketHeap.
deba@2284
   105
    ///
deba@2284
   106
    /// \sa MaxCardinalitySearch
deba@2284
   107
    typedef typename _min_cut_bits
deba@2284
   108
    ::HeapSelector<CapacityMap>
deba@2284
   109
    ::template Selector<Value, HeapCrossRef>
deba@2284
   110
    ::Heap Heap;
deba@2284
   111
deba@2284
   112
    /// \brief Instantiates a Heap.
deba@2284
   113
    ///
deba@2284
   114
    /// This function instantiates a \ref Heap. 
deba@2284
   115
    /// \param crossref The cross reference of the heap.
deba@2284
   116
    static Heap *createHeap(HeapCrossRef& crossref) {
deba@2284
   117
      return new Heap(crossref);
deba@2284
   118
    }
deba@2284
   119
deba@2284
   120
    /// \brief The type of the map that stores whether a nodes is processed.
deba@2284
   121
    ///
deba@2284
   122
    /// The type of the map that stores whether a nodes is processed.
deba@2284
   123
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
deba@2284
   124
    /// By default it is a NullMap.
deba@2284
   125
    typedef NullMap<typename Graph::Node, bool> ProcessedMap;
deba@2284
   126
deba@2284
   127
    /// \brief Instantiates a ProcessedMap.
deba@2284
   128
    ///
deba@2284
   129
    /// This function instantiates a \ref ProcessedMap. 
deba@2284
   130
    /// \param graph is the graph, to which
deba@2284
   131
    /// we would like to define the \ref ProcessedMap
deba@2284
   132
#ifdef DOXYGEN
deba@2284
   133
    static ProcessedMap *createProcessedMap(const Graph &graph)
deba@2284
   134
#else
deba@2284
   135
    static ProcessedMap *createProcessedMap(const Graph &)
deba@2284
   136
#endif
deba@2284
   137
    {
deba@2284
   138
      return new ProcessedMap();
deba@2284
   139
    }
deba@2284
   140
deba@2284
   141
    /// \brief The type of the map that stores the cardinalties of the nodes.
deba@2284
   142
    /// 
deba@2284
   143
    /// The type of the map that stores the cardinalities of the nodes.
deba@2284
   144
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
deba@2284
   145
    typedef typename Graph::template NodeMap<Value> CardinalityMap;
deba@2284
   146
deba@2284
   147
    /// \brief Instantiates a CardinalityMap.
deba@2284
   148
    ///
deba@2284
   149
    /// This function instantiates a \ref CardinalityMap. 
deba@2284
   150
    /// \param graph is the graph, to which we would like to define the \ref 
deba@2284
   151
    /// CardinalityMap
deba@2284
   152
    static CardinalityMap *createCardinalityMap(const Graph &graph) {
deba@2284
   153
      return new CardinalityMap(graph);
deba@2284
   154
    }
deba@2284
   155
deba@2337
   156
deba@2284
   157
  };
deba@2284
   158
  
deba@2284
   159
  /// \ingroup topology
deba@2284
   160
  ///
deba@2284
   161
  /// \brief Maximum Cardinality Search algorithm class.
deba@2284
   162
  ///
deba@2284
   163
  /// This class provides an efficient implementation of Maximum Cardinality 
deba@2284
   164
  /// Search algorithm. The maximum cardinality search chooses first time any 
deba@2284
   165
  /// node of the graph. Then every time it chooses the node which is connected
deba@2284
   166
  /// to the processed nodes at most in the sum of capacities on the out 
deba@2284
   167
  /// edges. If there is a cut in the graph the algorithm should choose
deba@2284
   168
  /// again any unprocessed node of the graph. Each node cardinality is
deba@2284
   169
  /// the sum of capacities on the out edges to the nodes which are processed
deba@2284
   170
  /// before the given node.
deba@2284
   171
  ///
deba@2284
   172
  /// The edge capacities are passed to the algorithm using a
deba@2284
   173
  /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any 
deba@2284
   174
  /// kind of capacity.
deba@2284
   175
  ///
deba@2284
   176
  /// The type of the capacity is determined by the \ref 
deba@2284
   177
  /// concepts::ReadMap::Value "Value" of the capacity map.
deba@2284
   178
  ///
deba@2284
   179
  /// It is also possible to change the underlying priority heap.
deba@2284
   180
  ///
deba@2284
   181
  ///
deba@2284
   182
  /// \param _Graph The graph type the algorithm runs on. The default value
deba@2284
   183
  /// is \ref ListGraph. The value of Graph is not used directly by
deba@2284
   184
  /// the search algorithm, it is only passed to 
deba@2284
   185
  /// \ref MaxCardinalitySearchDefaultTraits.
deba@2284
   186
  /// \param _CapacityMap This read-only EdgeMap determines the capacities of 
deba@2284
   187
  /// the edges. It is read once for each edge, so the map may involve in
deba@2284
   188
  /// relatively time consuming process to compute the edge capacity if
deba@2284
   189
  /// it is necessary. The default map type is \ref
deba@2284
   190
  /// concepts::Graph::EdgeMap "Graph::EdgeMap<int>".  The value
deba@2284
   191
  /// of CapacityMap is not used directly by search algorithm, it is only 
deba@2284
   192
  /// passed to \ref MaxCardinalitySearchDefaultTraits.  
deba@2284
   193
  /// \param _Traits Traits class to set various data types used by the 
deba@2284
   194
  /// algorithm.  The default traits class is 
deba@2284
   195
  /// \ref MaxCardinalitySearchDefaultTraits 
deba@2284
   196
  /// "MaxCardinalitySearchDefaultTraits<_Graph, _CapacityMap>".  
deba@2284
   197
  /// See \ref MaxCardinalitySearchDefaultTraits 
deba@2284
   198
  /// for the documentation of a MaxCardinalitySearch traits class.
deba@2284
   199
  ///
deba@2284
   200
  /// \author Balazs Dezso
deba@2284
   201
deba@2284
   202
#ifdef DOXYGEN
deba@2284
   203
  template <typename _Graph, typename _CapacityMap, typename _Traits>
deba@2284
   204
#else
deba@2284
   205
  template <typename _Graph = ListUGraph,
deba@2284
   206
	    typename _CapacityMap = typename _Graph::template EdgeMap<int>,
deba@2284
   207
	    typename _Traits = 
deba@2284
   208
            MaxCardinalitySearchDefaultTraits<_Graph, _CapacityMap> >
deba@2284
   209
#endif
deba@2284
   210
  class MaxCardinalitySearch {
deba@2284
   211
  public:
deba@2284
   212
    /// \brief \ref Exception for uninitialized parameters.
deba@2284
   213
    ///
deba@2284
   214
    /// This error represents problems in the initialization
deba@2284
   215
    /// of the parameters of the algorithms.
deba@2284
   216
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@2284
   217
    public:
deba@2284
   218
      virtual const char* what() const throw() {
deba@2284
   219
	return "lemon::MaxCardinalitySearch::UninitializedParameter";
deba@2284
   220
      }
deba@2284
   221
    };
deba@2284
   222
deba@2284
   223
    typedef _Traits Traits;
deba@2284
   224
    ///The type of the underlying graph.
deba@2284
   225
    typedef typename Traits::Graph Graph;
deba@2284
   226
    
deba@2284
   227
    ///The type of the capacity of the edges.
deba@2284
   228
    typedef typename Traits::CapacityMap::Value Value;
deba@2284
   229
    ///The type of the map that stores the edge capacities.
deba@2284
   230
    typedef typename Traits::CapacityMap CapacityMap;
deba@2284
   231
    ///The type of the map indicating if a node is processed.
deba@2284
   232
    typedef typename Traits::ProcessedMap ProcessedMap;
deba@2284
   233
    ///The type of the map that stores the cardinalities of the nodes.
deba@2284
   234
    typedef typename Traits::CardinalityMap CardinalityMap;
deba@2284
   235
    ///The cross reference type used for the current heap.
deba@2284
   236
    typedef typename Traits::HeapCrossRef HeapCrossRef;
deba@2284
   237
    ///The heap type used by the algorithm. It maximize the priorities.
deba@2284
   238
    typedef typename Traits::Heap Heap;
deba@2284
   239
  private:
deba@2284
   240
    /// Pointer to the underlying graph.
deba@2284
   241
    const Graph *_graph;
deba@2284
   242
    /// Pointer to the capacity map
deba@2284
   243
    const CapacityMap *_capacity;
deba@2284
   244
    ///Pointer to the map of cardinality.
deba@2284
   245
    CardinalityMap *_cardinality;
deba@2284
   246
    ///Indicates if \ref _cardinality is locally allocated (\c true) or not.
deba@2284
   247
    bool local_cardinality;
deba@2284
   248
    ///Pointer to the map of processed status of the nodes.
deba@2284
   249
    ProcessedMap *_processed;
deba@2284
   250
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
deba@2284
   251
    bool local_processed;
deba@2284
   252
    ///Pointer to the heap cross references.
deba@2284
   253
    HeapCrossRef *_heap_cross_ref;
deba@2284
   254
    ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
deba@2284
   255
    bool local_heap_cross_ref;
deba@2284
   256
    ///Pointer to the heap.
deba@2284
   257
    Heap *_heap;
deba@2284
   258
    ///Indicates if \ref _heap is locally allocated (\c true) or not.
deba@2284
   259
    bool local_heap;
deba@2284
   260
deba@2284
   261
  public :
deba@2284
   262
deba@2284
   263
    typedef MaxCardinalitySearch Create;
deba@2284
   264
 
deba@2284
   265
    ///\name Named template parameters
deba@2284
   266
deba@2284
   267
    ///@{
deba@2284
   268
deba@2284
   269
    template <class T>
deba@2284
   270
    struct DefCardinalityMapTraits : public Traits {
deba@2284
   271
      typedef T CardinalityMap;
deba@2284
   272
      static CardinalityMap *createCardinalityMap(const Graph &) 
deba@2284
   273
      {
deba@2284
   274
	throw UninitializedParameter();
deba@2284
   275
      }
deba@2284
   276
    };
deba@2284
   277
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@2284
   278
    /// CardinalityMap type
deba@2284
   279
    ///
deba@2284
   280
    /// \ref named-templ-param "Named parameter" for setting CardinalityMap 
deba@2284
   281
    /// type
deba@2284
   282
    template <class T>
deba@2284
   283
    struct DefCardinalityMap 
deba@2284
   284
      : public MaxCardinalitySearch<Graph, CapacityMap, 
deba@2284
   285
                                    DefCardinalityMapTraits<T> > { 
deba@2284
   286
      typedef MaxCardinalitySearch<Graph, CapacityMap, 
deba@2284
   287
                                   DefCardinalityMapTraits<T> > Create;
deba@2284
   288
    };
deba@2284
   289
    
deba@2284
   290
    template <class T>
deba@2284
   291
    struct DefProcessedMapTraits : public Traits {
deba@2284
   292
      typedef T ProcessedMap;
deba@2284
   293
      static ProcessedMap *createProcessedMap(const Graph &) {
deba@2284
   294
	throw UninitializedParameter();
deba@2284
   295
      }
deba@2284
   296
    };
deba@2284
   297
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@2284
   298
    /// ProcessedMap type
deba@2284
   299
    ///
deba@2284
   300
    /// \ref named-templ-param "Named parameter" for setting ProcessedMap type
deba@2284
   301
    ///
deba@2284
   302
    template <class T>
deba@2284
   303
    struct DefProcessedMap 
deba@2284
   304
      : public MaxCardinalitySearch<Graph, CapacityMap, 
deba@2284
   305
                                    DefProcessedMapTraits<T> > { 
deba@2284
   306
      typedef MaxCardinalitySearch<Graph, CapacityMap, 
deba@2284
   307
                                   DefProcessedMapTraits<T> > Create;
deba@2284
   308
    };
deba@2284
   309
    
deba@2284
   310
    template <class H, class CR>
deba@2284
   311
    struct DefHeapTraits : public Traits {
deba@2284
   312
      typedef CR HeapCrossRef;
deba@2284
   313
      typedef H Heap;
deba@2284
   314
      static HeapCrossRef *createHeapCrossRef(const Graph &) {
deba@2284
   315
	throw UninitializedParameter();
deba@2284
   316
      }
deba@2284
   317
      static Heap *createHeap(HeapCrossRef &) {
deba@2284
   318
	throw UninitializedParameter();
deba@2284
   319
      }
deba@2284
   320
    };
deba@2284
   321
    /// \brief \ref named-templ-param "Named parameter" for setting heap 
deba@2284
   322
    /// and cross reference type
deba@2284
   323
    ///
deba@2284
   324
    /// \ref named-templ-param "Named parameter" for setting heap and cross 
deba@2284
   325
    /// reference type
deba@2284
   326
    template <class H, class CR = typename Graph::template NodeMap<int> >
deba@2284
   327
    struct DefHeap
deba@2284
   328
      : public MaxCardinalitySearch<Graph, CapacityMap, 
deba@2284
   329
                                    DefHeapTraits<H, CR> > { 
deba@2284
   330
      typedef MaxCardinalitySearch< Graph, CapacityMap, 
deba@2284
   331
                                    DefHeapTraits<H, CR> > Create;
deba@2284
   332
    };
deba@2284
   333
deba@2284
   334
    template <class H, class CR>
deba@2284
   335
    struct DefStandardHeapTraits : public Traits {
deba@2284
   336
      typedef CR HeapCrossRef;
deba@2284
   337
      typedef H Heap;
deba@2284
   338
      static HeapCrossRef *createHeapCrossRef(const Graph &graph) {
deba@2284
   339
	return new HeapCrossRef(graph);
deba@2284
   340
      }
deba@2284
   341
      static Heap *createHeap(HeapCrossRef &crossref) {
deba@2284
   342
	return new Heap(crossref);
deba@2284
   343
      }
deba@2284
   344
    };
deba@2284
   345
deba@2284
   346
    /// \brief \ref named-templ-param "Named parameter" for setting heap and 
deba@2284
   347
    /// cross reference type with automatic allocation
deba@2284
   348
    ///
deba@2284
   349
    /// \ref named-templ-param "Named parameter" for setting heap and cross 
deba@2284
   350
    /// reference type. It can allocate the heap and the cross reference 
deba@2284
   351
    /// object if the cross reference's constructor waits for the graph as 
deba@2284
   352
    /// parameter and the heap's constructor waits for the cross reference.
deba@2284
   353
    template <class H, class CR = typename Graph::template NodeMap<int> >
deba@2284
   354
    struct DefStandardHeap
deba@2284
   355
      : public MaxCardinalitySearch<Graph, CapacityMap, 
deba@2284
   356
                                    DefStandardHeapTraits<H, CR> > { 
deba@2284
   357
      typedef MaxCardinalitySearch<Graph, CapacityMap, 
deba@2284
   358
                                   DefStandardHeapTraits<H, CR> > 
deba@2284
   359
      Create;
deba@2284
   360
    };
deba@2284
   361
    
deba@2284
   362
    ///@}
deba@2284
   363
deba@2284
   364
deba@2284
   365
  protected:
deba@2284
   366
deba@2284
   367
    MaxCardinalitySearch() {}
deba@2284
   368
deba@2284
   369
  public:      
deba@2284
   370
    
deba@2284
   371
    /// \brief Constructor.
deba@2284
   372
    ///
deba@2284
   373
    ///\param graph the graph the algorithm will run on.
deba@2284
   374
    ///\param capacity the capacity map used by the algorithm.
deba@2284
   375
    MaxCardinalitySearch(const Graph& graph, const CapacityMap& capacity) :
deba@2284
   376
      _graph(&graph), _capacity(&capacity),
deba@2284
   377
      _cardinality(0), local_cardinality(false),
deba@2284
   378
      _processed(0), local_processed(false),
deba@2284
   379
      _heap_cross_ref(0), local_heap_cross_ref(false),
deba@2284
   380
      _heap(0), local_heap(false)
deba@2284
   381
    { }
deba@2284
   382
    
deba@2284
   383
    /// \brief Destructor.
deba@2284
   384
    ~MaxCardinalitySearch() {
deba@2284
   385
      if(local_cardinality) delete _cardinality;
deba@2284
   386
      if(local_processed) delete _processed;
deba@2284
   387
      if(local_heap_cross_ref) delete _heap_cross_ref;
deba@2284
   388
      if(local_heap) delete _heap;
deba@2284
   389
    }
deba@2284
   390
deba@2284
   391
    /// \brief Sets the capacity map.
deba@2284
   392
    ///
deba@2284
   393
    /// Sets the capacity map.
deba@2284
   394
    /// \return <tt> (*this) </tt>
deba@2284
   395
    MaxCardinalitySearch &capacityMap(const CapacityMap &m) {
deba@2284
   396
      _capacity = &m;
deba@2284
   397
      return *this;
deba@2284
   398
    }
deba@2284
   399
deba@2284
   400
    /// \brief Sets the map storing the cardinalities calculated by the 
deba@2284
   401
    /// algorithm.
deba@2284
   402
    ///
deba@2284
   403
    /// Sets the map storing the cardinalities calculated by the algorithm.
deba@2284
   404
    /// If you don't use this function before calling \ref run(),
deba@2284
   405
    /// it will allocate one. The destuctor deallocates this
deba@2284
   406
    /// automatically allocated map, of course.
deba@2284
   407
    /// \return <tt> (*this) </tt>
deba@2284
   408
    MaxCardinalitySearch &cardinalityMap(CardinalityMap &m) {
deba@2284
   409
      if(local_cardinality) {
deba@2284
   410
	delete _cardinality;
deba@2284
   411
	local_cardinality=false;
deba@2284
   412
      }
deba@2284
   413
      _cardinality = &m;
deba@2284
   414
      return *this;
deba@2284
   415
    }
deba@2284
   416
deba@2284
   417
    /// \brief Sets the map storing the processed nodes.
deba@2284
   418
    ///
deba@2284
   419
    /// Sets the map storing the processed nodes.
deba@2284
   420
    /// If you don't use this function before calling \ref run(),
deba@2284
   421
    /// it will allocate one. The destuctor deallocates this
deba@2284
   422
    /// automatically allocated map, of course.
deba@2284
   423
    /// \return <tt> (*this) </tt>
deba@2284
   424
    MaxCardinalitySearch &processedMap(ProcessedMap &m) 
deba@2284
   425
    {
deba@2284
   426
      if(local_processed) {
deba@2284
   427
	delete _processed;
deba@2284
   428
	local_processed=false;
deba@2284
   429
      }
deba@2284
   430
      _processed = &m;
deba@2284
   431
      return *this;
deba@2284
   432
    }
deba@2284
   433
deba@2284
   434
    /// \brief Sets the heap and the cross reference used by algorithm.
deba@2284
   435
    ///
deba@2284
   436
    /// Sets the heap and the cross reference used by algorithm.
deba@2284
   437
    /// If you don't use this function before calling \ref run(),
deba@2284
   438
    /// it will allocate one. The destuctor deallocates this
deba@2284
   439
    /// automatically allocated map, of course.
deba@2284
   440
    /// \return <tt> (*this) </tt>
deba@2284
   441
    MaxCardinalitySearch &heap(Heap& heap, HeapCrossRef &crossRef) {
deba@2284
   442
      if(local_heap_cross_ref) {
deba@2284
   443
	delete _heap_cross_ref;
deba@2284
   444
	local_heap_cross_ref = false;
deba@2284
   445
      }
deba@2284
   446
      _heap_cross_ref = &crossRef;
deba@2284
   447
      if(local_heap) {
deba@2284
   448
	delete _heap;
deba@2284
   449
	local_heap = false;
deba@2284
   450
      }
deba@2284
   451
      _heap = &heap;
deba@2284
   452
      return *this;
deba@2284
   453
    }
deba@2284
   454
deba@2284
   455
  private:
deba@2284
   456
deba@2284
   457
    typedef typename Graph::Node Node;
deba@2284
   458
    typedef typename Graph::NodeIt NodeIt;
deba@2284
   459
    typedef typename Graph::Edge Edge;
deba@2284
   460
    typedef typename Graph::InEdgeIt InEdgeIt;
deba@2284
   461
deba@2284
   462
    void create_maps() {
deba@2284
   463
      if(!_cardinality) {
deba@2284
   464
	local_cardinality = true;
deba@2284
   465
	_cardinality = Traits::createCardinalityMap(*_graph);
deba@2284
   466
      }
deba@2284
   467
      if(!_processed) {
deba@2284
   468
	local_processed = true;
deba@2284
   469
	_processed = Traits::createProcessedMap(*_graph);
deba@2284
   470
      }
deba@2284
   471
      if (!_heap_cross_ref) {
deba@2284
   472
	local_heap_cross_ref = true;
deba@2284
   473
	_heap_cross_ref = Traits::createHeapCrossRef(*_graph);
deba@2284
   474
      }
deba@2284
   475
      if (!_heap) {
deba@2284
   476
	local_heap = true;
deba@2284
   477
	_heap = Traits::createHeap(*_heap_cross_ref);
deba@2284
   478
      }
deba@2284
   479
    }
deba@2284
   480
    
deba@2284
   481
    void finalizeNodeData(Node node, Value capacity) {
deba@2284
   482
      _processed->set(node, true);
deba@2284
   483
      _cardinality->set(node, capacity);
deba@2284
   484
    }
deba@2284
   485
deba@2284
   486
  public:
deba@2284
   487
    /// \name Execution control
deba@2284
   488
    /// The simplest way to execute the algorithm is to use
deba@2284
   489
    /// one of the member functions called \c run(...).
deba@2284
   490
    /// \n
deba@2284
   491
    /// If you need more control on the execution,
deba@2284
   492
    /// first you must call \ref init(), then you can add several source nodes
deba@2284
   493
    /// with \ref addSource().
deba@2284
   494
    /// Finally \ref start() will perform the actual path
deba@2284
   495
    /// computation.
deba@2284
   496
deba@2284
   497
    ///@{
deba@2284
   498
deba@2284
   499
    /// \brief Initializes the internal data structures.
deba@2284
   500
    ///
deba@2284
   501
    /// Initializes the internal data structures.
deba@2284
   502
    void init() {
deba@2284
   503
      create_maps();
deba@2284
   504
      _heap->clear();
deba@2284
   505
      for (NodeIt it(*_graph) ; it != INVALID ; ++it) {
deba@2284
   506
	_processed->set(it, false);
deba@2284
   507
	_heap_cross_ref->set(it, Heap::PRE_HEAP);
deba@2284
   508
      }
deba@2284
   509
    }
deba@2284
   510
    
deba@2284
   511
    /// \brief Adds a new source node.
deba@2284
   512
    /// 
deba@2284
   513
    /// Adds a new source node to the priority heap.
deba@2284
   514
    ///
deba@2284
   515
    /// It checks if the node has not yet been added to the heap.
deba@2284
   516
    void addSource(Node source, Value capacity = 0) {
deba@2284
   517
      if(_heap->state(source) == Heap::PRE_HEAP) {
deba@2284
   518
	_heap->push(source, capacity);
deba@2284
   519
      } 
deba@2284
   520
    }
deba@2284
   521
    
deba@2284
   522
    /// \brief Processes the next node in the priority heap
deba@2284
   523
    ///
deba@2284
   524
    /// Processes the next node in the priority heap.
deba@2284
   525
    ///
deba@2284
   526
    /// \return The processed node.
deba@2284
   527
    ///
deba@2284
   528
    /// \warning The priority heap must not be empty!
deba@2284
   529
    Node processNextNode() {
deba@2284
   530
      Node node = _heap->top(); 
deba@2284
   531
      finalizeNodeData(node, _heap->prio());
deba@2284
   532
      _heap->pop();
deba@2284
   533
      
deba@2284
   534
      for (InEdgeIt it(*_graph, node); it != INVALID; ++it) {
deba@2284
   535
	Node source = _graph->source(it); 
deba@2284
   536
	switch (_heap->state(source)) {
deba@2284
   537
	case Heap::PRE_HEAP:
deba@2284
   538
	  _heap->push(source, (*_capacity)[it]); 
deba@2284
   539
	  break;
deba@2284
   540
	case Heap::IN_HEAP:
deba@2284
   541
	  _heap->decrease(source, (*_heap)[source] + (*_capacity)[it]); 
deba@2284
   542
	  break;
deba@2284
   543
	case Heap::POST_HEAP:
deba@2284
   544
	  break;
deba@2284
   545
	}
deba@2284
   546
      }
deba@2284
   547
      return node;
deba@2284
   548
    }
deba@2284
   549
deba@2284
   550
    /// \brief Next node to be processed.
deba@2284
   551
    ///
deba@2284
   552
    /// Next node to be processed.
deba@2284
   553
    ///
deba@2284
   554
    /// \return The next node to be processed or INVALID if the 
deba@2284
   555
    /// priority heap is empty.
deba@2284
   556
    Node nextNode() { 
deba@2284
   557
      return _heap->empty() ? _heap->top() : INVALID;
deba@2284
   558
    }
deba@2284
   559
 
deba@2284
   560
    /// \brief Returns \c false if there are nodes
deba@2284
   561
    /// to be processed in the priority heap
deba@2284
   562
    ///
deba@2284
   563
    /// Returns \c false if there are nodes
deba@2284
   564
    /// to be processed in the priority heap
deba@2284
   565
    bool emptyQueue() { return _heap->empty(); }
deba@2284
   566
    /// \brief Returns the number of the nodes to be processed 
deba@2284
   567
    /// in the priority heap
deba@2284
   568
    ///
deba@2284
   569
    /// Returns the number of the nodes to be processed in the priority heap
deba@2284
   570
    int queueSize() { return _heap->size(); }
deba@2284
   571
    
deba@2284
   572
    /// \brief Executes the algorithm.
deba@2284
   573
    ///
deba@2284
   574
    /// Executes the algorithm.
deba@2284
   575
    ///
deba@2284
   576
    ///\pre init() must be called and at least one node should be added
deba@2284
   577
    /// with addSource() before using this function.
deba@2284
   578
    ///
deba@2284
   579
    /// This method runs the Maximum Cardinality Search algorithm from the 
deba@2284
   580
    /// source node(s).
deba@2284
   581
    void start() {
deba@2284
   582
      while ( !_heap->empty() ) processNextNode();
deba@2284
   583
    }
deba@2284
   584
    
deba@2284
   585
    /// \brief Executes the algorithm until \c dest is reached.
deba@2284
   586
    ///
deba@2284
   587
    /// Executes the algorithm until \c dest is reached.
deba@2284
   588
    ///
deba@2284
   589
    /// \pre init() must be called and at least one node should be added
deba@2284
   590
    /// with addSource() before using this function.
deba@2284
   591
    ///
deba@2284
   592
    /// This method runs the %MaxCardinalitySearch algorithm from the source 
deba@2284
   593
    /// nodes.
deba@2284
   594
    void start(Node dest) {
deba@2284
   595
      while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
deba@2284
   596
      if ( !_heap->empty() ) finalizeNodeData(_heap->top(), _heap->prio());
deba@2284
   597
    }
deba@2284
   598
    
deba@2284
   599
    /// \brief Executes the algorithm until a condition is met.
deba@2284
   600
    ///
deba@2284
   601
    /// Executes the algorithm until a condition is met.
deba@2284
   602
    ///
deba@2284
   603
    /// \pre init() must be called and at least one node should be added
deba@2284
   604
    /// with addSource() before using this function.
deba@2284
   605
    ///
deba@2284
   606
    /// \param nm must be a bool (or convertible) node map. The algorithm
deba@2284
   607
    /// will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
deba@2284
   608
    template <typename NodeBoolMap>
deba@2284
   609
    void start(const NodeBoolMap &nm) {
deba@2284
   610
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
deba@2284
   611
      if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
deba@2284
   612
    }
deba@2284
   613
    
deba@2284
   614
    /// \brief Runs the maximal cardinality search algorithm from node \c s.
deba@2284
   615
    ///
deba@2284
   616
    /// This method runs the %MaxCardinalitySearch algorithm from a root 
deba@2284
   617
    /// node \c s.
deba@2284
   618
    ///
deba@2284
   619
    ///\note d.run(s) is just a shortcut of the following code.
deba@2284
   620
    ///\code
deba@2284
   621
    ///  d.init();
deba@2284
   622
    ///  d.addSource(s);
deba@2284
   623
    ///  d.start();
deba@2284
   624
    ///\endcode
deba@2284
   625
    void run(Node s) {
deba@2284
   626
      init();
deba@2284
   627
      addSource(s);
deba@2284
   628
      start();
deba@2284
   629
    }
deba@2284
   630
deba@2284
   631
    /// \brief Runs the maximal cardinality search algorithm for the 
deba@2284
   632
    /// whole graph.
deba@2284
   633
    ///
deba@2284
   634
    /// This method runs the %MaxCardinalitySearch algorithm from all 
deba@2284
   635
    /// unprocessed node of the graph.
deba@2284
   636
    ///
deba@2284
   637
    ///\note d.run(s) is just a shortcut of the following code.
deba@2284
   638
    ///\code
deba@2284
   639
    ///  d.init();
deba@2284
   640
    ///  for (NodeIt it(graph); it != INVALID; ++it) {
deba@2284
   641
    ///    if (!d.reached(it)) {
deba@2284
   642
    ///      d.addSource(s);
deba@2284
   643
    ///      d.start();
deba@2284
   644
    ///    }
deba@2284
   645
    ///  }
deba@2284
   646
    ///\endcode
deba@2284
   647
    void run() {
deba@2284
   648
      init();
deba@2284
   649
      for (NodeIt it(*_graph); it != INVALID; ++it) {
deba@2284
   650
        if (!reached(it)) {
deba@2284
   651
          addSource(it);
deba@2284
   652
          start();
deba@2284
   653
        }
deba@2284
   654
      }
deba@2284
   655
    }
deba@2284
   656
    
deba@2284
   657
    ///@}
deba@2284
   658
deba@2284
   659
    /// \name Query Functions
deba@2284
   660
    /// The result of the maximum cardinality search algorithm can be 
deba@2284
   661
    /// obtained using these functions.
deba@2284
   662
    /// \n
deba@2284
   663
    /// Before the use of these functions, either run() or start() must be 
deba@2284
   664
    /// called.
deba@2284
   665
    
deba@2284
   666
    ///@{
deba@2284
   667
deba@2284
   668
    /// \brief The cardinality of a node.
deba@2284
   669
    ///
deba@2284
   670
    /// Returns the cardinality of a node.
deba@2284
   671
    /// \pre \ref run() must be called before using this function.
deba@2284
   672
    /// \warning If node \c v in unreachable from the root the return value
deba@2284
   673
    /// of this funcion is undefined.
deba@2284
   674
    Value cardinality(Node node) const { return (*_cardinality)[node]; }
deba@2284
   675
deba@2337
   676
    /// \brief The current cardinality of a node.
deba@2337
   677
    ///
deba@2337
   678
    /// Returns the current cardinality of a node.
deba@2337
   679
    /// \pre the given node should be reached but not processed
deba@2337
   680
    Value currentCardinality(Node node) const { return (*_heap)[node]; }
deba@2337
   681
deba@2284
   682
    /// \brief Returns a reference to the NodeMap of cardinalities.
deba@2284
   683
    ///
deba@2284
   684
    /// Returns a reference to the NodeMap of cardinalities. \pre \ref run() 
deba@2284
   685
    /// must be called before using this function.
deba@2284
   686
    const CardinalityMap &cardinalityMap() const { return *_cardinality;}
deba@2284
   687
 
deba@2284
   688
    /// \brief Checks if a node is reachable from the root.
deba@2284
   689
    ///
deba@2284
   690
    /// Returns \c true if \c v is reachable from the root.
deba@2284
   691
    /// \warning The source nodes are inditated as unreached.
deba@2284
   692
    /// \pre \ref run() must be called before using this function.
deba@2284
   693
    bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
deba@2284
   694
deba@2284
   695
    /// \brief Checks if a node is processed.
deba@2284
   696
    ///
deba@2284
   697
    /// Returns \c true if \c v is processed, i.e. the shortest
deba@2284
   698
    /// path to \c v has already found.
deba@2284
   699
    /// \pre \ref run() must be called before using this function.
deba@2284
   700
    bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
deba@2284
   701
    
deba@2284
   702
    ///@}
deba@2284
   703
  };
deba@2284
   704
deba@2284
   705
  /// \brief Default traits class of NagamochiIbaraki class.
deba@2284
   706
  ///
deba@2284
   707
  /// Default traits class of NagamochiIbaraki class.
deba@2284
   708
  /// \param Graph Graph type.
deba@2284
   709
  /// \param CapacityMap Type of length map.
deba@2284
   710
  template <typename _Graph, typename _CapacityMap>
deba@2284
   711
  struct NagamochiIbarakiDefaultTraits {
deba@2284
   712
    /// \brief The type of the capacity of the edges.
deba@2284
   713
    typedef typename _CapacityMap::Value Value;
deba@2284
   714
deba@2284
   715
    /// The graph type the algorithm runs on. 
deba@2284
   716
    typedef _Graph Graph;
deba@2284
   717
deba@2284
   718
    /// The AuxGraph type which is an Graph
deba@2284
   719
    typedef ListUGraph AuxGraph;
deba@2284
   720
deba@2284
   721
    /// \brief Instantiates a AuxGraph.
deba@2284
   722
    ///
deba@2284
   723
    /// This function instantiates a \ref AuxGraph. 
deba@2284
   724
    static AuxGraph *createAuxGraph() {
deba@2284
   725
      return new AuxGraph();
deba@2284
   726
    }
deba@2284
   727
deba@2284
   728
    /// \brief The type of the map that stores the edge capacities.
deba@2284
   729
    ///
deba@2284
   730
    /// The type of the map that stores the edge capacities.
deba@2284
   731
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
deba@2284
   732
    typedef _CapacityMap CapacityMap;
deba@2284
   733
deba@2284
   734
    /// \brief Instantiates a CapacityMap.
deba@2284
   735
    ///
deba@2284
   736
    /// This function instantiates a \ref CapacityMap.
deba@2284
   737
#ifdef DOXYGEN
deba@2284
   738
    static CapacityMap *createCapacityMap(const Graph& graph) 
deba@2284
   739
#else
deba@2284
   740
    static CapacityMap *createCapacityMap(const Graph&)
deba@2284
   741
#endif
deba@2284
   742
    {
deba@2284
   743
      throw UninitializedParameter();
deba@2284
   744
    }
deba@2284
   745
deba@2337
   746
    /// \brief The CutValueMap type
deba@2337
   747
    ///
deba@2337
   748
    /// The type of the map that stores the cut value of a node.
deba@2337
   749
    typedef AuxGraph::NodeMap<Value> AuxCutValueMap;
deba@2337
   750
deba@2337
   751
    /// \brief Instantiates a AuxCutValueMap.
deba@2337
   752
    ///
deba@2337
   753
    /// This function instantiates a \ref AuxCutValueMap. 
deba@2337
   754
    static AuxCutValueMap *createAuxCutValueMap(const AuxGraph& graph) {
deba@2337
   755
      return new AuxCutValueMap(graph);
deba@2337
   756
    }
deba@2337
   757
deba@2284
   758
    /// \brief The AuxCapacityMap type
deba@2284
   759
    ///
deba@2337
   760
    /// The type of the map that stores the auxiliary edge capacities.
deba@2284
   761
    typedef AuxGraph::UEdgeMap<Value> AuxCapacityMap;
deba@2284
   762
deba@2284
   763
    /// \brief Instantiates a AuxCapacityMap.
deba@2284
   764
    ///
deba@2284
   765
    /// This function instantiates a \ref AuxCapacityMap. 
deba@2284
   766
    static AuxCapacityMap *createAuxCapacityMap(const AuxGraph& graph) {
deba@2284
   767
      return new AuxCapacityMap(graph);
deba@2284
   768
    }
deba@2284
   769
deba@2284
   770
    /// \brief The cross reference type used by heap.
deba@2284
   771
    ///
deba@2284
   772
    /// The cross reference type used by heap.
deba@2284
   773
    /// Usually it is \c Graph::NodeMap<int>.
deba@2284
   774
    typedef AuxGraph::NodeMap<int> HeapCrossRef;
deba@2284
   775
deba@2284
   776
    /// \brief Instantiates a HeapCrossRef.
deba@2284
   777
    ///
deba@2284
   778
    /// This function instantiates a \ref HeapCrossRef. 
deba@2284
   779
    /// \param graph is the graph, to which we would like to define the 
deba@2284
   780
    /// HeapCrossRef.
deba@2284
   781
    static HeapCrossRef *createHeapCrossRef(const AuxGraph &graph) {
deba@2284
   782
      return new HeapCrossRef(graph);
deba@2284
   783
    }
deba@2284
   784
    
deba@2284
   785
    /// \brief The heap type used by NagamochiIbaraki algorithm.
deba@2284
   786
    ///
deba@2284
   787
    /// The heap type used by NagamochiIbaraki algorithm. It should
deba@2284
   788
    /// maximalize the priorities and the heap's key type is
deba@2284
   789
    /// the aux graph's node.
deba@2284
   790
    ///
deba@2284
   791
    /// \sa BinHeap
deba@2284
   792
    /// \sa NagamochiIbaraki
deba@2284
   793
    typedef typename _min_cut_bits
deba@2284
   794
    ::HeapSelector<CapacityMap>
deba@2284
   795
    ::template Selector<Value, HeapCrossRef>
deba@2284
   796
    ::Heap Heap;
deba@2284
   797
    
deba@2284
   798
    /// \brief Instantiates a Heap.
deba@2284
   799
    ///
deba@2284
   800
    /// This function instantiates a \ref Heap. 
deba@2284
   801
    /// \param crossref The cross reference of the heap.
deba@2284
   802
    static Heap *createHeap(HeapCrossRef& crossref) {
deba@2284
   803
      return new Heap(crossref);
deba@2284
   804
    }
deba@2284
   805
deba@2284
   806
    /// \brief Map from the AuxGraph's node type to the Graph's node type.
deba@2284
   807
    ///
deba@2284
   808
    /// Map from the AuxGraph's node type to the Graph's node type.
deba@2284
   809
    typedef typename AuxGraph
deba@2284
   810
    ::template NodeMap<typename Graph::Node> NodeRefMap;
deba@2284
   811
deba@2284
   812
    /// \brief Instantiates a NodeRefMap.
deba@2284
   813
    ///
deba@2284
   814
    /// This function instantiates a \ref NodeRefMap. 
deba@2284
   815
    static NodeRefMap *createNodeRefMap(const AuxGraph& graph) {
deba@2284
   816
      return new NodeRefMap(graph);
deba@2284
   817
    }
deba@2284
   818
deba@2284
   819
    /// \brief Map from the Graph's node type to the Graph's node type.
deba@2284
   820
    ///
deba@2284
   821
    /// Map from the Graph's node type to the Graph's node type.
deba@2284
   822
    typedef typename Graph
deba@2284
   823
    ::template NodeMap<typename Graph::Node> ListRefMap;
deba@2284
   824
deba@2284
   825
    /// \brief Instantiates a ListRefMap.
deba@2284
   826
    ///
deba@2284
   827
    /// This function instantiates a \ref ListRefMap. 
deba@2284
   828
    static ListRefMap *createListRefMap(const Graph& graph) {
deba@2284
   829
      return new ListRefMap(graph);
deba@2284
   830
    }
deba@2284
   831
    
deba@2284
   832
deba@2284
   833
  };
deba@2284
   834
deba@2284
   835
  /// \ingroup topology
deba@2284
   836
  ///
deba@2284
   837
  /// \brief Calculates the minimum cut in an undirected graph.
deba@2284
   838
  ///
deba@2284
   839
  /// Calculates the minimum cut in an undirected graph with the
deba@2284
   840
  /// Nagamochi-Ibaraki algorithm. The algorithm separates the graph's
deba@2284
   841
  /// nodes into two partitions with the minimum sum of edge capacities
deba@2284
   842
  /// between the two partitions. The algorithm can be used to test
deba@2284
   843
  /// the network reliability specifically to test how many links have
deba@2284
   844
  /// to be destroyed in the network to split it at least two
deba@2284
   845
  /// distinict subnetwork.
deba@2284
   846
  ///
deba@2284
   847
  /// The complexity of the algorithm is \f$ O(ne\log(n)) \f$ but with
deba@2337
   848
  /// Fibonacci heap it can be decreased to \f$ O(ne+n^2\log(n)) \f$. 
deba@2337
   849
  /// When capacity map is neutral then it uses BucketHeap which
deba@2284
   850
  /// results \f$ O(ne) \f$ time complexity.
deba@2337
   851
  ///
deba@2337
   852
  /// \warning The value type of the capacity map should be able to hold
deba@2337
   853
  /// any cut value of the graph, otherwise the result can overflow.
deba@2284
   854
#ifdef DOXYGEN
deba@2284
   855
  template <typename _Graph, typename _CapacityMap, typename _Traits>
deba@2284
   856
#else
deba@2284
   857
  template <typename _Graph = ListUGraph, 
deba@2284
   858
	    typename _CapacityMap = typename _Graph::template UEdgeMap<int>, 
deba@2284
   859
	    typename _Traits 
deba@2284
   860
            = NagamochiIbarakiDefaultTraits<_Graph, _CapacityMap> >
deba@2284
   861
#endif
deba@2284
   862
  class NagamochiIbaraki {
deba@2284
   863
  public:
deba@2284
   864
    /// \brief \ref Exception for uninitialized parameters.
deba@2284
   865
    ///
deba@2284
   866
    /// This error represents problems in the initialization
deba@2284
   867
    /// of the parameters of the algorithms.
deba@2284
   868
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@2284
   869
    public:
deba@2284
   870
      virtual const char* what() const throw() {
deba@2284
   871
	return "lemon::NagamochiIbaraki::UninitializedParameter";
deba@2284
   872
      }
deba@2284
   873
    };
deba@2284
   874
deba@2284
   875
deba@2284
   876
  private:
deba@2284
   877
deba@2284
   878
    typedef _Traits Traits;
deba@2284
   879
    /// The type of the underlying graph.
deba@2284
   880
    typedef typename Traits::Graph Graph;
deba@2284
   881
    
deba@2284
   882
    /// The type of the capacity of the edges.
deba@2284
   883
    typedef typename Traits::CapacityMap::Value Value;
deba@2284
   884
    /// The type of the map that stores the edge capacities.
deba@2284
   885
    typedef typename Traits::CapacityMap CapacityMap;
deba@2284
   886
    /// The type of the aux graph
deba@2284
   887
    typedef typename Traits::AuxGraph AuxGraph;
deba@2284
   888
    /// The type of the aux capacity map
deba@2284
   889
    typedef typename Traits::AuxCapacityMap AuxCapacityMap;
deba@2337
   890
    /// The type of the aux cut value map
deba@2337
   891
    typedef typename Traits::AuxCutValueMap AuxCutValueMap;
deba@2284
   892
    /// The cross reference type used for the current heap.
deba@2284
   893
    typedef typename Traits::HeapCrossRef HeapCrossRef;
deba@2284
   894
    /// The heap type used by the max cardinality algorithm.
deba@2284
   895
    typedef typename Traits::Heap Heap;
deba@2284
   896
    /// The node refrefernces between the original and aux graph type.
deba@2284
   897
    typedef typename Traits::NodeRefMap NodeRefMap;
deba@2284
   898
    /// The list node refrefernces in the original graph type.
deba@2284
   899
    typedef typename Traits::ListRefMap ListRefMap;
deba@2284
   900
deba@2284
   901
  public:
deba@2284
   902
deba@2284
   903
    ///\name Named template parameters
deba@2284
   904
deba@2284
   905
    ///@{
deba@2284
   906
deba@2284
   907
    struct DefNeutralCapacityTraits : public Traits {
deba@2284
   908
      typedef ConstMap<typename Graph::UEdge, Const<int, 1> > CapacityMap;
deba@2284
   909
      static CapacityMap *createCapacityMap(const Graph&) {
deba@2284
   910
	return new CapacityMap();
deba@2284
   911
      }
deba@2284
   912
    };
deba@2284
   913
    /// \brief \ref named-templ-param "Named parameter" for setting  
deba@2284
   914
    /// the capacity type to constMap<UEdge, int, 1>()
deba@2284
   915
    ///
deba@2284
   916
    /// \ref named-templ-param "Named parameter" for setting 
deba@2284
   917
    /// the capacity type to constMap<UEdge, int, 1>()
deba@2284
   918
    struct DefNeutralCapacity
deba@2284
   919
      : public NagamochiIbaraki<Graph, CapacityMap, 
deba@2284
   920
                                DefNeutralCapacityTraits> { 
deba@2284
   921
      typedef NagamochiIbaraki<Graph, CapacityMap, 
deba@2284
   922
                               DefNeutralCapacityTraits> Create;
deba@2284
   923
    };
deba@2284
   924
deba@2284
   925
deba@2284
   926
    template <class H, class CR>
deba@2284
   927
    struct DefHeapTraits : public Traits {
deba@2284
   928
      typedef CR HeapCrossRef;
deba@2284
   929
      typedef H Heap;
deba@2284
   930
      static HeapCrossRef *createHeapCrossRef(const AuxGraph &) {
deba@2284
   931
	throw UninitializedParameter();
deba@2284
   932
      }
deba@2284
   933
      static Heap *createHeap(HeapCrossRef &) {
deba@2284
   934
	throw UninitializedParameter();
deba@2284
   935
      }
deba@2284
   936
    };
deba@2284
   937
    /// \brief \ref named-templ-param "Named parameter" for setting heap 
deba@2284
   938
    /// and cross reference type
deba@2284
   939
    ///
deba@2284
   940
    /// \ref named-templ-param "Named parameter" for setting heap and cross 
deba@2284
   941
    /// reference type
deba@2284
   942
    template <class H, class CR = typename Graph::template NodeMap<int> >
deba@2284
   943
    struct DefHeap
deba@2284
   944
      : public NagamochiIbaraki<Graph, CapacityMap, 
deba@2284
   945
                                DefHeapTraits<H, CR> > { 
deba@2284
   946
      typedef NagamochiIbaraki< Graph, CapacityMap, 
deba@2284
   947
                                DefHeapTraits<H, CR> > Create;
deba@2284
   948
    };
deba@2284
   949
deba@2284
   950
    template <class H, class CR>
deba@2284
   951
    struct DefStandardHeapTraits : public Traits {
deba@2284
   952
      typedef CR HeapCrossRef;
deba@2284
   953
      typedef H Heap;
deba@2284
   954
      static HeapCrossRef *createHeapCrossRef(const AuxGraph &graph) {
deba@2284
   955
	return new HeapCrossRef(graph);
deba@2284
   956
      }
deba@2284
   957
      static Heap *createHeap(HeapCrossRef &crossref) {
deba@2284
   958
	return new Heap(crossref);
deba@2284
   959
      }
deba@2284
   960
    };
deba@2284
   961
deba@2284
   962
    /// \brief \ref named-templ-param "Named parameter" for setting heap and 
deba@2284
   963
    /// cross reference type with automatic allocation
deba@2284
   964
    ///
deba@2284
   965
    /// \ref named-templ-param "Named parameter" for setting heap and cross 
deba@2284
   966
    /// reference type. It can allocate the heap and the cross reference 
deba@2284
   967
    /// object if the cross reference's constructor waits for the graph as 
deba@2284
   968
    /// parameter and the heap's constructor waits for the cross reference.
deba@2284
   969
    template <class H, class CR = typename Graph::template NodeMap<int> >
deba@2284
   970
    struct DefStandardHeap
deba@2284
   971
      : public NagamochiIbaraki<Graph, CapacityMap, 
deba@2284
   972
                                DefStandardHeapTraits<H, CR> > { 
deba@2284
   973
      typedef NagamochiIbaraki<Graph, CapacityMap, 
deba@2284
   974
                               DefStandardHeapTraits<H, CR> > 
deba@2284
   975
      Create;
deba@2284
   976
    };
deba@2284
   977
deba@2284
   978
    ///@}
deba@2284
   979
deba@2284
   980
deba@2284
   981
  private:
deba@2284
   982
    /// Pointer to the underlying graph.
deba@2284
   983
    const Graph *_graph;
deba@2284
   984
    /// Pointer to the capacity map
deba@2284
   985
    const CapacityMap *_capacity;
deba@2284
   986
    /// \brief Indicates if \ref _capacity is locally allocated 
deba@2284
   987
    /// (\c true) or not.
deba@2284
   988
    bool local_capacity;
deba@2284
   989
deba@2284
   990
    /// Pointer to the aux graph.
deba@2284
   991
    AuxGraph *_aux_graph;
deba@2284
   992
    /// \brief Indicates if \ref _aux_graph is locally allocated 
deba@2284
   993
    /// (\c true) or not.
deba@2284
   994
    bool local_aux_graph;
deba@2284
   995
    /// Pointer to the aux capacity map
deba@2284
   996
    AuxCapacityMap *_aux_capacity;
deba@2284
   997
    /// \brief Indicates if \ref _aux_capacity is locally allocated 
deba@2284
   998
    /// (\c true) or not.
deba@2284
   999
    bool local_aux_capacity;
deba@2337
  1000
    /// Pointer to the aux cut value map
deba@2337
  1001
    AuxCutValueMap *_aux_cut_value;
deba@2337
  1002
    /// \brief Indicates if \ref _aux_cut_value is locally allocated 
deba@2337
  1003
    /// (\c true) or not.
deba@2337
  1004
    bool local_aux_cut_value;
deba@2284
  1005
    /// Pointer to the heap cross references.
deba@2284
  1006
    HeapCrossRef *_heap_cross_ref;
deba@2284
  1007
    /// \brief Indicates if \ref _heap_cross_ref is locally allocated 
deba@2284
  1008
    /// (\c true) or not.
deba@2284
  1009
    bool local_heap_cross_ref;
deba@2284
  1010
    /// Pointer to the heap.
deba@2284
  1011
    Heap *_heap;
deba@2284
  1012
    /// Indicates if \ref _heap is locally allocated (\c true) or not.
deba@2284
  1013
    bool local_heap;
deba@2284
  1014
deba@2284
  1015
    /// The min cut value.
deba@2284
  1016
    Value _min_cut;
deba@2284
  1017
    /// The number of the nodes of the aux graph.
deba@2284
  1018
    int _node_num;
deba@2337
  1019
    /// The first and last node of the min cut in the next list.
deba@2337
  1020
    std::vector<typename Graph::Node> _cut;
deba@2284
  1021
deba@2284
  1022
    /// \brief The first and last element in the list associated
deba@2284
  1023
    /// to the aux graph node.
deba@2284
  1024
    NodeRefMap *_first, *_last;
deba@2284
  1025
    /// \brief The next node in the node lists.
deba@2284
  1026
    ListRefMap *_next;
deba@2284
  1027
    
deba@2337
  1028
    void createStructures() {
deba@2284
  1029
      if (!_capacity) {
deba@2284
  1030
        local_capacity = true;
deba@2284
  1031
        _capacity = Traits::createCapacityMap(*_graph);
deba@2284
  1032
      }
deba@2284
  1033
      if(!_aux_graph) {
deba@2284
  1034
	local_aux_graph = true;
deba@2284
  1035
	_aux_graph = Traits::createAuxGraph();
deba@2284
  1036
      }
deba@2284
  1037
      if(!_aux_capacity) {
deba@2284
  1038
	local_aux_capacity = true;
deba@2284
  1039
	_aux_capacity = Traits::createAuxCapacityMap(*_aux_graph);
deba@2284
  1040
      }
deba@2337
  1041
      if(!_aux_cut_value) {
deba@2337
  1042
	local_aux_cut_value = true;
deba@2337
  1043
	_aux_cut_value = Traits::createAuxCutValueMap(*_aux_graph);
deba@2337
  1044
      }
deba@2284
  1045
deba@2284
  1046
      _first = Traits::createNodeRefMap(*_aux_graph);
deba@2284
  1047
      _last = Traits::createNodeRefMap(*_aux_graph);
deba@2284
  1048
deba@2284
  1049
      _next = Traits::createListRefMap(*_graph);
deba@2284
  1050
deba@2284
  1051
      if (!_heap_cross_ref) {
deba@2284
  1052
	local_heap_cross_ref = true;
deba@2284
  1053
	_heap_cross_ref = Traits::createHeapCrossRef(*_aux_graph);
deba@2284
  1054
      }
deba@2284
  1055
      if (!_heap) {
deba@2284
  1056
	local_heap = true;
deba@2284
  1057
	_heap = Traits::createHeap(*_heap_cross_ref);
deba@2284
  1058
      }
deba@2284
  1059
    }
deba@2284
  1060
deba@2337
  1061
    void createAuxGraph() {
deba@2337
  1062
      typename Graph::template NodeMap<typename AuxGraph::Node> ref(*_graph);
deba@2337
  1063
deba@2337
  1064
      for (typename Graph::NodeIt n(*_graph); n != INVALID; ++n) {
deba@2337
  1065
        _next->set(n, INVALID);
deba@2337
  1066
        typename AuxGraph::Node node = _aux_graph->addNode();
deba@2337
  1067
        _first->set(node, n);
deba@2337
  1068
        _last->set(node, n);
deba@2337
  1069
        ref.set(n, node);
deba@2337
  1070
      }
deba@2337
  1071
deba@2337
  1072
      typename AuxGraph::template NodeMap<typename AuxGraph::UEdge> 
deba@2337
  1073
      edges(*_aux_graph, INVALID);
deba@2337
  1074
deba@2337
  1075
      for (typename Graph::NodeIt n(*_graph); n != INVALID; ++n) {
deba@2337
  1076
        for (typename Graph::IncEdgeIt e(*_graph, n); e != INVALID; ++e) {
deba@2337
  1077
          typename Graph::Node tn = _graph->runningNode(e);
deba@2337
  1078
          if (n < tn || n == tn) continue;
deba@2337
  1079
          if (edges[ref[tn]] != INVALID) {
deba@2337
  1080
            Value value = 
deba@2337
  1081
              (*_aux_capacity)[edges[ref[tn]]] + (*_capacity)[e];
deba@2337
  1082
            _aux_capacity->set(edges[ref[tn]], value);
deba@2337
  1083
          } else {
deba@2337
  1084
            edges.set(ref[tn], _aux_graph->addEdge(ref[n], ref[tn]));
deba@2337
  1085
            Value value = (*_capacity)[e];
deba@2337
  1086
            _aux_capacity->set(edges[ref[tn]], value);            
deba@2337
  1087
          }
deba@2337
  1088
        }
deba@2337
  1089
        for (typename Graph::IncEdgeIt e(*_graph, n); e != INVALID; ++e) {
deba@2337
  1090
          typename Graph::Node tn = _graph->runningNode(e);
deba@2337
  1091
          edges.set(ref[tn], INVALID);
deba@2337
  1092
        }
deba@2337
  1093
      }
deba@2337
  1094
deba@2337
  1095
      _cut.resize(1, INVALID);
deba@2337
  1096
      _min_cut = std::numeric_limits<Value>::max();
deba@2337
  1097
      for (typename AuxGraph::NodeIt n(*_aux_graph); n != INVALID; ++n) {
deba@2337
  1098
        Value value = 0;
deba@2337
  1099
        for (typename AuxGraph::IncEdgeIt e(*_aux_graph, n); 
deba@2337
  1100
             e != INVALID; ++e) {
deba@2337
  1101
          value += (*_aux_capacity)[e];
deba@2337
  1102
        }
deba@2337
  1103
        if (_min_cut > value) {
deba@2337
  1104
          _min_cut = value;
deba@2337
  1105
          _cut[0] = (*_first)[n];
deba@2337
  1106
        } 
deba@2337
  1107
        (*_aux_cut_value)[n] = value;
deba@2337
  1108
      }
deba@2337
  1109
    }
deba@2337
  1110
    
deba@2337
  1111
deba@2284
  1112
  public :
deba@2284
  1113
deba@2284
  1114
    typedef NagamochiIbaraki Create;
deba@2284
  1115
deba@2284
  1116
deba@2284
  1117
    /// \brief Constructor.
deba@2284
  1118
    ///
deba@2284
  1119
    ///\param graph the graph the algorithm will run on.
deba@2284
  1120
    ///\param capacity the capacity map used by the algorithm.
deba@2284
  1121
    NagamochiIbaraki(const Graph& graph, const CapacityMap& capacity) 
deba@2284
  1122
      : _graph(&graph), 
deba@2284
  1123
        _capacity(&capacity), local_capacity(false),
deba@2284
  1124
        _aux_graph(0), local_aux_graph(false),
deba@2284
  1125
        _aux_capacity(0), local_aux_capacity(false),
deba@2337
  1126
        _aux_cut_value(0), local_aux_cut_value(false),
deba@2284
  1127
        _heap_cross_ref(0), local_heap_cross_ref(false),
deba@2284
  1128
        _heap(0), local_heap(false),
deba@2284
  1129
        _first(0), _last(0), _next(0) {}
deba@2284
  1130
deba@2284
  1131
    /// \brief Constructor.
deba@2284
  1132
    ///
deba@2284
  1133
    /// This constructor can be used only when the Traits class
deba@2284
  1134
    /// defines how can we instantiate a local capacity map.
deba@2284
  1135
    /// If the DefNeutralCapacity used the algorithm automatically
deba@2284
  1136
    /// construct the capacity map.
deba@2284
  1137
    ///
deba@2284
  1138
    ///\param graph the graph the algorithm will run on.
deba@2284
  1139
    NagamochiIbaraki(const Graph& graph) 
deba@2284
  1140
      : _graph(&graph), 
deba@2284
  1141
        _capacity(0), local_capacity(false),
deba@2284
  1142
        _aux_graph(0), local_aux_graph(false),
deba@2284
  1143
        _aux_capacity(0), local_aux_capacity(false),
deba@2337
  1144
        _aux_cut_value(0), local_aux_cut_value(false),
deba@2284
  1145
        _heap_cross_ref(0), local_heap_cross_ref(false),
deba@2284
  1146
        _heap(0), local_heap(false),
deba@2284
  1147
        _first(0), _last(0), _next(0) {}
deba@2284
  1148
deba@2284
  1149
    /// \brief Destructor.
deba@2284
  1150
    ///
deba@2284
  1151
    /// Destructor.
deba@2284
  1152
    ~NagamochiIbaraki() {
deba@2284
  1153
      if (local_heap) delete _heap;
deba@2284
  1154
      if (local_heap_cross_ref) delete _heap_cross_ref;
deba@2284
  1155
      if (_first) delete _first;
deba@2284
  1156
      if (_last) delete _last;
deba@2284
  1157
      if (_next) delete _next;
deba@2284
  1158
      if (local_aux_capacity) delete _aux_capacity;
deba@2337
  1159
      if (local_aux_cut_value) delete _aux_cut_value;
deba@2284
  1160
      if (local_aux_graph) delete _aux_graph;
deba@2284
  1161
      if (local_capacity) delete _capacity;
deba@2284
  1162
    }
deba@2284
  1163
deba@2284
  1164
    /// \brief Sets the heap and the cross reference used by algorithm.
deba@2284
  1165
    ///
deba@2284
  1166
    /// Sets the heap and the cross reference used by algorithm.
deba@2284
  1167
    /// If you don't use this function before calling \ref run(),
deba@2284
  1168
    /// it will allocate one. The destuctor deallocates this
deba@2284
  1169
    /// automatically allocated heap and cross reference, of course.
deba@2284
  1170
    /// \return <tt> (*this) </tt>
deba@2284
  1171
    NagamochiIbaraki &heap(Heap& heap, HeapCrossRef &crossRef)
deba@2284
  1172
    {
deba@2284
  1173
      if (local_heap_cross_ref) {
deba@2284
  1174
	delete _heap_cross_ref;
deba@2284
  1175
	local_heap_cross_ref=false;
deba@2284
  1176
      }
deba@2284
  1177
      _heap_cross_ref = &crossRef;
deba@2284
  1178
      if (local_heap) {
deba@2284
  1179
	delete _heap;
deba@2284
  1180
	local_heap=false;
deba@2284
  1181
      }
deba@2284
  1182
      _heap = &heap;
deba@2284
  1183
      return *this;
deba@2284
  1184
    }
deba@2284
  1185
deba@2284
  1186
    /// \brief Sets the aux graph.
deba@2284
  1187
    ///
deba@2284
  1188
    /// Sets the aux graph used by algorithm.
deba@2284
  1189
    /// If you don't use this function before calling \ref run(),
deba@2284
  1190
    /// it will allocate one. The destuctor deallocates this
deba@2284
  1191
    /// automatically allocated graph, of course.
deba@2284
  1192
    /// \return <tt> (*this) </tt>
deba@2284
  1193
    NagamochiIbaraki &auxGraph(AuxGraph& aux_graph)
deba@2284
  1194
    {
deba@2284
  1195
      if(local_aux_graph) {
deba@2284
  1196
	delete _aux_graph;
deba@2284
  1197
	local_aux_graph=false;
deba@2284
  1198
      }
deba@2284
  1199
      _aux_graph = &aux_graph;
deba@2284
  1200
      return *this;
deba@2284
  1201
    }
deba@2284
  1202
deba@2284
  1203
    /// \brief Sets the aux capacity map.
deba@2284
  1204
    ///
deba@2284
  1205
    /// Sets the aux capacity map used by algorithm.
deba@2284
  1206
    /// If you don't use this function before calling \ref run(),
deba@2284
  1207
    /// it will allocate one. The destuctor deallocates this
deba@2284
  1208
    /// automatically allocated graph, of course.
deba@2284
  1209
    /// \return <tt> (*this) </tt>
deba@2284
  1210
    NagamochiIbaraki &auxCapacityMap(AuxCapacityMap& aux_capacity_map)
deba@2284
  1211
    {
deba@2284
  1212
      if(local_aux_capacity) {
deba@2284
  1213
	delete _aux_capacity;
deba@2284
  1214
	local_aux_capacity=false;
deba@2284
  1215
      }
deba@2284
  1216
      _aux_capacity = &aux_capacity_map;
deba@2284
  1217
      return *this;
deba@2284
  1218
    }
deba@2284
  1219
deba@2284
  1220
    /// \name Execution control
deba@2284
  1221
    /// The simplest way to execute the algorithm is to use
deba@2284
  1222
    /// one of the member functions called \c run().
deba@2284
  1223
    /// \n
deba@2284
  1224
    /// If you need more control on the execution,
deba@2284
  1225
    /// first you must call \ref init() and then call the start()
deba@2284
  1226
    /// or proper times the processNextPhase() member functions.
deba@2284
  1227
deba@2284
  1228
    ///@{
deba@2284
  1229
deba@2284
  1230
    /// \brief Initializes the internal data structures.
deba@2284
  1231
    ///
deba@2284
  1232
    /// Initializes the internal data structures.
deba@2284
  1233
    void init() {
deba@2284
  1234
      _node_num = countNodes(*_graph);
deba@2337
  1235
      createStructures();
deba@2337
  1236
      createAuxGraph();
deba@2284
  1237
    }
deba@2284
  1238
deba@2337
  1239
  private:
deba@2337
  1240
deba@2337
  1241
    struct EdgeInfo {
deba@2337
  1242
      typename AuxGraph::Node source, target;
deba@2337
  1243
      Value capacity;
deba@2337
  1244
    };
deba@2337
  1245
    
deba@2337
  1246
    struct EdgeInfoLess {
deba@2337
  1247
      bool operator()(const EdgeInfo& left, const EdgeInfo& right) const {
deba@2337
  1248
        return (left.source < right.source) || 
deba@2337
  1249
          (left.source == right.source && left.target < right.target);
deba@2337
  1250
      }
deba@2337
  1251
    };
deba@2337
  1252
deba@2337
  1253
  public:
deba@2337
  1254
deba@2337
  1255
deba@2284
  1256
    /// \brief Processes the next phase
deba@2284
  1257
    ///
deba@2337
  1258
    /// Processes the next phase in the algorithm. The function should
deba@2337
  1259
    /// be called at most countNodes(graph) - 1 times to get surely
deba@2337
  1260
    /// the min cut in the graph.
deba@2284
  1261
    ///
deba@2284
  1262
    ///\return %True when the algorithm finished.
deba@2284
  1263
    bool processNextPhase() {
deba@2284
  1264
      if (_node_num <= 1) return true;
deba@2284
  1265
deba@2284
  1266
      typedef typename AuxGraph::Node Node;
deba@2284
  1267
      typedef typename AuxGraph::NodeIt NodeIt;
deba@2284
  1268
      typedef typename AuxGraph::UEdge UEdge;
deba@2337
  1269
      typedef typename AuxGraph::UEdgeIt UEdgeIt;
deba@2284
  1270
      typedef typename AuxGraph::IncEdgeIt IncEdgeIt;
deba@2284
  1271
      
deba@2337
  1272
      typename AuxGraph::template UEdgeMap<Value> _edge_cut(*_aux_graph);
deba@2337
  1273
deba@2337
  1274
deba@2337
  1275
      for (NodeIt n(*_aux_graph); n != INVALID; ++n) {
deba@2337
  1276
        _heap_cross_ref->set(n, Heap::PRE_HEAP);
deba@2284
  1277
      }
deba@2284
  1278
deba@2337
  1279
      std::vector<Node> nodes;
deba@2337
  1280
      nodes.reserve(_node_num);
deba@2337
  1281
      int sep = 0;
deba@2284
  1282
deba@2337
  1283
      Value alpha = 0;
deba@2337
  1284
      Value emc = std::numeric_limits<Value>::max();
deba@2284
  1285
deba@2337
  1286
      _heap->push(typename AuxGraph::NodeIt(*_aux_graph), 0);
deba@2337
  1287
      while (!_heap->empty()) {
deba@2337
  1288
        Node node = _heap->top();
deba@2337
  1289
        Value value = _heap->prio();
deba@2337
  1290
        
deba@2337
  1291
        _heap->pop();
deba@2337
  1292
        for (typename AuxGraph::IncEdgeIt e(*_aux_graph, node); 
deba@2337
  1293
             e != INVALID; ++e) {
deba@2337
  1294
          Node tn = _aux_graph->runningNode(e);
deba@2337
  1295
          switch (_heap->state(tn)) {
deba@2337
  1296
          case Heap::PRE_HEAP:
deba@2337
  1297
            _heap->push(tn, (*_aux_capacity)[e]);
deba@2337
  1298
            _edge_cut[e] = (*_heap)[tn];
deba@2337
  1299
            break;
deba@2337
  1300
          case Heap::IN_HEAP:
deba@2337
  1301
            _heap->decrease(tn, (*_aux_capacity)[e] + (*_heap)[tn]);
deba@2337
  1302
            _edge_cut[e] = (*_heap)[tn];
deba@2337
  1303
            break;
deba@2337
  1304
          case Heap::POST_HEAP:
deba@2337
  1305
            break;
deba@2337
  1306
          }
deba@2337
  1307
        }
deba@2284
  1308
deba@2337
  1309
        alpha += (*_aux_cut_value)[node];
deba@2337
  1310
        alpha -= 2 * value;
deba@2284
  1311
deba@2337
  1312
        nodes.push_back(node);
deba@2337
  1313
        if (!_heap->empty()) {
deba@2337
  1314
          if (alpha < emc) {
deba@2337
  1315
            emc = alpha;
deba@2337
  1316
            sep = nodes.size();
deba@2337
  1317
          }
deba@2284
  1318
        }
deba@2284
  1319
      }
deba@2284
  1320
deba@2337
  1321
      if ((int)nodes.size() < _node_num) {
deba@2337
  1322
        _aux_graph->clear();
deba@2337
  1323
        _node_num = 1;
deba@2337
  1324
        _cut.clear();
deba@2337
  1325
        for (int i = 0; i < (int)nodes.size(); ++i) {
deba@2337
  1326
          typename Graph::Node n = (*_first)[nodes[i]];
deba@2337
  1327
          while (n != INVALID) {
deba@2337
  1328
            _cut.push_back(n);
deba@2337
  1329
            n = (*_next)[n];
deba@2337
  1330
          }
deba@2337
  1331
        }
deba@2337
  1332
        _min_cut = 0;
deba@2337
  1333
        return true;
deba@2284
  1334
      }
deba@2284
  1335
deba@2337
  1336
      if (emc < _min_cut) {
deba@2337
  1337
        _cut.clear();
deba@2337
  1338
        for (int i = 0; i < sep; ++i) {
deba@2337
  1339
          typename Graph::Node n = (*_first)[nodes[i]];
deba@2337
  1340
          while (n != INVALID) {
deba@2337
  1341
            _cut.push_back(n);
deba@2337
  1342
            n = (*_next)[n];
deba@2337
  1343
          }
deba@2337
  1344
        }
deba@2337
  1345
        _min_cut = emc;
deba@2337
  1346
      }
deba@2284
  1347
deba@2337
  1348
      typedef typename AuxGraph::template NodeMap<int> UfeCr;
deba@2337
  1349
      UfeCr ufecr(*_aux_graph);
deba@2337
  1350
      typedef UnionFindEnum<UfeCr> Ufe; 
deba@2337
  1351
      Ufe ufe(ufecr);
deba@2337
  1352
deba@2337
  1353
      for (typename AuxGraph::NodeIt n(*_aux_graph); n != INVALID; ++n) {
deba@2337
  1354
        ufe.insert(n);
deba@2337
  1355
      }
deba@2337
  1356
deba@2337
  1357
      for (typename AuxGraph::UEdgeIt e(*_aux_graph); e != INVALID; ++e) {
deba@2337
  1358
        if (_edge_cut[e] >= emc) {
deba@2337
  1359
          ufe.join(_aux_graph->source(e), _aux_graph->target(e));
deba@2284
  1360
        }
deba@2284
  1361
      }
deba@2284
  1362
deba@2337
  1363
      if (ufe.size((typename Ufe::ClassIt)(ufe)) == _node_num) {
deba@2337
  1364
        _aux_graph->clear();
deba@2337
  1365
        _node_num = 1;
deba@2337
  1366
        return true;
deba@2337
  1367
      }
deba@2337
  1368
      
deba@2337
  1369
      std::vector<typename AuxGraph::Node> remnodes;
deba@2337
  1370
deba@2337
  1371
      typename AuxGraph::template NodeMap<UEdge> edges(*_aux_graph, INVALID);
deba@2337
  1372
      for (typename Ufe::ClassIt c(ufe); c != INVALID; ++c) {
deba@2337
  1373
        if (ufe.size(c) == 1) continue;
deba@2337
  1374
        for (typename Ufe::ItemIt r(ufe, c); r != INVALID; ++r) {
deba@2337
  1375
          if ((Node)r == (Node)c) continue;
deba@2337
  1376
          _next->set((*_last)[c], (*_first)[r]);
deba@2337
  1377
          _last->set(c, (*_last)[r]);
deba@2337
  1378
          remnodes.push_back(r);
deba@2337
  1379
          --_node_num;
deba@2337
  1380
        }
deba@2337
  1381
      }
deba@2337
  1382
deba@2337
  1383
      std::vector<EdgeInfo> addedges;
deba@2337
  1384
      std::vector<UEdge> remedges;
deba@2337
  1385
deba@2337
  1386
      for (typename AuxGraph::UEdgeIt e(*_aux_graph);
deba@2337
  1387
           e != INVALID; ++e) {
deba@2337
  1388
        Node sn = ufe.find(_aux_graph->source(e));
deba@2337
  1389
        Node tn = ufe.find(_aux_graph->target(e));
deba@2337
  1390
        if ((ufe.size(sn) == 1 && ufe.size(tn) == 1)) {
deba@2337
  1391
          continue;
deba@2337
  1392
        }
deba@2337
  1393
        if (sn == tn) {
deba@2337
  1394
          remedges.push_back(e);
deba@2337
  1395
          continue;
deba@2337
  1396
        }
deba@2337
  1397
        EdgeInfo info;
deba@2337
  1398
        if (sn < tn) {
deba@2337
  1399
          info.source = sn;
deba@2337
  1400
          info.target = tn;
deba@2337
  1401
        } else {
deba@2337
  1402
          info.source = tn;
deba@2337
  1403
          info.target = sn;
deba@2337
  1404
        }
deba@2337
  1405
        info.capacity = (*_aux_capacity)[e];
deba@2337
  1406
        addedges.push_back(info);
deba@2337
  1407
        remedges.push_back(e);
deba@2337
  1408
      }
deba@2337
  1409
deba@2337
  1410
      for (int i = 0; i < (int)remedges.size(); ++i) {
deba@2337
  1411
        _aux_graph->erase(remedges[i]);
deba@2337
  1412
      }
deba@2337
  1413
deba@2337
  1414
      sort(addedges.begin(), addedges.end(), EdgeInfoLess());
deba@2337
  1415
deba@2337
  1416
      {
deba@2337
  1417
        int i = 0;
deba@2337
  1418
        while (i < (int)addedges.size()) {
deba@2337
  1419
          Node sn = addedges[i].source;
deba@2337
  1420
          Node tn = addedges[i].target;
deba@2337
  1421
          Value ec = addedges[i].capacity;
deba@2337
  1422
          ++i;
deba@2337
  1423
          while (i < (int)addedges.size() && 
deba@2337
  1424
                 sn == addedges[i].source && tn == addedges[i].target) {
deba@2337
  1425
            ec += addedges[i].capacity;
deba@2337
  1426
            ++i;
deba@2337
  1427
          }
deba@2337
  1428
          typename AuxGraph::UEdge ne = _aux_graph->addEdge(sn, tn);
deba@2337
  1429
          (*_aux_capacity)[ne] = ec;
deba@2337
  1430
        }
deba@2337
  1431
      }
deba@2337
  1432
deba@2337
  1433
      for (typename Ufe::ClassIt c(ufe); c != INVALID; ++c) {
deba@2337
  1434
        if (ufe.size(c) == 1) continue;
deba@2337
  1435
        Value cutvalue = 0;
deba@2337
  1436
        for (typename AuxGraph::IncEdgeIt e(*_aux_graph, c);
deba@2337
  1437
             e != INVALID; ++e) {
deba@2337
  1438
          cutvalue += (*_aux_capacity)[e];
deba@2337
  1439
        }
deba@2337
  1440
        
deba@2337
  1441
        (*_aux_cut_value)[c] = cutvalue;
deba@2337
  1442
        
deba@2337
  1443
      }
deba@2337
  1444
deba@2337
  1445
      for (int i = 0; i < (int)remnodes.size(); ++i) {
deba@2337
  1446
        _aux_graph->erase(remnodes[i]);
deba@2337
  1447
      }
deba@2337
  1448
deba@2284
  1449
      return _node_num == 1;
deba@2284
  1450
    }
deba@2284
  1451
deba@2284
  1452
    /// \brief Executes the algorithm.
deba@2284
  1453
    ///
deba@2284
  1454
    /// Executes the algorithm.
deba@2284
  1455
    ///
deba@2284
  1456
    /// \pre init() must be called
deba@2284
  1457
    void start() {
deba@2284
  1458
      while (!processNextPhase());
deba@2284
  1459
    }
deba@2284
  1460
deba@2284
  1461
deba@2284
  1462
    /// \brief Runs %NagamochiIbaraki algorithm.
deba@2284
  1463
    ///
deba@2284
  1464
    /// This method runs the %Min cut algorithm
deba@2284
  1465
    ///
deba@2284
  1466
    /// \note mc.run(s) is just a shortcut of the following code.
deba@2284
  1467
    ///\code
deba@2284
  1468
    ///  mc.init();
deba@2284
  1469
    ///  mc.start();
deba@2284
  1470
    ///\endcode
deba@2284
  1471
    void run() {
deba@2284
  1472
      init();
deba@2284
  1473
      start();
deba@2284
  1474
    }
deba@2284
  1475
deba@2284
  1476
    ///@}
deba@2284
  1477
deba@2284
  1478
    /// \name Query Functions 
deba@2284
  1479
    ///
deba@2284
  1480
    /// The result of the %NagamochiIbaraki
deba@2284
  1481
    /// algorithm can be obtained using these functions.\n 
deba@2284
  1482
    /// Before the use of these functions, either run() or start()
deba@2284
  1483
    /// must be called.
deba@2284
  1484
    
deba@2284
  1485
    ///@{
deba@2284
  1486
deba@2284
  1487
    /// \brief Returns the min cut value.
deba@2284
  1488
    ///
deba@2284
  1489
    /// Returns the min cut value if the algorithm finished.
deba@2284
  1490
    /// After the first processNextPhase() it is a value of a
deba@2284
  1491
    /// valid cut in the graph.
deba@2284
  1492
    Value minCut() const {
deba@2284
  1493
      return _min_cut;
deba@2284
  1494
    }
deba@2284
  1495
deba@2284
  1496
    /// \brief Returns a min cut in a NodeMap.
deba@2284
  1497
    ///
deba@2284
  1498
    /// It sets the nodes of one of the two partitions to true in
deba@2284
  1499
    /// the given BoolNodeMap. The map contains a valid cut if the
deba@2284
  1500
    /// map have been set false previously. 
deba@2284
  1501
    template <typename NodeMap>
deba@2284
  1502
    Value quickMinCut(NodeMap& nodeMap) const { 
deba@2337
  1503
      for (int i = 0; i < (int)_cut.size(); ++i) {
deba@2337
  1504
        nodeMap.set(_cut[i], true);
deba@2337
  1505
      }
deba@2284
  1506
      return minCut();
deba@2284
  1507
    }
deba@2284
  1508
deba@2284
  1509
    /// \brief Returns a min cut in a NodeMap.
deba@2284
  1510
    ///
deba@2284
  1511
    /// It sets the nodes of one of the two partitions to true and
deba@2284
  1512
    /// the other partition to false. The function first set all of the
deba@2284
  1513
    /// nodes to false and after it call the quickMinCut() member.
deba@2284
  1514
    template <typename NodeMap>
deba@2284
  1515
    Value minCut(NodeMap& nodeMap) const { 
deba@2284
  1516
      for (typename Graph::NodeIt it(*_graph); it != INVALID; ++it) {
deba@2284
  1517
        nodeMap.set(it, false);      
deba@2284
  1518
      }
deba@2284
  1519
      quickMinCut(nodeMap);
deba@2284
  1520
      return minCut();
deba@2284
  1521
    }
deba@2284
  1522
deba@2284
  1523
    /// \brief Returns a min cut in an EdgeMap.
deba@2284
  1524
    ///
deba@2284
  1525
    /// If an undirected edge is in a min cut then it will be
deba@2284
  1526
    /// set to true and the others will be set to false in the given map.
deba@2284
  1527
    template <typename EdgeMap>
deba@2284
  1528
    Value cutEdges(EdgeMap& edgeMap) const {
deba@2284
  1529
      typename Graph::template NodeMap<bool> cut(*_graph, false);
deba@2284
  1530
      quickMinCut(cut);
deba@2284
  1531
      for (typename Graph::EdgeIt it(*_graph); it != INVALID; ++it) {
deba@2284
  1532
        edgeMap.set(it, cut[_graph->source(it)] ^ cut[_graph->target(it)]);
deba@2284
  1533
      }
deba@2284
  1534
      return minCut();
deba@2284
  1535
    }
deba@2284
  1536
deba@2284
  1537
    ///@}
deba@2284
  1538
deba@2337
  1539
  private:
deba@2337
  1540
deba@2337
  1541
deba@2284
  1542
  };
deba@2284
  1543
}
deba@2284
  1544
deba@2284
  1545
#endif