lemon/prim.h
author kpeter
Sun, 05 Oct 2008 13:46:07 +0000
changeset 2621 814ba94d9989
parent 2490 31a93dd6f714
permissions -rw-r--r--
Bug fix in min_cost_flow_test.cc
deba@1912
     1
/* -*- C++ -*-
deba@1912
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1912
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1912
     8
 *
deba@1912
     9
 * Permission to use, modify and distribute this software is granted
deba@1912
    10
 * provided that this copyright notice appears in all copies. For
deba@1912
    11
 * precise terms see the accompanying LICENSE file.
deba@1912
    12
 *
deba@1912
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1912
    14
 * express or implied, and with no claim as to its suitability for any
deba@1912
    15
 * purpose.
deba@1912
    16
 *
deba@1912
    17
 */
deba@1912
    18
deba@1912
    19
#ifndef LEMON_PRIM_H
deba@1912
    20
#define LEMON_PRIM_H
deba@1912
    21
deba@1912
    22
///\ingroup spantree
deba@1912
    23
///\file
deba@1912
    24
///\brief Prim algorithm to compute minimum spanning tree.
deba@1912
    25
deba@1912
    26
#include <lemon/list_graph.h>
deba@1912
    27
#include <lemon/bin_heap.h>
deba@1993
    28
#include <lemon/bits/invalid.h>
deba@1912
    29
#include <lemon/error.h>
deba@1912
    30
#include <lemon/maps.h>
deba@1993
    31
#include <lemon/bits/traits.h>
deba@1912
    32
alpar@2260
    33
#include <lemon/concepts/ugraph.h>
deba@1912
    34
deba@1912
    35
