lemon/nagamochi_ibaraki.h
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 28 Apr 2015 18:13:42 +0200
changeset 1343 20f95cd51aba
parent 1130 eb252f805431
child 1416 f179aa1045a4
permissions -rw-r--r--
Merge bugfix #595
deba@1017
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@1017
     2
 *
deba@1017
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@1017
     4
 *
alpar@1270
     5
 * Copyright (C) 2003-2013
deba@1017
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1017
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1017
     8
 *
deba@1017
     9
 * Permission to use, modify and distribute this software is granted
deba@1017
    10
 * provided that this copyright notice appears in all copies. For
deba@1017
    11
 * precise terms see the accompanying LICENSE file.
deba@1017
    12
 *
deba@1017
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1017
    14
 * express or implied, and with no claim as to its suitability for any
deba@1017
    15
 * purpose.
deba@1017
    16
 *
deba@1017
    17
 */
deba@1017
    18
deba@1017
    19
#ifndef LEMON_NAGAMOCHI_IBARAKI_H
deba@1017
    20
#define LEMON_NAGAMOCHI_IBARAKI_H
deba@1017
    21
deba@1017
    22
deba@1017
    23
/// \ingroup min_cut
deba@1017
    24
/// \file
deba@1017
    25
/// \brief Implementation of the Nagamochi-Ibaraki algorithm.
deba@1017
    26
deba@1017
    27
#include <lemon/core.h>
deba@1017
    28
#include <lemon/bin_heap.h>
deba@1017
    29
#include <lemon/bucket_heap.h>
deba@1017
    30
#include <lemon/maps.h>
deba@1017
    31
#include <lemon/radix_sort.h>
deba@1017
    32
#include <lemon/unionfind.h>
deba@1017
    33
deba@1017
    34
#include <cassert>
deba@1017
    35
deba@1017
    36
