1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/nagamochi_ibaraki.h Tue Nov 16 07:46:01 2010 +0100
1.3 @@ -0,0 +1,697 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2010
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_NAGAMOCHI_IBARAKI_H
1.23 +#define LEMON_NAGAMOCHI_IBARAKI_H
1.24 +
1.25 +
1.26 +/// \ingroup min_cut
1.27 +/// \file
1.28 +/// \brief Implementation of the Nagamochi-Ibaraki algorithm.
1.29 +
1.30 +#include <lemon/core.h>
1.31 +#include <lemon/bin_heap.h>
1.32 +#include <lemon/bucket_heap.h>
1.33 +#include <lemon/maps.h>
1.34 +#include <lemon/radix_sort.h>
1.35 +#include <lemon/unionfind.h>
1.36 +
1.37 +#include <cassert>
1.38 +
1.39 +namespace lemon {
1.40 +
1.41 + /// \brief Default traits class for NagamochiIbaraki class.
1.42 + ///
1.43 + /// Default traits class for NagamochiIbaraki class.
1.44 + /// \param GR The undirected graph type.
1.45 + /// \param CM Type of capacity map.
1.46 + template <typename GR, typename CM>
1.47 + struct NagamochiIbarakiDefaultTraits {
1.48 + /// The type of the capacity map.
1.49 + typedef typename CM::Value Value;
1.50 +
1.51 + /// The undirected graph type the algorithm runs on.
1.52 + typedef GR Graph;
1.53 +
1.54 + /// \brief The type of the map that stores the edge capacities.
1.55 + ///
1.56 + /// The type of the map that stores the edge capacities.
1.57 + /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
1.58 + typedef CM CapacityMap;
1.59 +
1.60 + /// \brief Instantiates a CapacityMap.
1.61 + ///
1.62 + /// This function instantiates a \ref CapacityMap.
1.63 +#ifdef DOXYGEN
1.64 + static CapacityMap *createCapacityMap(const Graph& graph)
1.65 +#else
1.66 + static CapacityMap *createCapacityMap(const Graph&)
1.67 +#endif
1.68 + {
1.69 + LEMON_ASSERT(false, "CapacityMap is not initialized");
1.70 + return 0; // ignore warnings
1.71 + }
1.72 +
1.73 + /// \brief The cross reference type used by heap.
1.74 + ///
1.75 + /// The cross reference type used by heap.
1.76 + /// Usually \c Graph::NodeMap<int>.
1.77 + typedef typename Graph::template NodeMap<int> HeapCrossRef;
1.78 +
1.79 + /// \brief Instantiates a HeapCrossRef.
1.80 + ///
1.81 + /// This function instantiates a \ref HeapCrossRef.
1.82 + /// \param g is the graph, to which we would like to define the
1.83 + /// \ref HeapCrossRef.
1.84 + static HeapCrossRef *createHeapCrossRef(const Graph& g) {
1.85 + return new HeapCrossRef(g);
1.86 + }
1.87 +
1.88 + /// \brief The heap type used by NagamochiIbaraki algorithm.
1.89 + ///
1.90 + /// The heap type used by NagamochiIbaraki algorithm. It has to
1.91 + /// maximize the priorities.
1.92 + ///
1.93 + /// \sa BinHeap
1.94 + /// \sa NagamochiIbaraki
1.95 + typedef BinHeap<Value, HeapCrossRef, std::greater<Value> > Heap;
1.96 +
1.97 + /// \brief Instantiates a Heap.
1.98 + ///
1.99 + /// This function instantiates a \ref Heap.
1.100 + /// \param r is the cross reference of the heap.
1.101 + static Heap *createHeap(HeapCrossRef& r) {
1.102 + return new Heap(r);
1.103 + }
1.104 + };
1.105 +
1.106 + /// \ingroup min_cut
1.107 + ///
1.108 + /// \brief Calculates the minimum cut in an undirected graph.
1.109 + ///
1.110 + /// Calculates the minimum cut in an undirected graph with the
1.111 + /// Nagamochi-Ibaraki algorithm. The algorithm separates the graph's
1.112 + /// nodes into two partitions with the minimum sum of edge capacities
1.113 + /// between the two partitions. The algorithm can be used to test
1.114 + /// the network reliability, especially to test how many links have
1.115 + /// to be destroyed in the network to split it to at least two
1.116 + /// distinict subnetworks.
1.117 + ///
1.118 + /// The complexity of the algorithm is \f$ O(nm\log(n)) \f$ but with
1.119 + /// \ref FibHeap "Fibonacci heap" it can be decreased to
1.120 + /// \f$ O(nm+n^2\log(n)) \f$. When the edges have unit capacities,
1.121 + /// \c BucketHeap can be used which yields \f$ O(nm) \f$ time
1.122 + /// complexity.
1.123 + ///
1.124 + /// \warning The value type of the capacity map should be able to
1.125 + /// hold any cut value of the graph, otherwise the result can
1.126 + /// overflow.
1.127 + /// \note This capacity is supposed to be integer type.
1.128 +#ifdef DOXYGEN
1.129 + template <typename GR, typename CM, typename TR>
1.130 +#else
1.131 + template <typename GR,
1.132 + typename CM = typename GR::template EdgeMap<int>,
1.133 + typename TR = NagamochiIbarakiDefaultTraits<GR, CM> >
1.134 +#endif
1.135 + class NagamochiIbaraki {
1.136 + public:
1.137 +
1.138 + typedef TR Traits;
1.139 + /// The type of the underlying graph.
1.140 + typedef typename Traits::Graph Graph;
1.141 +
1.142 + /// The type of the capacity map.
1.143 + typedef typename Traits::CapacityMap CapacityMap;
1.144 + /// The value type of the capacity map.
1.145 + typedef typename Traits::CapacityMap::Value Value;
1.146 +
1.147 + /// The heap type used by the algorithm.
1.148 + typedef typename Traits::Heap Heap;
1.149 + /// The cross reference type used for the heap.
1.150 + typedef typename Traits::HeapCrossRef HeapCrossRef;
1.151 +
1.152 + ///\name Named template parameters
1.153 +
1.154 + ///@{
1.155 +
1.156 + struct SetUnitCapacityTraits : public Traits {
1.157 + typedef ConstMap<typename Graph::Edge, Const<int, 1> > CapacityMap;
1.158 + static CapacityMap *createCapacityMap(const Graph&) {
1.159 + return new CapacityMap();
1.160 + }
1.161 + };
1.162 +
1.163 + /// \brief \ref named-templ-param "Named parameter" for setting
1.164 + /// the capacity map to a constMap<Edge, int, 1>() instance
1.165 + ///
1.166 + /// \ref named-templ-param "Named parameter" for setting
1.167 + /// the capacity map to a constMap<Edge, int, 1>() instance
1.168 + struct SetUnitCapacity
1.169 + : public NagamochiIbaraki<Graph, CapacityMap,
1.170 + SetUnitCapacityTraits> {
1.171 + typedef NagamochiIbaraki<Graph, CapacityMap,
1.172 + SetUnitCapacityTraits> Create;
1.173 + };
1.174 +
1.175 +
1.176 + template <class H, class CR>
1.177 + struct SetHeapTraits : public Traits {
1.178 + typedef CR HeapCrossRef;
1.179 + typedef H Heap;
1.180 + static HeapCrossRef *createHeapCrossRef(int num) {
1.181 + LEMON_ASSERT(false, "HeapCrossRef is not initialized");
1.182 + return 0; // ignore warnings
1.183 + }
1.184 + static Heap *createHeap(HeapCrossRef &) {
1.185 + LEMON_ASSERT(false, "Heap is not initialized");
1.186 + return 0; // ignore warnings
1.187 + }
1.188 + };
1.189 +
1.190 + /// \brief \ref named-templ-param "Named parameter" for setting
1.191 + /// heap and cross reference type
1.192 + ///
1.193 + /// \ref named-templ-param "Named parameter" for setting heap and
1.194 + /// cross reference type. The heap has to maximize the priorities.
1.195 + template <class H, class CR = RangeMap<int> >
1.196 + struct SetHeap
1.197 + : public NagamochiIbaraki<Graph, CapacityMap, SetHeapTraits<H, CR> > {
1.198 + typedef NagamochiIbaraki< Graph, CapacityMap, SetHeapTraits<H, CR> >
1.199 + Create;
1.200 + };
1.201 +
1.202 + template <class H, class CR>
1.203 + struct SetStandardHeapTraits : public Traits {
1.204 + typedef CR HeapCrossRef;
1.205 + typedef H Heap;
1.206 + static HeapCrossRef *createHeapCrossRef(int size) {
1.207 + return new HeapCrossRef(size);
1.208 + }
1.209 + static Heap *createHeap(HeapCrossRef &crossref) {
1.210 + return new Heap(crossref);
1.211 + }
1.212 + };
1.213 +
1.214 + /// \brief \ref named-templ-param "Named parameter" for setting
1.215 + /// heap and cross reference type with automatic allocation
1.216 + ///
1.217 + /// \ref named-templ-param "Named parameter" for setting heap and
1.218 + /// cross reference type with automatic allocation. They should
1.219 + /// have standard constructor interfaces to be able to
1.220 + /// automatically created by the algorithm (i.e. the graph should
1.221 + /// be passed to the constructor of the cross reference and the
1.222 + /// cross reference should be passed to the constructor of the
1.223 + /// heap). However, external heap and cross reference objects
1.224 + /// could also be passed to the algorithm using the \ref heap()
1.225 + /// function before calling \ref run() or \ref init(). The heap
1.226 + /// has to maximize the priorities.
1.227 + /// \sa SetHeap
1.228 + template <class H, class CR = RangeMap<int> >
1.229 + struct SetStandardHeap
1.230 + : public NagamochiIbaraki<Graph, CapacityMap,
1.231 + SetStandardHeapTraits<H, CR> > {
1.232 + typedef NagamochiIbaraki<Graph, CapacityMap,
1.233 + SetStandardHeapTraits<H, CR> > Create;
1.234 + };
1.235 +
1.236 + ///@}
1.237 +
1.238 +
1.239 + private:
1.240 +
1.241 + const Graph &_graph;
1.242 + const CapacityMap *_capacity;
1.243 + bool _local_capacity; // unit capacity
1.244 +
1.245 + struct ArcData {
1.246 + typename Graph::Node target;
1.247 + int prev, next;
1.248 + };
1.249 + struct EdgeData {
1.250 + Value capacity;
1.251 + Value cut;
1.252 + };
1.253 +
1.254 + struct NodeData {
1.255 + int first_arc;
1.256 + typename Graph::Node prev, next;
1.257 + int curr_arc;
1.258 + typename Graph::Node last_rep;
1.259 + Value sum;
1.260 + };
1.261 +
1.262 + typename Graph::template NodeMap<NodeData> *_nodes;
1.263 + std::vector<ArcData> _arcs;
1.264 + std::vector<EdgeData> _edges;
1.265 +
1.266 + typename Graph::Node _first_node;
1.267 + int _node_num;
1.268 +
1.269 + Value _min_cut;
1.270 +
1.271 + HeapCrossRef *_heap_cross_ref;
1.272 + bool _local_heap_cross_ref;
1.273 + Heap *_heap;
1.274 + bool _local_heap;
1.275 +
1.276 + typedef typename Graph::template NodeMap<typename Graph::Node> NodeList;
1.277 + NodeList *_next_rep;
1.278 +
1.279 + typedef typename Graph::template NodeMap<bool> MinCutMap;
1.280 + MinCutMap *_cut_map;
1.281 +
1.282 + void createStructures() {
1.283 + if (!_nodes) {
1.284 + _nodes = new (typename Graph::template NodeMap<NodeData>)(_graph);
1.285 + }
1.286 + if (!_capacity) {
1.287 + _local_capacity = true;
1.288 + _capacity = Traits::createCapacityMap(_graph);
1.289 + }
1.290 + if (!_heap_cross_ref) {
1.291 + _local_heap_cross_ref = true;
1.292 + _heap_cross_ref = Traits::createHeapCrossRef(_graph);
1.293 + }
1.294 + if (!_heap) {
1.295 + _local_heap = true;
1.296 + _heap = Traits::createHeap(*_heap_cross_ref);
1.297 + }
1.298 + if (!_next_rep) {
1.299 + _next_rep = new NodeList(_graph);
1.300 + }
1.301 + if (!_cut_map) {
1.302 + _cut_map = new MinCutMap(_graph);
1.303 + }
1.304 + }
1.305 +
1.306 + public :
1.307 +
1.308 + typedef NagamochiIbaraki Create;
1.309 +
1.310 +
1.311 + /// \brief Constructor.
1.312 + ///
1.313 + /// \param graph The graph the algorithm runs on.
1.314 + /// \param capacity The capacity map used by the algorithm.
1.315 + NagamochiIbaraki(const Graph& graph, const CapacityMap& capacity)
1.316 + : _graph(graph), _capacity(&capacity), _local_capacity(false),
1.317 + _nodes(0), _arcs(), _edges(), _min_cut(),
1.318 + _heap_cross_ref(0), _local_heap_cross_ref(false),
1.319 + _heap(0), _local_heap(false),
1.320 + _next_rep(0), _cut_map(0) {}
1.321 +
1.322 + /// \brief Constructor.
1.323 + ///
1.324 + /// This constructor can be used only when the Traits class
1.325 + /// defines how can the local capacity map be instantiated.
1.326 + /// If the SetUnitCapacity used the algorithm automatically
1.327 + /// constructs the capacity map.
1.328 + ///
1.329 + ///\param graph The graph the algorithm runs on.
1.330 + NagamochiIbaraki(const Graph& graph)
1.331 + : _graph(graph), _capacity(0), _local_capacity(false),
1.332 + _nodes(0), _arcs(), _edges(), _min_cut(),
1.333 + _heap_cross_ref(0), _local_heap_cross_ref(false),
1.334 + _heap(0), _local_heap(false),
1.335 + _next_rep(0), _cut_map(0) {}
1.336 +
1.337 + /// \brief Destructor.
1.338 + ///
1.339 + /// Destructor.
1.340 + ~NagamochiIbaraki() {
1.341 + if (_local_capacity) delete _capacity;
1.342 + if (_nodes) delete _nodes;
1.343 + if (_local_heap) delete _heap;
1.344 + if (_local_heap_cross_ref) delete _heap_cross_ref;
1.345 + if (_next_rep) delete _next_rep;
1.346 + if (_cut_map) delete _cut_map;
1.347 + }
1.348 +
1.349 + /// \brief Sets the heap and the cross reference used by algorithm.
1.350 + ///
1.351 + /// Sets the heap and the cross reference used by algorithm.
1.352 + /// If you don't use this function before calling \ref run(),
1.353 + /// it will allocate one. The destuctor deallocates this
1.354 + /// automatically allocated heap and cross reference, of course.
1.355 + /// \return <tt> (*this) </tt>
1.356 + NagamochiIbaraki &heap(Heap& hp, HeapCrossRef &cr)
1.357 + {
1.358 + if (_local_heap_cross_ref) {
1.359 + delete _heap_cross_ref;
1.360 + _local_heap_cross_ref = false;
1.361 + }
1.362 + _heap_cross_ref = &cr;
1.363 + if (_local_heap) {
1.364 + delete _heap;
1.365 + _local_heap = false;
1.366 + }
1.367 + _heap = &hp;
1.368 + return *this;
1.369 + }
1.370 +
1.371 + /// \name Execution control
1.372 + /// The simplest way to execute the algorithm is to use
1.373 + /// one of the member functions called \c run().
1.374 + /// \n
1.375 + /// If you need more control on the execution,
1.376 + /// first you must call \ref init() and then call the start()
1.377 + /// or proper times the processNextPhase() member functions.
1.378 +
1.379 + ///@{
1.380 +
1.381 + /// \brief Initializes the internal data structures.
1.382 + ///
1.383 + /// Initializes the internal data structures.
1.384 + void init() {
1.385 + createStructures();
1.386 +
1.387 + int edge_num = countEdges(_graph);
1.388 + _edges.resize(edge_num);
1.389 + _arcs.resize(2 * edge_num);
1.390 +
1.391 + typename Graph::Node prev = INVALID;
1.392 + _node_num = 0;
1.393 + for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
1.394 + (*_cut_map)[n] = false;
1.395 + (*_next_rep)[n] = INVALID;
1.396 + (*_nodes)[n].last_rep = n;
1.397 + (*_nodes)[n].first_arc = -1;
1.398 + (*_nodes)[n].curr_arc = -1;
1.399 + (*_nodes)[n].prev = prev;
1.400 + if (prev != INVALID) {
1.401 + (*_nodes)[prev].next = n;
1.402 + }
1.403 + (*_nodes)[n].next = INVALID;
1.404 + (*_nodes)[n].sum = 0;
1.405 + prev = n;
1.406 + ++_node_num;
1.407 + }
1.408 +
1.409 + _first_node = typename Graph::NodeIt(_graph);
1.410 +
1.411 + int index = 0;
1.412 + for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
1.413 + for (typename Graph::OutArcIt a(_graph, n); a != INVALID; ++a) {
1.414 + typename Graph::Node m = _graph.target(a);
1.415 +
1.416 + if (!(n < m)) continue;
1.417 +
1.418 + (*_nodes)[n].sum += (*_capacity)[a];
1.419 + (*_nodes)[m].sum += (*_capacity)[a];
1.420 +
1.421 + int c = (*_nodes)[m].curr_arc;
1.422 + if (c != -1 && _arcs[c ^ 1].target == n) {
1.423 + _edges[c >> 1].capacity += (*_capacity)[a];
1.424 + } else {
1.425 + _edges[index].capacity = (*_capacity)[a];
1.426 +
1.427 + _arcs[index << 1].prev = -1;
1.428 + if ((*_nodes)[n].first_arc != -1) {
1.429 + _arcs[(*_nodes)[n].first_arc].prev = (index << 1);
1.430 + }
1.431 + _arcs[index << 1].next = (*_nodes)[n].first_arc;
1.432 + (*_nodes)[n].first_arc = (index << 1);
1.433 + _arcs[index << 1].target = m;
1.434 +
1.435 + (*_nodes)[m].curr_arc = (index << 1);
1.436 +
1.437 + _arcs[(index << 1) | 1].prev = -1;
1.438 + if ((*_nodes)[m].first_arc != -1) {
1.439 + _arcs[(*_nodes)[m].first_arc].prev = ((index << 1) | 1);
1.440 + }
1.441 + _arcs[(index << 1) | 1].next = (*_nodes)[m].first_arc;
1.442 + (*_nodes)[m].first_arc = ((index << 1) | 1);
1.443 + _arcs[(index << 1) | 1].target = n;
1.444 +
1.445 + ++index;
1.446 + }
1.447 + }
1.448 + }
1.449 +
1.450 + typename Graph::Node cut_node = INVALID;
1.451 + _min_cut = std::numeric_limits<Value>::max();
1.452 +
1.453 + for (typename Graph::Node n = _first_node;
1.454 + n != INVALID; n = (*_nodes)[n].next) {
1.455 + if ((*_nodes)[n].sum < _min_cut) {
1.456 + cut_node = n;
1.457 + _min_cut = (*_nodes)[n].sum;
1.458 + }
1.459 + }
1.460 + (*_cut_map)[cut_node] = true;
1.461 + if (_min_cut == 0) {
1.462 + _first_node = INVALID;
1.463 + }
1.464 + }
1.465 +
1.466 + public:
1.467 +
1.468 + /// \brief Processes the next phase
1.469 + ///
1.470 + /// Processes the next phase in the algorithm. It must be called
1.471 + /// at most one less the number of the nodes in the graph.
1.472 + ///
1.473 + ///\return %True when the algorithm finished.
1.474 + bool processNextPhase() {
1.475 + if (_first_node == INVALID) return true;
1.476 +
1.477 + _heap->clear();
1.478 + for (typename Graph::Node n = _first_node;
1.479 + n != INVALID; n = (*_nodes)[n].next) {
1.480 + (*_heap_cross_ref)[n] = Heap::PRE_HEAP;
1.481 + }
1.482 +
1.483 + std::vector<typename Graph::Node> order;
1.484 + order.reserve(_node_num);
1.485 + int sep = 0;
1.486 +
1.487 + Value alpha = 0;
1.488 + Value pmc = std::numeric_limits<Value>::max();
1.489 +
1.490 + _heap->push(_first_node, static_cast<Value>(0));
1.491 + while (!_heap->empty()) {
1.492 + typename Graph::Node n = _heap->top();
1.493 + Value v = _heap->prio();
1.494 +
1.495 + _heap->pop();
1.496 + for (int a = (*_nodes)[n].first_arc; a != -1; a = _arcs[a].next) {
1.497 + switch (_heap->state(_arcs[a].target)) {
1.498 + case Heap::PRE_HEAP:
1.499 + {
1.500 + Value nv = _edges[a >> 1].capacity;
1.501 + _heap->push(_arcs[a].target, nv);
1.502 + _edges[a >> 1].cut = nv;
1.503 + } break;
1.504 + case Heap::IN_HEAP:
1.505 + {
1.506 + Value nv = _edges[a >> 1].capacity + (*_heap)[_arcs[a].target];
1.507 + _heap->decrease(_arcs[a].target, nv);
1.508 + _edges[a >> 1].cut = nv;
1.509 + } break;
1.510 + case Heap::POST_HEAP:
1.511 + break;
1.512 + }
1.513 + }
1.514 +
1.515 + alpha += (*_nodes)[n].sum;
1.516 + alpha -= 2 * v;
1.517 +
1.518 + order.push_back(n);
1.519 + if (!_heap->empty()) {
1.520 + if (alpha < pmc) {
1.521 + pmc = alpha;
1.522 + sep = order.size();
1.523 + }
1.524 + }
1.525 + }
1.526 +
1.527 + if (static_cast<int>(order.size()) < _node_num) {
1.528 + _first_node = INVALID;
1.529 + for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
1.530 + (*_cut_map)[n] = false;
1.531 + }
1.532 + for (int i = 0; i < static_cast<int>(order.size()); ++i) {
1.533 + typename Graph::Node n = order[i];
1.534 + while (n != INVALID) {
1.535 + (*_cut_map)[n] = true;
1.536 + n = (*_next_rep)[n];
1.537 + }
1.538 + }
1.539 + _min_cut = 0;
1.540 + return true;
1.541 + }
1.542 +
1.543 + if (pmc < _min_cut) {
1.544 + for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
1.545 + (*_cut_map)[n] = false;
1.546 + }
1.547 + for (int i = 0; i < sep; ++i) {
1.548 + typename Graph::Node n = order[i];
1.549 + while (n != INVALID) {
1.550 + (*_cut_map)[n] = true;
1.551 + n = (*_next_rep)[n];
1.552 + }
1.553 + }
1.554 + _min_cut = pmc;
1.555 + }
1.556 +
1.557 + for (typename Graph::Node n = _first_node;
1.558 + n != INVALID; n = (*_nodes)[n].next) {
1.559 + bool merged = false;
1.560 + for (int a = (*_nodes)[n].first_arc; a != -1; a = _arcs[a].next) {
1.561 + if (!(_edges[a >> 1].cut < pmc)) {
1.562 + if (!merged) {
1.563 + for (int b = (*_nodes)[n].first_arc; b != -1; b = _arcs[b].next) {
1.564 + (*_nodes)[_arcs[b].target].curr_arc = b;
1.565 + }
1.566 + merged = true;
1.567 + }
1.568 + typename Graph::Node m = _arcs[a].target;
1.569 + int nb = 0;
1.570 + for (int b = (*_nodes)[m].first_arc; b != -1; b = nb) {
1.571 + nb = _arcs[b].next;
1.572 + if ((b ^ a) == 1) continue;
1.573 + typename Graph::Node o = _arcs[b].target;
1.574 + int c = (*_nodes)[o].curr_arc;
1.575 + if (c != -1 && _arcs[c ^ 1].target == n) {
1.576 + _edges[c >> 1].capacity += _edges[b >> 1].capacity;
1.577 + (*_nodes)[n].sum += _edges[b >> 1].capacity;
1.578 + if (_edges[b >> 1].cut < _edges[c >> 1].cut) {
1.579 + _edges[b >> 1].cut = _edges[c >> 1].cut;
1.580 + }
1.581 + if (_arcs[b ^ 1].prev != -1) {
1.582 + _arcs[_arcs[b ^ 1].prev].next = _arcs[b ^ 1].next;
1.583 + } else {
1.584 + (*_nodes)[o].first_arc = _arcs[b ^ 1].next;
1.585 + }
1.586 + if (_arcs[b ^ 1].next != -1) {
1.587 + _arcs[_arcs[b ^ 1].next].prev = _arcs[b ^ 1].prev;
1.588 + }
1.589 + } else {
1.590 + if (_arcs[a].next != -1) {
1.591 + _arcs[_arcs[a].next].prev = b;
1.592 + }
1.593 + _arcs[b].next = _arcs[a].next;
1.594 + _arcs[b].prev = a;
1.595 + _arcs[a].next = b;
1.596 + _arcs[b ^ 1].target = n;
1.597 +
1.598 + (*_nodes)[n].sum += _edges[b >> 1].capacity;
1.599 + (*_nodes)[o].curr_arc = b;
1.600 + }
1.601 + }
1.602 +
1.603 + if (_arcs[a].prev != -1) {
1.604 + _arcs[_arcs[a].prev].next = _arcs[a].next;
1.605 + } else {
1.606 + (*_nodes)[n].first_arc = _arcs[a].next;
1.607 + }
1.608 + if (_arcs[a].next != -1) {
1.609 + _arcs[_arcs[a].next].prev = _arcs[a].prev;
1.610 + }
1.611 +
1.612 + (*_nodes)[n].sum -= _edges[a >> 1].capacity;
1.613 + (*_next_rep)[(*_nodes)[n].last_rep] = m;
1.614 + (*_nodes)[n].last_rep = (*_nodes)[m].last_rep;
1.615 +
1.616 + if ((*_nodes)[m].prev != INVALID) {
1.617 + (*_nodes)[(*_nodes)[m].prev].next = (*_nodes)[m].next;
1.618 + } else{
1.619 + _first_node = (*_nodes)[m].next;
1.620 + }
1.621 + if ((*_nodes)[m].next != INVALID) {
1.622 + (*_nodes)[(*_nodes)[m].next].prev = (*_nodes)[m].prev;
1.623 + }
1.624 + --_node_num;
1.625 + }
1.626 + }
1.627 + }
1.628 +
1.629 + if (_node_num == 1) {
1.630 + _first_node = INVALID;
1.631 + return true;
1.632 + }
1.633 +
1.634 + return false;
1.635 + }
1.636 +
1.637 + /// \brief Executes the algorithm.
1.638 + ///
1.639 + /// Executes the algorithm.
1.640 + ///
1.641 + /// \pre init() must be called
1.642 + void start() {
1.643 + while (!processNextPhase()) {}
1.644 + }
1.645 +
1.646 +
1.647 + /// \brief Runs %NagamochiIbaraki algorithm.
1.648 + ///
1.649 + /// This method runs the %Min cut algorithm
1.650 + ///
1.651 + /// \note mc.run(s) is just a shortcut of the following code.
1.652 + ///\code
1.653 + /// mc.init();
1.654 + /// mc.start();
1.655 + ///\endcode
1.656 + void run() {
1.657 + init();
1.658 + start();
1.659 + }
1.660 +
1.661 + ///@}
1.662 +
1.663 + /// \name Query Functions
1.664 + ///
1.665 + /// The result of the %NagamochiIbaraki
1.666 + /// algorithm can be obtained using these functions.\n
1.667 + /// Before the use of these functions, either run() or start()
1.668 + /// must be called.
1.669 +
1.670 + ///@{
1.671 +
1.672 + /// \brief Returns the min cut value.
1.673 + ///
1.674 + /// Returns the min cut value if the algorithm finished.
1.675 + /// After the first processNextPhase() it is a value of a
1.676 + /// valid cut in the graph.
1.677 + Value minCutValue() const {
1.678 + return _min_cut;
1.679 + }
1.680 +
1.681 + /// \brief Returns a min cut in a NodeMap.
1.682 + ///
1.683 + /// It sets the nodes of one of the two partitions to true and
1.684 + /// the other partition to false.
1.685 + /// \param cutMap A \ref concepts::WriteMap "writable" node map with
1.686 + /// \c bool (or convertible) value type.
1.687 + template <typename CutMap>
1.688 + Value minCutMap(CutMap& cutMap) const {
1.689 + for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
1.690 + cutMap.set(n, (*_cut_map)[n]);
1.691 + }
1.692 + return minCutValue();
1.693 + }
1.694 +
1.695 + ///@}
1.696 +
1.697 + };
1.698 +}
1.699 +
1.700 +#endif