namespace lemon {
deba@1912
    36
deba@1912
    37
  ///Default traits class of Prim class.
deba@1912
    38
deba@1912
    39
  ///Default traits class of Prim class.
deba@1912
    40
  ///\param GR Graph type.
deba@2030
    41
  ///\param CM Type of cost map.
deba@2030
    42
  template<class GR, class CM>
deba@1912
    43
  struct PrimDefaultTraits{
deba@1912
    44
    ///The graph type the algorithm runs on. 
deba@1912
    45
    typedef GR UGraph;
deba@1912
    46
    ///The type of the map that stores the edge costs.
deba@1912
    47
deba@1912
    48
    ///The type of the map that stores the edge costs.
alpar@2260
    49
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
deba@2030
    50
    typedef CM CostMap;
deba@1912
    51
    //The type of the cost of the edges.
deba@2030
    52
    typedef typename CM::Value Value;
deba@1912
    53
    /// The cross reference type used by heap.
deba@1912
    54
deba@1912
    55
    /// The cross reference type used by heap.
deba@1912
    56
    /// Usually it is \c UGraph::NodeMap<int>.
deba@1912
    57
    typedef typename UGraph::template NodeMap<int> HeapCrossRef;
deba@1912
    58
    ///Instantiates a HeapCrossRef.
deba@1912
    59
deba@1912
    60
    ///This function instantiates a \ref HeapCrossRef. 
alpar@1953
    61
    /// \param _graph is the graph, to which we would like to define the 
deba@1912
    62
    /// HeapCrossRef.
deba@1912
    63
    static HeapCrossRef *createHeapCrossRef(const GR &_graph){
deba@1912
    64
      return new HeapCrossRef(_graph);
deba@1912
    65
    }
deba@1912
    66
    
deba@1912
    67
    ///The heap type used by Prim algorithm.
deba@1912
    68
deba@1912
    69
    ///The heap type used by Prim algorithm.
deba@1912
    70
    ///
deba@1912
    71
    ///\sa BinHeap
deba@1912
    72
    ///\sa Prim
mqrelly@2263
    73
    typedef BinHeap<typename CM::Value,
deba@1912
    74
		    HeapCrossRef, std::less<Value> > Heap;
deba@1912
    75
deba@1912
    76
    static Heap *createHeap(HeapCrossRef& _ref){
deba@1912
    77
      return new Heap(_ref);
deba@1912
    78
    }
deba@1912
    79
deba@1912
    80
    ///\brief The type of the map that stores the last
deba@1912
    81
    ///edges of the minimum spanning tree.
deba@1912
    82
    /// 
deba@1912
    83
    ///The type of the map that stores the last
deba@1912
    84
    ///edges of the minimum spanning tree.
alpar@2260
    85
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
deba@1912
    86
    ///
deba@1912
    87
    typedef typename UGraph::template NodeMap<typename GR::UEdge> PredMap;
deba@1912
    88
    ///Instantiates a PredMap.
deba@1912
    89
 
deba@1912
    90
    ///This function instantiates a \ref PredMap. 
alpar@1953
    91
    ///\param _graph is the graph, to which we would like to define the PredMap.
deba@1912
    92
    static PredMap *createPredMap(const GR &_graph){
deba@1912
    93
      return new PredMap(_graph);
deba@1912
    94
    }
deba@1912
    95
deba@1912
    96
    ///The type of the map that stores whether an edge is in the
deba@1912
    97
    ///spanning tree or not.
deba@1912
    98
deba@1912
    99
    ///The type of the map that stores whether an edge is in the
deba@1912
   100
    ///spanning tree or not.
deba@1912
   101
    ///By default it is a NullMap.
deba@1912
   102
    typedef NullMap<typename UGraph::UEdge,bool> TreeMap;
deba@1912
   103
    ///Instantiates a TreeMap.
deba@1912
   104
deba@1912
   105
    ///This function instantiates a \ref TreeMap.
alpar@1953
   106
    ///
alpar@1953
   107
    ///The first parameter is the graph, to which
deba@1912
   108
    ///we would like to define the \ref TreeMap
deba@1912
   109
    static TreeMap *createTreeMap(const GR &){
deba@1912
   110
      return new TreeMap();
deba@1912
   111
    }
deba@1912
   112
deba@1912
   113
    ///The type of the map that stores whether a nodes is processed.
deba@1912
   114
 
deba@1912
   115
    ///The type of the map that stores whether a nodes is processed.
alpar@2260
   116
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
deba@1912
   117
    ///By default it is a NodeMap<bool>.
deba@1912
   118
    typedef NullMap<typename UGraph::Node,bool> ProcessedMap;
deba@1912
   119
    ///Instantiates a ProcessedMap.
deba@1912
   120
 
deba@1912
   121
    ///This function instantiates a \ref ProcessedMap. 
alpar@1953
   122
    ///\param _graph is the graph, to which
deba@1912
   123
    ///we would like to define the \ref ProcessedMap
deba@1912
   124
#ifdef DOXYGEN
deba@1912
   125
    static ProcessedMap *createProcessedMap(const GR &_graph)
deba@1912
   126
#else
deba@1912
   127
    static ProcessedMap *createProcessedMap(const GR &)
deba@1912
   128
#endif
deba@1912
   129
    {
deba@1912
   130
      return new ProcessedMap();
deba@1912
   131
    }
deba@1912
   132
  };
deba@1912
   133
  
deba@1912
   134
  ///%Prim algorithm class to find a minimum spanning tree.
deba@1912
   135
  
deba@1912
   136
  /// \ingroup spantree
deba@1912
   137
  ///This class provides an efficient implementation of %Prim algorithm.
deba@1912
   138
  ///
deba@2042
   139
  ///The running time is \f$ O(e\log(n)) \f$ where e is the number of edges and
deba@1912
   140
  ///n is the number of nodes in the graph.
deba@1912
   141
  ///
deba@1912
   142
  ///The edge costs are passed to the algorithm using a
alpar@2260
   143
  ///\ref concepts::ReadMap "ReadMap",
deba@1912
   144
  ///so it is easy to change it to any kind of cost.
deba@1912
   145
  ///
deba@1912
   146
  ///The type of the cost is determined by the
alpar@2260
   147
  ///\ref concepts::ReadMap::Value "Value" of the cost map.
deba@1912
   148
  ///
deba@1912
   149
  ///It is also possible to change the underlying priority heap.
deba@1912
   150
  ///
deba@1912
   151
  ///\param GR The graph type the algorithm runs on. The default value
deba@1912
   152
  ///is \ref ListUGraph. The value of GR is not used directly by
deba@1912
   153
  ///Prim, it is only passed to \ref PrimDefaultTraits.
deba@1912
   154
  ///
deba@2030
   155
  ///\param CM This read-only UEdgeMap determines the costs of the
deba@1912
   156
  ///edges. It is read once for each edge, so the map may involve in
deba@1912
   157
  ///relatively time consuming process to compute the edge cost if
deba@1912
   158
  ///it is necessary. The default map type is \ref
alpar@2260
   159
  ///concepts::UGraph::UEdgeMap "UGraph::UEdgeMap<int>".  The value
deba@2030
   160
  ///of CM is not used directly by Prim, it is only passed to \ref
deba@1912
   161
  ///PrimDefaultTraits.
deba@1912
   162
  ///
deba@1912
   163
  ///\param TR Traits class to set
deba@1912
   164
  ///various data types used by the algorithm.  The default traits
deba@1912
   165
  ///class is \ref PrimDefaultTraits
deba@2030
   166
  ///"PrimDefaultTraits<GR,CM>".  See \ref
deba@1912
   167
  ///PrimDefaultTraits for the documentation of a Prim traits
deba@1912
   168
  ///class.
deba@1912
   169
  ///
deba@1912
   170
  ///\author Balazs Attila Mihaly
deba@1912
   171
deba@1912
   172
#ifdef DOXYGEN
deba@1912
   173
  template <typename GR,
deba@2030
   174
	    typename CM,
deba@1912
   175
	    typename TR>
deba@1912
   176
#else
deba@1912
   177
  template <typename GR=ListUGraph,
deba@2030
   178
	    typename CM=typename GR::template UEdgeMap<int>,
deba@2030
   179
	    typename TR=PrimDefaultTraits<GR,CM> >
deba@1912
   180
#endif
deba@1912
   181
  class Prim {
deba@1912
   182
  public:
deba@2030
   183
    
deba@2030
   184
    /// \brief \ref Exception for uninitialized parameters.
deba@2030
   185
    ///
deba@2030
   186
    /// This error represents problems in the initialization
deba@2030
   187
    /// of the parameters of the algorithms.
deba@1912
   188
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@1912
   189
    public:
alpar@2151
   190
      virtual const char* what() const throw() {
deba@1912
   191
	return "lemon::Prim::UninitializedParameter";
deba@1912
   192
      }
deba@1912
   193
    };
deba@1912
   194
deba@1912
   195
    typedef TR Traits;
deba@1912
   196
    ///The type of the underlying graph.
deba@1912
   197
    typedef typename TR::UGraph UGraph;
deba@1912
   198
    ///\e
deba@1912
   199
    typedef typename UGraph::Node Node;
deba@1912
   200
    ///\e
deba@1912
   201
    typedef typename UGraph::NodeIt NodeIt;
deba@1912
   202
    ///\e
deba@1912
   203
    typedef typename UGraph::UEdge UEdge;
deba@1912
   204
    ///\e
deba@1912
   205
    typedef typename UGraph::IncEdgeIt IncEdgeIt;
deba@1912
   206
    
deba@1912
   207
    ///The type of the cost of the edges.
deba@1912
   208
    typedef typename TR::CostMap::Value Value;
deba@1912
   209
    ///The type of the map that stores the edge costs.
deba@1912
   210
    typedef typename TR::CostMap CostMap;
deba@1912
   211
    ///\brief The type of the map that stores the last
deba@1912
   212
    ///predecessor edges of the spanning tree.
deba@1912
   213
    typedef typename TR::PredMap PredMap;
deba@1912
   214
    ///Edges of the spanning tree.
deba@1912
   215
    typedef typename TR::TreeMap TreeMap;
deba@1912
   216
    ///The type of the map indicating if a node is processed.
deba@1912
   217
    typedef typename TR::ProcessedMap ProcessedMap;
deba@1912
   218
    ///The cross reference type used for the current heap.
deba@1912
   219
    typedef typename TR::HeapCrossRef HeapCrossRef;
deba@1912
   220
    ///The heap type used by the prim algorithm.
deba@1912
   221
    typedef typename TR::Heap Heap;
deba@1912
   222
  private:
deba@1912
   223
    /// Pointer to the underlying graph.
deba@1912
   224
    const UGraph *graph;
deba@1912
   225
    /// Pointer to the cost map
deba@1912
   226
    const CostMap *cost;
deba@1912
   227
    ///Pointer to the map of predecessors edges.
deba@1912
   228
    PredMap *_pred;
deba@1912
   229
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
deba@1912
   230
    bool local_pred;
deba@1912
   231
    ///Pointer to the map of tree edges.
deba@1912
   232
    TreeMap *_tree;
deba@1912
   233
    ///Indicates if \ref _tree is locally allocated (\c true) or not.
deba@1912
   234
    bool local_tree;
deba@1912
   235
    ///Pointer to the map of processed status of the nodes.
deba@1912
   236
    ProcessedMap *_processed;
deba@1912
   237
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
deba@1912
   238
    bool local_processed;
deba@1912
   239
    ///Pointer to the heap cross references.
deba@1912
   240
    HeapCrossRef *_heap_cross_ref;
deba@1912
   241
    ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
deba@1912
   242
    bool local_heap_cross_ref;
deba@1912
   243
    ///Pointer to the heap.
deba@1912
   244
    Heap *_heap;
deba@1912
   245
    ///Indicates if \ref _heap is locally allocated (\c true) or not.
deba@1912
   246
    bool local_heap;
deba@1912
   247
deba@1912
   248
    ///Creates the maps if necessary.
deba@1912
   249
    void create_maps(){
deba@1912
   250
      if(!_pred) {
deba@1912
   251
	local_pred = true;
deba@1912
   252
	_pred = Traits::createPredMap(*graph);
deba@1912
   253
      }
deba@1912
   254
      if(!_tree) {
deba@1912
   255
	local_tree = true;
deba@1912
   256
	_tree = Traits::createTreeMap(*graph);
deba@1912
   257
      }
deba@1912
   258
      if(!_processed) {
deba@1912
   259
	local_processed = true;
deba@1912
   260
	_processed = Traits::createProcessedMap(*graph);
deba@1912
   261
      }
deba@1912
   262
      if (!_heap_cross_ref) {
deba@1912
   263
	local_heap_cross_ref = true;
deba@1912
   264
	_heap_cross_ref = Traits::createHeapCrossRef(*graph);
deba@1912
   265
      }
deba@1912
   266
      if (!_heap) {
deba@1912
   267
	local_heap = true;
deba@1912
   268
	_heap = Traits::createHeap(*_heap_cross_ref);
deba@1912
   269
      }
deba@1912
   270
    }
deba@1912
   271
    
deba@1912
   272
  public :
deba@1912
   273
deba@1912
   274
    typedef Prim Create;
deba@1912
   275
 
deba@1912
   276
    ///\name Named template parameters
deba@1912
   277
deba@1912
   278
    ///@{
deba@1912
   279
deba@1912
   280
    template <class T>
deba@1912
   281
    struct DefPredMapTraits : public Traits {
deba@1912
   282
      typedef T PredMap;
deba@1912
   283
      static PredMap *createPredMap(const UGraph &_graph){
deba@1912
   284
	throw UninitializedParameter();
deba@1912
   285
      }
deba@1912
   286
    };
deba@2490
   287
    ///\brief \ref named-templ-param "Named parameter" for setting PredMap type
deba@2490
   288
    ///
deba@1912
   289
    ///\ref named-templ-param "Named parameter" for setting PredMap type
deba@1912
   290
    ///
deba@1912
   291
    template <class T>
deba@1912
   292
    struct DefPredMap 
deba@1912
   293
      : public Prim< UGraph, CostMap, DefPredMapTraits<T> > {
deba@1912
   294
      typedef Prim< UGraph, CostMap, DefPredMapTraits<T> > Create;
deba@1912
   295
    };
deba@1912
   296
    
deba@1912
   297
    template <class T>
deba@1912
   298
    struct DefProcessedMapTraits : public Traits {
deba@1912
   299
      typedef T ProcessedMap;
deba@1912
   300
      static ProcessedMap *createProcessedMap(const UGraph &_graph){
deba@1912
   301
	throw UninitializedParameter();
deba@1912
   302
      }
deba@1912
   303
    };
deba@2490
   304
    ///\brief \ref named-templ-param "Named parameter" for setting
deba@2490
   305
    ///ProcessedMap type
deba@2490
   306
    ///
deba@1912
   307
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
deba@1912
   308
    ///
deba@1912
   309
    template <class T>
deba@1912
   310
    struct DefProcessedMap 
deba@1912
   311
      : public Prim< UGraph, CostMap, DefProcessedMapTraits<T> > { 
deba@1912
   312
      typedef Prim< UGraph, CostMap, DefProcessedMapTraits<T> > Create;
deba@1912
   313
    };
deba@1912
   314
    
deba@1912
   315
    struct DefGraphProcessedMapTraits : public Traits {
deba@1912
   316
      typedef typename UGraph::template NodeMap<bool> ProcessedMap;
deba@1912
   317
      static ProcessedMap *createProcessedMap(const UGraph &_graph){
deba@1912
   318
	return new ProcessedMap(_graph);
deba@1912
   319
      }
deba@1912
   320
    };
deba@1912
   321
deba@1912
   322
deba@1912
   323
    template <class H, class CR>
deba@1912
   324
    struct DefHeapTraits : public Traits {
deba@1912
   325
      typedef CR HeapCrossRef;
deba@1912
   326
      typedef H Heap;
deba@1912
   327
      static HeapCrossRef *createHeapCrossRef(const UGraph &) {
deba@1912
   328
	throw UninitializedParameter();
deba@1912
   329
      }
deba@1912
   330
      static Heap *createHeap(HeapCrossRef &){
deba@1912
   331
	return UninitializedParameter();
deba@1912
   332
      }
deba@1912
   333
    };
deba@2230
   334
    ///\brief \ref named-templ-param "Named parameter" for setting
deba@2230
   335
    ///heap and cross reference type
deba@2230
   336
    ///
deba@1912
   337
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1912
   338
    ///reference type
deba@1912
   339
    ///
deba@1912
   340
    template <class H, class CR = typename UGraph::template NodeMap<int> >
deba@1912
   341
    struct DefHeap
deba@1912
   342
      : public Prim< UGraph, CostMap, DefHeapTraits<H, CR> > {
deba@1912
   343
      typedef Prim< UGraph, CostMap, DefHeapTraits<H, CR> > Create;
deba@1912
   344
    };
deba@1912
   345
deba@1912
   346
    template <class H, class CR>
deba@1912
   347
    struct DefStandardHeapTraits : public Traits {
deba@1912
   348
      typedef CR HeapCrossRef;
deba@1912
   349
      typedef H Heap;
deba@1912
   350
      static HeapCrossRef *createHeapCrossRef(const UGraph &_graph) {
deba@1912
   351
	return new HeapCrossRef(_graph);
deba@1912
   352
      }
deba@1912
   353
      static Heap *createHeap(HeapCrossRef &ref){
deba@1912
   354
	return new Heap(ref);
deba@1912
   355
      }
deba@1912
   356
    };
deba@2230
   357
    ///\brief \ref named-templ-param "Named parameter" for setting
deba@2230
   358
    ///heap and cross reference type with automatic allocation
deba@2230
   359
    ///
deba@1912
   360
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1912
   361
    ///reference type. It can allocate the heap and the cross reference 
deba@1912
   362
    ///object if the cross reference's constructor waits for the graph as 
deba@1912
   363
    ///parameter and the heap's constructor waits for the cross reference.
deba@1912
   364
    template <class H, class CR = typename UGraph::template NodeMap<int> >
deba@1912
   365
    struct DefStandardHeap
deba@1912
   366
      : public Prim< UGraph, CostMap, DefStandardHeapTraits<H, CR> > { 
deba@1912
   367
      typedef Prim< UGraph, CostMap, DefStandardHeapTraits<H, CR> > 
deba@1912
   368
      Create;
deba@1912
   369
    };
deba@1912
   370
deba@1912
   371
    template <class TM>
deba@1912
   372
    struct DefTreeMapTraits : public Traits {
deba@1912
   373
      typedef TM TreeMap;
deba@1912
   374
      static TreeMap *createTreeMap(const UGraph &) {
deba@1912
   375
        throw UninitializedParameter();
deba@1912
   376
      }
deba@1912
   377
    };
deba@2490
   378
    ///\brief \ref named-templ-param "Named parameter" for setting
deba@2490
   379
    ///TreeMap
deba@2490
   380
    ///
deba@1912
   381
    ///\ref named-templ-param "Named parameter" for setting TreeMap
deba@1912
   382
    ///
deba@1912
   383
    template <class TM>
deba@1912
   384
    struct DefTreeMap
deba@1912
   385
      : public Prim< UGraph, CostMap, DefTreeMapTraits<TM> > {
deba@1912
   386
      typedef Prim< UGraph, CostMap, DefTreeMapTraits<TM> > Create;
deba@1912
   387
    };    
deba@1912
   388
deba@1912
   389
    struct DefGraphTreeMapTraits : public Traits {
deba@1912
   390
      typedef typename UGraph::template NodeMap<bool> TreeMap;
deba@1912
   391
      static TreeMap *createTreeMap(const UGraph &_graph){
deba@1912
   392
	return new TreeMap(_graph);
deba@1912
   393
      }
deba@1912
   394
    };
deba@1912
   395
deba@1912
   396
    ///@}
deba@1912
   397
deba@1912
   398
deba@1912
   399
  protected:
deba@1912
   400
deba@1912
   401
    Prim() {}
deba@1912
   402
deba@1912
   403
  public:      
deba@1912
   404
    
deba@1912
   405
    ///Constructor.
deba@2397
   406
    ///
deba@1912
   407
    ///\param _graph the graph the algorithm will run on.
deba@1912
   408
    ///\param _cost the cost map used by the algorithm.
deba@1912
   409
    Prim(const UGraph& _graph, const CostMap& _cost) :
deba@1912
   410
      graph(&_graph), cost(&_cost),
deba@2397
   411
      _pred(0), local_pred(false),
deba@2397
   412
      _tree(0), local_tree(false),
deba@2397
   413
      _processed(0), local_processed(false),
deba@2397
   414
      _heap_cross_ref(0), local_heap_cross_ref(false),
deba@2397
   415
      _heap(0), local_heap(false)
deba@1912
   416
    {
alpar@2260
   417
      checkConcept<concepts::UGraph, UGraph>();
deba@1912
   418
    }
deba@1912
   419
    
deba@1912
   420
    ///Destructor.
deba@1912
   421
    ~Prim(){
deba@1912
   422
      if(local_pred) delete _pred;
deba@1912
   423
      if(local_tree) delete _tree;
deba@1912
   424
      if(local_processed) delete _processed;
deba@1912
   425
      if(local_heap_cross_ref) delete _heap_cross_ref;
deba@1912
   426
      if(local_heap) delete _heap;
deba@1912
   427
    }
deba@1912
   428
deba@1912
   429
    ///\brief Sets the cost map.
deba@2397
   430
    ///
deba@1912
   431
    ///Sets the cost map.
deba@1912
   432
    ///\return <tt> (*this) </tt>
deba@1912
   433
    Prim &costMap(const CostMap &m){
deba@1912
   434
      cost = &m;
deba@1912
   435
      return *this;
deba@1912
   436
    }
deba@1912
   437
deba@1912
   438
    ///\brief Sets the map storing the predecessor edges.
deba@2397
   439
    ///
deba@1912
   440
    ///Sets the map storing the predecessor edges.
deba@1912
   441
    ///If you don't use this function before calling \ref run(),
deba@1912
   442
    ///it will allocate one. The destuctor deallocates this
deba@1912
   443
    ///automatically allocated map, of course.
deba@1912
   444
    ///\return <tt> (*this) </tt>
deba@1912
   445
    Prim &predMap(PredMap &m){
deba@1912
   446
      if(local_pred) {
deba@1912
   447
	delete _pred;
deba@1912
   448
	local_pred=false;
deba@1912
   449
      }
deba@1912
   450
      _pred = &m;
deba@1912
   451
      return *this;
deba@1912
   452
    }
deba@1912
   453
deba@1912
   454
    ///\brief Sets the map storing the tree edges.
deba@2397
   455
    ///
deba@1912
   456
    ///Sets the map storing the tree edges.
deba@1912
   457
    ///If you don't use this function before calling \ref run(),
deba@1912
   458
    ///it will allocate one. The destuctor deallocates this
deba@1912
   459
    ///automatically allocated map, of course.
deba@1912
   460
    ///By default this is a NullMap.
deba@1912
   461
    ///\return <tt> (*this) </tt>
deba@1912
   462
    Prim &treeMap(TreeMap &m){
deba@1912
   463
      if(local_tree) {
deba@1912
   464
	delete _tree;
deba@1912
   465
	local_tree=false;
deba@1912
   466
      }
deba@1912
   467
      _tree = &m;
deba@1912
   468
      return *this;
deba@1912
   469
    }
deba@1912
   470
deba@1912
   471
    ///\brief Sets the heap and the cross reference used by algorithm.
deba@2397
   472
    ///
deba@1912
   473
    ///Sets the heap and the cross reference used by algorithm.
deba@1912
   474
    ///If you don't use this function before calling \ref run(),
deba@1912
   475
    ///it will allocate one. The destuctor deallocates this
deba@1912
   476
    ///automatically allocated map, of course.
deba@1912
   477
    ///\return <tt> (*this) </tt>
deba@1912
   478
    Prim &heap(Heap& heap, HeapCrossRef &crossRef){
deba@1912
   479
      if(local_heap_cross_ref) {
deba@1912
   480
	delete _heap_cross_ref;
deba@1912
   481
	local_heap_cross_ref=false;
deba@1912
   482
      }
deba@1912
   483
      _heap_cross_ref = &crossRef;
deba@1912
   484
      if(local_heap) {
deba@1912
   485
	delete _heap;
deba@1912
   486
	local_heap=false;
deba@1912
   487
      }
deba@1912
   488
      _heap = &heap;
deba@1912
   489
      return *this;
deba@1912
   490
    }
deba@1912
   491
deba@1912
   492
  public:
deba@1912
   493
    ///\name Execution control
deba@1912
   494
    ///The simplest way to execute the algorithm is to use
deba@1912
   495
    ///one of the member functions called \c run(...).
deba@1912
   496
    ///\n
deba@1912
   497
    ///If you need more control on the execution,
deba@1912
   498
    ///first you must call \ref init(), then you can add several source nodes
deba@1912
   499
    ///with \ref addSource().
deba@1912
   500
    ///Finally \ref start() will perform the actual path
deba@1912
   501
    ///computation.
deba@1912
   502
deba@1912
   503
    ///@{
deba@1912
   504
deba@1912
   505
    ///\brief Initializes the internal data structures.
deba@2397
   506
    ///
deba@1912
   507
    ///Initializes the internal data structures.
deba@1912
   508
    ///
deba@1912
   509
    void init(){
deba@1912
   510
      create_maps();
deba@1912
   511
      _heap->clear();
deba@1912
   512
      for ( NodeIt u(*graph) ; u!=INVALID ; ++u ) {
deba@1912
   513
	_pred->set(u,INVALID);
deba@1912
   514
	_processed->set(u,false);
deba@1912
   515
	_heap_cross_ref->set(u,Heap::PRE_HEAP);
deba@1912
   516
      }
deba@1912
   517
    }
deba@1912
   518
    
deba@1912
   519
    ///\brief Adds a new source node.
deba@2397
   520
    ///
deba@1912
   521
    ///Adds a new source node to the priority heap.
deba@1912
   522
    ///
deba@1912
   523
    ///It checks if the node has already been added to the heap and
deba@1912
   524
    ///it is pushed to the heap only if it was not in the heap.
deba@1912
   525
    void addSource(Node s){
deba@1912
   526
      if(_heap->state(s) != Heap::IN_HEAP) {
deba@1912
   527
	_heap->push(s,Value());
deba@1912
   528
      }
deba@1912
   529
    }
deba@1912
   530
    ///\brief Processes the next node in the priority heap
deba@2397
   531
    ///
deba@1912
   532
    ///Processes the next node in the priority heap.
deba@1912
   533
    ///
deba@1912
   534
    ///\return The processed node.
deba@1912
   535
    ///
deba@1912
   536
    ///\warning The priority heap must not be empty!
deba@1912
   537
    Node processNextNode(){
deba@1912
   538
      Node v=_heap->top(); 
deba@1912
   539
      _heap->pop();
deba@1912
   540
      _processed->set(v,true);
deba@1912
   541
      
deba@1912
   542
      for(IncEdgeIt e(*graph,v); e!=INVALID; ++e) {
deba@1912
   543
	Node w=graph->oppositeNode(v,e);
deba@1912
   544
	switch(_heap->state(w)) {
deba@1912
   545
	case Heap::PRE_HEAP:
deba@1912
   546
	  _heap->push(w,(*cost)[e]);
deba@1912
   547
	  _pred->set(w,e);
deba@1912
   548
	  break;
deba@1912
   549
	case Heap::IN_HEAP:
deba@1912
   550
	  if ( (*cost)[e] < (*_heap)[w] ) {
deba@1912
   551
	    _heap->decrease(w,(*cost)[e]); 
deba@1912
   552
	    _pred->set(w,e);
deba@1912
   553
	  }
deba@1912
   554
	  break;
deba@1912
   555
	case Heap::POST_HEAP:
deba@1912
   556
	  break;
deba@1912
   557
	}
deba@1912
   558
      }
deba@1912
   559
      if ((*_pred)[v]!=INVALID)_tree->set((*_pred)[v],true);
deba@1912
   560
      return v;
deba@1912
   561
    }
deba@1912
   562
deba@1912
   563
    ///\brief Next node to be processed.
deba@2397
   564
    ///
deba@1912
   565
    ///Next node to be processed.
deba@1912
   566
    ///
deba@1912
   567
    ///\return The next node to be processed or INVALID if the priority heap
deba@1912
   568
    /// is empty.
deba@1912
   569
    Node nextNode(){ 
deba@1912
   570
      return _heap->empty()?_heap->top():INVALID;
deba@1912
   571
    }
deba@1912
   572
 
deba@2397
   573
    ///\brief Returns \c false if there are nodes to be processed in
deba@2397
   574
    ///the priority heap
deba@1912
   575
    ///
deba@1912
   576
    ///Returns \c false if there are nodes
deba@1912
   577
    ///to be processed in the priority heap
deba@1912
   578
    bool emptyQueue() { return _heap->empty(); }
deba@1912
   579
deba@2397
   580
    ///\brief Returns the number of the nodes to be processed in the
deba@2397
   581
    ///priority heap
deba@2397
   582
    ///
deba@1912
   583
    ///Returns the number of the nodes to be processed in the priority heap
deba@1912
   584
    ///
deba@1912
   585
    int queueSize() { return _heap->size(); }
deba@1912
   586
    
deba@1912
   587
    ///\brief Executes the algorithm.
deba@2397
   588
    ///
deba@1912
   589
    ///Executes the algorithm.
deba@1912
   590
    ///
deba@1912
   591
    ///\pre init() must be called and at least one node should be added
deba@1912
   592
    ///with addSource() before using this function.
deba@1912
   593
    ///
deba@1912
   594
    ///This method runs the %Prim algorithm from the node(s)
deba@1912
   595
    ///in order to compute the
deba@1912
   596
    ///minimum spanning tree.
deba@1912
   597
    ///
deba@1912
   598
    void start(){
deba@1912
   599
      while ( !_heap->empty() ) processNextNode();
deba@1912
   600
    }
deba@1912
   601
    
deba@1912
   602
    ///\brief Executes the algorithm until a condition is met.
deba@2397
   603
    ///
deba@1912
   604
    ///Executes the algorithm until a condition is met.
deba@1912
   605
    ///
deba@1912
   606
    ///\pre init() must be called and at least one node should be added
deba@1912
   607
    ///with addSource() before using this function.
deba@1912
   608
    ///
deba@1912
   609
    ///\param nm must be a bool (or convertible) node map. The algorithm
deba@1912
   610
    ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
deba@1912
   611
    template<class NodeBoolMap>
deba@1912
   612
    void start(const NodeBoolMap &nm){
deba@1912
   613
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
deba@1912
   614
      if ( !_heap->empty() ) _processed->set(_heap->top(),true);
deba@1912
   615
    }
deba@1912
   616
    
deba@1912
   617
    ///\brief Runs %Prim algorithm.
deba@2397
   618
    ///
deba@1912
   619
    ///This method runs the %Prim algorithm
deba@1912
   620
    ///in order to compute the
deba@1912
   621
    ///minimum spanning tree (or minimum spanning forest).
deba@1912
   622
    ///The method also works on graphs that has more than one components.
deba@1912
   623
    ///In this case it computes the minimum spanning forest.
deba@1912
   624
    void run() {
deba@1912
   625
      init();
deba@1912
   626
      for(NodeIt it(*graph);it!=INVALID;++it){
deba@1912
   627
	if(!processed(it)){
deba@1912
   628
	  addSource(it);
deba@1912
   629
	  start();
deba@1912
   630
	}
deba@1912
   631
      }
deba@1912
   632
    }
deba@1912
   633
deba@1912
   634
    ///\brief Runs %Prim algorithm from node \c s.
deba@2397
   635
    ///
deba@1912
   636
    ///This method runs the %Prim algorithm from node \c s
deba@1912
   637
    ///in order to
deba@1912
   638
    ///compute the
deba@1912
   639
    ///minimun spanning tree
deba@1912
   640
    ///
deba@2397
   641
    ///\note p.run(s) is just a shortcut of the following code.
deba@1912
   642
    ///\code
deba@2397
   643
    ///  p.init();
deba@2397
   644
    ///  p.addSource(s);
deba@2397
   645
    ///  p.start();
deba@1912
   646
    ///\endcode
deba@1912
   647
    ///\note If the graph has more than one components, the method
deba@1912
   648
    ///will compute the minimun spanning tree for only one component.
deba@1912
   649
    ///
deba@1912
   650
    ///See \ref run() if you want to compute the minimal spanning forest.
deba@1912
   651
    void run(Node s){
deba@1912
   652
      init();
deba@1912
   653
      addSource(s);
deba@1912
   654
      start();
deba@1912
   655
    }
deba@1912
   656
    
deba@1912
   657
    ///@}
deba@1912
   658
deba@1912
   659
    ///\name Query Functions
deba@1912
   660
    ///The result of the %Prim algorithm can be obtained using these
deba@1912
   661
    ///functions.\n
deba@1912
   662
    ///Before the use of these functions,
deba@1912
   663
    ///either run() or start() must be called.
deba@1912
   664
    
deba@1912
   665
    ///@{
deba@1912
   666
deba@1912
   667
    ///\brief Returns the 'previous edge' of the minimum spanning tree.
deba@1912
   668
deba@2397
   669
    ///For a node \c v it returns the 'previous edge' of the minimum
deba@2397
   670
    ///spanning tree, i.e. it returns the edge from where \c v was
deba@2397
   671
    ///reached. For a source node or an unreachable node it is \ref
deba@2397
   672
    ///INVALID.  The minimum spanning tree used here is equal to the
deba@2397
   673
    ///minimum spanning tree used in \ref predNode().  
deba@2397
   674
    ///\pre \ref run() or \ref start() must be called before using
deba@2397
   675
    ///this function.
deba@1912
   676
    UEdge predEdge(Node v) const { return (*_pred)[v]; }
deba@1912
   677
deba@2397
   678
    ///\brief Returns the 'previous node' of the minimum spanning
deba@2397
   679
    ///tree.
deba@2397
   680
    ///
deba@2397
   681
    ///For a node \c v it returns the 'previous node' of the minimum
deba@2397
   682
    ///spanning tree, i.e. it returns the node from where \c v was
deba@2397
   683
    ///reached. For a source node or an unreachable node it is \ref
deba@2397
   684
    ///INVALID.  //The minimum spanning tree used here is equal to the
deba@2397
   685
    ///minimum spanning tree used in \ref predEdge().  
deba@2397
   686
    ///\pre \ref run() or \ref start() must be called before using
deba@2397
   687
    ///this function.
deba@1912
   688
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
deba@1912
   689
				  graph->source((*_pred)[v]); }
