lemon/prim.h
author alpar
Tue, 18 Jul 2006 13:29:59 +0000
changeset 2151 38ec4a930c05
parent 2042 bdc953f2a449
child 2230 67af33b34394
permissions -rw-r--r--
exceptionName() has been thrown away
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@1956
     5
 * Copyright (C) 2003-2006
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
deba@1912
    33
#include <lemon/concept/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.
deba@1912
    49
    ///It must meet the \ref concept::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
deba@2030
    73
    typedef BinHeap<typename UGraph::Node, 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.
deba@1912
    85
    ///It must meet the \ref concept::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.
deba@1912
   116
    ///It must meet the \ref concept::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
deba@1912
   143
  ///\ref concept::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
deba@1912
   147
  ///\ref concept::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
deba@1912
   159
  ///concept::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@1912
   287
    ///\ref named-templ-param "Named parameter" for setting PredMap type
deba@1912
   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@1912
   304
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
deba@1912
   305
deba@1912
   306
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
deba@1912
   307
    ///
deba@1912
   308
    template <class T>
deba@1912
   309
    struct DefProcessedMap 
deba@1912
   310
      : public Prim< UGraph, CostMap, DefProcessedMapTraits<T> > { 
deba@1912
   311
      typedef Prim< UGraph, CostMap, DefProcessedMapTraits<T> > Create;
deba@1912
   312
    };
deba@1912
   313
    
deba@1912
   314
    struct DefGraphProcessedMapTraits : public Traits {
deba@1912
   315
      typedef typename UGraph::template NodeMap<bool> ProcessedMap;
deba@1912
   316
      static ProcessedMap *createProcessedMap(const UGraph &_graph){
deba@1912
   317
	return new ProcessedMap(_graph);
deba@1912
   318
      }
deba@1912
   319
    };
deba@1912
   320
deba@1912
   321
deba@1912
   322
    template <class H, class CR>
deba@1912
   323
    struct DefHeapTraits : public Traits {
deba@1912
   324
      typedef CR HeapCrossRef;
deba@1912
   325
      typedef H Heap;
deba@1912
   326
      static HeapCrossRef *createHeapCrossRef(const UGraph &) {
deba@1912
   327
	throw UninitializedParameter();
deba@1912
   328
      }
deba@1912
   329
      static Heap *createHeap(HeapCrossRef &){
deba@1912
   330
	return UninitializedParameter();
deba@1912
   331
      }
deba@1912
   332
    };
deba@1912
   333
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1912
   334
    ///reference type
deba@1912
   335
deba@1912
   336
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1912
   337
    ///reference type
deba@1912
   338
    ///
deba@1912
   339
    template <class H, class CR = typename UGraph::template NodeMap<int> >
deba@1912
   340
    struct DefHeap
deba@1912
   341
      : public Prim< UGraph, CostMap, DefHeapTraits<H, CR> > {
deba@1912
   342
      typedef Prim< UGraph, CostMap, DefHeapTraits<H, CR> > Create;
deba@1912
   343
    };
deba@1912
   344
deba@1912
   345
    template <class H, class CR>
deba@1912
   346
    struct DefStandardHeapTraits : public Traits {
deba@1912
   347
      typedef CR HeapCrossRef;
deba@1912
   348
      typedef H Heap;
deba@1912
   349
      static HeapCrossRef *createHeapCrossRef(const UGraph &_graph) {
deba@1912
   350
	return new HeapCrossRef(_graph);
deba@1912
   351
      }
deba@1912
   352
      static Heap *createHeap(HeapCrossRef &ref){
deba@1912
   353
	return new Heap(ref);
deba@1912
   354
      }
deba@1912
   355
    };
deba@1912
   356
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1912
   357
    ///reference type with automatic allocation
deba@1912
   358
deba@1912
   359
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1912
   360
    ///reference type. It can allocate the heap and the cross reference 