namespace lemon {
deba@1017
    37
deba@1017
    38
  /// \brief Default traits class for NagamochiIbaraki class.
deba@1017
    39
  ///
deba@1017
    40
  /// Default traits class for NagamochiIbaraki class.
deba@1017
    41
  /// \param GR The undirected graph type.
deba@1017
    42
  /// \param CM Type of capacity map.
deba@1017
    43
  template <typename GR, typename CM>
deba@1017
    44
  struct NagamochiIbarakiDefaultTraits {
deba@1017
    45
    /// The type of the capacity map.
deba@1017
    46
    typedef typename CM::Value Value;
deba@1017
    47
deba@1017
    48
    /// The undirected graph type the algorithm runs on.
deba@1017
    49
    typedef GR Graph;
deba@1017
    50
deba@1017
    51
    /// \brief The type of the map that stores the edge capacities.
deba@1017
    52
    ///
deba@1017
    53
    /// The type of the map that stores the edge capacities.
deba@1017
    54
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
deba@1017
    55
    typedef CM CapacityMap;
deba@1017
    56
deba@1017
    57
    /// \brief Instantiates a CapacityMap.
deba@1017
    58
    ///
deba@1017
    59
    /// This function instantiates a \ref CapacityMap.
deba@1017
    60
#ifdef DOXYGEN
deba@1017
    61
    static CapacityMap *createCapacityMap(const Graph& graph)
deba@1017
    62
#else
deba@1017
    63
    static CapacityMap *createCapacityMap(const Graph&)
deba@1017
    64
#endif
deba@1017
    65
    {
deba@1017
    66
        LEMON_ASSERT(false, "CapacityMap is not initialized");
deba@1017
    67
        return 0; // ignore warnings
deba@1017
    68
    }
deba@1017
    69
deba@1017
    70
    /// \brief The cross reference type used by heap.
deba@1017
    71
    ///
deba@1017
    72
    /// The cross reference type used by heap.
deba@1017
    73
    /// Usually \c Graph::NodeMap<int>.
deba@1017
    74
    typedef typename Graph::template NodeMap<int> HeapCrossRef;
deba@1017
    75
deba@1017
    76
    /// \brief Instantiates a HeapCrossRef.
deba@1017
    77
    ///
deba@1017
    78
    /// This function instantiates a \ref HeapCrossRef.
deba@1017
    79
    /// \param g is the graph, to which we would like to define the
deba@1017
    80
    /// \ref HeapCrossRef.
deba@1017
    81
    static HeapCrossRef *createHeapCrossRef(const Graph& g) {
deba@1017
    82
      return new HeapCrossRef(g);
deba@1017
    83
    }
deba@1017
    84
deba@1017
    85
    /// \brief The heap type used by NagamochiIbaraki algorithm.
deba@1017
    86
    ///
deba@1017
    87
    /// The heap type used by NagamochiIbaraki algorithm. It has to
deba@1017
    88
    /// maximize the priorities.
deba@1017
    89
    ///
deba@1017
    90
    /// \sa BinHeap
deba@1017
    91
    /// \sa NagamochiIbaraki
deba@1017
    92
    typedef BinHeap<Value, HeapCrossRef, std::greater<Value> > Heap;
deba@1017
    93
deba@1017
    94
    /// \brief Instantiates a Heap.
deba@1017
    95
    ///
deba@1017
    96
    /// This function instantiates a \ref Heap.
deba@1017
    97
    /// \param r is the cross reference of the heap.
deba@1017
    98
    static Heap *createHeap(HeapCrossRef& r) {
deba@1017
    99
      return new Heap(r);
deba@1017
   100
    }
deba@1017
   101
  };
deba@1017
   102
deba@1017
   103
  /// \ingroup min_cut
deba@1017
   104
  ///
deba@1017
   105
  /// \brief Calculates the minimum cut in an undirected graph.
deba@1017
   106
  ///
deba@1017
   107
  /// Calculates the minimum cut in an undirected graph with the
deba@1017
   108
  /// Nagamochi-Ibaraki algorithm. The algorithm separates the graph's
deba@1017
   109
  /// nodes into two partitions with the minimum sum of edge capacities
deba@1017
   110
  /// between the two partitions. The algorithm can be used to test
deba@1017
   111
  /// the network reliability, especially to test how many links have
deba@1017
   112
  /// to be destroyed in the network to split it to at least two
deba@1017
   113
  /// distinict subnetworks.
deba@1017
   114
  ///
deba@1017
   115
  /// The complexity of the algorithm is \f$ O(nm\log(n)) \f$ but with
deba@1017
   116
  /// \ref FibHeap "Fibonacci heap" it can be decreased to
deba@1017
   117
  /// \f$ O(nm+n^2\log(n)) \f$.  When the edges have unit capacities,
deba@1017
   118
  /// \c BucketHeap can be used which yields \f$ O(nm) \f$ time
deba@1017
   119
  /// complexity.
deba@1017
   120
  ///
deba@1017
   121
  /// \warning The value type of the capacity map should be able to
deba@1017
   122
  /// hold any cut value of the graph, otherwise the result can
deba@1017
   123
  /// overflow.
deba@1017
   124
  /// \note This capacity is supposed to be integer type.
deba@1017
   125
#ifdef DOXYGEN
deba@1017
   126
  template <typename GR, typename CM, typename TR>
deba@1017
   127
#else
deba@1017
   128
  template <typename GR,
deba@1017
   129
            typename CM = typename GR::template EdgeMap<int>,
deba@1017
   130
            typename TR = NagamochiIbarakiDefaultTraits<GR, CM> >
deba@1017
   131
#endif
deba@1017
   132
  class NagamochiIbaraki {
deba@1017
   133
  public:
deba@1017
   134
deba@1017
   135
    typedef TR Traits;
deba@1017
   136
    /// The type of the underlying graph.
deba@1017
   137
    typedef typename Traits::Graph Graph;
deba@1017
   138
deba@1017
   139
    /// The type of the capacity map.
deba@1017
   140
    typedef typename Traits::CapacityMap CapacityMap;
deba@1017
   141
    /// The value type of the capacity map.
deba@1017
   142
    typedef typename Traits::CapacityMap::Value Value;
deba@1017
   143
deba@1017
   144
    /// The heap type used by the algorithm.
deba@1017
   145
    typedef typename Traits::Heap Heap;
deba@1017
   146
    /// The cross reference type used for the heap.
deba@1017
   147
    typedef typename Traits::HeapCrossRef HeapCrossRef;
deba@1017
   148
deba@1017
   149
    ///\name Named template parameters
deba@1017
   150
deba@1017
   151
    ///@{
deba@1017
   152
deba@1017
   153
    struct SetUnitCapacityTraits : public Traits {
deba@1017
   154
      typedef ConstMap<typename Graph::Edge, Const<int, 1> > CapacityMap;
deba@1017
   155
      static CapacityMap *createCapacityMap(const Graph&) {
deba@1017
   156
        return new CapacityMap();
deba@1017
   157
      }
deba@1017
   158
    };
deba@1017
   159
deba@1017
   160
    /// \brief \ref named-templ-param "Named parameter" for setting
deba@1017
   161
    /// the capacity map to a constMap<Edge, int, 1>() instance
deba@1017
   162
    ///
deba@1017
   163
    /// \ref named-templ-param "Named parameter" for setting
deba@1017
   164
    /// the capacity map to a constMap<Edge, int, 1>() instance
deba@1017
   165
    struct SetUnitCapacity
deba@1017
   166
      : public NagamochiIbaraki<Graph, CapacityMap,
deba@1017
   167
                                SetUnitCapacityTraits> {
deba@1017
   168
      typedef NagamochiIbaraki<Graph, CapacityMap,
deba@1017
   169
                               SetUnitCapacityTraits> Create;
deba@1017
   170
    };
deba@1017
   171
deba@1017
   172
deba@1017
   173
    template <class H, class CR>
deba@1017
   174
    struct SetHeapTraits : public Traits {
deba@1017
   175
      typedef CR HeapCrossRef;
deba@1017
   176
      typedef H Heap;
deba@1017
   177
      static HeapCrossRef *createHeapCrossRef(int num) {
deba@1017
   178
        LEMON_ASSERT(false, "HeapCrossRef is not initialized");
deba@1017
   179
        return 0; // ignore warnings
deba@1017
   180
      }
deba@1017
   181
      static Heap *createHeap(HeapCrossRef &) {
deba@1017
   182
        LEMON_ASSERT(false, "Heap is not initialized");
deba@1017
   183
        return 0; // ignore warnings
deba@1017
   184
      }
deba@1017
   185
    };
deba@1017
   186
deba@1017
   187
    /// \brief \ref named-templ-param "Named parameter" for setting
deba@1017
   188
    /// heap and cross reference type
deba@1017
   189
    ///
deba@1017
   190
    /// \ref named-templ-param "Named parameter" for setting heap and
deba@1017
   191
    /// cross reference type. The heap has to maximize the priorities.
deba@1017
   192
    template <class H, class CR = RangeMap<int> >
deba@1017
   193
    struct SetHeap
deba@1017
   194
      : public NagamochiIbaraki<Graph, CapacityMap, SetHeapTraits<H, CR> > {
deba@1017
   195
      typedef NagamochiIbaraki< Graph, CapacityMap, SetHeapTraits<H, CR> >
deba@1017
   196
      Create;
deba@1017
   197
    };
deba@1017
   198
deba@1017
   199
    template <class H, class CR>
deba@1017
   200
    struct SetStandardHeapTraits : public Traits {
deba@1017
   201
      typedef CR HeapCrossRef;
deba@1017
   202
      typedef H Heap;
deba@1017
   203
      static HeapCrossRef *createHeapCrossRef(int size) {
deba@1017
   204
        return new HeapCrossRef(size);
deba@1017
   205
      }
deba@1017
   206
      static Heap *createHeap(HeapCrossRef &crossref) {
deba@1017
   207
        return new Heap(crossref);
deba@1017
   208
      }
deba@1017
   209
    };
deba@1017
   210
deba@1017
   211
    /// \brief \ref named-templ-param "Named parameter" for setting
deba@1017
   212
    /// heap and cross reference type with automatic allocation
deba@1017
   213
    ///
deba@1017
   214
    /// \ref named-templ-param "Named parameter" for setting heap and
deba@1017
   215
    /// cross reference type with automatic allocation. They should
deba@1017
   216
    /// have standard constructor interfaces to be able to
deba@1017
   217
    /// automatically created by the algorithm (i.e. the graph should
deba@1017
   218
    /// be passed to the constructor of the cross reference and the
deba@1017
   219
    /// cross reference should be passed to the constructor of the
deba@1017
   220
    /// heap). However, external heap and cross reference objects
deba@1017
   221
    /// could also be passed to the algorithm using the \ref heap()
deba@1017
   222
    /// function before calling \ref run() or \ref init(). The heap
deba@1017
   223
    /// has to maximize the priorities.
deba@1017
   224
    /// \sa SetHeap
deba@1017
   225
    template <class H, class CR = RangeMap<int> >
deba@1017
   226
    struct SetStandardHeap
deba@1017
   227
      : public NagamochiIbaraki<Graph, CapacityMap,
deba@1017
   228
                                SetStandardHeapTraits<H, CR> > {
deba@1017
   229
      typedef NagamochiIbaraki<Graph, CapacityMap,
deba@1017
   230
                               SetStandardHeapTraits<H, CR> > Create;
deba@1017
   231
    };
deba@1017
   232
deba@1017
   233
    ///@}
deba@1017
   234
deba@1017
   235
deba@1017
   236
  private:
deba@1017
   237
deba@1017
   238
    const Graph &_graph;
deba@1017
   239
    const CapacityMap *_capacity;
deba@1017
   240
    bool _local_capacity; // unit capacity
deba@1017
   241
deba@1017
   242
    struct ArcData {
deba@1017
   243
      typename Graph::Node target;
deba@1017
   244
      int prev, next;
deba@1017
   245
    };
deba@1017
   246
    struct EdgeData {
deba@1017
   247
      Value capacity;
deba@1017
   248
      Value cut;
deba@1017
   249
    };
deba@1017
   250
deba@1017
   251
    struct NodeData {
deba@1017
   252
      int first_arc;
deba@1017
   253
      typename Graph::Node prev, next;
deba@1017
   254
      int curr_arc;
deba@1017
   255
      typename Graph::Node last_rep;
deba@1017
   256
      Value sum;
deba@1017
   257
    };
deba@1017
   258
deba@1017
   259
    typename Graph::template NodeMap<NodeData> *_nodes;
deba@1017
   260
    std::vector<ArcData> _arcs;
deba@1017
   261
    std::vector<EdgeData> _edges;
deba@1017
   262
deba@1017
   263
    typename Graph::Node _first_node;
deba@1017
   264
    int _node_num;
deba@1017
   265
deba@1017
   266
    Value _min_cut;
deba@1017
   267
deba@1017
   268
    HeapCrossRef *_heap_cross_ref;
deba@1017
   269
    bool _local_heap_cross_ref;
deba@1017
   270
    Heap *_heap;
deba@1017
   271
    bool _local_heap;
deba@1017
   272
deba@1017
   273
    typedef typename Graph::template NodeMap<typename Graph::Node> NodeList;
deba@1017
   274
    NodeList *_next_rep;
deba@1017
   275
deba@1017
   276
    typedef typename Graph::template NodeMap<bool> MinCutMap;
deba@1017
   277
    MinCutMap *_cut_map;
deba@1017
   278
deba@1017
   279
    void createStructures() {
deba@1017
   280
      if (!_nodes) {
deba@1017
   281
        _nodes = new (typename Graph::template NodeMap<NodeData>)(_graph);
deba@1017
   282
      }
deba@1017
   283
      if (!_capacity) {
deba@1017
   284
        _local_capacity = true;
deba@1017
   285
        _capacity = Traits::createCapacityMap(_graph);
deba@1017
   286
      }
deba@1017
   287
      if (!_heap_cross_ref) {
deba@1017
   288
        _local_heap_cross_ref = true;
deba@1017
   289
        _heap_cross_ref = Traits::createHeapCrossRef(_graph);
deba@1017
   290
      }
deba@1017
   291
      if (!_heap) {
deba@1017
   292
        _local_heap = true;
deba@1017
   293
        _heap = Traits::createHeap(*_heap_cross_ref);
deba@1017
   294
      }
deba@1017
   295
      if (!_next_rep) {
deba@1017
   296
        _next_rep = new NodeList(_graph);
deba@1017
   297
      }
deba@1017
   298
      if (!_cut_map) {
deba@1017
   299
        _cut_map = new MinCutMap(_graph);
deba@1017
   300
      }
deba@1017
   301
    }
deba@1017
   302
alpar@1130
   303
  protected:
alpar@1130
   304
    //This is here to avoid a gcc-3.3 compilation error.
alpar@1130
   305
    //It should never be called.
alpar@1270
   306
    NagamochiIbaraki() {}
alpar@1270
   307
alpar@1130
   308
  public:
deba@1017
   309
deba@1017
   310
    typedef NagamochiIbaraki Create;
deba@1017
   311
deba@1017
   312
deba@1017
   313
    /// \brief Constructor.
deba@1017
   314
    ///
deba@1017
   315
    /// \param graph The graph the algorithm runs on.
deba@1017
   316
    /// \param capacity The capacity map used by the algorithm.
deba@1017
   317
    NagamochiIbaraki(const Graph& graph, const CapacityMap& capacity)
deba@1017
   318
      : _graph(graph), _capacity(&capacity), _local_capacity(false),
deba@1017
   319
        _nodes(0), _arcs(), _edges(), _min_cut(),
deba@1017
   320
        _heap_cross_ref(0), _local_heap_cross_ref(false),
deba@1017
   321
        _heap(0), _local_heap(false),
deba@1017
   322
        _next_rep(0), _cut_map(0) {}
deba@1017
   323
deba@1017
   324
    /// \brief Constructor.
deba@1017
   325
    ///
deba@1017
   326
    /// This constructor can be used only when the Traits class
deba@1017
   327
    /// defines how can the local capacity map be instantiated.
deba@1017
   328
    /// If the SetUnitCapacity used the algorithm automatically
deba@1017
   329
    /// constructs the capacity map.
deba@1017
   330
    ///
deba@1017
   331
    ///\param graph The graph the algorithm runs on.
deba@1017
   332
    NagamochiIbaraki(const Graph& graph)
deba@1017
   333
      : _graph(graph), _capacity(0), _local_capacity(false),
deba@1017
   334
        _nodes(0), _arcs(), _edges(), _min_cut(),
deba@1017
   335
        _heap_cross_ref(0), _local_heap_cross_ref(false),
deba@1017
   336
        _heap(0), _local_heap(false),
deba@1017
   337
        _next_rep(0), _cut_map(0) {}
deba@1017
   338
deba@1017
   339
    /// \brief Destructor.
deba@1017
   340
    ///
deba@1017
   341
    /// Destructor.
deba@1017
   342
    ~NagamochiIbaraki() {
deba@1017
   343
      if (_local_capacity) delete _capacity;
deba@1017
   344
      if (_nodes) delete _nodes;
deba@1017
   345
      if (_local_heap) delete _heap;
deba@1017
   346
      if (_local_heap_cross_ref) delete _heap_cross_ref;
deba@1017
   347
      if (_next_rep) delete _next_rep;
deba@1017
   348
      if (_cut_map) delete _cut_map;
deba@1017
   349
    }
deba@1017
   350
deba@1017
   351
    /// \brief Sets the heap and the cross reference used by algorithm.
deba@1017
   352
    ///
deba@1017
   353
    /// Sets the heap and the cross reference used by algorithm.
deba@1017
   354
    /// If you don't use this function before calling \ref run(),
deba@1017
   355
    /// it will allocate one. The destuctor deallocates this
deba@1017
   356
    /// automatically allocated heap and cross reference, of course.
deba@1017
   357
    /// \return <tt> (*this) </tt>
deba@1017
   358
    NagamochiIbaraki &heap(Heap& hp, HeapCrossRef &cr)
deba@1017
   359
    {
deba@1017
   360
      if (_local_heap_cross_ref) {
deba@1017
   361
        delete _heap_cross_ref;
deba@1017
   362
        _local_heap_cross_ref = false;
deba@1017
   363
      }
deba@1017
   364
      _heap_cross_ref = &cr;
deba@1017
   365
      if (_local_heap) {
deba@1017
   366
        delete _heap;
deba@1017
   367
        _local_heap = false;
deba@1017
   368
      }
deba@1017
   369
      _heap = &hp;
deba@1017
   370
      return *this;
deba@1017
   371
    }
deba@1017
   372
deba@1017
   373
    /// \name Execution control
deba@1017
   374
    /// The simplest way to execute the algorithm is to use
deba@1017
   375
    /// one of the member functions called \c run().
deba@1017
   376
    /// \n
deba@1017
   377
    /// If you need more control on the execution,
deba@1017
   378
    /// first you must call \ref init() and then call the start()
deba@1017
   379
    /// or proper times the processNextPhase() member functions.
deba@1017
   380
deba@1017
   381
    ///@{
deba@1017
   382
deba@1017
   383
    /// \brief Initializes the internal data structures.
deba@1017
   384
    ///
deba@1017
   385
    /// Initializes the internal data structures.
deba@1017
   386
    void init() {
deba@1017
   387
      createStructures();
deba@1017
   388
deba@1017
   389
      int edge_num = countEdges(_graph);
deba@1017
   390
      _edges.resize(edge_num);
deba@1017
   391
      _arcs.resize(2 * edge_num);
deba@1017
   392
deba@1017
   393
      typename Graph::Node prev = INVALID;
deba@1017
   394
      _node_num = 0;
deba@1017
   395
      for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
deba@1017
   396
        (*_cut_map)[n] = false;
deba@1017
   397
        (*_next_rep)[n] = INVALID;
deba@1017
   398
        (*_nodes)[n].last_rep = n;
deba@1017
   399
        (*_nodes)[n].first_arc = -1;
deba@1017
   400
        (*_nodes)[n].curr_arc = -1;
deba@1017
   401
        (*_nodes)[n].prev = prev;
deba@1017
   402
        if (prev != INVALID) {
deba@1017
   403
          (*_nodes)[prev].next = n;
deba@1017
   404
        }
deba@1017
   405
        (*_nodes)[n].next = INVALID;
deba@1017
   406
        (*_nodes)[n].sum = 0;
deba@1017
   407
        prev = n;
deba@1017
   408
        ++_node_num;
deba@1017
   409
      }
deba@1017
   410
deba@1017
   411
      _first_node = typename Graph::NodeIt(_graph);
deba@1017
   412
deba@1017
   413
      int index = 0;
deba@1017
   414
      for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
deba@1017
   415
        for (typename Graph::OutArcIt a(_graph, n); a != INVALID; ++a) {
deba@1017
   416
          typename Graph::Node m = _graph.target(a);
alpar@1270
   417
deba@1017
   418
          if (!(n < m)) continue;
deba@1017
   419
deba@1017
   420
          (*_nodes)[n].sum += (*_capacity)[a];
deba@1017
   421
          (*_nodes)[m].sum += (*_capacity)[a];
alpar@1270
   422
deba@1017
   423
          int c = (*_nodes)[m].curr_arc;
deba@1017
   424
          if (c != -1 && _arcs[c ^ 1].target == n) {
deba@1017
   425
            _edges[c >> 1].capacity += (*_capacity)[a];
deba@1017
   426
          } else {
deba@1017
   427
            _edges[index].capacity = (*_capacity)[a];
alpar@1270
   428
deba@1017
   429
            _arcs[index << 1].prev = -1;
deba@1017
   430
            if ((*_nodes)[n].first_arc != -1) {
deba@1017
   431
              _arcs[(*_nodes)[n].first_arc].prev = (index << 1);
deba@1017
   432
            }
deba@1017
   433
            _arcs[index << 1].next = (*_nodes)[n].first_arc;
deba@1017
   434
            (*_nodes)[n].first_arc = (index << 1);
deba@1017
   435
            _arcs[index << 1].target = m;
deba@1017
   436
deba@1017
   437
            (*_nodes)[m].curr_arc = (index << 1);
alpar@1270
   438
deba@1017
   439
            _arcs[(index << 1) | 1].prev = -1;
deba@1017
   440
            if ((*_nodes)[m].first_arc != -1) {
deba@1017
   441
              _arcs[(*_nodes)[m].first_arc].prev = ((index << 1) | 1);
deba@1017
   442
            }
deba@1017
   443
            _arcs[(index << 1) | 1].next = (*_nodes)[m].first_arc;
deba@1017
   444
            (*_nodes)[m].first_arc = ((index << 1) | 1);
deba@1017
   445
            _arcs[(index << 1) | 1].target = n;
alpar@1270
   446
deba@1017
   447
            ++index;
deba@1017
   448
          }
deba@1017
   449
        }
deba@1017
   450
      }
deba@1017
   451
deba@1017
   452
      typename Graph::Node cut_node = INVALID;
deba@1017
   453
      _min_cut = std::numeric_limits<Value>::max();
deba@1017
   454
alpar@1270
   455
      for (typename Graph::Node n = _first_node;
deba@1017
   456
           n != INVALID; n = (*_nodes)[n].next) {
deba@1017
   457
        if ((*_nodes)[n].sum < _min_cut) {
deba@1017
   458
          cut_node = n;
deba@1017
   459
          _min_cut = (*_nodes)[n].sum;
deba@1017
   460
        }
deba@1017
   461
      }
deba@1017
   462
      (*_cut_map)[cut_node] = true;
deba@1017
   463
      if (_min_cut == 0) {
deba@1017
   464
        _first_node = INVALID;
deba@1017
   465
      }
deba@1017
   466
    }