deba@1912
   690
    
deba@2397
   691
    ///\brief Returns a reference to the NodeMap of the edges of the
deba@1912
   692
    ///minimum spanning tree.
deba@2397
   693
    ///
deba@2397
   694
    ///Returns a reference to the NodeMap of the edges of the minimum
deba@2397
   695
    ///spanning tree.
deba@2397
   696
    ///\pre \ref run() or \ref start() must be called before using
deba@2397
   697
    ///this function.
deba@1912
   698
    const PredMap &predMap() const { return *_pred;}
deba@1912
   699
 
deba@1912
   700
    ///\brief Returns a reference to the tree edges map.
deba@1912
   701
deba@1912
   702
    ///Returns a reference to the TreeEdgeMap of the edges of the
deba@2397
   703
    ///minimum spanning tree. The value of the map is \c true only if
deba@2397
   704
    ///the edge is in the minimum spanning tree.
deba@2397
   705
    ///
deba@1912
   706
    ///\warning By default, the TreeEdgeMap is a NullMap.
deba@1912
   707
    ///
deba@2397
   708
    ///If it is not set before the execution of the algorithm, use the
deba@2397
   709
    ///\ref treeMap(TreeMap&) function (after the execution) to set an
deba@2397
   710
    ///UEdgeMap with the edges of the minimum spanning tree in O(n)
