lemon/prim.h
author jacint
Thu, 30 Mar 2006 15:34:56 +0000
changeset 2024 4ab8a25def3c
parent 1979 c2992fd74dad
child 2030 d769d2eb4d50
permissions -rw-r--r--
tolerance class incorporated
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@1912
    41
  ///\param LM Type of cost map.
deba@1912
    42
  template<class GR, class LM>
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@1912
    50
    typedef LM CostMap;
deba@1912
    51
    //The type of the cost of the edges.
deba@1912
    52
    typedef typename LM::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@1912
    73
    typedef BinHeap<typename UGraph::Node, typename LM::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@1912
   139
  ///The running time is O(e*log n) 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@1912
   155
  ///\param LM 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@1912
   160
  ///of LM 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@1912
   166
  ///"PrimDefaultTraits<GR,LM>".  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@1912
   174
	    typename LM,
deba@1912
   175
	    typename TR>
deba@1912
   176
#else
deba@1912
   177
  template <typename GR=ListUGraph,
deba@1912
   178
	    typename LM=typename GR::template UEdgeMap<int>,
deba@1912
   179
	    typename TR=PrimDefaultTraits<GR,LM> >
deba@1912
   180
#endif
deba@1912
   181
  class Prim {
deba@1912
   182
  public:
deba@1912
   183
    /**
deba@1912
   184
     * \brief \ref Exception for uninitialized parameters.
deba@1912
   185
     *
deba@1912
   186
     * This error represents problems in the initialization
deba@1912
   187
     * of the parameters of the algorithms.
deba@1912
   188
     */
deba@1912
   189
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@1912
   190
    public:
deba@1912
   191
      virtual const char* exceptionName() const {
deba@1912
   192
	return "lemon::Prim::UninitializedParameter";
deba@1912
   193
      }
deba@1912
   194
    };
deba@1912
   195
deba@1912
   196
    typedef TR Traits;
deba@1912
   197
    ///The type of the underlying graph.
deba@1912
   198
    typedef typename TR::UGraph UGraph;
deba@1912
   199
    ///\e
deba@1912
   200
    typedef typename UGraph::Node Node;
deba@1912
   201
    ///\e
deba@1912
   202
    typedef typename UGraph::NodeIt NodeIt;
deba@1912
   203
    ///\e
deba@1912
   204
    typedef typename UGraph::UEdge UEdge;
deba@1912
   205
    ///\e
deba@1912
   206
    typedef typename UGraph::IncEdgeIt IncEdgeIt;
deba@1912
   207
    
deba@1912
   208
    ///The type of the cost of the edges.
deba@1912
   209
    typedef typename TR::CostMap::Value Value;
deba@1912
   210
    ///The type of the map that stores the edge costs.
deba@1912
   211
    typedef typename TR::CostMap CostMap;
deba@1912
   212
    ///\brief The type of the map that stores the last
deba@1912
   213
    ///predecessor edges of the spanning tree.
deba@1912
   214
    typedef typename TR::PredMap PredMap;
deba@1912
   215
    ///Edges of the spanning tree.
deba@1912
   216
    typedef typename TR::TreeMap TreeMap;
deba@1912
   217
    ///The type of the map indicating if a node is processed.
deba@1912
   218
    typedef typename TR::ProcessedMap ProcessedMap;
deba@1912
   219
    ///The cross reference type used for the current heap.
deba@1912
   220
    typedef typename TR::HeapCrossRef HeapCrossRef;
deba@1912
   221
    ///The heap type used by the prim algorithm.
deba@1912
   222
    typedef typename TR::Heap Heap;
deba@1912
   223
  private:
deba@1912
   224
    /// Pointer to the underlying graph.
deba@1912
   225
    const UGraph *graph;
deba@1912
   226
    /// Pointer to the cost map
deba@1912
   227
    const CostMap *cost;
deba@1912
   228
    ///Pointer to the map of predecessors edges.
deba@1912
   229
    PredMap *_pred;
deba@1912
   230
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
deba@1912
   231
    bool local_pred;
deba@1912
   232
    ///Pointer to the map of tree edges.
deba@1912
   233
    TreeMap *_tree;
deba@1912
   234
    ///Indicates if \ref _tree is locally allocated (\c true) or not.
deba@1912
   235
    bool local_tree;
deba@1912
   236
    ///Pointer to the map of processed status of the nodes.
