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