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