deba@1017
   467
deba@1017
   468
  public:
deba@1017
   469
deba@1017
   470
    /// \brief Processes the next phase
deba@1017
   471
    ///
deba@1017
   472
    /// Processes the next phase in the algorithm. It must be called
deba@1017
   473
    /// at most one less the number of the nodes in the graph.
deba@1017
   474
    ///
deba@1017
   475
    ///\return %True when the algorithm finished.
deba@1017
   476
    bool processNextPhase() {
deba@1017
   477
      if (_first_node == INVALID) return true;
deba@1017
   478
deba@1017
   479
      _heap->clear();
alpar@1270
   480
      for (typename Graph::Node n = _first_node;
deba@1017
   481
           n != INVALID; n = (*_nodes)[n].next) {
deba@1017
   482
        (*_heap_cross_ref)[n] = Heap::PRE_HEAP;
deba@1017
   483
      }
deba@1017
   484
deba@1017
   485
      std::vector<typename Graph::Node> order;
deba@1017
   486
      order.reserve(_node_num);
deba@1017
   487
      int sep = 0;
deba@1017
   488
deba@1017
   489
      Value alpha = 0;
deba@1017
   490
      Value pmc = std::numeric_limits<Value>::max();
deba@1017
   491
deba@1017
   492
      _heap->push(_first_node, static_cast<Value>(0));
deba@1017
   493
      while (!_heap->empty()) {
deba@1017
   494
        typename Graph::Node n = _heap->top();
deba@1017
   495
        Value v = _heap->prio();
deba@1017
   496
deba@1017
   497
        _heap->pop();
deba@1017
   498
        for (int a = (*_nodes)[n].first_arc; a != -1; a = _arcs[a].next) {
deba@1017
   499
          switch (_heap->state(_arcs[a].target)) {
alpar@1270
   500
          case Heap::PRE_HEAP:
deba@1017
   501
            {
deba@1017
   502
              Value nv = _edges[a >> 1].capacity;
deba@1017
   503
              _heap->push(_arcs[a].target, nv);
deba@1017
   504
              _edges[a >> 1].cut = nv;
deba@1017
   505
            } break;
deba@1017
   506
          case Heap::IN_HEAP:
deba@1017
   507
            {
deba@1017
   508
              Value nv = _edges[a >> 1].capacity + (*_heap)[_arcs[a].target];
deba@1017
   509
              _heap->decrease(_arcs[a].target, nv);
deba@1017
   510
              _edges[a >> 1].cut = nv;
deba@1017
   511
            } break;
deba@1017
   512
          case Heap::POST_HEAP:
deba@1017
   513
            break;
deba@1017
   514
          }
deba@1017
   515
        }
deba@1017
   516
deba@1017
   517
        alpha += (*_nodes)[n].sum;
deba@1017
   518
        alpha -= 2 * v;
deba@1017
   519
deba@1017
   520
        order.push_back(n);
deba@1017
   521
        if (!_heap->empty()) {
deba@1017
   522
          if (alpha < pmc) {
deba@1017
   523
            pmc = alpha;
deba@1017
   524
            sep = order.size();
deba@1017
   525
          }
deba@1017
   526
        }
deba@1017
   527
      }
deba@1017
   528
deba@1017
   529
      if (static_cast<int>(order.size()) < _node_num) {
deba@1017
   530
        _first_node = INVALID;
deba@1017
   531
        for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
deba@1017
   532
          (*_cut_map)[n] = false;
deba@1017
   533
        }
deba@1017
   534
        for (int i = 0; i < static_cast<int>(order.size()); ++i) {
deba@1017
   535
          typename Graph::Node n = order[i];
deba@1017
   536
          while (n != INVALID) {
deba@1017
   537
            (*_cut_map)[n] = true;
deba@1017
   538
            n = (*_next_rep)[n];
deba@1017
   539
          }
deba@1017
   540
        }
deba@1017
   541
        _min_cut = 0;
deba@1017
   542
        return true;
deba@1017
   543
      }