deba@1912
   361
    ///object if the cross reference's constructor waits for the graph as 
deba@1912
   362
    ///parameter and the heap's constructor waits for the cross reference.
deba@1912
   363
    template <class H, class CR = typename UGraph::template NodeMap<int> >
deba@1912
   364
    struct DefStandardHeap
deba@1912
   365
      : public Prim< UGraph, CostMap, DefStandardHeapTraits<H, CR> > { 
deba@1912
   366
      typedef Prim< UGraph, CostMap, DefStandardHeapTraits<H, CR> > 
deba@1912
   367
      Create;
deba@1912
   368
    };
deba@1912
   369
deba@1912
   370
    template <class TM>
deba@1912
   371
    struct DefTreeMapTraits : public Traits {
deba@1912
   372
      typedef TM TreeMap;
deba@1912
   373
      static TreeMap *createTreeMap(const UGraph &) {
deba@1912
   374
        throw UninitializedParameter();
deba@1912
   375
      }
deba@1912
   376
    };
deba@1912
   377
    ///\ref named-templ-param "Named parameter" for setting TreeMap
deba@1912
   378
deba@1912
   379
    ///\ref named-templ-param "Named parameter" for setting TreeMap
deba@1912
   380
    ///
deba@1912
   381
    template <class TM>
deba@1912
   382
    struct DefTreeMap
deba@1912
   383
      : public Prim< UGraph, CostMap, DefTreeMapTraits<TM> > {
deba@1912
   384
      typedef Prim< UGraph, CostMap, DefTreeMapTraits<TM> > Create;
deba@1912
   385
    };    
deba@1912
   386
deba@1912
   387
    struct DefGraphTreeMapTraits : public Traits {
deba@1912
   388
      typedef typename UGraph::template NodeMap<bool> TreeMap;
deba@1912
   389
      static TreeMap *createTreeMap(const UGraph &_graph){
deba@1912
   390
	return new TreeMap(_graph);
deba@1912
   391
      }
deba@1912
   392
    };
deba@1912
   393
deba@1912
   394
    ///@}
deba@1912
   395
deba@1912
   396
deba@1912
   397
  protected:
deba@1912
   398
deba@1912
   399
    Prim() {}
deba@1912
   400
deba@1912
   401
  public:      
deba@1912
   402
    
deba@1912
   403
    ///Constructor.
deba@1912
   404
    
deba@1912
   405
    ///\param _graph the graph the algorithm will run on.
deba@1912
   406
    ///\param _cost the cost map used by the algorithm.
deba@1912
   407
    Prim(const UGraph& _graph, const CostMap& _cost) :
deba@1912
   408
      graph(&_graph), cost(&_cost),
deba@1912
   409
      _pred(NULL), local_pred(false),
deba@1912
   410
      _tree(NULL), local_tree(false),
deba@1912
   411
      _processed(NULL), local_processed(false),
deba@1912
   412
      _heap_cross_ref(NULL), local_heap_cross_ref(false),
deba@1912
   413
      _heap(NULL), local_heap(false)
deba@1912
   414
    {
deba@1912
   415
      checkConcept<concept::UGraph, UGraph>();
deba@1912
   416
    }
deba@1912
   417
    
deba@1912
   418
    ///Destructor.
deba@1912
   419
    ~Prim(){
deba@1912
   420
      if(local_pred) delete _pred;
deba@1912
   421
      if(local_tree) delete _tree;
deba@1912
   422
      if(local_processed) delete _processed;
deba@1912
   423
      if(local_heap_cross_ref) delete _heap_cross_ref;
deba@1912
   424
      if(local_heap) delete _heap;
deba@1912
   425
    }
deba@1912
   426
deba@1912
   427
    ///\brief Sets the cost map.
deba@1912
   428
deba@1912
   429
    ///Sets the cost map.
deba@1912
   430
    ///\return <tt> (*this) </tt>