deba@2397
   711
    ///time where n is the number of nodes in the graph.
deba@2397
   712
    ///\pre \ref run() or \ref start() must be called before using
deba@2397
   713
    ///this function.
deba@1912
   714
    const TreeMap &treeMap() const { return *_tree;}
deba@1912
   715
 
deba@1912
   716
    ///\brief Sets the tree edges map.
deba@2397
   717
    ///
deba@1912
   718
    ///Sets the TreeMap of the edges of the minimum spanning tree.
deba@1912
   719
    ///The map values belonging to the edges of the minimum
alpar@1953
   720
    ///spanning tree are set to \c tree_edge_value or \c true by default,
deba@1912
   721
    ///the other map values remain untouched.
deba@1912
   722
    ///
deba@2397
   723
    ///\pre \ref run() or \ref start() must be called before using
deba@2397
   724
    ///this function.
deba@1912
   725
deba@1912
   726
    template<class TreeMap>
deba@2397
   727
    void quickTreeEdges(TreeMap& tree) const {
deba@1912
   728
      for(NodeIt i(*graph);i!=INVALID;++i){
deba@2397
   729
        if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],true);
deba@1912
   730
      }
deba@1912
   731
    }
deba@1912
   732
deba@1912
   733
    ///\brief Sets the tree edges map.