deba@1017
   544
deba@1017
   545
      if (pmc < _min_cut) {
deba@1017
   546
        for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
deba@1017
   547
          (*_cut_map)[n] = false;
deba@1017
   548
        }
deba@1017
   549
        for (int i = 0; i < sep; ++i) {
deba@1017
   550
          typename Graph::Node n = order[i];
deba@1017
   551
          while (n != INVALID) {
deba@1017
   552
            (*_cut_map)[n] = true;
deba@1017
   553
            n = (*_next_rep)[n];
deba@1017
   554
          }
deba@1017
   555
        }
deba@1017
   556
        _min_cut = pmc;
deba@1017
   557
      }
deba@1017
   558
deba@1017
   559
      for (typename Graph::Node n = _first_node;
deba@1017
   560
           n != INVALID; n = (*_nodes)[n].next) {
deba@1017
   561
        bool merged = false;
deba@1017
   562
        for (int a = (*_nodes)[n].first_arc; a != -1; a = _arcs[a].next) {
deba@1017
   563
          if (!(_edges[a >> 1].cut < pmc)) {
deba@1017
   564
            if (!merged) {
deba@1017
   565
              for (int b = (*_nodes)[n].first_arc; b != -1; b = _arcs[b].next) {
alpar@1270
   566
                (*_nodes)[_arcs[b].target].curr_arc = b;
deba@1017
   567
              }
deba@1017
   568
              merged = true;
deba@1017
   569
            }
deba@1017
   570
            typename Graph::Node m = _arcs[a].target;
deba@1017
   571
            int nb = 0;
deba@1017
   572
            for (int b = (*_nodes)[m].first_arc; b != -1; b = nb) {
deba@1017
   573
              nb = _arcs[b].next;
deba@1017
   574
              if ((b ^ a) == 1) continue;
deba@1017
   575
              typename Graph::Node o = _arcs[b].target;
alpar@1270
   576
              int c = (*_nodes)[o].curr_arc;
deba@1017
   577
              if (c != -1 && _arcs[c ^ 1].target == n) {
deba@1017
   578
                _edges[c >> 1].capacity += _edges[b >> 1].capacity;
deba@1017
   579
                (*_nodes)[n].sum += _edges[b >> 1].capacity;
deba@1017
   580
                if (_edges[b >> 1].cut < _edges[c >> 1].cut) {
deba@1017
   581
                  _edges[b >> 1].cut = _edges[c >> 1].cut;
deba@1017
   582
                }
deba@1017
   583
                if (_arcs[b ^ 1].prev != -1) {
deba@1017
   584
                  _arcs[_arcs[b ^ 1].prev].next = _arcs[b ^ 1].next;
deba@1017
   585
                } else {
deba@1017
   586
                  (*_nodes)[o].first_arc = _arcs[b ^ 1].next;
deba@1017
   587
                }
deba@1017
   588
                if (_arcs[b ^ 1].next != -1) {
deba@1017
   589
                  _arcs[_arcs[b ^ 1].next].prev = _arcs[b ^ 1].prev;
deba@1017
   590
                }
deba@1017
   591
              } else {
deba@1017
   592
                if (_arcs[a].next != -1) {
deba@1017
   593
                  _arcs[_arcs[a].next].prev = b;
deba@1017
   594
                }
deba@1017
   595
                _arcs[b].next = _arcs[a].next;
deba@1017
   596
                _arcs[b].prev = a;
deba@1017
   597
                _arcs[a].next = b;
deba@1017
   598
                _arcs[b ^ 1].target = n;
deba@1017
   599
deba@1017
   600
                (*_nodes)[n].sum += _edges[b >> 1].capacity;
deba@1017
   601
                (*_nodes)[o].curr_arc = b;
deba@1017
   602
              }
deba@1017
   603
            }
deba@1017
   604
deba@1017
   605
            if (_arcs[a].prev != -1) {
deba@1017
   606
              _arcs[_arcs[a].prev].next = _arcs[a].next;
deba@1017
   607
            } else {
deba@1017
   608
              (*_nodes)[n].first_arc = _arcs[a].next;
alpar@1270
   609
            }
deba@1017
   610
            if (_arcs[a].next != -1) {
deba@1017
   611
              _arcs[_arcs[a].next].prev = _arcs[a].prev;
deba@1017
   612
            }
deba@1017
   613
deba@1017
   614
            (*_nodes)[n].sum -= _edges[a >> 1].capacity;
deba@1017
   615
            (*_next_rep)[(*_nodes)[n].last_rep] = m;
deba@1017
   616
            (*_nodes)[n].last_rep = (*_nodes)[m].last_rep;
alpar@1270
   617
deba@1017
   618
            if ((*_nodes)[m].prev != INVALID) {
deba@1017
   619
              (*_nodes)[(*_nodes)[m].prev].next = (*_nodes)[m].next;
deba@1017
   620
            } else{
deba@1017
   621
              _first_node = (*_nodes)[m].next;
deba@1017
   622
            }
deba@1017
   623
            if ((*_nodes)[m].next != INVALID) {
deba@1017
   624
              (*_nodes)[(*_nodes)[m].next].prev = (*_nodes)[m].prev;
deba@1017
   625
            }
deba@1017
   626
            --_node_num;
deba@1017
   627
          }
deba@1017
   628
        }
deba@1017
   629
      }