deba@1912
   431
    Prim &costMap(const CostMap &m){
deba@1912
   432
      cost = &m;
deba@1912
   433
      return *this;
deba@1912
   434
    }
deba@1912
   435
deba@1912
   436
    ///\brief Sets the map storing the predecessor edges.
deba@1912
   437
deba@1912
   438
    ///Sets the map storing the predecessor edges.
deba@1912
   439
    ///If you don't use this function before calling \ref run(),
deba@1912
   440
    ///it will allocate one. The destuctor deallocates this
deba@1912
   441
    ///automatically allocated map, of course.
deba@1912
   442
    ///\return <tt> (*this) </tt>
deba@1912
   443
    Prim &predMap(PredMap &m){
deba@1912
   444
      if(local_pred) {
deba@1912
   445
	delete _pred;
deba@1912
   446
	local_pred=false;
deba@1912
   447
      }
deba@1912
   448
      _pred = &m;
deba@1912
   449
      return *this;
deba@1912
   450
    }
deba@1912
   451
deba@1912
   452
    ///\brief Sets the map storing the tree edges.
deba@1912
   453
deba@1912
   454
    ///Sets the map storing the tree edges.
deba@1912
   455
    ///If you don't use this function before calling \ref run(),
deba@1912
   456
    ///it will allocate one. The destuctor deallocates this
deba@1912
   457
    ///automatically allocated map, of course.
deba@1912
   458
    ///By default this is a NullMap.
deba@1912
   459
    ///\return <tt> (*this) </tt>
deba@1912
   460
    Prim &treeMap(TreeMap &m){
deba@1912
   461
      if(local_tree) {
deba@1912
   462
	delete _tree;
deba@1912
   463
	local_tree=false;
deba@1912
   464
      }
deba@1912
   465
      _tree = &m;
deba@1912
   466
      return *this;
deba@1912
   467
    }
deba@1912
   468
deba@1912
   469
    ///\brief Sets the heap and the cross reference used by algorithm.
deba@1912
   470
deba@1912
   471
    ///Sets the heap and the cross reference used by algorithm.
deba@1912
   472
    ///If you don't use this function before calling \ref run(),
deba@1912
   473
    ///it will allocate one. The destuctor deallocates this
deba@1912
   474
    ///automatically allocated map, of course.
deba@1912
   475
    ///\return <tt> (*this) </tt>
deba@1912
   476
    Prim &heap(Heap& heap, HeapCrossRef &crossRef){
deba@1912
   477
      if(local_heap_cross_ref) {
deba@1912
   478
	delete _heap_cross_ref;
deba@1912
   479
	local_heap_cross_ref=false;
deba@1912
   480
      }
deba@1912
   481
      _heap_cross_ref = &crossRef;
deba@1912
   482
      if(local_heap) {
deba@1912
   483
	delete _heap;
deba@1912
   484
	local_heap=false;
deba@1912
   485
      }
deba@1912
   486
      _heap = &heap;
deba@1912
   487
      return *this;
deba@1912
   488
    }
deba@1912
   489
deba@1912
   490
  public:
deba@1912
   491
    ///\name Execution control
deba@1912
   492
    ///The simplest way to execute the algorithm is to use
deba@1912
   493
    ///one of the member functions called \c run(...).
deba@1912
   494
    ///\n
deba@1912
   495
    ///If you need more control on the execution,
deba@1912
   496
    ///first you must call \ref init(), then you can add several source nodes
deba@1912
   497
    ///with \ref addSource().
deba@1912
   498
    ///Finally \ref start() will perform the actual path
deba@1912
   499
    ///computation.
deba@1912
   500
deba@1912
   501
    ///@{
deba@1912
   502
deba@1912
   503
    ///\brief Initializes the internal data structures.
deba@1912
   504
deba@1912
   505
    ///Initializes the internal data structures.
deba@1912
   506
    ///