deba@1912
   237
    ProcessedMap *_processed;
deba@1912
   238
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
deba@1912
   239
    bool local_processed;
deba@1912
   240
    ///Pointer to the heap cross references.
deba@1912
   241
    HeapCrossRef *_heap_cross_ref;
deba@1912
   242
    ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
deba@1912
   243
    bool local_heap_cross_ref;
deba@1912
   244
    ///Pointer to the heap.
deba@1912
   245
    Heap *_heap;
deba@1912
   246
    ///Indicates if \ref _heap is locally allocated (\c true) or not.
deba@1912
   247
    bool local_heap;
deba@1912
   248
deba@1912
   249
    ///Creates the maps if necessary.
deba@1912
   250
    void create_maps(){
deba@1912
   251
      if(!_pred) {
deba@1912
   252
	local_pred = true;
deba@1912
   253
	_pred = Traits::createPredMap(*graph);
deba@1912
   254
      }
deba@1912
   255
      if(!_tree) {
deba@1912
   256
	local_tree = true;
deba@1912
   257
	_tree = Traits::createTreeMap(*graph);
deba@1912
   258
      }
deba@1912
   259
      if(!_processed) {
deba@1912
   260
	local_processed = true;
deba@1912
   261
	_processed = Traits::createProcessedMap(*graph);
deba@1912
   262
      }
deba@1912
   263
      if (!_heap_cross_ref) {
deba@1912
   264
	local_heap_cross_ref = true;
deba@1912
   265
	_heap_cross_ref = Traits::createHeapCrossRef(*graph);
deba@1912
   266
      }
deba@1912
   267
      if (!_heap) {
deba@1912
   268
	local_heap = true;
deba@1912
   269
	_heap = Traits::createHeap(*_heap_cross_ref);
deba@1912
   270
      }
deba@1912
   271
    }
deba@1912
   272
    
deba@1912
   273
  public :
deba@1912
   274
deba@1912
   275
    typedef Prim Create;
deba@1912
   276
 
deba@1912
   277
    ///\name Named template parameters
deba@1912
   278
deba@1912
   279
    ///@{
deba@1912
   280
deba@1912
   281
    template <class T>
deba@1912
   282
    struct DefPredMapTraits : public Traits {
deba@1912
   283
      typedef T PredMap;
deba@1912
   284
      static PredMap *createPredMap(const UGraph &_graph){
deba@1912
   285
	throw UninitializedParameter();
deba@1912
   286
      }
deba@1912
   287
    };
deba@1912
   288
    ///\ref named-templ-param "Named parameter" for setting PredMap type
deba@1912
   289
deba@1912
   290
    ///\ref named-templ-param "Named parameter" for setting PredMap type
deba@1912
   291
    ///
deba@1912
   292
    template <class T>
deba@1912
   293
    struct DefPredMap 
deba@1912
   294
      : public Prim< UGraph, CostMap, DefPredMapTraits<T> > {
deba@1912
   295
      typedef Prim< UGraph, CostMap, DefPredMapTraits<T> > Create;
deba@1912
   296
    };
deba@1912
   297
    
deba@1912
   298
    template <class T>
deba@1912
   299
    struct DefProcessedMapTraits : public Traits {
deba@1912
   300
      typedef T ProcessedMap;
deba@1912
   301
      static ProcessedMap *createProcessedMap(const UGraph &_graph){
deba@1912
   302
	throw UninitializedParameter();
deba@1912
   303
      }
deba@1912
   304
    };
deba@1912
   305
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
deba@1912
   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@1912
   334
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1912
   335
    ///reference type
deba@1912
   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@1912
   357
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1912
   358
    ///reference type with automatic allocation
deba@1912
   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@1912
   378
    ///\ref named-templ-param "Named parameter" for setting TreeMap
deba@1912
   379
deba@1912
   380
    ///\ref named-templ-param "Named parameter" for setting TreeMap
deba@1912
   381
    ///
deba@1912
   382
    template <class TM>
deba@1912
   383
    struct DefTreeMap
deba@1912
   384
      : public Prim< UGraph, CostMap, DefTreeMapTraits<TM> > {
deba@1912
   385
      typedef Prim< UGraph, CostMap, DefTreeMapTraits<TM> > Create;
deba@1912
   386
    };    
deba@1912
   387