deba@2397
   734
    ///
deba@1912
   735
    ///Sets the TreeMap of the edges of the minimum spanning tree.
deba@1912
   736
    ///The map values belonging to the edges of the minimum
alpar@1953
   737
    ///spanning tree are set to \c tree_edge_value or \c true by default while
deba@1912
   738
    ///the edge values not belonging to the minimum spanning tree are set to
alpar@1953
   739
    ///\c tree_default_value or \c false by default.
deba@1912
   740
    ///
deba@2397
   741
    ///\pre \ref run() or \ref start() must be called before using
deba@2397
   742
    ///this function.
deba@2397
   743
    template <class TreeMap>
deba@2397
   744
    void treeEdges(TreeMap& tree) const {
deba@2397
   745
      typedef typename ItemSetTraits<UGraph,UEdge>::ItemIt TreeMapIt;
deba@2397
   746
      for(TreeMapIt i(*graph); i != INVALID; ++i) {
deba@2397
   747
	tree.set(i,false);
deba@2397
   748
      }
deba@2397
   749
      for(NodeIt i(*graph); i != INVALID; ++i){
deba@2397
   750
        if((*_pred)[i] != INVALID) tree.set((*_pred)[i],true);
deba@1912
   751
      }
deba@1912
   752
    }
deba@1912
   753