deba@1912
   507
    void init(){
deba@1912
   508
      create_maps();
deba@1912
   509
      _heap->clear();
deba@1912
   510
      for ( NodeIt u(*graph) ; u!=INVALID ; ++u ) {
deba@1912
   511
	_pred->set(u,INVALID);
deba@1912
   512
	_processed->set(u,false);
deba@1912
   513
	_heap_cross_ref->set(u,Heap::PRE_HEAP);
deba@1912
   514
      }
deba@1912
   515
    }
deba@1912
   516
    
deba@1912
   517
    ///\brief Adds a new source node.
deba@1912
   518
deba@1912
   519
    ///Adds a new source node to the priority heap.
deba@1912
   520
    ///
deba@1912
   521
    ///It checks if the node has already been added to the heap and
deba@1912
   522
    ///it is pushed to the heap only if it was not in the heap.
deba@1912
   523
    void addSource(Node s){
deba@1912
   524
      if(_heap->state(s) != Heap::IN_HEAP) {
deba@1912
   525
	_heap->push(s,Value());
deba@1912
   526
      }
deba@1912
   527
    }
deba@1912
   528
    ///\brief Processes the next node in the priority heap
deba@1912
   529
deba@1912
   530
    ///Processes the next node in the priority heap.
deba@1912
   531
    ///
deba@1912
   532
    ///\return The processed node.
deba@1912
   533
    ///
deba@1912
   534
    ///\warning The priority heap must not be empty!
deba@1912
   535
    Node processNextNode(){
deba@1912
   536
      Node v=_heap->top(); 
deba@1912
   537
      _heap->pop();
deba@1912
   538
      _processed->set(v,true);
deba@1912
   539
      
deba@1912
   540
      for(IncEdgeIt e(*graph,v); e!=INVALID; ++e) {
deba@1912
   541
	Node w=graph->oppositeNode(v,e);
deba@1912
   542
	switch(_heap->state(w)) {
deba@1912
   543
	case Heap::PRE_HEAP:
deba@1912
   544
	  _heap->push(w,(*cost)[e]);
deba@1912
   545
	  _pred->set(w,e);
deba@1912
   546
	  break;
deba@1912
   547
	case Heap::IN_HEAP:
deba@1912
   548
	  if ( (*cost)[e] < (*_heap)[w] ) {
deba@1912
   549
	    _heap->decrease(w,(*cost)[e]); 
deba@1912
   550
	    _pred->set(w,e);
deba@1912
   551
	  }
deba@1912
   552
	  break;
deba@1912
   553
	case Heap::POST_HEAP:
deba@1912
   554
	  break;
deba@1912
   555
	}
deba@1912
   556
      }
deba@1912
   557
      if ((*_pred)[v]!=INVALID)_tree->set((*_pred)[v],true);
deba@1912
   558
      return v;
deba@1912
   559
    }
deba@1912
   560
deba@1912
   561
    ///\brief Next node to be processed.
deba@1912
   562
    
deba@1912
   563
    ///Next node to be processed.
deba@1912
   564
    ///
deba@1912
   565
    ///\return The next node to be processed or INVALID if the priority heap
deba@1912
   566
    /// is empty.
deba@1912
   567
    Node nextNode(){ 
deba@1912
   568
      return _heap->empty()?_heap->top():INVALID;
deba@1912
   569
    }
deba@1912
   570
 
deba@1912
   571
    ///\brief Returns \c false if there are nodes to be processed in the priority heap
deba@1912
   572
    ///
deba@1912
   573
    ///Returns \c false if there are nodes
deba@1912
   574
    ///to be processed in the priority heap
deba@1912
   575
    bool emptyQueue() { return _heap->empty(); }
deba@1912
   576
    ///\brief Returns the number of the nodes to be processed in the priority heap
deba@1912
   577
deba@1912
   578
    ///Returns the number of the nodes to be processed in the priority heap
deba@1912
   579
    ///