deba@1912
   388
    struct DefGraphTreeMapTraits : public Traits {
deba@1912
   389
      typedef typename UGraph::template NodeMap<bool> TreeMap;
deba@1912
   390
      static TreeMap *createTreeMap(const UGraph &_graph){
deba@1912
   391
	return new TreeMap(_graph);
deba@1912
   392
      }
deba@1912
   393
    };
deba@1912
   394
deba@1912
   395
    ///@}
deba@1912
   396
deba@1912
   397
deba@1912
   398
  protected:
deba@1912
   399
deba@1912
   400
    Prim() {}
deba@1912
   401
deba@1912
   402
  public:      
deba@1912
   403
    
deba@1912
   404
    ///Constructor.
deba@1912
   405
    
deba@1912
   406
    ///\param _graph the graph the algorithm will run on.
deba@1912
   407
    ///\param _cost the cost map used by the algorithm.
deba@1912
   408
    Prim(const UGraph& _graph, const CostMap& _cost) :
deba@1912
   409
      graph(&_graph), cost(&_cost),
deba@1912
   410
      _pred(NULL), local_pred(false),
deba@1912
   411
      _tree(NULL), local_tree(false),
deba@1912
   412
      _processed(NULL), local_processed(false),
deba@1912
   413
      _heap_cross_ref(NULL), local_heap_cross_ref(false),
deba@1912
   414
      _heap(NULL), local_heap(false)
deba@1912
   415
    {
deba@1912
   416
      checkConcept<concept::UGraph, UGraph>();
deba@1912
   417
    }
deba@1912
   418
    
deba@1912
   419
    ///Destructor.
deba@1912
   420
    ~Prim(){
deba@1912
   421
      if(local_pred) delete _pred;
deba@1912
   422
      if(local_tree) delete _tree;
deba@1912
   423
      if(local_processed) delete _processed;
deba@1912
   424
      if(local_heap_cross_ref) delete _heap_cross_ref;
deba@1912
   425
      if(local_heap) delete _heap;
deba@1912
   426
    }
deba@1912
   427
deba@1912
   428
    ///\brief Sets the cost map.
deba@1912
   429
deba@1912
   430
    ///Sets the cost map.
deba@1912
   431
    ///\return <tt> (*this) </tt>
deba@1912
   432
    Prim &costMap(const CostMap &m){
deba@1912
   433
      cost = &m;
deba@1912
   434
      return *this;
deba@1912
   435
    }
deba@1912
   436
deba@1912
   437
    ///\brief Sets the map storing the predecessor edges.
deba@1912
   438
deba@1912
   439
    ///Sets the map storing the predecessor edges.
deba@1912
   440
    ///If you don't use this function before calling \ref run(),
deba@1912
   441
    ///it will allocate one. The destuctor deallocates this
deba@1912
   442
    ///automatically allocated map, of course.
deba@1912
   443
    ///\return <tt> (*this) </tt>
deba@1912
   444
    Prim &predMap(PredMap &m){
deba@1912
   445
      if(local_pred) {
deba@1912
   446
	delete _pred;
deba@1912
   447
	local_pred=false;
deba@1912
   448
      }
deba@1912
   449
      _pred = &m;
deba@1912
   450
      return *this;
deba@1912
   451
    }
deba@1912
   452
deba@1912
   453
    ///\brief Sets the map storing the tree edges.
deba@1912
   454
deba@1912
   455
    ///Sets the map storing the tree edges.
deba@1912
   456
    ///If you don't use this function before calling \ref run(),
deba@1912
   457
    ///it will allocate one. The destuctor deallocates this
deba@1912
   458
    ///automatically allocated map, of course.
deba@1912
   459
    ///By default this is a NullMap.
deba@1912
   460
    ///\return <tt> (*this) </tt>
deba@1912
   461
    Prim &treeMap(TreeMap &m){
deba@1912
   462
      if(local_tree) {
deba@1912
   463
	delete _tree;
deba@1912
   464
	local_tree=false;
deba@1912
   465
      }
deba@1912
   466
      _tree = &m;
deba@1912
   467
      return *this;
deba@1912
   468
    }
deba@1912
   469
deba@1912
   470
    ///\brief Sets the heap and the cross reference used by algorithm.
deba@1912
   471
deba@1912
   472
    ///Sets the heap and the cross reference used by algorithm.
deba@1912
   473
    ///If you don't use this function before calling \ref run(),