deba@1912
   754
    ///\brief Checks if a node is reachable from the starting node.
deba@2397
   755
    ///
deba@1912
   756
    ///Returns \c true if \c v is reachable from the starting node.
deba@1912
   757
    ///\warning The source nodes are inditated as unreached.
deba@2397
   758
    ///\pre \ref run() or \ref start() must be called before using
deba@2397
   759
    ///this function.
deba@1912
   760
    ///
deba@1912
   761
    bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
deba@1912
   762
deba@1912
   763
    ///\brief Checks if a node is processed.
deba@1912
   764
    ///
deba@2397
   765
    ///Returns \c true if \c v is processed, i.e. \c v is already
deba@2397
   766
    ///connencted to the minimum spanning tree.  \pre \ref run() or
deba@2397
   767
    ///\ref start() must be called before using this function.
deba@1912
   768
    bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
deba@1912
   769
    
deba@1912
   770
deba@1912
   771
    ///\brief Checks if an edge is in the spanning tree or not.
deba@2397
   772
    ///
deba@1912
   773
    ///Checks if an edge is in the spanning tree or not.
deba@1912
   774
    ///\param e is the edge that will be checked
deba@1912
   775
    ///\return \c true if e is in the spanning tree, \c false otherwise
deba@1912
   776
    bool tree(UEdge e){
deba@1912
   777
      return (*_pred)[*graph.source(e)]==e || (*_pred)[*graph.target(e)]==e;
deba@1912
   778
    }