deba@1912
   580
    int queueSize() { return _heap->size(); }
deba@1912
   581
    
deba@1912
   582
    ///\brief Executes the algorithm.
deba@1912
   583
deba@1912
   584
    ///Executes the algorithm.
deba@1912
   585
    ///
deba@1912
   586
    ///\pre init() must be called and at least one node should be added
deba@1912
   587
    ///with addSource() before using this function.
deba@1912
   588
    ///
deba@1912
   589
    ///This method runs the %Prim algorithm from the node(s)
deba@1912
   590
    ///in order to compute the
deba@1912
   591
    ///minimum spanning tree.
deba@1912
   592
    ///
deba@1912
   593
    void start(){
deba@1912
   594
      while ( !_heap->empty() ) processNextNode();
deba@1912
   595
    }
deba@1912
   596
    
deba@1912
   597
    ///\brief Executes the algorithm until a condition is met.
deba@1912
   598
deba@1912
   599
    ///Executes the algorithm until a condition is met.
deba@1912
   600
    ///
deba@1912
   601
    ///\pre init() must be called and at least one node should be added
deba@1912
   602
    ///with addSource() before using this function.
deba@1912
   603
    ///
deba@1912
   604
    ///\param nm must be a bool (or convertible) node map. The algorithm
deba@1912
   605
    ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
deba@1912
   606
    template<class NodeBoolMap>
deba@1912
   607
    void start(const NodeBoolMap &nm){
deba@1912
   608
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
deba@1912
   609
      if ( !_heap->empty() ) _processed->set(_heap->top(),true);
deba@1912
   610
    }
deba@1912
   611
    
deba@1912
   612
    ///\brief Runs %Prim algorithm.
deba@1912
   613
    
deba@1912
   614
    ///This method runs the %Prim algorithm
deba@1912
   615
    ///in order to compute the
deba@1912
   616
    ///minimum spanning tree (or minimum spanning forest).
deba@1912
   617
    ///The method also works on graphs that has more than one components.
deba@1912
   618
    ///In this case it computes the minimum spanning forest.
deba@1912
   619
    void run() {
deba@1912
   620
      init();
deba@1912
   621
      for(NodeIt it(*graph);it!=INVALID;++it){
deba@1912
   622
	if(!processed(it)){
deba@1912
   623
	  addSource(it);
deba@1912
   624
	  start();
deba@1912
   625
	}
deba@1912
   626
      }
deba@1912
   627
    }
deba@1912
   628
deba@1912
   629
    ///\brief Runs %Prim algorithm from node \c s.
deba@1912
   630
    
deba@1912
   631
    ///This method runs the %Prim algorithm from node \c s
deba@1912
   632
    ///in order to
deba@1912
   633
    ///compute the
deba@1912
   634
    ///minimun spanning tree
deba@1912
   635
    ///
deba@1912
   636
    ///\note d.run(s) is just a shortcut of the following code.
deba@1912
   637
    ///\code
deba@1912
   638
    ///  d.init();
deba@1912
   639
    ///  d.addSource(s);
deba@1912
   640
    ///  d.start();
deba@1912
   641
    ///\endcode
deba@1912
   642
    ///\note If the graph has more than one components, the method
deba@1912
   643
    ///will compute the minimun spanning tree for only one component.
deba@1912
   644
    ///
deba@1912
   645
    ///See \ref run() if you want to compute the minimal spanning forest.
deba@1912
   646
    void run(Node s){
deba@1912
   647
      init();
deba@1912
   648
      addSource(s);
deba@1912
   649
      start();
deba@1912
   650
    }
deba@1912
   651
    
deba@1912
   652
    ///@}
deba@1912
   653
deba@1912
   654
    ///\name Query Functions
deba@1912
   655
    ///The result of the %Prim algorithm can be obtained using these
deba@1912
   656
    ///functions.\n
deba@1912
   657
    ///Before the use of these functions,
deba@1912
   658
    ///either run() or start() must be called.
