lemon/prim.h
author deba
Tue, 31 Jan 2006 20:14:52 +0000
changeset 1934 272fa8a0b680
child 1953 d4f411003580
permissions -rw-r--r--
Bug fix

ANodeIt/BNodeIt should not be inherited from ANode/BNode

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