deba@2397
   779
deba@2397
   780
    ///\brief Returns the value of the total cost of the spanning tree.
deba@2397
   781
    ///
deba@2397
   782
    /// Returns the value of the total cost of the spanning tree.
deba@2397
   783
    Value treeValue() const {
deba@2397
   784
      Value value = 0;
deba@2397
   785
      for(NodeIt i(*graph); i!= INVALID; ++i){
deba@2397
   786
        if ((*_pred)[i] != INVALID) value += (*cost)[(*_pred)[i]];
deba@2397
   787
      }
deba@2397
   788
      return value;
deba@2397
   789
    }
deba@1912
   790
    ///@}
deba@1912
   791
  };
deba@1912
   792
deba@1912
   793
deba@1912
   794
  /// \ingroup spantree
deba@1912
   795
  ///
deba@1912
   796
  /// \brief Function type interface for Prim algorithm.
deba@1912
   797
  ///
deba@1912
   798
  /// Function type interface for Prim algorithm.
deba@1912
   799
  /// \param graph the UGraph that the algorithm runs on
deba@1912
   800
  /// \param cost the CostMap of the edges
deba@1912
   801
  /// \retval tree the EdgeMap that contains whether an edge is in 
deba@1912
   802
  /// the spanning tree or not
deba@2397
   803
  /// \return The total cost of the spanning tree