deba@1912
   659
    
deba@1912
   660
    ///@{
deba@1912
   661
deba@1912
   662
    ///\brief Returns the 'previous edge' of the minimum spanning tree.
deba@1912
   663
deba@1912
   664
    ///For a node \c v it returns the 'previous edge' of the minimum spanning tree,
deba@1912
   665
    ///i.e. it returns the edge from where \c v was reached. For a source node
deba@1912
   666
    ///or an unreachable node it is \ref INVALID.
deba@1912
   667
    ///The minimum spanning tree used here is equal to the minimum spanning tree used
deba@1912
   668
    ///in \ref predNode().  \pre \ref run() or \ref start() must be called before
deba@1912
   669
    ///using this function.
deba@1912
   670
    UEdge predEdge(Node v) const { return (*_pred)[v]; }
deba@1912
   671
deba@1912
   672
    ///\brief Returns the 'previous node' of the minimum spanning tree.
deba@1912
   673
deba@1912
   674
    ///For a node \c v it returns the 'previous node' of the minimum spanning tree,
deba@1912
   675
    ///i.e. it returns the node from where \c v was reached. For a source node
deba@1912
   676
    ///or an unreachable node it is \ref INVALID.
deba@1912
   677
    //The minimum spanning tree used here is equal to the minimum spanning
deba@1912
   678
    ///tree used in \ref predEdge().  \pre \ref run() or \ref start() must be called
deba@1912
   679
    ///before using this function.
deba@1912
   680
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
deba@1912
   681
				  graph->source((*_pred)[v]); }
deba@1912
   682
    
deba@1912
   683
    ///\brief Returns a reference to the NodeMap of the edges of the minimum spanning tree.
deba@1912
   684
deba@1912
   685
    ///Returns a reference to the NodeMap of the edges of the
deba@1912
   686
    ///minimum spanning tree.
deba@1912
   687
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   688
    const PredMap &predMap() const { return *_pred;}
deba@1912
   689
 
deba@1912
   690
    ///\brief Returns a reference to the tree edges map.
deba@1912
   691
deba@1912
   692
    ///Returns a reference to the TreeEdgeMap of the edges of the
deba@1912
   693
    ///minimum spanning tree. The value of the map is \c true only if the edge is in
deba@1912
   694
    ///the minimum spanning tree.
deba@1912
   695
    ///\warning By default, the TreeEdgeMap is a NullMap.
deba@1912
   696
    ///
deba@1912
   697
    ///If it is not set before the execution of the algorithm, use the \ref
deba@1912
   698
    ///treeMap(TreeMap&) function (after the execution) to set an UEdgeMap with the
deba@1912
   699
    ///edges of the minimum spanning tree in O(n) time where n is the number of
deba@1912
   700
    ///nodes in the graph.
deba@1912
   701
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   702
    const TreeMap &treeMap() const { return *_tree;}
deba@1912
   703
 
deba@1912
   704
    ///\brief Sets the tree edges map.
deba@1912
   705
deba@1912
   706
    ///Sets the TreeMap of the edges of the minimum spanning tree.
deba@1912
   707
    ///The map values belonging to the edges of the minimum
alpar@1953
   708
    ///spanning tree are set to \c tree_edge_value or \c true by default,
deba@1912
   709
    ///the other map values remain untouched.
deba@1912
   710
    ///
deba@1912
   711
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   712
deba@1912
   713
    template<class TreeMap>
deba@1912
   714
    void quickTreeEdges(
deba@1912
   715
        TreeMap& tree,
deba@1912
   716
        const typename TreeMap::Value& tree_edge_value=true) const {
deba@1912
   717
      for(NodeIt i(*graph);i!=INVALID;++i){
deba@1912
   718
        if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value);
deba@1912
   719
      }
deba@1912
   720
    }
deba@1912
   721
deba@1912
   722
    ///\brief Sets the tree edges map.