deba@1017
   630
deba@1017
   631
      if (_node_num == 1) {
deba@1017
   632
        _first_node = INVALID;
deba@1017
   633
        return true;
deba@1017
   634
      }
deba@1017
   635
deba@1017
   636
      return false;
deba@1017
   637
    }
deba@1017
   638
deba@1017
   639
    /// \brief Executes the algorithm.
deba@1017
   640
    ///
deba@1017
   641
    /// Executes the algorithm.
deba@1017
   642
    ///
deba@1017
   643
    /// \pre init() must be called
deba@1017
   644
    void start() {
deba@1017
   645
      while (!processNextPhase()) {}
deba@1017
   646
    }
deba@1017
   647
deba@1017
   648
deba@1017
   649
    /// \brief Runs %NagamochiIbaraki algorithm.
deba@1017
   650
    ///
deba@1017
   651
    /// This method runs the %Min cut algorithm
deba@1017
   652
    ///
deba@1017
   653
    /// \note mc.run(s) is just a shortcut of the following code.
deba@1017
   654
    ///\code
deba@1017
   655
    ///  mc.init();
deba@1017
   656
    ///  mc.start();
deba@1017
   657
    ///\endcode
deba@1017
   658
    void run() {
deba@1017
   659
      init();
deba@1017
   660
      start();
deba@1017
   661
    }