deba@1912
   474
    ///it will allocate one. The destuctor deallocates this
deba@1912
   475
    ///automatically allocated map, of course.
deba@1912
   476
    ///\return <tt> (*this) </tt>
deba@1912
   477
    Prim &heap(Heap& heap, HeapCrossRef &crossRef){
deba@1912
   478
      if(local_heap_cross_ref) {
deba@1912
   479
	delete _heap_cross_ref;
deba@1912
   480
	local_heap_cross_ref=false;
deba@1912
   481
      }
deba@1912
   482
      _heap_cross_ref = &crossRef;
deba@1912
   483
      if(local_heap) {
deba@1912
   484
	delete _heap;
deba@1912
   485
	local_heap=false;
deba@1912
   486
      }
deba@1912
   487
      _heap = &heap;
deba@1912
   488
      return *this;
deba@1912
   489
    }
deba@1912
   490
deba@1912
   491
  public:
deba@1912
   492
    ///\name Execution control
deba@1912
   493
    ///The simplest way to execute the algorithm is to use
deba@1912
   494
    ///one of the member functions called \c run(...).
deba@1912
   495
    ///\n
deba@1912
   496
    ///If you need more control on the execution,
deba@1912
   497
    ///first you must call \ref init(), then you can add several source nodes
deba@1912
   498
    ///with \ref addSource().
deba@1912
   499
    ///Finally \ref start() will perform the actual path
deba@1912
   500
    ///computation.
deba@1912
   501
deba@1912
   502
    ///@{
deba@1912
   503
deba@1912
   504
    ///\brief Initializes the internal data structures.
deba@1912
   505
deba@1912
   506
    ///Initializes the internal data structures.
deba@1912
   507
    ///
deba@1912
   508
    void init(){
deba@1912
   509
      create_maps();
deba@1912
   510
      _heap->clear();
deba@1912
   511
      for ( NodeIt u(*graph) ; u!=INVALID ; ++u ) {
deba@1912
   512
	_pred->set(u,INVALID);
deba@1912
   513
	_processed->set(u,false);
deba@1912
   514
	_heap_cross_ref->set(u,Heap::PRE_HEAP);
deba@1912
   515
      }
deba@1912
   516
    }
deba@1912
   517
    
deba@1912
   518
    ///\brief Adds a new source node.
deba@1912
   519
deba@1912
   520
    ///Adds a new source node to the priority heap.
deba@1912
   521
    ///
deba@1912
   522
    ///It checks if the node has already been added to the heap and
deba@1912
   523
    ///it is pushed to the heap only if it was not in the heap.
deba@1912
   524
    void addSource(Node s){
deba@1912
   525
      if(_heap->state(s) != Heap::IN_HEAP) {
deba@1912
   526
	_heap->push(s,Value());
deba@1912
   527
      }
deba@1912
   528
    }
deba@1912
   529
    ///\brief Processes the next node in the priority heap
deba@1912
   530
deba@1912
   531
    ///Processes the next node in the priority heap.
deba@1912
   532
    ///
deba@1912
   533
    ///\return The processed node.
deba@1912
   534
    ///
deba@1912
   535
    ///\warning The priority heap must not be empty!
deba@1912
   536
    Node processNextNode(){
deba@1912
   537
      Node v=_heap->top(); 
deba@1912
   538
      _heap->pop();
deba@1912
   539
      _processed->set(v,true);
deba@1912
   540
      
deba@1912
   541
      for(IncEdgeIt e(*graph,v); e!=INVALID; ++e) {
deba@1912
   542
	Node w=graph->oppositeNode(v,e);
deba@1912
   543
	switch(_heap->state(w)) {
deba@1912
   544
	case Heap::PRE_HEAP:
deba@1912
   545
	  _heap->push(w,(*cost)[e]);
deba@1912
   546
	  _pred->set(w,e);
deba@1912
   547
	  break;
deba@1912
   548
	case Heap::IN_HEAP:
deba@1912
   549
	  if ( (*cost)[e] < (*_heap)[w] ) {
deba@1912
   550
	    _heap->decrease(w,(*cost)[e]); 
deba@1912
   551
	    _pred->set(w,e);
deba@1912
   552
	  }
deba@1912
   553
	  break;
deba@1912
   554
	case Heap::POST_HEAP:
deba@1912
   555
	  break;
deba@1912
   556
	}
deba@1912
   557
      }
deba@1912
   558
      if ((*_pred)[v]!=INVALID)_tree->set((*_pred)[v],true);
deba@1912
   559
      return v;
deba@1912
   560
    }
