lemon/prim.h
changeset 1912 d9205a711324
child 1953 d4f411003580
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/prim.h	Fri Jan 27 08:17:25 2006 +0000
     1.3 @@ -0,0 +1,792 @@
     1.4 +/* -*- C++ -*-
     1.5 + * lemon/prim.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef LEMON_PRIM_H
    1.21 +#define LEMON_PRIM_H
    1.22 +
    1.23 +///\ingroup spantree
    1.24 +///\file
    1.25 +///\brief Prim algorithm to compute minimum spanning tree.
    1.26 +
    1.27 +#include <lemon/list_graph.h>
    1.28 +#include <lemon/bin_heap.h>
    1.29 +#include <lemon/invalid.h>
    1.30 +#include <lemon/error.h>
    1.31 +#include <lemon/maps.h>
    1.32 +#include <lemon/traits.h>
    1.33 +
    1.34 +#include <lemon/concept/ugraph.h>
    1.35 +
    1.36 +namespace lemon {
    1.37 +
    1.38 +  ///Default traits class of Prim class.
    1.39 +
    1.40 +  ///Default traits class of Prim class.
    1.41 +  ///\param GR Graph type.
    1.42 +  ///\param LM Type of cost map.
    1.43 +  template<class GR, class LM>
    1.44 +  struct PrimDefaultTraits{
    1.45 +    ///The graph type the algorithm runs on. 
    1.46 +    typedef GR UGraph;
    1.47 +    ///The type of the map that stores the edge costs.
    1.48 +
    1.49 +    ///The type of the map that stores the edge costs.
    1.50 +    ///It must meet the \ref concept::ReadMap "ReadMap" concept.
    1.51 +    typedef LM CostMap;
    1.52 +    //The type of the cost of the edges.
    1.53 +    typedef typename LM::Value Value;
    1.54 +    /// The cross reference type used by heap.
    1.55 +
    1.56 +    /// The cross reference type used by heap.
    1.57 +    /// Usually it is \c UGraph::NodeMap<int>.
    1.58 +    typedef typename UGraph::template NodeMap<int> HeapCrossRef;
    1.59 +    ///Instantiates a HeapCrossRef.
    1.60 +
    1.61 +    ///This function instantiates a \ref HeapCrossRef. 
    1.62 +    /// \param G is the graph, to which we would like to define the 
    1.63 +    /// HeapCrossRef.
    1.64 +    static HeapCrossRef *createHeapCrossRef(const GR &_graph){
    1.65 +      return new HeapCrossRef(_graph);
    1.66 +    }
    1.67 +    
    1.68 +    ///The heap type used by Prim algorithm.
    1.69 +
    1.70 +    ///The heap type used by Prim algorithm.
    1.71 +    ///
    1.72 +    ///\sa BinHeap
    1.73 +    ///\sa Prim
    1.74 +    typedef BinHeap<typename UGraph::Node, typename LM::Value,
    1.75 +		    HeapCrossRef, std::less<Value> > Heap;
    1.76 +
    1.77 +    static Heap *createHeap(HeapCrossRef& _ref){
    1.78 +      return new Heap(_ref);
    1.79 +    }
    1.80 +
    1.81 +    ///\brief The type of the map that stores the last
    1.82 +    ///edges of the minimum spanning tree.
    1.83 +    /// 
    1.84 +    ///The type of the map that stores the last
    1.85 +    ///edges of the minimum spanning tree.
    1.86 +    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
    1.87 +    ///
    1.88 +    typedef typename UGraph::template NodeMap<typename GR::UEdge> PredMap;
    1.89 +    ///Instantiates a PredMap.
    1.90 + 
    1.91 +    ///This function instantiates a \ref PredMap. 
    1.92 +    ///\param G is the graph, to which we would like to define the PredMap.
    1.93 +    static PredMap *createPredMap(const GR &_graph){
    1.94 +      return new PredMap(_graph);
    1.95 +    }
    1.96 +
    1.97 +    ///The type of the map that stores whether an edge is in the
    1.98 +    ///spanning tree or not.
    1.99 +
   1.100 +    ///The type of the map that stores whether an edge is in the
   1.101 +    ///spanning tree or not.
   1.102 +    ///By default it is a NullMap.
   1.103 +    typedef NullMap<typename UGraph::UEdge,bool> TreeMap;
   1.104 +    ///Instantiates a TreeMap.
   1.105 +
   1.106 +    ///This function instantiates a \ref TreeMap.
   1.107 +    ///\param g is the graph, to which
   1.108 +    ///we would like to define the \ref TreeMap
   1.109 +    static TreeMap *createTreeMap(const GR &){
   1.110 +      return new TreeMap();
   1.111 +    }
   1.112 +
   1.113 +    ///The type of the map that stores whether a nodes is processed.
   1.114 + 
   1.115 +    ///The type of the map that stores whether a nodes is processed.
   1.116 +    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
   1.117 +    ///By default it is a NodeMap<bool>.
   1.118 +    typedef NullMap<typename UGraph::Node,bool> ProcessedMap;
   1.119 +    ///Instantiates a ProcessedMap.
   1.120 + 
   1.121 +    ///This function instantiates a \ref ProcessedMap. 
   1.122 +    ///\param g is the graph, to which
   1.123 +    ///we would like to define the \ref ProcessedMap
   1.124 +#ifdef DOXYGEN
   1.125 +    static ProcessedMap *createProcessedMap(const GR &_graph)
   1.126 +#else
   1.127 +    static ProcessedMap *createProcessedMap(const GR &)
   1.128 +#endif
   1.129 +    {
   1.130 +      return new ProcessedMap();
   1.131 +    }
   1.132 +  };
   1.133 +  
   1.134 +  ///%Prim algorithm class to find a minimum spanning tree.
   1.135 +  
   1.136 +  /// \ingroup spantree
   1.137 +  ///This class provides an efficient implementation of %Prim algorithm.
   1.138 +  ///
   1.139 +  ///The running time is O(e*log n) where e is the number of edges and
   1.140 +  ///n is the number of nodes in the graph.
   1.141 +  ///
   1.142 +  ///The edge costs are passed to the algorithm using a
   1.143 +  ///\ref concept::ReadMap "ReadMap",
   1.144 +  ///so it is easy to change it to any kind of cost.
   1.145 +  ///
   1.146 +  ///The type of the cost is determined by the
   1.147 +  ///\ref concept::ReadMap::Value "Value" of the cost map.
   1.148 +  ///
   1.149 +  ///It is also possible to change the underlying priority heap.
   1.150 +  ///
   1.151 +  ///\param GR The graph type the algorithm runs on. The default value
   1.152 +  ///is \ref ListUGraph. The value of GR is not used directly by
   1.153 +  ///Prim, it is only passed to \ref PrimDefaultTraits.
   1.154 +  ///
   1.155 +  ///\param LM This read-only UEdgeMap determines the costs of the
   1.156 +  ///edges. It is read once for each edge, so the map may involve in
   1.157 +  ///relatively time consuming process to compute the edge cost if
   1.158 +  ///it is necessary. The default map type is \ref
   1.159 +  ///concept::UGraph::UEdgeMap "UGraph::UEdgeMap<int>".  The value
   1.160 +  ///of LM is not used directly by Prim, it is only passed to \ref
   1.161 +  ///PrimDefaultTraits.
   1.162 +  ///
   1.163 +  ///\param TR Traits class to set
   1.164 +  ///various data types used by the algorithm.  The default traits
   1.165 +  ///class is \ref PrimDefaultTraits
   1.166 +  ///"PrimDefaultTraits<GR,LM>".  See \ref
   1.167 +  ///PrimDefaultTraits for the documentation of a Prim traits
   1.168 +  ///class.
   1.169 +  ///
   1.170 +  ///\author Balazs Attila Mihaly
   1.171 +
   1.172 +#ifdef DOXYGEN
   1.173 +  template <typename GR,
   1.174 +	    typename LM,
   1.175 +	    typename TR>
   1.176 +#else
   1.177 +  template <typename GR=ListUGraph,
   1.178 +	    typename LM=typename GR::template UEdgeMap<int>,
   1.179 +	    typename TR=PrimDefaultTraits<GR,LM> >
   1.180 +#endif
   1.181 +  class Prim {
   1.182 +  public:
   1.183 +    /**
   1.184 +     * \brief \ref Exception for uninitialized parameters.
   1.185 +     *
   1.186 +     * This error represents problems in the initialization
   1.187 +     * of the parameters of the algorithms.
   1.188 +     */
   1.189 +    class UninitializedParameter : public lemon::UninitializedParameter {
   1.190 +    public:
   1.191 +      virtual const char* exceptionName() const {
   1.192 +	return "lemon::Prim::UninitializedParameter";
   1.193 +      }
   1.194 +    };
   1.195 +
   1.196 +    typedef TR Traits;
   1.197 +    ///The type of the underlying graph.
   1.198 +    typedef typename TR::UGraph UGraph;
   1.199 +    ///\e
   1.200 +    typedef typename UGraph::Node Node;
   1.201 +    ///\e
   1.202 +    typedef typename UGraph::NodeIt NodeIt;
   1.203 +    ///\e
   1.204 +    typedef typename UGraph::UEdge UEdge;
   1.205 +    ///\e
   1.206 +    typedef typename UGraph::IncEdgeIt IncEdgeIt;
   1.207 +    
   1.208 +    ///The type of the cost of the edges.
   1.209 +    typedef typename TR::CostMap::Value Value;
   1.210 +    ///The type of the map that stores the edge costs.
   1.211 +    typedef typename TR::CostMap CostMap;
   1.212 +    ///\brief The type of the map that stores the last
   1.213 +    ///predecessor edges of the spanning tree.
   1.214 +    typedef typename TR::PredMap PredMap;
   1.215 +    ///Edges of the spanning tree.
   1.216 +    typedef typename TR::TreeMap TreeMap;
   1.217 +    ///The type of the map indicating if a node is processed.
   1.218 +    typedef typename TR::ProcessedMap ProcessedMap;
   1.219 +    ///The cross reference type used for the current heap.
   1.220 +    typedef typename TR::HeapCrossRef HeapCrossRef;
   1.221 +    ///The heap type used by the prim algorithm.
   1.222 +    typedef typename TR::Heap Heap;
   1.223 +  private:
   1.224 +    /// Pointer to the underlying graph.
   1.225 +    const UGraph *graph;
   1.226 +    /// Pointer to the cost map
   1.227 +    const CostMap *cost;
   1.228 +    ///Pointer to the map of predecessors edges.
   1.229 +    PredMap *_pred;
   1.230 +    ///Indicates if \ref _pred is locally allocated (\c true) or not.
   1.231 +    bool local_pred;
   1.232 +    ///Pointer to the map of tree edges.
   1.233 +    TreeMap *_tree;
   1.234 +    ///Indicates if \ref _tree is locally allocated (\c true) or not.
   1.235 +    bool local_tree;
   1.236 +    ///Pointer to the map of processed status of the nodes.
   1.237 +    ProcessedMap *_processed;
   1.238 +    ///Indicates if \ref _processed is locally allocated (\c true) or not.
   1.239 +    bool local_processed;
   1.240 +    ///Pointer to the heap cross references.
   1.241 +    HeapCrossRef *_heap_cross_ref;
   1.242 +    ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
   1.243 +    bool local_heap_cross_ref;
   1.244 +    ///Pointer to the heap.
   1.245 +    Heap *_heap;
   1.246 +    ///Indicates if \ref _heap is locally allocated (\c true) or not.
   1.247 +    bool local_heap;
   1.248 +
   1.249 +    ///Creates the maps if necessary.
   1.250 +    void create_maps(){
   1.251 +      if(!_pred) {
   1.252 +	local_pred = true;
   1.253 +	_pred = Traits::createPredMap(*graph);
   1.254 +      }
   1.255 +      if(!_tree) {
   1.256 +	local_tree = true;
   1.257 +	_tree = Traits::createTreeMap(*graph);
   1.258 +      }
   1.259 +      if(!_processed) {
   1.260 +	local_processed = true;
   1.261 +	_processed = Traits::createProcessedMap(*graph);
   1.262 +      }
   1.263 +      if (!_heap_cross_ref) {
   1.264 +	local_heap_cross_ref = true;
   1.265 +	_heap_cross_ref = Traits::createHeapCrossRef(*graph);
   1.266 +      }
   1.267 +      if (!_heap) {
   1.268 +	local_heap = true;
   1.269 +	_heap = Traits::createHeap(*_heap_cross_ref);
   1.270 +      }
   1.271 +    }
   1.272 +    
   1.273 +  public :
   1.274 +
   1.275 +    typedef Prim Create;
   1.276 + 
   1.277 +    ///\name Named template parameters
   1.278 +
   1.279 +    ///@{
   1.280 +
   1.281 +    template <class T>
   1.282 +    struct DefPredMapTraits : public Traits {
   1.283 +      typedef T PredMap;
   1.284 +      static PredMap *createPredMap(const UGraph &_graph){
   1.285 +	throw UninitializedParameter();
   1.286 +      }
   1.287 +    };
   1.288 +    ///\ref named-templ-param "Named parameter" for setting PredMap type
   1.289 +
   1.290 +    ///\ref named-templ-param "Named parameter" for setting PredMap type
   1.291 +    ///
   1.292 +    template <class T>
   1.293 +    struct DefPredMap 
   1.294 +      : public Prim< UGraph, CostMap, DefPredMapTraits<T> > {
   1.295 +      typedef Prim< UGraph, CostMap, DefPredMapTraits<T> > Create;
   1.296 +    };
   1.297 +    
   1.298 +    template <class T>
   1.299 +    struct DefProcessedMapTraits : public Traits {
   1.300 +      typedef T ProcessedMap;
   1.301 +      static ProcessedMap *createProcessedMap(const UGraph &_graph){
   1.302 +	throw UninitializedParameter();
   1.303 +      }
   1.304 +    };
   1.305 +    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   1.306 +
   1.307 +    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   1.308 +    ///
   1.309 +    template <class T>
   1.310 +    struct DefProcessedMap 
   1.311 +      : public Prim< UGraph, CostMap, DefProcessedMapTraits<T> > { 
   1.312 +      typedef Prim< UGraph, CostMap, DefProcessedMapTraits<T> > Create;
   1.313 +    };
   1.314 +    
   1.315 +    struct DefGraphProcessedMapTraits : public Traits {
   1.316 +      typedef typename UGraph::template NodeMap<bool> ProcessedMap;
   1.317 +      static ProcessedMap *createProcessedMap(const UGraph &_graph){
   1.318 +	return new ProcessedMap(_graph);
   1.319 +      }
   1.320 +    };
   1.321 +
   1.322 +
   1.323 +    template <class H, class CR>
   1.324 +    struct DefHeapTraits : public Traits {
   1.325 +      typedef CR HeapCrossRef;
   1.326 +      typedef H Heap;
   1.327 +      static HeapCrossRef *createHeapCrossRef(const UGraph &) {
   1.328 +	throw UninitializedParameter();
   1.329 +      }
   1.330 +      static Heap *createHeap(HeapCrossRef &){
   1.331 +	return UninitializedParameter();
   1.332 +      }
   1.333 +    };
   1.334 +    ///\ref named-templ-param "Named parameter" for setting heap and cross 
   1.335 +    ///reference type
   1.336 +
   1.337 +    ///\ref named-templ-param "Named parameter" for setting heap and cross 
   1.338 +    ///reference type
   1.339 +    ///
   1.340 +    template <class H, class CR = typename UGraph::template NodeMap<int> >
   1.341 +    struct DefHeap
   1.342 +      : public Prim< UGraph, CostMap, DefHeapTraits<H, CR> > {
   1.343 +      typedef Prim< UGraph, CostMap, DefHeapTraits<H, CR> > Create;
   1.344 +    };
   1.345 +
   1.346 +    template <class H, class CR>
   1.347 +    struct DefStandardHeapTraits : public Traits {
   1.348 +      typedef CR HeapCrossRef;
   1.349 +      typedef H Heap;
   1.350 +      static HeapCrossRef *createHeapCrossRef(const UGraph &_graph) {
   1.351 +	return new HeapCrossRef(_graph);
   1.352 +      }
   1.353 +      static Heap *createHeap(HeapCrossRef &ref){
   1.354 +	return new Heap(ref);
   1.355 +      }
   1.356 +    };
   1.357 +    ///\ref named-templ-param "Named parameter" for setting heap and cross 
   1.358 +    ///reference type with automatic allocation
   1.359 +
   1.360 +    ///\ref named-templ-param "Named parameter" for setting heap and cross 
   1.361 +    ///reference type. It can allocate the heap and the cross reference 
   1.362 +    ///object if the cross reference's constructor waits for the graph as 
   1.363 +    ///parameter and the heap's constructor waits for the cross reference.
   1.364 +    template <class H, class CR = typename UGraph::template NodeMap<int> >
   1.365 +    struct DefStandardHeap
   1.366 +      : public Prim< UGraph, CostMap, DefStandardHeapTraits<H, CR> > { 
   1.367 +      typedef Prim< UGraph, CostMap, DefStandardHeapTraits<H, CR> > 
   1.368 +      Create;
   1.369 +    };
   1.370 +
   1.371 +    template <class TM>
   1.372 +    struct DefTreeMapTraits : public Traits {
   1.373 +      typedef TM TreeMap;
   1.374 +      static TreeMap *createTreeMap(const UGraph &) {
   1.375 +        throw UninitializedParameter();
   1.376 +      }
   1.377 +    };
   1.378 +    ///\ref named-templ-param "Named parameter" for setting TreeMap
   1.379 +
   1.380 +    ///\ref named-templ-param "Named parameter" for setting TreeMap
   1.381 +    ///
   1.382 +    template <class TM>
   1.383 +    struct DefTreeMap
   1.384 +      : public Prim< UGraph, CostMap, DefTreeMapTraits<TM> > {
   1.385 +      typedef Prim< UGraph, CostMap, DefTreeMapTraits<TM> > Create;
   1.386 +    };    
   1.387 +
   1.388 +    struct DefGraphTreeMapTraits : public Traits {
   1.389 +      typedef typename UGraph::template NodeMap<bool> TreeMap;
   1.390 +      static TreeMap *createTreeMap(const UGraph &_graph){
   1.391 +	return new TreeMap(_graph);
   1.392 +      }
   1.393 +    };
   1.394 +
   1.395 +    ///@}
   1.396 +
   1.397 +
   1.398 +  protected:
   1.399 +
   1.400 +    Prim() {}
   1.401 +
   1.402 +  public:      
   1.403 +    
   1.404 +    ///Constructor.
   1.405 +    
   1.406 +    ///\param _graph the graph the algorithm will run on.
   1.407 +    ///\param _cost the cost map used by the algorithm.
   1.408 +    Prim(const UGraph& _graph, const CostMap& _cost) :
   1.409 +      graph(&_graph), cost(&_cost),
   1.410 +      _pred(NULL), local_pred(false),
   1.411 +      _tree(NULL), local_tree(false),
   1.412 +      _processed(NULL), local_processed(false),
   1.413 +      _heap_cross_ref(NULL), local_heap_cross_ref(false),
   1.414 +      _heap(NULL), local_heap(false)
   1.415 +    {
   1.416 +      checkConcept<concept::UGraph, UGraph>();
   1.417 +    }
   1.418 +    
   1.419 +    ///Destructor.
   1.420 +    ~Prim(){
   1.421 +      if(local_pred) delete _pred;
   1.422 +      if(local_tree) delete _tree;
   1.423 +      if(local_processed) delete _processed;
   1.424 +      if(local_heap_cross_ref) delete _heap_cross_ref;
   1.425 +      if(local_heap) delete _heap;
   1.426 +    }
   1.427 +
   1.428 +    ///\brief Sets the cost map.
   1.429 +
   1.430 +    ///Sets the cost map.
   1.431 +    ///\return <tt> (*this) </tt>
   1.432 +    Prim &costMap(const CostMap &m){
   1.433 +      cost = &m;
   1.434 +      return *this;
   1.435 +    }
   1.436 +
   1.437 +    ///\brief Sets the map storing the predecessor edges.
   1.438 +
   1.439 +    ///Sets the map storing the predecessor edges.
   1.440 +    ///If you don't use this function before calling \ref run(),
   1.441 +    ///it will allocate one. The destuctor deallocates this
   1.442 +    ///automatically allocated map, of course.
   1.443 +    ///\return <tt> (*this) </tt>
   1.444 +    Prim &predMap(PredMap &m){
   1.445 +      if(local_pred) {
   1.446 +	delete _pred;
   1.447 +	local_pred=false;
   1.448 +      }
   1.449 +      _pred = &m;
   1.450 +      return *this;
   1.451 +    }
   1.452 +
   1.453 +    ///\brief Sets the map storing the tree edges.
   1.454 +
   1.455 +    ///Sets the map storing the tree edges.
   1.456 +    ///If you don't use this function before calling \ref run(),
   1.457 +    ///it will allocate one. The destuctor deallocates this
   1.458 +    ///automatically allocated map, of course.
   1.459 +    ///By default this is a NullMap.
   1.460 +    ///\return <tt> (*this) </tt>
   1.461 +    Prim &treeMap(TreeMap &m){
   1.462 +      if(local_tree) {
   1.463 +	delete _tree;
   1.464 +	local_tree=false;
   1.465 +      }
   1.466 +      _tree = &m;
   1.467 +      return *this;
   1.468 +    }
   1.469 +
   1.470 +    ///\brief Sets the heap and the cross reference used by algorithm.
   1.471 +
   1.472 +    ///Sets the heap and the cross reference used by algorithm.
   1.473 +    ///If you don't use this function before calling \ref run(),
   1.474 +    ///it will allocate one. The destuctor deallocates this
   1.475 +    ///automatically allocated map, of course.
   1.476 +    ///\return <tt> (*this) </tt>
   1.477 +    Prim &heap(Heap& heap, HeapCrossRef &crossRef){
   1.478 +      if(local_heap_cross_ref) {
   1.479 +	delete _heap_cross_ref;
   1.480 +	local_heap_cross_ref=false;
   1.481 +      }
   1.482 +      _heap_cross_ref = &crossRef;
   1.483 +      if(local_heap) {
   1.484 +	delete _heap;
   1.485 +	local_heap=false;
   1.486 +      }
   1.487 +      _heap = &heap;
   1.488 +      return *this;
   1.489 +    }
   1.490 +
   1.491 +  public:
   1.492 +    ///\name Execution control
   1.493 +    ///The simplest way to execute the algorithm is to use
   1.494 +    ///one of the member functions called \c run(...).
   1.495 +    ///\n
   1.496 +    ///If you need more control on the execution,
   1.497 +    ///first you must call \ref init(), then you can add several source nodes
   1.498 +    ///with \ref addSource().
   1.499 +    ///Finally \ref start() will perform the actual path
   1.500 +    ///computation.
   1.501 +
   1.502 +    ///@{
   1.503 +
   1.504 +    ///\brief Initializes the internal data structures.
   1.505 +
   1.506 +    ///Initializes the internal data structures.
   1.507 +    ///
   1.508 +    void init(){
   1.509 +      create_maps();
   1.510 +      _heap->clear();
   1.511 +      for ( NodeIt u(*graph) ; u!=INVALID ; ++u ) {
   1.512 +	_pred->set(u,INVALID);
   1.513 +	_processed->set(u,false);
   1.514 +	_heap_cross_ref->set(u,Heap::PRE_HEAP);
   1.515 +      }
   1.516 +    }
   1.517 +    
   1.518 +    ///\brief Adds a new source node.
   1.519 +
   1.520 +    ///Adds a new source node to the priority heap.
   1.521 +    ///
   1.522 +    ///It checks if the node has already been added to the heap and
   1.523 +    ///it is pushed to the heap only if it was not in the heap.
   1.524 +    void addSource(Node s){
   1.525 +      if(_heap->state(s) != Heap::IN_HEAP) {
   1.526 +	_heap->push(s,Value());
   1.527 +      }
   1.528 +    }
   1.529 +    ///\brief Processes the next node in the priority heap
   1.530 +
   1.531 +    ///Processes the next node in the priority heap.
   1.532 +    ///
   1.533 +    ///\return The processed node.
   1.534 +    ///
   1.535 +    ///\warning The priority heap must not be empty!
   1.536 +    Node processNextNode(){
   1.537 +      Node v=_heap->top(); 
   1.538 +      _heap->pop();
   1.539 +      _processed->set(v,true);
   1.540 +      
   1.541 +      for(IncEdgeIt e(*graph,v); e!=INVALID; ++e) {
   1.542 +	Node w=graph->oppositeNode(v,e);
   1.543 +	switch(_heap->state(w)) {
   1.544 +	case Heap::PRE_HEAP:
   1.545 +	  _heap->push(w,(*cost)[e]);
   1.546 +	  _pred->set(w,e);
   1.547 +	  break;
   1.548 +	case Heap::IN_HEAP:
   1.549 +	  if ( (*cost)[e] < (*_heap)[w] ) {
   1.550 +	    _heap->decrease(w,(*cost)[e]); 
   1.551 +	    _pred->set(w,e);
   1.552 +	  }
   1.553 +	  break;
   1.554 +	case Heap::POST_HEAP:
   1.555 +	  break;
   1.556 +	}
   1.557 +      }
   1.558 +      if ((*_pred)[v]!=INVALID)_tree->set((*_pred)[v],true);
   1.559 +      return v;
   1.560 +    }
   1.561 +
   1.562 +    ///\brief Next node to be processed.
   1.563 +    
   1.564 +    ///Next node to be processed.
   1.565 +    ///
   1.566 +    ///\return The next node to be processed or INVALID if the priority heap
   1.567 +    /// is empty.
   1.568 +    Node nextNode(){ 
   1.569 +      return _heap->empty()?_heap->top():INVALID;
   1.570 +    }
   1.571 + 
   1.572 +    ///\brief Returns \c false if there are nodes to be processed in the priority heap
   1.573 +    ///
   1.574 +    ///Returns \c false if there are nodes
   1.575 +    ///to be processed in the priority heap
   1.576 +    bool emptyQueue() { return _heap->empty(); }
   1.577 +    ///\brief Returns the number of the nodes to be processed in the priority heap
   1.578 +
   1.579 +    ///Returns the number of the nodes to be processed in the priority heap
   1.580 +    ///
   1.581 +    int queueSize() { return _heap->size(); }
   1.582 +    
   1.583 +    ///\brief Executes the algorithm.
   1.584 +
   1.585 +    ///Executes the algorithm.
   1.586 +    ///
   1.587 +    ///\pre init() must be called and at least one node should be added
   1.588 +    ///with addSource() before using this function.
   1.589 +    ///
   1.590 +    ///This method runs the %Prim algorithm from the node(s)
   1.591 +    ///in order to compute the
   1.592 +    ///minimum spanning tree.
   1.593 +    ///
   1.594 +    void start(){
   1.595 +      while ( !_heap->empty() ) processNextNode();
   1.596 +    }
   1.597 +    
   1.598 +    ///\brief Executes the algorithm until a condition is met.
   1.599 +
   1.600 +    ///Executes the algorithm until a condition is met.
   1.601 +    ///
   1.602 +    ///\pre init() must be called and at least one node should be added
   1.603 +    ///with addSource() before using this function.
   1.604 +    ///
   1.605 +    ///\param nm must be a bool (or convertible) node map. The algorithm
   1.606 +    ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
   1.607 +    template<class NodeBoolMap>
   1.608 +    void start(const NodeBoolMap &nm){
   1.609 +      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
   1.610 +      if ( !_heap->empty() ) _processed->set(_heap->top(),true);
   1.611 +    }
   1.612 +    
   1.613 +    ///\brief Runs %Prim algorithm.
   1.614 +    
   1.615 +    ///This method runs the %Prim algorithm
   1.616 +    ///in order to compute the
   1.617 +    ///minimum spanning tree (or minimum spanning forest).
   1.618 +    ///The method also works on graphs that has more than one components.
   1.619 +    ///In this case it computes the minimum spanning forest.
   1.620 +    void run() {
   1.621 +      init();
   1.622 +      for(NodeIt it(*graph);it!=INVALID;++it){
   1.623 +	if(!processed(it)){
   1.624 +	  addSource(it);
   1.625 +	  start();
   1.626 +	}
   1.627 +      }
   1.628 +    }
   1.629 +
   1.630 +    ///\brief Runs %Prim algorithm from node \c s.
   1.631 +    
   1.632 +    ///This method runs the %Prim algorithm from node \c s
   1.633 +    ///in order to
   1.634 +    ///compute the
   1.635 +    ///minimun spanning tree
   1.636 +    ///
   1.637 +    ///\note d.run(s) is just a shortcut of the following code.
   1.638 +    ///\code
   1.639 +    ///  d.init();
   1.640 +    ///  d.addSource(s);
   1.641 +    ///  d.start();
   1.642 +    ///\endcode
   1.643 +    ///\note If the graph has more than one components, the method
   1.644 +    ///will compute the minimun spanning tree for only one component.
   1.645 +    ///
   1.646 +    ///See \ref run() if you want to compute the minimal spanning forest.
   1.647 +    void run(Node s){
   1.648 +      init();
   1.649 +      addSource(s);
   1.650 +      start();
   1.651 +    }
   1.652 +    
   1.653 +    ///@}
   1.654 +
   1.655 +    ///\name Query Functions
   1.656 +    ///The result of the %Prim algorithm can be obtained using these
   1.657 +    ///functions.\n
   1.658 +    ///Before the use of these functions,
   1.659 +    ///either run() or start() must be called.
   1.660 +    
   1.661 +    ///@{
   1.662 +
   1.663 +    ///\brief Returns the 'previous edge' of the minimum spanning tree.
   1.664 +
   1.665 +    ///For a node \c v it returns the 'previous edge' of the minimum spanning tree,
   1.666 +    ///i.e. it returns the edge from where \c v was reached. For a source node
   1.667 +    ///or an unreachable node it is \ref INVALID.
   1.668 +    ///The minimum spanning tree used here is equal to the minimum spanning tree used
   1.669 +    ///in \ref predNode().  \pre \ref run() or \ref start() must be called before
   1.670 +    ///using this function.
   1.671 +    UEdge predEdge(Node v) const { return (*_pred)[v]; }
   1.672 +
   1.673 +    ///\brief Returns the 'previous node' of the minimum spanning tree.
   1.674 +
   1.675 +    ///For a node \c v it returns the 'previous node' of the minimum spanning tree,
   1.676 +    ///i.e. it returns the node from where \c v was reached. For a source node
   1.677 +    ///or an unreachable node it is \ref INVALID.
   1.678 +    //The minimum spanning tree used here is equal to the minimum spanning
   1.679 +    ///tree used in \ref predEdge().  \pre \ref run() or \ref start() must be called
   1.680 +    ///before using this function.
   1.681 +    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   1.682 +				  graph->source((*_pred)[v]); }
   1.683 +    
   1.684 +    ///\brief Returns a reference to the NodeMap of the edges of the minimum spanning tree.
   1.685 +
   1.686 +    ///Returns a reference to the NodeMap of the edges of the
   1.687 +    ///minimum spanning tree.
   1.688 +    ///\pre \ref run() or \ref start() must be called before using this function.
   1.689 +    const PredMap &predMap() const { return *_pred;}
   1.690 + 
   1.691 +    ///\brief Returns a reference to the tree edges map.
   1.692 +
   1.693 +    ///Returns a reference to the TreeEdgeMap of the edges of the
   1.694 +    ///minimum spanning tree. The value of the map is \c true only if the edge is in
   1.695 +    ///the minimum spanning tree.
   1.696 +    ///\warning By default, the TreeEdgeMap is a NullMap.
   1.697 +    ///
   1.698 +    ///If it is not set before the execution of the algorithm, use the \ref
   1.699 +    ///treeMap(TreeMap&) function (after the execution) to set an UEdgeMap with the
   1.700 +    ///edges of the minimum spanning tree in O(n) time where n is the number of
   1.701 +    ///nodes in the graph.
   1.702 +    ///\pre \ref run() or \ref start() must be called before using this function.
   1.703 +    const TreeMap &treeMap() const { return *_tree;}
   1.704 + 
   1.705 +    ///\brief Sets the tree edges map.
   1.706 +
   1.707 +    ///Sets the TreeMap of the edges of the minimum spanning tree.
   1.708 +    ///The map values belonging to the edges of the minimum
   1.709 +    ///spanning tree are set to \param tree_edge_value or \c true by default,
   1.710 +    ///the other map values remain untouched.
   1.711 +    ///
   1.712 +    ///\pre \ref run() or \ref start() must be called before using this function.
   1.713 +
   1.714 +    template<class TreeMap>
   1.715 +    void quickTreeEdges(
   1.716 +        TreeMap& tree,
   1.717 +        const typename TreeMap::Value& tree_edge_value=true) const {
   1.718 +      for(NodeIt i(*graph);i!=INVALID;++i){
   1.719 +        if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value);
   1.720 +      }
   1.721 +    }
   1.722 +
   1.723 +    ///\brief Sets the tree edges map.
   1.724 +
   1.725 +    ///Sets the TreeMap of the edges of the minimum spanning tree.
   1.726 +    ///The map values belonging to the edges of the minimum
   1.727 +    ///spanning tree are set to \param tree_edge_value or \c true by default while
   1.728 +    ///the edge values not belonging to the minimum spanning tree are set to
   1.729 +    ///\param tree_default_value or \c false by default.
   1.730 +    ///
   1.731 +    ///\pre \ref run() or \ref start() must be called before using this function.
   1.732 +
   1.733 +    template<class TreeMap>
   1.734 +    void treeEdges(
   1.735 +        TreeMap& tree,
   1.736 +        const typename TreeMap::Value& tree_edge_value=true,
   1.737 +        const typename TreeMap::Value& tree_default_value=false) const {
   1.738 +      for(typename ItemSetTraits<UGraph,UEdge>::ItemIt i(*graph);i!=INVALID;++i)
   1.739 +	tree.set(i,tree_default_value);
   1.740 +      for(NodeIt i(*graph);i!=INVALID;++i){
   1.741 +        if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value);
   1.742 +      }
   1.743 +    }
   1.744 +
   1.745 +    ///\brief Checks if a node is reachable from the starting node.
   1.746 +
   1.747 +    ///Returns \c true if \c v is reachable from the starting node.
   1.748 +    ///\warning The source nodes are inditated as unreached.
   1.749 +    ///\pre \ref run() or \ref start() must be called before using this function.
   1.750 +    ///
   1.751 +    bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
   1.752 +
   1.753 +    ///\brief Checks if a node is processed.
   1.754 +
   1.755 +    ///Returns \c true if \c v is processed, i.e. \c v is already connencted to the
   1.756 +    ///minimum spanning tree.
   1.757 +    ///\pre \ref run() or \ref start() must be called before using this function.
   1.758 +    ///
   1.759 +    bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
   1.760 +    
   1.761 +
   1.762 +    ///\brief Checks if an edge is in the spanning tree or not.
   1.763 +
   1.764 +    ///Checks if an edge is in the spanning tree or not.
   1.765 +    ///\param e is the edge that will be checked
   1.766 +    ///\return \c true if e is in the spanning tree, \c false otherwise
   1.767 +    bool tree(UEdge e){
   1.768 +      return (*_pred)[*graph.source(e)]==e || (*_pred)[*graph.target(e)]==e;
   1.769 +    }
   1.770 +    ///@}
   1.771 +  };
   1.772 +
   1.773 +
   1.774 +  /// \ingroup spantree
   1.775 +  ///
   1.776 +  /// \brief Function type interface for Prim algorithm.
   1.777 +  ///
   1.778 +  /// Function type interface for Prim algorithm.
   1.779 +  /// \param graph the UGraph that the algorithm runs on
   1.780 +  /// \param cost the CostMap of the edges
   1.781 +  /// \retval tree the EdgeMap that contains whether an edge is in 
   1.782 +  /// the spanning tree or not
   1.783 +  ///
   1.784 +  ///\sa Prim
   1.785 +  template<class Graph,class CostMap,class TreeMap>
   1.786 +  void prim(const Graph& graph, const CostMap& cost,TreeMap& tree){
   1.787 +    typename Prim<Graph,CostMap>::template DefTreeMap<TreeMap>::
   1.788 +      Create prm(graph,cost);
   1.789 +    prm.treeMap(tree);
   1.790 +    prm.run();
   1.791 +  };
   1.792 +
   1.793 +} //END OF NAMESPACE LEMON
   1.794 +
   1.795 +#endif