deba@1017
   662
deba@1017
   663
    ///@}
deba@1017
   664
deba@1017
   665
    /// \name Query Functions
deba@1017
   666
    ///
deba@1017
   667
    /// The result of the %NagamochiIbaraki
deba@1017
   668
    /// algorithm can be obtained using these functions.\n
deba@1017
   669
    /// Before the use of these functions, either run() or start()
deba@1017
   670
    /// must be called.
deba@1017
   671
deba@1017
   672
    ///@{
deba@1017
   673
deba@1017
   674
    /// \brief Returns the min cut value.
deba@1017
   675
    ///
deba@1017
   676
    /// Returns the min cut value if the algorithm finished.
deba@1017
   677
    /// After the first processNextPhase() it is a value of a
deba@1017
   678
    /// valid cut in the graph.
deba@1017
   679
    Value minCutValue() const {
deba@1017
   680
      return _min_cut;
deba@1017
   681
    }
deba@1017
   682
deba@1017
   683
    /// \brief Returns a min cut in a NodeMap.
deba@1017
   684
    ///
deba@1017
   685
    /// It sets the nodes of one of the two partitions to true and
deba@1017
   686
    /// the other partition to false.
deba@1017
   687
    /// \param cutMap A \ref concepts::WriteMap "writable" node map with
deba@1017
   688
    /// \c bool (or convertible) value type.
deba@1017
   689
    template <typename CutMap>
deba@1017
   690
    Value minCutMap(CutMap& cutMap) const {
deba@1017
   691
      for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
deba@1017
   692
        cutMap.set(n, (*_cut_map)[n]);
deba@1017
   693
      }
deba@1017
   694
      return minCutValue();
deba@1017
   695
    }
deba@1017
   696
deba@1017
   697
    ///@}
deba@1017
   698
deba@1017
   699
  };
deba@1017
   700
}
deba@1017
   701
deba@1017
   702
#endif