deba@1912
   561
deba@1912
   562
    ///\brief Next node to be processed.
deba@1912
   563
    
deba@1912
   564
    ///Next node to be processed.
deba@1912
   565
    ///
deba@1912
   566
    ///\return The next node to be processed or INVALID if the priority heap
deba@1912
   567
    /// is empty.
deba@1912
   568
    Node nextNode(){ 
deba@1912
   569
      return _heap->empty()?_heap->top():INVALID;
deba@1912
   570
    }
deba@1912
   571
 
deba@1912
   572
    ///\brief Returns \c false if there are nodes to be processed in the priority heap
deba@1912
   573
    ///
deba@1912
   574
    ///Returns \c false if there are nodes
deba@1912
   575
    ///to be processed in the priority heap
deba@1912
   576
    bool emptyQueue() { return _heap->empty(); }
deba@1912
   577
    ///\brief Returns the number of the nodes to be processed in the priority heap
deba@1912
   578
deba@1912
   579
    ///Returns the number of the nodes to be processed in the priority heap
deba@1912
   580
    ///
deba@1912
   581
    int queueSize() { return _heap->size(); }
deba@1912
   582
    
deba@1912
   583
    ///\brief Executes the algorithm.
deba@1912
   584
deba@1912
   585
    ///Executes the algorithm.
deba@1912
   586
    ///
deba@1912
   587
    ///\pre init() must be called and at least one node should be added
deba@1912
   588
    ///with addSource() before using this function.
deba@1912
   589
    ///
deba@1912
   590
    ///This method runs the %Prim algorithm from the node(s)
deba@1912
   591
    ///in order to compute the
deba@1912
   592
    ///minimum spanning tree.
deba@1912
   593
    ///
deba@1912
   594
    void start(){
deba@1912
   595
      while ( !_heap->empty() ) processNextNode();
deba@1912
   596
    }
deba@1912
   597
    
deba@1912
   598
    ///\brief Executes the algorithm until a condition is met.
deba@1912
   599
deba@1912
   600
    ///Executes the algorithm until a condition is met.
deba@1912
   601
    ///
deba@1912
   602
    ///\pre init() must be called and at least one node should be added
deba@1912
   603
    ///with addSource() before using this function.
deba@1912
   604
    ///
deba@1912
   605
    ///\param nm must be a bool (or convertible) node map. The algorithm
deba@1912
   606
    ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
deba@1912
   607
    template<class NodeBoolMap>
deba@1912
   608
    void start(const NodeBoolMap &nm){
deba@1912
   609
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
deba@1912
   610
      if ( !_heap->empty() ) _processed->set(_heap->top(),true);
deba@1912
   611
    }
deba@1912
   612
    
deba@1912
   613
    ///\brief Runs %Prim algorithm.
deba@1912
   614
    
deba@1912
   615
    ///This method runs the %Prim algorithm
deba@1912
   616
    ///in order to compute the
deba@1912
   617
    ///minimum spanning tree (or minimum spanning forest).
deba@1912
   618
    ///The method also works on graphs that has more than one components.
deba@1912
   619
    ///In this case it computes the minimum spanning forest.
deba@1912
   620
    void run() {
deba@1912
   621
      init();
deba@1912
   622
      for(NodeIt it(*graph);it!=INVALID;++it){
deba@1912
   623
	if(!processed(it)){
deba@1912
   624
	  addSource(it);
deba@1912
   625
	  start();
deba@1912
   626
	}
deba@1912
   627
      }
deba@1912
   628
    }
deba@1912
   629
deba@1912
   630
    ///\brief Runs %Prim algorithm from node \c s.
deba@1912
   631
    
deba@1912
   632
    ///This method runs the %Prim algorithm from node \c s
deba@1912
   633
    ///in order to
deba@1912
   634
    ///compute the
deba@1912
   635
    ///minimun spanning tree
deba@1912
   636
    ///
deba@1912
   637
    ///\note d.run(s) is just a shortcut of the following code.
deba@1912
   638
    ///\code
deba@1912
   639
    ///  d.init();
deba@1912
   640
    ///  d.addSource(s);
deba@1912
   641
    ///  d.start();
