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