lemon/min_cost_arborescence.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 17 Apr 2009 18:04:36 +0200
changeset 609 e6927fe719e6
child 559 c5fd2d996909
permissions -rw-r--r--
Support >= and <= constraints in NetworkSimplex (#219, #234)

By default the same inequality constraints are supported as by
Circulation (the GEQ form), but the LEQ form can also be selected
using the problemType() function.

The documentation of the min. cost flow module is reworked and
extended with important notes and explanations about the different
variants of the problem and about the dual solution and optimality
conditions.
deba@501
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@501
     2
 *
deba@501
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@501
     4
 *
deba@501
     5
 * Copyright (C) 2003-2008
deba@501
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@501
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@501
     8
 *
deba@501
     9
 * Permission to use, modify and distribute this software is granted
deba@501
    10
 * provided that this copyright notice appears in all copies. For
deba@501
    11
 * precise terms see the accompanying LICENSE file.
deba@501
    12
 *
deba@501
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@501
    14
 * express or implied, and with no claim as to its suitability for any
deba@501
    15
 * purpose.
deba@501
    16
 *
deba@501
    17
 */
deba@501
    18
deba@501
    19
#ifndef LEMON_MIN_COST_ARBORESCENCE_H
deba@501
    20
#define LEMON_MIN_COST_ARBORESCENCE_H
deba@501
    21
deba@501
    22
///\ingroup spantree
deba@501
    23
///\file
deba@501
    24
///\brief Minimum Cost Arborescence algorithm.
deba@501
    25
deba@501
    26
#include <vector>
deba@501
    27
deba@501
    28
#include <lemon/list_graph.h>
deba@501
    29
#include <lemon/bin_heap.h>
deba@501
    30
#include <lemon/assert.h>
deba@501
    31
deba@501
    32
namespace lemon {
deba@501
    33
deba@501
    34
deba@501
    35
  /// \brief Default traits class for MinCostArborescence class.
deba@501
    36
  ///
deba@501
    37
  /// Default traits class for MinCostArborescence class.
deba@501
    38
  /// \param _Digraph Digraph type.
deba@501
    39
  /// \param _CostMap Type of cost map.
deba@501
    40
  template <class _Digraph, class _CostMap>
deba@501
    41
  struct MinCostArborescenceDefaultTraits{
deba@501
    42
deba@501
    43
    /// \brief The digraph type the algorithm runs on.
deba@501
    44
    typedef _Digraph Digraph;
deba@501
    45
deba@501
    46
    /// \brief The type of the map that stores the arc costs.
deba@501
    47
    ///
deba@501
    48
    /// The type of the map that stores the arc costs.
deba@501
    49
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
deba@501
    50
    typedef _CostMap CostMap;
deba@501
    51
deba@501
    52
    /// \brief The value type of the costs.
deba@501
    53
    ///
deba@501
    54
    /// The value type of the costs.
deba@501
    55
    typedef typename CostMap::Value Value;
deba@501
    56
deba@501
    57
    /// \brief The type of the map that stores which arcs are in the
deba@501
    58
    /// arborescence.
deba@501
    59
    ///
deba@501
    60
    /// The type of the map that stores which arcs are in the
deba@501
    61
    /// arborescence.  It must meet the \ref concepts::WriteMap
deba@501
    62
    /// "WriteMap" concept.  Initially it will be set to false on each
deba@501
    63
    /// arc. After it will set all arborescence arcs once.
deba@501
    64
    typedef typename Digraph::template ArcMap<bool> ArborescenceMap;
deba@501
    65
deba@501
    66
    /// \brief Instantiates a ArborescenceMap.
deba@501
    67
    ///
deba@501
    68
    /// This function instantiates a \ref ArborescenceMap.
deba@501
    69
    /// \param digraph is the graph, to which we would like to
deba@501
    70
    /// calculate the ArborescenceMap.
deba@501
    71
    static ArborescenceMap *createArborescenceMap(const Digraph &digraph){
deba@501
    72
      return new ArborescenceMap(digraph);
deba@501
    73
    }
deba@501
    74
deba@501
    75
    /// \brief The type of the PredMap
deba@501
    76
    ///
deba@501
    77
    /// The type of the PredMap. It is a node map with an arc value type.
deba@501
    78
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
deba@501
    79
deba@501
    80
    /// \brief Instantiates a PredMap.
deba@501
    81
    ///
deba@501
    82
    /// This function instantiates a \ref PredMap.
deba@501
    83
    /// \param _digraph is the digraph, to which we would like to define the
deba@501
    84
    /// PredMap.
deba@501
    85
    static PredMap *createPredMap(const Digraph &digraph){
deba@501
    86
      return new PredMap(digraph);
deba@501
    87
    }
deba@501
    88
deba@501
    89
  };
deba@501
    90
deba@501
    91
  /// \ingroup spantree
deba@501
    92
  ///
deba@501
    93
  /// \brief %MinCostArborescence algorithm class.
deba@501
    94
  ///
deba@501
    95
  /// This class provides an efficient implementation of
deba@501
    96
  /// %MinCostArborescence algorithm. The arborescence is a tree
deba@501
    97
  /// which is directed from a given source node of the digraph. One or
deba@501
    98
  /// more sources should be given for the algorithm and it will calculate
deba@501
    99
  /// the minimum cost subgraph which are union of arborescences with the
deba@501
   100
  /// given sources and spans all the nodes which are reachable from the
deba@501
   101
  /// sources. The time complexity of the algorithm is \f$ O(n^2+e) \f$.
deba@501
   102
  ///
deba@501
   103
  /// The algorithm provides also an optimal dual solution, therefore
deba@501
   104
  /// the optimality of the solution can be checked.
deba@501
   105
  ///
deba@501
   106
  /// \param _Digraph The digraph type the algorithm runs on. The default value
deba@501
   107
  /// is \ref ListDigraph.
deba@501
   108
  /// \param _CostMap This read-only ArcMap determines the costs of the
deba@501
   109
  /// arcs. It is read once for each arc, so the map may involve in
deba@501
   110
  /// relatively time consuming process to compute the arc cost if
deba@501
   111
  /// it is necessary. The default map type is \ref
deba@501
   112
  /// concepts::Digraph::ArcMap "Digraph::ArcMap<int>".
deba@501
   113
  /// \param _Traits Traits class to set various data types used
deba@501
   114
  /// by the algorithm. The default traits class is
deba@501
   115
  /// \ref MinCostArborescenceDefaultTraits
deba@501
   116
  /// "MinCostArborescenceDefaultTraits<_Digraph, _CostMap>".  See \ref
deba@501
   117
  /// MinCostArborescenceDefaultTraits for the documentation of a
deba@501
   118
  /// MinCostArborescence traits class.
deba@501
   119
  ///
deba@501
   120
  /// \author Balazs Dezso
deba@501
   121
#ifndef DOXYGEN
deba@501
   122
  template <typename _Digraph = ListDigraph,
deba@501
   123
            typename _CostMap = typename _Digraph::template ArcMap<int>,
deba@501
   124
            typename _Traits =
deba@501
   125
            MinCostArborescenceDefaultTraits<_Digraph, _CostMap> >
deba@501
   126
#else
deba@501
   127
  template <typename _Digraph, typename _CostMap, typedef _Traits>
deba@501
   128
#endif
deba@501
   129
  class MinCostArborescence {
deba@501
   130
  public:
deba@501
   131
deba@501
   132
    /// The traits.
deba@501
   133
    typedef _Traits Traits;
deba@501
   134
    /// The type of the underlying digraph.
deba@501
   135
    typedef typename Traits::Digraph Digraph;
deba@501
   136
    /// The type of the map that stores the arc costs.
deba@501
   137
    typedef typename Traits::CostMap CostMap;
deba@501
   138
    ///The type of the costs of the arcs.
deba@501
   139
    typedef typename Traits::Value Value;
deba@501
   140
    ///The type of the predecessor map.
deba@501
   141
    typedef typename Traits::PredMap PredMap;
deba@501
   142
    ///The type of the map that stores which arcs are in the arborescence.
deba@501
   143
    typedef typename Traits::ArborescenceMap ArborescenceMap;
deba@501
   144
deba@501
   145
    typedef MinCostArborescence Create;
deba@501
   146
deba@501
   147
  private:
deba@501
   148
deba@501
   149
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
deba@501
   150
deba@501
   151
    struct CostArc {
deba@501
   152
deba@501
   153
      Arc arc;
deba@501
   154
      Value value;
deba@501
   155
deba@501
   156
      CostArc() {}
deba@501
   157
      CostArc(Arc _arc, Value _value) : arc(_arc), value(_value) {}
deba@501
   158
deba@501
   159
    };
deba@501
   160
deba@501
   161
    const Digraph *_digraph;
deba@501
   162
    const CostMap *_cost;
deba@501
   163
deba@501
   164
    PredMap *_pred;
deba@501
   165
    bool local_pred;
deba@501
   166
deba@501
   167
    ArborescenceMap *_arborescence;
deba@501
   168
    bool local_arborescence;
deba@501
   169
deba@501
   170
    typedef typename Digraph::template ArcMap<int> ArcOrder;
deba@501
   171
    ArcOrder *_arc_order;
deba@501
   172
deba@501
   173
    typedef typename Digraph::template NodeMap<int> NodeOrder;
deba@501
   174
    NodeOrder *_node_order;
deba@501
   175
deba@501
   176
    typedef typename Digraph::template NodeMap<CostArc> CostArcMap;
deba@501
   177
    CostArcMap *_cost_arcs;
deba@501
   178
deba@501
   179
    struct StackLevel {
deba@501
   180
deba@501
   181
      std::vector<CostArc> arcs;
deba@501
   182
      int node_level;
deba@501
   183
deba@501
   184
    };
deba@501
   185
deba@501
   186
    std::vector<StackLevel> level_stack;
deba@501
   187
    std::vector<Node> queue;
deba@501
   188
deba@501
   189
    typedef std::vector<typename Digraph::Node> DualNodeList;
deba@501
   190
deba@501
   191
    DualNodeList _dual_node_list;
deba@501
   192
deba@501
   193
    struct DualVariable {
deba@501
   194
      int begin, end;
deba@501
   195
      Value value;
deba@501
   196
deba@501
   197
      DualVariable(int _begin, int _end, Value _value)
deba@501
   198
        : begin(_begin), end(_end), value(_value) {}
deba@501
   199
deba@501
   200
    };
deba@501
   201
deba@501
   202
    typedef std::vector<DualVariable> DualVariables;
deba@501
   203
deba@501
   204
    DualVariables _dual_variables;
deba@501
   205
deba@501
   206
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
deba@501
   207
deba@501
   208
    HeapCrossRef *_heap_cross_ref;
deba@501
   209
deba@501
   210
    typedef BinHeap<int, HeapCrossRef> Heap;
deba@501
   211
deba@501
   212
    Heap *_heap;
deba@501
   213
deba@501
   214
  protected:
deba@501
   215
deba@501
   216
    MinCostArborescence() {}
deba@501
   217
deba@501
   218
  private:
deba@501
   219
deba@501
   220
    void createStructures() {
deba@501
   221
      if (!_pred) {
deba@501
   222
        local_pred = true;
deba@501
   223
        _pred = Traits::createPredMap(*_digraph);
deba@501
   224
      }
deba@501
   225
      if (!_arborescence) {
deba@501
   226
        local_arborescence = true;
deba@501
   227
        _arborescence = Traits::createArborescenceMap(*_digraph);
deba@501
   228
      }
deba@501
   229
      if (!_arc_order) {
deba@501
   230
        _arc_order = new ArcOrder(*_digraph);
deba@501
   231
      }
deba@501
   232
      if (!_node_order) {
deba@501
   233
        _node_order = new NodeOrder(*_digraph);
deba@501
   234
      }
deba@501
   235
      if (!_cost_arcs) {
deba@501
   236
        _cost_arcs = new CostArcMap(*_digraph);
deba@501
   237
      }
deba@501
   238
      if (!_heap_cross_ref) {
deba@501
   239
        _heap_cross_ref = new HeapCrossRef(*_digraph, -1);
deba@501
   240
      }
deba@501
   241
      if (!_heap) {
deba@501
   242
        _heap = new Heap(*_heap_cross_ref);
deba@501
   243
      }
deba@501
   244
    }
deba@501
   245
deba@501
   246
    void destroyStructures() {
deba@501
   247
      if (local_arborescence) {
deba@501
   248
        delete _arborescence;
deba@501
   249
      }
deba@501
   250
      if (local_pred) {
deba@501
   251
        delete _pred;
deba@501
   252
      }
deba@501
   253
      if (_arc_order) {
deba@501
   254
        delete _arc_order;
deba@501
   255
      }
deba@501
   256
      if (_node_order) {
deba@501
   257
        delete _node_order;
deba@501
   258
      }
deba@501
   259
      if (_cost_arcs) {
deba@501
   260
        delete _cost_arcs;
deba@501
   261
      }
deba@501
   262
      if (_heap) {
deba@501
   263
        delete _heap;
deba@501
   264
      }
deba@501
   265
      if (_heap_cross_ref) {
deba@501
   266
        delete _heap_cross_ref;
deba@501
   267
      }
deba@501
   268
    }
deba@501
   269
deba@501
   270
    Arc prepare(Node node) {
deba@501
   271
      std::vector<Node> nodes;
deba@501
   272
      (*_node_order)[node] = _dual_node_list.size();
deba@501
   273
      StackLevel level;
deba@501
   274
      level.node_level = _dual_node_list.size();
deba@501
   275
      _dual_node_list.push_back(node);
deba@501
   276
      for (InArcIt it(*_digraph, node); it != INVALID; ++it) {
deba@501
   277
        Arc arc = it;
deba@501
   278
        Node source = _digraph->source(arc);
deba@501
   279
        Value value = (*_cost)[it];
deba@501
   280
        if (source == node || (*_node_order)[source] == -3) continue;
deba@501
   281
        if ((*_cost_arcs)[source].arc == INVALID) {
deba@501
   282
          (*_cost_arcs)[source].arc = arc;
deba@501
   283
          (*_cost_arcs)[source].value = value;
deba@501
   284
          nodes.push_back(source);
deba@501
   285
        } else {
deba@501
   286
          if ((*_cost_arcs)[source].value > value) {
deba@501
   287
            (*_cost_arcs)[source].arc = arc;
deba@501
   288
            (*_cost_arcs)[source].value = value;
deba@501
   289
          }
deba@501
   290
        }
deba@501
   291
      }
deba@501
   292
      CostArc minimum = (*_cost_arcs)[nodes[0]];
deba@501
   293
      for (int i = 1; i < int(nodes.size()); ++i) {
deba@501
   294
        if ((*_cost_arcs)[nodes[i]].value < minimum.value) {
deba@501
   295
          minimum = (*_cost_arcs)[nodes[i]];
deba@501
   296
        }
deba@501
   297
      }
deba@501
   298
      _arc_order->set(minimum.arc, _dual_variables.size());
deba@501
   299
      DualVariable var(_dual_node_list.size() - 1,
deba@501
   300
                       _dual_node_list.size(), minimum.value);
deba@501
   301
      _dual_variables.push_back(var);
deba@501
   302
      for (int i = 0; i < int(nodes.size()); ++i) {
deba@501
   303
        (*_cost_arcs)[nodes[i]].value -= minimum.value;
deba@501
   304
        level.arcs.push_back((*_cost_arcs)[nodes[i]]);
deba@501
   305
        (*_cost_arcs)[nodes[i]].arc = INVALID;
deba@501
   306
      }
deba@501
   307
      level_stack.push_back(level);
deba@501
   308
      return minimum.arc;
deba@501
   309
    }
deba@501
   310
deba@501
   311
    Arc contract(Node node) {
deba@501
   312
      int node_bottom = bottom(node);
deba@501
   313
      std::vector<Node> nodes;
deba@501
   314
      while (!level_stack.empty() &&
deba@501
   315
             level_stack.back().node_level >= node_bottom) {
deba@501
   316
        for (int i = 0; i < int(level_stack.back().arcs.size()); ++i) {
deba@501
   317
          Arc arc = level_stack.back().arcs[i].arc;
deba@501
   318
          Node source = _digraph->source(arc);
deba@501
   319
          Value value = level_stack.back().arcs[i].value;
deba@501
   320
          if ((*_node_order)[source] >= node_bottom) continue;
deba@501
   321
          if ((*_cost_arcs)[source].arc == INVALID) {
deba@501
   322
            (*_cost_arcs)[source].arc = arc;
deba@501
   323
            (*_cost_arcs)[source].value = value;
deba@501
   324
            nodes.push_back(source);
deba@501
   325
          } else {
deba@501
   326
            if ((*_cost_arcs)[source].value > value) {
deba@501
   327
              (*_cost_arcs)[source].arc = arc;
deba@501
   328
              (*_cost_arcs)[source].value = value;
deba@501
   329
            }
deba@501
   330
          }
deba@501
   331
        }
deba@501
   332
        level_stack.pop_back();
deba@501
   333
      }
deba@501
   334
      CostArc minimum = (*_cost_arcs)[nodes[0]];
deba@501
   335
      for (int i = 1; i < int(nodes.size()); ++i) {
deba@501
   336
        if ((*_cost_arcs)[nodes[i]].value < minimum.value) {
deba@501
   337
          minimum = (*_cost_arcs)[nodes[i]];
deba@501
   338
        }
deba@501
   339
      }
deba@501
   340
      _arc_order->set(minimum.arc, _dual_variables.size());
deba@501
   341
      DualVariable var(node_bottom, _dual_node_list.size(), minimum.value);
deba@501
   342
      _dual_variables.push_back(var);
deba@501
   343
      StackLevel level;
deba@501
   344
      level.node_level = node_bottom;
deba@501
   345
      for (int i = 0; i < int(nodes.size()); ++i) {
deba@501
   346
        (*_cost_arcs)[nodes[i]].value -= minimum.value;
deba@501
   347
        level.arcs.push_back((*_cost_arcs)[nodes[i]]);
deba@501
   348
        (*_cost_arcs)[nodes[i]].arc = INVALID;
deba@501
   349
      }
deba@501
   350
      level_stack.push_back(level);
deba@501
   351
      return minimum.arc;
deba@501
   352
    }
deba@501
   353
deba@501
   354
    int bottom(Node node) {
deba@501
   355
      int k = level_stack.size() - 1;
deba@501
   356
      while (level_stack[k].node_level > (*_node_order)[node]) {
deba@501
   357
        --k;
deba@501
   358
      }
deba@501
   359
      return level_stack[k].node_level;
deba@501
   360
    }
deba@501
   361
deba@501
   362
    void finalize(Arc arc) {
deba@501
   363
      Node node = _digraph->target(arc);
deba@501
   364
      _heap->push(node, (*_arc_order)[arc]);
deba@501
   365
      _pred->set(node, arc);
deba@501
   366
      while (!_heap->empty()) {
deba@501
   367
        Node source = _heap->top();
deba@501
   368
        _heap->pop();
deba@501
   369
        _node_order->set(source, -1);
deba@501
   370
        for (OutArcIt it(*_digraph, source); it != INVALID; ++it) {
deba@501
   371
          if ((*_arc_order)[it] < 0) continue;
deba@501
   372
          Node target = _digraph->target(it);
deba@501
   373
          switch(_heap->state(target)) {
deba@501
   374
          case Heap::PRE_HEAP:
deba@501
   375
            _heap->push(target, (*_arc_order)[it]);
deba@501
   376
            _pred->set(target, it);
deba@501
   377
            break;
deba@501
   378
          case Heap::IN_HEAP:
deba@501
   379
            if ((*_arc_order)[it] < (*_heap)[target]) {
deba@501
   380
              _heap->decrease(target, (*_arc_order)[it]);
deba@501
   381
              _pred->set(target, it);
deba@501
   382
            }
deba@501
   383
            break;
deba@501
   384
          case Heap::POST_HEAP:
deba@501
   385
            break;
deba@501
   386
          }
deba@501
   387
        }
deba@501
   388
        _arborescence->set((*_pred)[source], true);
deba@501
   389
      }
deba@501
   390
    }
deba@501
   391
deba@501
   392
deba@501
   393
  public:
deba@501
   394
deba@501
   395
    /// \name Named template parameters
deba@501
   396
deba@501
   397
    /// @{
deba@501
   398
deba@501
   399
    template <class T>
deba@501
   400
    struct DefArborescenceMapTraits : public Traits {
deba@501
   401
      typedef T ArborescenceMap;
deba@501
   402
      static ArborescenceMap *createArborescenceMap(const Digraph &)
deba@501
   403
      {
deba@501
   404
        LEMON_ASSERT(false, "ArborescenceMap is not initialized");
deba@501
   405
        return 0; // ignore warnings
deba@501
   406
      }
deba@501
   407
    };
deba@501
   408
deba@501
   409
    /// \brief \ref named-templ-param "Named parameter" for
deba@501
   410
    /// setting ArborescenceMap type
deba@501
   411
    ///
deba@501
   412
    /// \ref named-templ-param "Named parameter" for setting
deba@501
   413
    /// ArborescenceMap type
deba@501
   414
    template <class T>
deba@501
   415
    struct DefArborescenceMap
deba@501
   416
      : public MinCostArborescence<Digraph, CostMap,
deba@501
   417
                                   DefArborescenceMapTraits<T> > {
deba@501
   418
    };
deba@501
   419
deba@501
   420
    template <class T>
deba@501
   421
    struct DefPredMapTraits : public Traits {
deba@501
   422
      typedef T PredMap;
deba@501
   423
      static PredMap *createPredMap(const Digraph &)
deba@501
   424
      {
deba@501
   425
        LEMON_ASSERT(false, "PredMap is not initialized");
deba@501
   426
      }
deba@501
   427
    };
deba@501
   428
deba@501
   429
    /// \brief \ref named-templ-param "Named parameter" for
deba@501
   430
    /// setting PredMap type
deba@501
   431
    ///
deba@501
   432
    /// \ref named-templ-param "Named parameter" for setting
deba@501
   433
    /// PredMap type
deba@501
   434
    template <class T>
deba@501
   435
    struct DefPredMap
deba@501
   436
      : public MinCostArborescence<Digraph, CostMap, DefPredMapTraits<T> > {
deba@501
   437
    };
deba@501
   438
deba@501
   439
    /// @}
deba@501
   440
deba@501
   441
    /// \brief Constructor.
deba@501
   442
    ///
deba@501
   443
    /// \param _digraph The digraph the algorithm will run on.
deba@501
   444
    /// \param _cost The cost map used by the algorithm.
deba@501
   445
    MinCostArborescence(const Digraph& digraph, const CostMap& cost)
deba@501
   446
      : _digraph(&digraph), _cost(&cost), _pred(0), local_pred(false),
deba@501
   447
        _arborescence(0), local_arborescence(false),
deba@501
   448
        _arc_order(0), _node_order(0), _cost_arcs(0),
deba@501
   449
        _heap_cross_ref(0), _heap(0) {}
deba@501
   450
deba@501
   451
    /// \brief Destructor.
deba@501
   452
    ~MinCostArborescence() {
deba@501
   453
      destroyStructures();
deba@501
   454
    }
deba@501
   455
deba@501
   456
    /// \brief Sets the arborescence map.
deba@501
   457
    ///
deba@501
   458
    /// Sets the arborescence map.
deba@501
   459
    /// \return \c (*this)
deba@501
   460
    MinCostArborescence& arborescenceMap(ArborescenceMap& m) {
deba@501
   461
      if (local_arborescence) {
deba@501
   462
        delete _arborescence;
deba@501
   463
      }
deba@501
   464
      local_arborescence = false;
deba@501
   465
      _arborescence = &m;
deba@501
   466
      return *this;
deba@501
   467
    }
deba@501
   468
deba@501
   469
    /// \brief Sets the arborescence map.
deba@501
   470
    ///
deba@501
   471
    /// Sets the arborescence map.
deba@501
   472
    /// \return \c (*this)
deba@501
   473
    MinCostArborescence& predMap(PredMap& m) {
deba@501
   474
      if (local_pred) {
deba@501
   475
        delete _pred;
deba@501
   476
      }
deba@501
   477
      local_pred = false;
deba@501
   478
      _pred = &m;
deba@501
   479
      return *this;
deba@501
   480
    }
deba@501
   481
deba@501
   482
    /// \name Query Functions
deba@501
   483
    /// The result of the %MinCostArborescence algorithm can be obtained
deba@501
   484
    /// using these functions.\n
deba@501
   485
    /// Before the use of these functions,
deba@501
   486
    /// either run() or start() must be called.
deba@501
   487
deba@501
   488
    /// @{
deba@501
   489
deba@501
   490
    /// \brief Returns a reference to the arborescence map.
deba@501
   491
    ///
deba@501
   492
    /// Returns a reference to the arborescence map.
deba@501
   493
    const ArborescenceMap& arborescenceMap() const {
deba@501
   494
      return *_arborescence;
deba@501
   495
    }
deba@501
   496
deba@501
   497
    /// \brief Returns true if the arc is in the arborescence.
deba@501
   498
    ///
deba@501
   499
    /// Returns true if the arc is in the arborescence.
deba@501
   500
    /// \param arc The arc of the digraph.
deba@501
   501
    /// \pre \ref run() must be called before using this function.
deba@501
   502
    bool arborescence(Arc arc) const {
deba@501
   503
      return (*_pred)[_digraph->target(arc)] == arc;
deba@501
   504
    }
deba@501
   505
deba@501
   506
    /// \brief Returns a reference to the pred map.
deba@501
   507
    ///
deba@501
   508
    /// Returns a reference to the pred map.
deba@501
   509
    const PredMap& predMap() const {
deba@501
   510
      return *_pred;
deba@501
   511
    }
deba@501
   512
deba@501
   513
    /// \brief Returns the predecessor arc of the given node.
deba@501
   514
    ///
deba@501
   515
    /// Returns the predecessor arc of the given node.
deba@501
   516
    Arc pred(Node node) const {
deba@501
   517
      return (*_pred)[node];
deba@501
   518
    }
deba@501
   519
deba@501
   520
    /// \brief Returns the cost of the arborescence.
deba@501
   521
    ///
deba@501
   522
    /// Returns the cost of the arborescence.
deba@501
   523
    Value arborescenceValue() const {
deba@501
   524
      Value sum = 0;
deba@501
   525
      for (ArcIt it(*_digraph); it != INVALID; ++it) {
deba@501
   526
        if (arborescence(it)) {
deba@501
   527
          sum += (*_cost)[it];
deba@501
   528
        }
deba@501
   529
      }
deba@501
   530
      return sum;
deba@501
   531
    }
deba@501
   532
deba@501
   533
    /// \brief Indicates that a node is reachable from the sources.
deba@501
   534
    ///
deba@501
   535
    /// Indicates that a node is reachable from the sources.
deba@501
   536
    bool reached(Node node) const {
deba@501
   537
      return (*_node_order)[node] != -3;
deba@501
   538
    }
deba@501
   539
deba@501
   540
    /// \brief Indicates that a node is processed.
deba@501
   541
    ///
deba@501
   542
    /// Indicates that a node is processed. The arborescence path exists
deba@501
   543
    /// from the source to the given node.
deba@501
   544
    bool processed(Node node) const {
deba@501
   545
      return (*_node_order)[node] == -1;
deba@501
   546
    }
deba@501
   547
deba@501
   548
    /// \brief Returns the number of the dual variables in basis.
deba@501
   549
    ///
deba@501
   550
    /// Returns the number of the dual variables in basis.
deba@501
   551
    int dualNum() const {
deba@501
   552
      return _dual_variables.size();
deba@501
   553
    }
deba@501
   554
deba@501
   555
    /// \brief Returns the value of the dual solution.
deba@501
   556
    ///
deba@501
   557
    /// Returns the value of the dual solution. It should be
deba@501
   558
    /// equal to the arborescence value.
deba@501
   559
    Value dualValue() const {
deba@501
   560
      Value sum = 0;
deba@501
   561
      for (int i = 0; i < int(_dual_variables.size()); ++i) {
deba@501
   562
        sum += _dual_variables[i].value;
deba@501
   563
      }
deba@501
   564
      return sum;
deba@501
   565
    }
deba@501
   566
deba@501
   567
    /// \brief Returns the number of the nodes in the dual variable.
deba@501
   568
    ///
deba@501
   569
    /// Returns the number of the nodes in the dual variable.
deba@501
   570
    int dualSize(int k) const {
deba@501
   571
      return _dual_variables[k].end - _dual_variables[k].begin;
deba@501
   572
    }
deba@501
   573
deba@501
   574
    /// \brief Returns the value of the dual variable.
deba@501
   575
    ///
deba@501
   576
    /// Returns the the value of the dual variable.
deba@501
   577
    const Value& dualValue(int k) const {
deba@501
   578
      return _dual_variables[k].value;
deba@501
   579
    }
deba@501
   580
deba@501
   581
    /// \brief Lemon iterator for get a dual variable.
deba@501
   582
    ///
deba@501
   583
    /// Lemon iterator for get a dual variable. This class provides
deba@501
   584
    /// a common style lemon iterator which gives back a subset of
deba@501
   585
    /// the nodes.
deba@501
   586
    class DualIt {
deba@501
   587
    public:
deba@501
   588
deba@501
   589
      /// \brief Constructor.
deba@501
   590
      ///
deba@501
   591
      /// Constructor for get the nodeset of the variable.
deba@501
   592
      DualIt(const MinCostArborescence& algorithm, int variable)
deba@501
   593
        : _algorithm(&algorithm)
deba@501
   594
      {
deba@501
   595
        _index = _algorithm->_dual_variables[variable].begin;
deba@501
   596
        _last = _algorithm->_dual_variables[variable].end;
deba@501
   597
      }
deba@501
   598
deba@501
   599
      /// \brief Conversion to node.
deba@501
   600
      ///
deba@501
   601
      /// Conversion to node.
deba@501
   602
      operator Node() const {
deba@501
   603
        return _algorithm->_dual_node_list[_index];
deba@501
   604
      }
deba@501
   605
deba@501
   606
      /// \brief Increment operator.
deba@501
   607
      ///
deba@501
   608
      /// Increment operator.
deba@501
   609
      DualIt& operator++() {
deba@501
   610
        ++_index;
deba@501
   611
        return *this;
deba@501
   612
      }
deba@501
   613
deba@501
   614
      /// \brief Validity checking
deba@501
   615
      ///
deba@501
   616
      /// Checks whether the iterator is invalid.
deba@501
   617
      bool operator==(Invalid) const {
deba@501
   618
        return _index == _last;
deba@501
   619
      }
deba@501
   620
deba@501
   621
      /// \brief Validity checking
deba@501
   622
      ///
deba@501
   623
      /// Checks whether the iterator is valid.
deba@501
   624
      bool operator!=(Invalid) const {
deba@501
   625
        return _index != _last;
deba@501
   626
      }
deba@501
   627
deba@501
   628
    private:
deba@501
   629
      const MinCostArborescence* _algorithm;
deba@501
   630
      int _index, _last;
deba@501
   631
    };
deba@501
   632
deba@501
   633
    /// @}
deba@501
   634
deba@501
   635
    /// \name Execution control
deba@501
   636
    /// The simplest way to execute the algorithm is to use
deba@501
   637
    /// one of the member functions called \c run(...). \n
deba@501
   638
    /// If you need more control on the execution,
deba@501
   639
    /// first you must call \ref init(), then you can add several
deba@501
   640
    /// source nodes with \ref addSource().
deba@501
   641
    /// Finally \ref start() will perform the arborescence
deba@501
   642
    /// computation.
deba@501
   643
deba@501
   644
    ///@{
deba@501
   645
deba@501
   646
    /// \brief Initializes the internal data structures.
deba@501
   647
    ///
deba@501
   648
    /// Initializes the internal data structures.
deba@501
   649
    ///
deba@501
   650
    void init() {
deba@501
   651
      createStructures();
deba@501
   652
      _heap->clear();
deba@501
   653
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
deba@501
   654
        (*_cost_arcs)[it].arc = INVALID;
deba@501
   655
        _node_order->set(it, -3);
deba@501
   656
        _heap_cross_ref->set(it, Heap::PRE_HEAP);
deba@501
   657
        _pred->set(it, INVALID);
deba@501
   658
      }
deba@501
   659
      for (ArcIt it(*_digraph); it != INVALID; ++it) {
deba@501
   660
        _arborescence->set(it, false);
deba@501
   661
        _arc_order->set(it, -1);
deba@501
   662
      }
deba@501
   663
      _dual_node_list.clear();
deba@501
   664
      _dual_variables.clear();
deba@501
   665
    }
deba@501
   666
deba@501
   667
    /// \brief Adds a new source node.
deba@501
   668
    ///
deba@501
   669
    /// Adds a new source node to the algorithm.
deba@501
   670
    void addSource(Node source) {
deba@501
   671
      std::vector<Node> nodes;
deba@501
   672
      nodes.push_back(source);
deba@501
   673
      while (!nodes.empty()) {
deba@501
   674
        Node node = nodes.back();
deba@501
   675
        nodes.pop_back();
deba@501
   676
        for (OutArcIt it(*_digraph, node); it != INVALID; ++it) {
deba@501
   677
          Node target = _digraph->target(it);
deba@501
   678
          if ((*_node_order)[target] == -3) {
deba@501
   679
            (*_node_order)[target] = -2;
deba@501
   680
            nodes.push_back(target);
deba@501
   681
            queue.push_back(target);
deba@501
   682
          }
deba@501
   683
        }
deba@501
   684
      }
deba@501
   685
      (*_node_order)[source] = -1;
deba@501
   686
    }
deba@501
   687
deba@501
   688
    /// \brief Processes the next node in the priority queue.
deba@501
   689
    ///
deba@501
   690
    /// Processes the next node in the priority queue.
deba@501
   691
    ///
deba@501
   692
    /// \return The processed node.
deba@501
   693
    ///
deba@501
   694
    /// \warning The queue must not be empty!
deba@501
   695
    Node processNextNode() {
deba@501
   696
      Node node = queue.back();
deba@501
   697
      queue.pop_back();
deba@501
   698
      if ((*_node_order)[node] == -2) {
deba@501
   699
        Arc arc = prepare(node);
deba@501
   700
        Node source = _digraph->source(arc);
deba@501
   701
        while ((*_node_order)[source] != -1) {
deba@501
   702
          if ((*_node_order)[source] >= 0) {
deba@501
   703
            arc = contract(source);
deba@501
   704
          } else {
deba@501
   705
            arc = prepare(source);
deba@501
   706
          }
deba@501
   707
          source = _digraph->source(arc);
deba@501
   708
        }
deba@501
   709
        finalize(arc);
deba@501
   710
        level_stack.clear();
deba@501
   711
      }
deba@501
   712
      return node;
deba@501
   713
    }
deba@501
   714
deba@501
   715
    /// \brief Returns the number of the nodes to be processed.
deba@501
   716
    ///
deba@501
   717
    /// Returns the number of the nodes to be processed.
deba@501
   718
    int queueSize() const {
deba@501
   719
      return queue.size();
deba@501
   720
    }
deba@501
   721
deba@501
   722
    /// \brief Returns \c false if there are nodes to be processed.
deba@501
   723
    ///
deba@501
   724
    /// Returns \c false if there are nodes to be processed.
deba@501
   725
    bool emptyQueue() const {
deba@501
   726
      return queue.empty();
deba@501
   727
    }
deba@501
   728
deba@501
   729
    /// \brief Executes the algorithm.
deba@501
   730
    ///
deba@501
   731
    /// Executes the algorithm.
deba@501
   732
    ///
deba@501
   733
    /// \pre init() must be called and at least one node should be added
deba@501
   734
    /// with addSource() before using this function.
deba@501
   735
    ///
deba@501
   736
    ///\note mca.start() is just a shortcut of the following code.
deba@501
   737
    ///\code
deba@501
   738
    ///while (!mca.emptyQueue()) {
deba@501
   739
    ///  mca.processNextNode();
deba@501
   740
    ///}
deba@501
   741
    ///\endcode
deba@501
   742
    void start() {
deba@501
   743
      while (!emptyQueue()) {
deba@501
   744
        processNextNode();
deba@501
   745
      }
deba@501
   746
    }
deba@501
   747
deba@501
   748
    /// \brief Runs %MinCostArborescence algorithm from node \c s.
deba@501
   749
    ///
deba@501
   750
    /// This method runs the %MinCostArborescence algorithm from
deba@501
   751
    /// a root node \c s.
deba@501
   752
    ///
deba@501
   753
    /// \note mca.run(s) is just a shortcut of the following code.
deba@501
   754
    /// \code
deba@501
   755
    /// mca.init();
deba@501
   756
    /// mca.addSource(s);
deba@501
   757
    /// mca.start();
deba@501
   758
    /// \endcode
deba@501
   759
    void run(Node node) {
deba@501
   760
      init();
deba@501
   761
      addSource(node);
deba@501
   762
      start();
deba@501
   763
    }
deba@501
   764
deba@501
   765
    ///@}
deba@501
   766
deba@501
   767
  };
deba@501
   768
deba@501
   769
  /// \ingroup spantree
deba@501
   770
  ///
deba@501
   771
  /// \brief Function type interface for MinCostArborescence algorithm.
deba@501
   772
  ///
deba@501
   773
  /// Function type interface for MinCostArborescence algorithm.
deba@501
   774
  /// \param digraph The Digraph that the algorithm runs on.
deba@501
   775
  /// \param cost The CostMap of the arcs.
deba@501
   776
  /// \param source The source of the arborescence.
deba@501
   777
  /// \retval arborescence The bool ArcMap which stores the arborescence.
deba@501
   778
  /// \return The cost of the arborescence.
deba@501
   779
  ///
deba@501
   780
  /// \sa MinCostArborescence
deba@501
   781
  template <typename Digraph, typename CostMap, typename ArborescenceMap>
deba@501
   782
  typename CostMap::Value minCostArborescence(const Digraph& digraph,
deba@501
   783
                                              const CostMap& cost,
deba@501
   784
                                              typename Digraph::Node source,
deba@501
   785
                                              ArborescenceMap& arborescence) {
deba@501
   786
    typename MinCostArborescence<Digraph, CostMap>
deba@501
   787
      ::template DefArborescenceMap<ArborescenceMap>
deba@501
   788
      ::Create mca(digraph, cost);
deba@501
   789
    mca.arborescenceMap(arborescence);
deba@501
   790
    mca.run(source);
deba@501
   791
    return mca.arborescenceValue();
deba@501
   792
  }
deba@501
   793
deba@501
   794
}
deba@501
   795
deba@501
   796
#endif