deba@1912
   642
    ///\endcode
deba@1912
   643
    ///\note If the graph has more than one components, the method
deba@1912
   644
    ///will compute the minimun spanning tree for only one component.
deba@1912
   645
    ///
deba@1912
   646
    ///See \ref run() if you want to compute the minimal spanning forest.
deba@1912
   647
    void run(Node s){
deba@1912
   648
      init();
deba@1912
   649
      addSource(s);
deba@1912
   650
      start();
deba@1912
   651
    }
deba@1912
   652
    
deba@1912
   653
    ///@}
deba@1912
   654
deba@1912
   655
    ///\name Query Functions
deba@1912
   656
    ///The result of the %Prim algorithm can be obtained using these
deba@1912
   657
    ///functions.\n
deba@1912
   658
    ///Before the use of these functions,
deba@1912
   659
    ///either run() or start() must be called.
deba@1912
   660
    
deba@1912
   661
    ///@{
deba@1912
   662
deba@1912
   663
    ///\brief Returns the 'previous edge' of the minimum spanning tree.
deba@1912
   664
deba@1912
   665
    ///For a node \c v it returns the 'previous edge' of the minimum spanning tree,
deba@1912
   666
    ///i.e. it returns the edge from where \c v was reached. For a source node
deba@1912
   667
    ///or an unreachable node it is \ref INVALID.
deba@1912
   668
    ///The minimum spanning tree used here is equal to the minimum spanning tree used
deba@1912
   669
    ///in \ref predNode().  \pre \ref run() or \ref start() must be called before
deba@1912
   670
    ///using this function.
deba@1912
   671
    UEdge predEdge(Node v) const { return (*_pred)[v]; }
deba@1912
   672
deba@1912
   673
    ///\brief Returns the 'previous node' of the minimum spanning tree.
deba@1912
   674
deba@1912
   675
    ///For a node \c v it returns the 'previous node' of the minimum spanning tree,
deba@1912
   676
    ///i.e. it returns the node from where \c v was reached. For a source node
deba@1912
   677
    ///or an unreachable node it is \ref INVALID.
deba@1912
   678
    //The minimum spanning tree used here is equal to the minimum spanning
deba@1912
   679
    ///tree used in \ref predEdge().  \pre \ref run() or \ref start() must be called
deba@1912
   680
    ///before using this function.
deba@1912
   681
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
deba@1912
   682
				  graph->source((*_pred)[v]); }
deba@1912
   683
    
deba@1912
   684
    ///\brief Returns a reference to the NodeMap of the edges of the minimum spanning tree.
deba@1912
   685
deba@1912
   686
    ///Returns a reference to the NodeMap of the edges of the
deba@1912
   687
    ///minimum spanning tree.
deba@1912
   688
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   689
    const PredMap &predMap() const { return *_pred;}
deba@1912
   690
 
deba@1912
   691
    ///\brief Returns a reference to the tree edges map.
deba@1912
   692
deba@1912
   693
    ///Returns a reference to the TreeEdgeMap of the edges of the
deba@1912
   694
    ///minimum spanning tree. The value of the map is \c true only if the edge is in
deba@1912
   695
    ///the minimum spanning tree.
deba@1912
   696
    ///\warning By default, the TreeEdgeMap is a NullMap.
deba@1912
   697
    ///
deba@1912
   698
    ///If it is not set before the execution of the algorithm, use the \ref
deba@1912
   699
    ///treeMap(TreeMap&) function (after the execution) to set an UEdgeMap with the
deba@1912
   700
    ///edges of the minimum spanning tree in O(n) time where n is the number of
deba@1912
   701
    ///nodes in the graph.
deba@1912
   702
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   703
    const TreeMap &treeMap() const { return *_tree;}
deba@1912
   704
 
deba@1912
   705
    ///\brief Sets the tree edges map.
deba@1912
   706
deba@1912
   707
    ///Sets the TreeMap of the edges of the minimum spanning tree.
deba@1912
   708
    ///The map values belonging to the edges of the minimum
alpar@1953
   709
    ///spanning tree are set to \c tree_edge_value or \c true by default,
deba@1912
   710
    ///the other map values remain untouched.
deba@1912
   711
    ///
deba@1912
   712
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   713
deba@1912
   714
    template<class TreeMap>
