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