deba@1912
   804
  ///
deba@1912
   805
  ///\sa Prim
deba@1912
   806
  template<class Graph,class CostMap,class TreeMap>
deba@2397
   807
  typename CostMap::Value prim(const Graph& graph, 
deba@2397
   808
                               const CostMap& cost,
deba@2397
   809
                               TreeMap& tree){
deba@2397
   810
    typename Prim<Graph,CostMap>::
deba@2397
   811
      template DefTreeMap<TreeMap>::
deba@1912
   812
      Create prm(graph,cost);
deba@1912
   813
    prm.treeMap(tree);
deba@1912
   814
    prm.run();
deba@2397
   815
    return prm.treeValue();
deba@2397
   816
  }
deba@2397
   817
deba@2397
   818
  /// \ingroup spantree
deba@2397
   819
  ///
deba@2397
   820
  /// \brief Function type interface for Prim algorithm.
deba@2397
   821
  ///
deba@2397
   822
  /// Function type interface for Prim algorithm.
deba@2397
   823
  /// \param graph the UGraph that the algorithm runs on
deba@2397
   824
  /// \param cost the CostMap of the edges
deba@2397
   825
  /// the spanning tree or not
deba@2397
   826
  /// \return The total cost of the spanning tree
deba@2397
   827
  ///
deba@2397
   828
  ///\sa Prim
deba@2397
   829
  template<class Graph,class CostMap,class TreeMap>
deba@2397
   830
  typename CostMap::Value prim(const Graph& graph, 
deba@2397
   831
                               const CostMap& cost){
deba@2397
   832
    typename Prim<Graph,CostMap>::
deba@2397
   833
      Create prm(graph,cost);
deba@2397
   834
    prm.run();
deba@2397
   835
    return prm.treeValue();
deba@1979
   836
  }
deba@1912
   837
deba@1912
   838
} //END OF NAMESPACE LEMON
deba@1912
   839
deba@1912
   840
#endif