deba@1912
   715
    void quickTreeEdges(
deba@1912
   716
        TreeMap& tree,
deba@1912
   717
        const typename TreeMap::Value& tree_edge_value=true) const {
deba@1912
   718
      for(NodeIt i(*graph);i!=INVALID;++i){
deba@1912
   719
        if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value);
deba@1912
   720
      }
deba@1912
   721
    }
deba@1912
   722
deba@1912
   723
    ///\brief Sets the tree edges map.
deba@1912
   724
deba@1912
   725
    ///Sets the TreeMap of the edges of the minimum spanning tree.
deba@1912
   726
    ///The map values belonging to the edges of the minimum
alpar@1953
   727
    ///spanning tree are set to \c tree_edge_value or \c true by default while
deba@1912
   728
    ///the edge values not belonging to the minimum spanning tree are set to
alpar@1953
   729
    ///\c tree_default_value or \c false by default.
deba@1912
   730
    ///
deba@1912
   731
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   732
deba@1912
   733
    template<class TreeMap>
deba@1912
   734
    void treeEdges(
deba@1912
   735
        TreeMap& tree,
deba@1912
   736
        const typename TreeMap::Value& tree_edge_value=true,
deba@1912
   737
        const typename TreeMap::Value& tree_default_value=false) const {
deba@1912
   738
      for(typename ItemSetTraits<UGraph,UEdge>::ItemIt i(*graph);i!=INVALID;++i)
deba@1912
   739
	tree.set(i,tree_default_value);
deba@1912
   740
      for(NodeIt i(*graph);i!=INVALID;++i){
deba@1912
   741
        if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value);
deba@1912
   742
      }
deba@1912
   743
    }
deba@1912
   744
deba@1912
   745
    ///\brief Checks if a node is reachable from the starting node.
deba@1912
   746
deba@1912
   747
    ///Returns \c true if \c v is reachable from the starting node.
deba@1912
   748
    ///\warning The source nodes are inditated as unreached.
deba@1912
   749
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   750
    ///
deba@1912
   751
    bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
deba@1912
   752
deba@1912
   753
    ///\brief Checks if a node is processed.
deba@1912
   754
deba@1912
   755
    ///Returns \c true if \c v is processed, i.e. \c v is already connencted to the
deba@1912
   756
    ///minimum spanning tree.
deba@1912
   757
    ///\pre \ref run() or \ref start() must be called before using this function.
deba@1912
   758
    ///
deba@1912
   759
    bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
deba@1912
   760
    
deba@1912
   761
deba@1912
   762
    ///\brief Checks if an edge is in the spanning tree or not.
deba@1912
   763
deba@1912
   764
    ///Checks if an edge is in the spanning tree or not.
deba@1912
   765
    ///\param e is the edge that will be checked
deba@1912
   766
    ///\return \c true if e is in the spanning tree, \c false otherwise
deba@1912
   767
    bool tree(UEdge e){
deba@1912
   768
      return (*_pred)[*graph.source(e)]==e || (*_pred)[*graph.target(e)]==e;
deba@1912
   769
    }
deba@1912
   770
    ///@}
deba@1912
   771
  };
deba@1912
   772
deba@1912
   773
deba@1912
   774
  /// \ingroup spantree
deba@1912
   775
  ///
deba@1912
   776
  /// \brief Function type interface for Prim algorithm.
deba@1912
   777
  ///
deba@1912
   778
  /// Function type interface for Prim algorithm.
deba@1912
   779
  /// \param graph the UGraph that the algorithm runs on
deba@1912
   780
  /// \param cost the CostMap of the edges
deba@1912
   781
  /// \retval tree the EdgeMap that contains whether an edge is in 
deba@1912
   782
  /// the spanning tree or not
deba@1912
   783
  ///
deba@1912
   784
  ///\sa Prim
deba@1912
   785
  template<class Graph,class CostMap,class TreeMap>
deba@1912
   786
  void prim(const Graph& graph, const CostMap& cost,TreeMap& tree){
deba@1912
   787
    typename Prim<Graph,CostMap>::template DefTreeMap<TreeMap>::
deba@1912
   788
      Create prm(graph,cost);
deba@1912
   789
    prm.treeMap(tree);
deba@1912
   790
    prm.run();
deba@1979
   791
  }
deba@1912
   792
deba@1912
   793
} //END OF NAMESPACE LEMON
deba@1912
   794
deba@1912
   795
#endif