deba@1912
   723
deba@1912
   724
    ///Sets the TreeMap of the edges of the minimum spanning tree.
deba@1912
   725
    ///The map values belonging to the edges of the minimum
alpar@1953
   726
    ///spanning tree are set to \c tree_edge_value or \c true by default while
deba@1912
   727
    ///the edge values not belonging to the minimum spanning tree are set to
alpar@1953
   728
    ///\c tree_default_value or \c false by default.
deba@1912
   729
    ///
deba@1912
   730
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   731
deba@1912
   732
    template<class TreeMap>
deba@1912
   733
    void treeEdges(
deba@1912
   734
        TreeMap& tree,
deba@1912
   735
        const typename TreeMap::Value& tree_edge_value=true,
deba@1912
   736
        const typename TreeMap::Value& tree_default_value=false) const {
deba@1912
   737
      for(typename ItemSetTraits<UGraph,UEdge>::ItemIt i(*graph);i!=INVALID;++i)
deba@1912
   738
	tree.set(i,tree_default_value);
deba@1912
   739
      for(NodeIt i(*graph);i!=INVALID;++i){
deba@1912
   740
        if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value);
deba@1912
   741
      }
deba@1912
   742
    }
deba@1912
   743
deba@1912
   744
    ///\brief Checks if a node is reachable from the starting node.
deba@1912
   745
deba@1912
   746
    ///Returns \c true if \c v is reachable from the starting node.
deba@1912
   747
    ///\warning The source nodes are inditated as unreached.
deba@1912
   748
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   749
    ///
deba@1912
   750
    bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
deba@1912
   751
deba@1912
   752
    ///\brief Checks if a node is processed.
deba@1912
   753
deba@1912
   754
    ///Returns \c true if \c v is processed, i.e. \c v is already connencted to the
deba@1912
   755
    ///minimum spanning tree.
deba@1912
   756
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   757
    ///
deba@1912
   758
    bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
deba@1912
   759
    
deba@1912
   760
deba@1912
   761
    ///\brief Checks if an edge is in the spanning tree or not.
deba@1912
   762
deba@1912
   763
    ///Checks if an edge is in the spanning tree or not.
deba@1912
   764
    ///\param e is the edge that will be checked
deba@1912
   765
    ///\return \c true if e is in the spanning tree, \c false otherwise
deba@1912
   766
    bool tree(UEdge e){
deba@1912
   767
      return (*_pred)[*graph.source(e)]==e || (*_pred)[*graph.target(e)]==e;
deba@1912
   768
    }
deba@1912
   769
    ///@}
deba@1912
   770
  };
deba@1912
   771
deba@1912
   772
deba@1912
   773
  /// \ingroup spantree
deba@1912
   774
  ///
deba@1912
   775
  /// \brief Function type interface for Prim algorithm.
deba@1912
   776
  ///
deba@1912
   777
  /// Function type interface for Prim algorithm.
deba@1912
   778
  /// \param graph the UGraph that the algorithm runs on
deba@1912
   779
  /// \param cost the CostMap of the edges
deba@1912
   780
  /// \retval tree the EdgeMap that contains whether an edge is in 
deba@1912
   781
  /// the spanning tree or not
deba@1912
   782
  ///
deba@1912
   783
  ///\sa Prim
deba@1912
   784
  template<class Graph,class CostMap,class TreeMap>
deba@1912
   785
  void prim(const Graph& graph, const CostMap& cost,TreeMap& tree){
deba@1912
   786
    typename Prim<Graph,CostMap>::template DefTreeMap<TreeMap>::
deba@1912
   787
      Create prm(graph,cost);
deba@1912
   788
    prm.treeMap(tree);
deba@1912
   789
    prm.run();
deba@1979
   790
  }
deba@1912
   791
deba@1912
   792
} //END OF NAMESPACE LEMON
deba@1912
   793
deba@1912
   794
#endif