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