Merge
authorAlpar Juttner <alpar@cs.elte.hu>
Thu, 20 Mar 2008 12:12:24 +0000
changeset 10170f3967ca6eb
parent 95 cc7e6b8b59bf
parent 100 4f754b4cf82b
child 102 81563e019fa4
Merge
lemon/Makefile.am
     1.1 --- a/lemon/Makefile.am	Tue Mar 18 16:45:21 2008 +0100
     1.2 +++ b/lemon/Makefile.am	Thu Mar 20 12:12:24 2008 +0000
     1.3 @@ -17,12 +17,17 @@
     1.4  
     1.5  lemon_HEADERS += \
     1.6          lemon/arg_parser.h \
     1.7 -        lemon/concept_check.h \
     1.8 +        lemon/bfs.h \
     1.9 +        lemon/bin_heap.h \
    1.10 +        lemon/dfs.h \
    1.11 +        lemon/dijkstra.h \
    1.12          lemon/dim2.h \
    1.13  	lemon/error.h \
    1.14 +        lemon/graph_utils.h \
    1.15  	lemon/list_graph.h \
    1.16  	lemon/maps.h \
    1.17  	lemon/math.h \
    1.18 +	lemon/path.h \
    1.19          lemon/random.h \
    1.20          lemon/tolerance.h
    1.21  
    1.22 @@ -34,6 +39,7 @@
    1.23  	lemon/bits/graph_extender.h \
    1.24          lemon/bits/invalid.h \
    1.25  	lemon/bits/map_extender.h \
    1.26 +	lemon/bits/path_dump.h \
    1.27  	lemon/bits/traits.h \
    1.28          lemon/bits/utility.h \
    1.29  	lemon/bits/vector_map.h
    1.30 @@ -42,5 +48,7 @@
    1.31  	lemon/concept_check.h \
    1.32  	lemon/concepts/digraph.h \
    1.33  	lemon/concepts/graph.h \
    1.34 +	lemon/concepts/heap.h \
    1.35  	lemon/concepts/maps.h \
    1.36 +	lemon/concepts/path.h \
    1.37  	lemon/concepts/graph_components.h
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/lemon/bfs.h	Thu Mar 20 12:12:24 2008 +0000
     2.3 @@ -0,0 +1,1597 @@
     2.4 +/* -*- C++ -*-
     2.5 + *
     2.6 + * This file is a part of LEMON, a generic C++ optimization library
     2.7 + *
     2.8 + * Copyright (C) 2003-2008
     2.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    2.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    2.11 + *
    2.12 + * Permission to use, modify and distribute this software is granted
    2.13 + * provided that this copyright notice appears in all copies. For
    2.14 + * precise terms see the accompanying LICENSE file.
    2.15 + *
    2.16 + * This software is provided "AS IS" with no warranty of any kind,
    2.17 + * express or implied, and with no claim as to its suitability for any
    2.18 + * purpose.
    2.19 + *
    2.20 + */
    2.21 +
    2.22 +#ifndef LEMON_BFS_H
    2.23 +#define LEMON_BFS_H
    2.24 +
    2.25 +///\ingroup search
    2.26 +///\file
    2.27 +///\brief Bfs algorithm.
    2.28 +
    2.29 +#include <lemon/list_graph.h>
    2.30 +#include <lemon/graph_utils.h>
    2.31 +#include <lemon/bits/path_dump.h>
    2.32 +#include <lemon/bits/invalid.h>
    2.33 +#include <lemon/error.h>
    2.34 +#include <lemon/maps.h>
    2.35 +
    2.36 +namespace lemon {
    2.37 +
    2.38 +
    2.39 +  
    2.40 +  ///Default traits class of Bfs class.
    2.41 +
    2.42 +  ///Default traits class of Bfs class.
    2.43 +  ///\param GR Digraph type.
    2.44 +  template<class GR>
    2.45 +  struct BfsDefaultTraits
    2.46 +  {
    2.47 +    ///The digraph type the algorithm runs on. 
    2.48 +    typedef GR Digraph;
    2.49 +    ///\brief The type of the map that stores the last
    2.50 +    ///arcs of the shortest paths.
    2.51 +    /// 
    2.52 +    ///The type of the map that stores the last
    2.53 +    ///arcs of the shortest paths.
    2.54 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    2.55 +    ///
    2.56 +    typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap;
    2.57 +    ///Instantiates a PredMap.
    2.58 + 
    2.59 +    ///This function instantiates a \ref PredMap. 
    2.60 +    ///\param G is the digraph, to which we would like to define the PredMap.
    2.61 +    ///\todo The digraph alone may be insufficient to initialize
    2.62 +    static PredMap *createPredMap(const GR &G) 
    2.63 +    {
    2.64 +      return new PredMap(G);
    2.65 +    }
    2.66 +    ///The type of the map that indicates which nodes are processed.
    2.67 + 
    2.68 +    ///The type of the map that indicates which nodes are processed.
    2.69 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    2.70 +    ///\todo named parameter to set this type, function to read and write.
    2.71 +    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    2.72 +    ///Instantiates a ProcessedMap.
    2.73 + 
    2.74 +    ///This function instantiates a \ref ProcessedMap. 
    2.75 +    ///\param g is the digraph, to which
    2.76 +    ///we would like to define the \ref ProcessedMap
    2.77 +#ifdef DOXYGEN
    2.78 +    static ProcessedMap *createProcessedMap(const GR &g)
    2.79 +#else
    2.80 +    static ProcessedMap *createProcessedMap(const GR &)
    2.81 +#endif
    2.82 +    {
    2.83 +      return new ProcessedMap();
    2.84 +    }
    2.85 +    ///The type of the map that indicates which nodes are reached.
    2.86 + 
    2.87 +    ///The type of the map that indicates which nodes are reached.
    2.88 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    2.89 +    ///\todo named parameter to set this type, function to read and write.
    2.90 +    typedef typename Digraph::template NodeMap<bool> ReachedMap;
    2.91 +    ///Instantiates a ReachedMap.
    2.92 + 
    2.93 +    ///This function instantiates a \ref ReachedMap. 
    2.94 +    ///\param G is the digraph, to which
    2.95 +    ///we would like to define the \ref ReachedMap.
    2.96 +    static ReachedMap *createReachedMap(const GR &G)
    2.97 +    {
    2.98 +      return new ReachedMap(G);
    2.99 +    }
   2.100 +    ///The type of the map that stores the dists of the nodes.
   2.101 + 
   2.102 +    ///The type of the map that stores the dists of the nodes.
   2.103 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   2.104 +    ///
   2.105 +    typedef typename Digraph::template NodeMap<int> DistMap;
   2.106 +    ///Instantiates a DistMap.
   2.107 + 
   2.108 +    ///This function instantiates a \ref DistMap. 
   2.109 +    ///\param G is the digraph, to which we would like to define the \ref DistMap
   2.110 +    static DistMap *createDistMap(const GR &G)
   2.111 +    {
   2.112 +      return new DistMap(G);
   2.113 +    }
   2.114 +  };
   2.115 +  
   2.116 +  ///%BFS algorithm class.
   2.117 +  
   2.118 +  ///\ingroup search
   2.119 +  ///This class provides an efficient implementation of the %BFS algorithm.
   2.120 +  ///
   2.121 +  ///\param GR The digraph type the algorithm runs on. The default value is
   2.122 +  ///\ref ListDigraph. The value of GR is not used directly by Bfs, it
   2.123 +  ///is only passed to \ref BfsDefaultTraits.
   2.124 +  ///\param TR Traits class to set various data types used by the algorithm.
   2.125 +  ///The default traits class is
   2.126 +  ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
   2.127 +  ///See \ref BfsDefaultTraits for the documentation of
   2.128 +  ///a Bfs traits class.
   2.129 +  ///
   2.130 +  ///\author Alpar Juttner
   2.131 +
   2.132 +#ifdef DOXYGEN
   2.133 +  template <typename GR,
   2.134 +	    typename TR>
   2.135 +#else
   2.136 +  template <typename GR=ListDigraph,
   2.137 +	    typename TR=BfsDefaultTraits<GR> >
   2.138 +#endif
   2.139 +  class Bfs {
   2.140 +  public:
   2.141 +    /**
   2.142 +     * \brief \ref Exception for uninitialized parameters.
   2.143 +     *
   2.144 +     * This error represents problems in the initialization
   2.145 +     * of the parameters of the algorithms.
   2.146 +     */
   2.147 +    class UninitializedParameter : public lemon::UninitializedParameter {
   2.148 +    public:
   2.149 +      virtual const char* what() const throw() {
   2.150 +	return "lemon::Bfs::UninitializedParameter";
   2.151 +      }
   2.152 +    };
   2.153 +
   2.154 +    typedef TR Traits;
   2.155 +    ///The type of the underlying digraph.
   2.156 +    typedef typename TR::Digraph Digraph;
   2.157 +    
   2.158 +    ///\brief The type of the map that stores the last
   2.159 +    ///arcs of the shortest paths.
   2.160 +    typedef typename TR::PredMap PredMap;
   2.161 +    ///The type of the map indicating which nodes are reached.
   2.162 +    typedef typename TR::ReachedMap ReachedMap;
   2.163 +    ///The type of the map indicating which nodes are processed.
   2.164 +    typedef typename TR::ProcessedMap ProcessedMap;
   2.165 +    ///The type of the map that stores the dists of the nodes.
   2.166 +    typedef typename TR::DistMap DistMap;
   2.167 +  private:
   2.168 +
   2.169 +    typedef typename Digraph::Node Node;
   2.170 +    typedef typename Digraph::NodeIt NodeIt;
   2.171 +    typedef typename Digraph::Arc Arc;
   2.172 +    typedef typename Digraph::OutArcIt OutArcIt;
   2.173 +
   2.174 +    /// Pointer to the underlying digraph.
   2.175 +    const Digraph *G;
   2.176 +    ///Pointer to the map of predecessors arcs.
   2.177 +    PredMap *_pred;
   2.178 +    ///Indicates if \ref _pred is locally allocated (\c true) or not.
   2.179 +    bool local_pred;
   2.180 +    ///Pointer to the map of distances.
   2.181 +    DistMap *_dist;
   2.182 +    ///Indicates if \ref _dist is locally allocated (\c true) or not.
   2.183 +    bool local_dist;
   2.184 +    ///Pointer to the map of reached status of the nodes.
   2.185 +    ReachedMap *_reached;
   2.186 +    ///Indicates if \ref _reached is locally allocated (\c true) or not.
   2.187 +    bool local_reached;
   2.188 +    ///Pointer to the map of processed status of the nodes.
   2.189 +    ProcessedMap *_processed;
   2.190 +    ///Indicates if \ref _processed is locally allocated (\c true) or not.
   2.191 +    bool local_processed;
   2.192 +
   2.193 +    std::vector<typename Digraph::Node> _queue;
   2.194 +    int _queue_head,_queue_tail,_queue_next_dist;
   2.195 +    int _curr_dist;
   2.196 +
   2.197 +    ///Creates the maps if necessary.
   2.198 +    
   2.199 +    ///\todo Better memory allocation (instead of new).
   2.200 +    void create_maps() 
   2.201 +    {
   2.202 +      if(!_pred) {
   2.203 +	local_pred = true;
   2.204 +	_pred = Traits::createPredMap(*G);
   2.205 +      }
   2.206 +      if(!_dist) {
   2.207 +	local_dist = true;
   2.208 +	_dist = Traits::createDistMap(*G);
   2.209 +      }
   2.210 +      if(!_reached) {
   2.211 +	local_reached = true;
   2.212 +	_reached = Traits::createReachedMap(*G);
   2.213 +      }
   2.214 +      if(!_processed) {
   2.215 +	local_processed = true;
   2.216 +	_processed = Traits::createProcessedMap(*G);
   2.217 +      }
   2.218 +    }
   2.219 +
   2.220 +  protected:
   2.221 +    
   2.222 +    Bfs() {}
   2.223 +    
   2.224 +  public:
   2.225 + 
   2.226 +    typedef Bfs Create;
   2.227 +
   2.228 +    ///\name Named template parameters
   2.229 +
   2.230 +    ///@{
   2.231 +
   2.232 +    template <class T>
   2.233 +    struct DefPredMapTraits : public Traits {
   2.234 +      typedef T PredMap;
   2.235 +      static PredMap *createPredMap(const Digraph &) 
   2.236 +      {
   2.237 +	throw UninitializedParameter();
   2.238 +      }
   2.239 +    };
   2.240 +    ///\brief \ref named-templ-param "Named parameter" for setting
   2.241 +    ///PredMap type
   2.242 +    ///
   2.243 +    ///\ref named-templ-param "Named parameter" for setting PredMap type
   2.244 +    ///
   2.245 +    template <class T>
   2.246 +    struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > { 
   2.247 +      typedef Bfs< Digraph, DefPredMapTraits<T> > Create;
   2.248 +    };
   2.249 +    
   2.250 +    template <class T>
   2.251 +    struct DefDistMapTraits : public Traits {
   2.252 +      typedef T DistMap;
   2.253 +      static DistMap *createDistMap(const Digraph &) 
   2.254 +      {
   2.255 +	throw UninitializedParameter();
   2.256 +      }
   2.257 +    };
   2.258 +    ///\brief \ref named-templ-param "Named parameter" for setting
   2.259 +    ///DistMap type
   2.260 +    ///
   2.261 +    ///\ref named-templ-param "Named parameter" for setting DistMap type
   2.262 +    ///
   2.263 +    template <class T>
   2.264 +    struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > { 
   2.265 +      typedef Bfs< Digraph, DefDistMapTraits<T> > Create;
   2.266 +    };
   2.267 +    
   2.268 +    template <class T>
   2.269 +    struct DefReachedMapTraits : public Traits {
   2.270 +      typedef T ReachedMap;
   2.271 +      static ReachedMap *createReachedMap(const Digraph &) 
   2.272 +      {
   2.273 +	throw UninitializedParameter();
   2.274 +      }
   2.275 +    };
   2.276 +    ///\brief \ref named-templ-param "Named parameter" for setting
   2.277 +    ///ReachedMap type
   2.278 +    ///
   2.279 +    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
   2.280 +    ///
   2.281 +    template <class T>
   2.282 +    struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > { 
   2.283 +      typedef Bfs< Digraph, DefReachedMapTraits<T> > Create;
   2.284 +    };
   2.285 +    
   2.286 +    template <class T>
   2.287 +    struct DefProcessedMapTraits : public Traits {
   2.288 +      typedef T ProcessedMap;
   2.289 +      static ProcessedMap *createProcessedMap(const Digraph &) 
   2.290 +      {
   2.291 +	throw UninitializedParameter();
   2.292 +      }
   2.293 +    };
   2.294 +    ///\brief \ref named-templ-param "Named parameter" for setting
   2.295 +    ///ProcessedMap type
   2.296 +    ///
   2.297 +    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   2.298 +    ///
   2.299 +    template <class T>
   2.300 +    struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > {
   2.301 +      typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create;
   2.302 +    };
   2.303 +    
   2.304 +    struct DefDigraphProcessedMapTraits : public Traits {
   2.305 +      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
   2.306 +      static ProcessedMap *createProcessedMap(const Digraph &G) 
   2.307 +      {
   2.308 +	return new ProcessedMap(G);
   2.309 +      }
   2.310 +    };
   2.311 +    ///\brief \ref named-templ-param "Named parameter"
   2.312 +    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
   2.313 +    ///
   2.314 +    ///\ref named-templ-param "Named parameter"
   2.315 +    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
   2.316 +    ///If you don't set it explicitly, it will be automatically allocated.
   2.317 +    template <class T>
   2.318 +    struct DefProcessedMapToBeDefaultMap :
   2.319 +      public Bfs< Digraph, DefDigraphProcessedMapTraits> { 
   2.320 +      typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create;
   2.321 +    };
   2.322 +    
   2.323 +    ///@}
   2.324 +
   2.325 +  public:      
   2.326 +    
   2.327 +    ///Constructor.
   2.328 +    
   2.329 +    ///\param _G the digraph the algorithm will run on.
   2.330 +    ///
   2.331 +    Bfs(const Digraph& _G) :
   2.332 +      G(&_G),
   2.333 +      _pred(NULL), local_pred(false),
   2.334 +      _dist(NULL), local_dist(false),
   2.335 +      _reached(NULL), local_reached(false),
   2.336 +      _processed(NULL), local_processed(false)
   2.337 +    { }
   2.338 +    
   2.339 +    ///Destructor.
   2.340 +    ~Bfs() 
   2.341 +    {
   2.342 +      if(local_pred) delete _pred;
   2.343 +      if(local_dist) delete _dist;
   2.344 +      if(local_reached) delete _reached;
   2.345 +      if(local_processed) delete _processed;
   2.346 +    }
   2.347 +
   2.348 +    ///Sets the map storing the predecessor arcs.
   2.349 +
   2.350 +    ///Sets the map storing the predecessor arcs.
   2.351 +    ///If you don't use this function before calling \ref run(),
   2.352 +    ///it will allocate one. The destructor deallocates this
   2.353 +    ///automatically allocated map, of course.
   2.354 +    ///\return <tt> (*this) </tt>
   2.355 +    Bfs &predMap(PredMap &m) 
   2.356 +    {
   2.357 +      if(local_pred) {
   2.358 +	delete _pred;
   2.359 +	local_pred=false;
   2.360 +      }
   2.361 +      _pred = &m;
   2.362 +      return *this;
   2.363 +    }
   2.364 +
   2.365 +    ///Sets the map indicating the reached nodes.
   2.366 +
   2.367 +    ///Sets the map indicating the reached nodes.
   2.368 +    ///If you don't use this function before calling \ref run(),
   2.369 +    ///it will allocate one. The destructor deallocates this
   2.370 +    ///automatically allocated map, of course.
   2.371 +    ///\return <tt> (*this) </tt>
   2.372 +    Bfs &reachedMap(ReachedMap &m) 
   2.373 +    {
   2.374 +      if(local_reached) {
   2.375 +	delete _reached;
   2.376 +	local_reached=false;
   2.377 +      }
   2.378 +      _reached = &m;
   2.379 +      return *this;
   2.380 +    }
   2.381 +
   2.382 +    ///Sets the map indicating the processed nodes.
   2.383 +
   2.384 +    ///Sets the map indicating the processed nodes.
   2.385 +    ///If you don't use this function before calling \ref run(),
   2.386 +    ///it will allocate one. The destructor deallocates this
   2.387 +    ///automatically allocated map, of course.
   2.388 +    ///\return <tt> (*this) </tt>
   2.389 +    Bfs &processedMap(ProcessedMap &m) 
   2.390 +    {
   2.391 +      if(local_processed) {
   2.392 +	delete _processed;
   2.393 +	local_processed=false;
   2.394 +      }
   2.395 +      _processed = &m;
   2.396 +      return *this;
   2.397 +    }
   2.398 +
   2.399 +    ///Sets the map storing the distances calculated by the algorithm.
   2.400 +
   2.401 +    ///Sets the map storing the distances calculated by the algorithm.
   2.402 +    ///If you don't use this function before calling \ref run(),
   2.403 +    ///it will allocate one. The destructor deallocates this
   2.404 +    ///automatically allocated map, of course.
   2.405 +    ///\return <tt> (*this) </tt>
   2.406 +    Bfs &distMap(DistMap &m) 
   2.407 +    {
   2.408 +      if(local_dist) {
   2.409 +	delete _dist;
   2.410 +	local_dist=false;
   2.411 +      }
   2.412 +      _dist = &m;
   2.413 +      return *this;
   2.414 +    }
   2.415 +
   2.416 +  public:
   2.417 +    ///\name Execution control
   2.418 +    ///The simplest way to execute the algorithm is to use
   2.419 +    ///one of the member functions called \c run(...).
   2.420 +    ///\n
   2.421 +    ///If you need more control on the execution,
   2.422 +    ///first you must call \ref init(), then you can add several source nodes
   2.423 +    ///with \ref addSource().
   2.424 +    ///Finally \ref start() will perform the actual path
   2.425 +    ///computation.
   2.426 +
   2.427 +    ///@{
   2.428 +
   2.429 +    ///\brief Initializes the internal data structures.
   2.430 +    ///
   2.431 +    ///Initializes the internal data structures.
   2.432 +    ///
   2.433 +    void init()
   2.434 +    {
   2.435 +      create_maps();
   2.436 +      _queue.resize(countNodes(*G));
   2.437 +      _queue_head=_queue_tail=0;
   2.438 +      _curr_dist=1;
   2.439 +      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   2.440 +	_pred->set(u,INVALID);
   2.441 +	_reached->set(u,false);
   2.442 +	_processed->set(u,false);
   2.443 +      }
   2.444 +    }
   2.445 +    
   2.446 +    ///Adds a new source node.
   2.447 +
   2.448 +    ///Adds a new source node to the set of nodes to be processed.
   2.449 +    ///
   2.450 +    void addSource(Node s)
   2.451 +    {
   2.452 +      if(!(*_reached)[s])
   2.453 +	{
   2.454 +	  _reached->set(s,true);
   2.455 +	  _pred->set(s,INVALID);
   2.456 +	  _dist->set(s,0);
   2.457 +	  _queue[_queue_head++]=s;
   2.458 +	  _queue_next_dist=_queue_head;
   2.459 +	}
   2.460 +    }
   2.461 +    
   2.462 +    ///Processes the next node.
   2.463 +
   2.464 +    ///Processes the next node.
   2.465 +    ///
   2.466 +    ///\return The processed node.
   2.467 +    ///
   2.468 +    ///\warning The queue must not be empty!
   2.469 +    Node processNextNode()
   2.470 +    {
   2.471 +      if(_queue_tail==_queue_next_dist) {
   2.472 +	_curr_dist++;
   2.473 +	_queue_next_dist=_queue_head;
   2.474 +      }
   2.475 +      Node n=_queue[_queue_tail++];
   2.476 +      _processed->set(n,true);
   2.477 +      Node m;
   2.478 +      for(OutArcIt e(*G,n);e!=INVALID;++e)
   2.479 +	if(!(*_reached)[m=G->target(e)]) {
   2.480 +	  _queue[_queue_head++]=m;
   2.481 +	  _reached->set(m,true);
   2.482 +	  _pred->set(m,e);
   2.483 +	  _dist->set(m,_curr_dist);
   2.484 +	}
   2.485 +      return n;
   2.486 +    }
   2.487 +
   2.488 +    ///Processes the next node.
   2.489 +
   2.490 +    ///Processes the next node. And checks that the given target node
   2.491 +    ///is reached. If the target node is reachable from the processed
   2.492 +    ///node then the reached parameter will be set true. The reached
   2.493 +    ///parameter should be initially false.
   2.494 +    ///
   2.495 +    ///\param target The target node.
   2.496 +    ///\retval reach Indicates that the target node is reached.
   2.497 +    ///\return The processed node.
   2.498 +    ///
   2.499 +    ///\warning The queue must not be empty!
   2.500 +    Node processNextNode(Node target, bool& reach)
   2.501 +    {
   2.502 +      if(_queue_tail==_queue_next_dist) {
   2.503 +	_curr_dist++;
   2.504 +	_queue_next_dist=_queue_head;
   2.505 +      }
   2.506 +      Node n=_queue[_queue_tail++];
   2.507 +      _processed->set(n,true);
   2.508 +      Node m;
   2.509 +      for(OutArcIt e(*G,n);e!=INVALID;++e)
   2.510 +	if(!(*_reached)[m=G->target(e)]) {
   2.511 +	  _queue[_queue_head++]=m;
   2.512 +	  _reached->set(m,true);
   2.513 +	  _pred->set(m,e);
   2.514 +	  _dist->set(m,_curr_dist);
   2.515 +          reach = reach || (target == m);
   2.516 +	}
   2.517 +      return n;
   2.518 +    }
   2.519 +
   2.520 +    ///Processes the next node.
   2.521 +
   2.522 +    ///Processes the next node. And checks that at least one of
   2.523 +    ///reached node has true value in the \c nm node map. If one node
   2.524 +    ///with true value is reachable from the processed node then the
   2.525 +    ///rnode parameter will be set to the first of such nodes.
   2.526 +    ///
   2.527 +    ///\param nm The node map of possible targets.
   2.528 +    ///\retval rnode The reached target node.
   2.529 +    ///\return The processed node.
   2.530 +    ///
   2.531 +    ///\warning The queue must not be empty!
   2.532 +    template<class NM>
   2.533 +    Node processNextNode(const NM& nm, Node& rnode)
   2.534 +    {
   2.535 +      if(_queue_tail==_queue_next_dist) {
   2.536 +	_curr_dist++;
   2.537 +	_queue_next_dist=_queue_head;
   2.538 +      }
   2.539 +      Node n=_queue[_queue_tail++];
   2.540 +      _processed->set(n,true);
   2.541 +      Node m;
   2.542 +      for(OutArcIt e(*G,n);e!=INVALID;++e)
   2.543 +	if(!(*_reached)[m=G->target(e)]) {
   2.544 +	  _queue[_queue_head++]=m;
   2.545 +	  _reached->set(m,true);
   2.546 +	  _pred->set(m,e);
   2.547 +	  _dist->set(m,_curr_dist);
   2.548 +	  if (nm[m] && rnode == INVALID) rnode = m;
   2.549 +	}
   2.550 +      return n;
   2.551 +    }
   2.552 +      
   2.553 +    ///Next node to be processed.
   2.554 +
   2.555 +    ///Next node to be processed.
   2.556 +    ///
   2.557 +    ///\return The next node to be processed or INVALID if the queue is
   2.558 +    /// empty.
   2.559 +    Node nextNode()
   2.560 +    { 
   2.561 +      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
   2.562 +    }
   2.563 + 
   2.564 +    ///\brief Returns \c false if there are nodes
   2.565 +    ///to be processed in the queue
   2.566 +    ///
   2.567 +    ///Returns \c false if there are nodes
   2.568 +    ///to be processed in the queue
   2.569 +    bool emptyQueue() { return _queue_tail==_queue_head; }
   2.570 +    ///Returns the number of the nodes to be processed.
   2.571 +    
   2.572 +    ///Returns the number of the nodes to be processed in the queue.
   2.573 +    int queueSize() { return _queue_head-_queue_tail; }
   2.574 +    
   2.575 +    ///Executes the algorithm.
   2.576 +
   2.577 +    ///Executes the algorithm.
   2.578 +    ///
   2.579 +    ///\pre init() must be called and at least one node should be added
   2.580 +    ///with addSource() before using this function.
   2.581 +    ///
   2.582 +    ///This method runs the %BFS algorithm from the root node(s)
   2.583 +    ///in order to
   2.584 +    ///compute the
   2.585 +    ///shortest path to each node. The algorithm computes
   2.586 +    ///- The shortest path tree.
   2.587 +    ///- The distance of each node from the root(s).
   2.588 +    void start()
   2.589 +    {
   2.590 +      while ( !emptyQueue() ) processNextNode();
   2.591 +    }
   2.592 +    
   2.593 +    ///Executes the algorithm until \c dest is reached.
   2.594 +
   2.595 +    ///Executes the algorithm until \c dest is reached.
   2.596 +    ///
   2.597 +    ///\pre init() must be called and at least one node should be added
   2.598 +    ///with addSource() before using this function.
   2.599 +    ///
   2.600 +    ///This method runs the %BFS algorithm from the root node(s)
   2.601 +    ///in order to compute the shortest path to \c dest.
   2.602 +    ///The algorithm computes
   2.603 +    ///- The shortest path to \c  dest.
   2.604 +    ///- The distance of \c dest from the root(s).
   2.605 +    void start(Node dest)
   2.606 +    {
   2.607 +      bool reach = false;
   2.608 +      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
   2.609 +    }
   2.610 +    
   2.611 +    ///Executes the algorithm until a condition is met.
   2.612 +
   2.613 +    ///Executes the algorithm until a condition is met.
   2.614 +    ///
   2.615 +    ///\pre init() must be called and at least one node should be added
   2.616 +    ///with addSource() before using this function.
   2.617 +    ///
   2.618 +    ///\param nm must be a bool (or convertible) node map. The
   2.619 +    ///algorithm will stop when it reaches a node \c v with
   2.620 +    /// <tt>nm[v]</tt> true.
   2.621 +    ///
   2.622 +    ///\return The reached node \c v with <tt>nm[v]</tt> true or
   2.623 +    ///\c INVALID if no such node was found.
   2.624 +    template<class NM>
   2.625 +    Node start(const NM &nm)
   2.626 +    {
   2.627 +      Node rnode = INVALID;
   2.628 +      while ( !emptyQueue() && rnode == INVALID ) {
   2.629 +	processNextNode(nm, rnode);
   2.630 +      }
   2.631 +      return rnode;
   2.632 +    }
   2.633 +    
   2.634 +    ///Runs %BFS algorithm from node \c s.
   2.635 +    
   2.636 +    ///This method runs the %BFS algorithm from a root node \c s
   2.637 +    ///in order to
   2.638 +    ///compute the
   2.639 +    ///shortest path to each node. The algorithm computes
   2.640 +    ///- The shortest path tree.
   2.641 +    ///- The distance of each node from the root.
   2.642 +    ///
   2.643 +    ///\note b.run(s) is just a shortcut of the following code.
   2.644 +    ///\code
   2.645 +    ///  b.init();
   2.646 +    ///  b.addSource(s);
   2.647 +    ///  b.start();
   2.648 +    ///\endcode
   2.649 +    void run(Node s) {
   2.650 +      init();
   2.651 +      addSource(s);
   2.652 +      start();
   2.653 +    }
   2.654 +    
   2.655 +    ///Finds the shortest path between \c s and \c t.
   2.656 +    
   2.657 +    ///Finds the shortest path between \c s and \c t.
   2.658 +    ///
   2.659 +    ///\return The length of the shortest s---t path if there exists one,
   2.660 +    ///0 otherwise.
   2.661 +    ///\note Apart from the return value, b.run(s) is
   2.662 +    ///just a shortcut of the following code.
   2.663 +    ///\code
   2.664 +    ///  b.init();
   2.665 +    ///  b.addSource(s);
   2.666 +    ///  b.start(t);
   2.667 +    ///\endcode
   2.668 +    int run(Node s,Node t) {
   2.669 +      init();
   2.670 +      addSource(s);
   2.671 +      start(t);
   2.672 +      return reached(t) ? _curr_dist : 0;
   2.673 +    }
   2.674 +    
   2.675 +    ///@}
   2.676 +
   2.677 +    ///\name Query Functions
   2.678 +    ///The result of the %BFS algorithm can be obtained using these
   2.679 +    ///functions.\n
   2.680 +    ///Before the use of these functions,
   2.681 +    ///either run() or start() must be calleb.
   2.682 +    
   2.683 +    ///@{
   2.684 +
   2.685 +    typedef PredMapPath<Digraph, PredMap> Path;
   2.686 +
   2.687 +    ///Gives back the shortest path.
   2.688 +    
   2.689 +    ///Gives back the shortest path.
   2.690 +    ///\pre The \c t should be reachable from the source.
   2.691 +    Path path(Node t) 
   2.692 +    {
   2.693 +      return Path(*G, *_pred, t);
   2.694 +    }
   2.695 +
   2.696 +    ///The distance of a node from the root(s).
   2.697 +
   2.698 +    ///Returns the distance of a node from the root(s).
   2.699 +    ///\pre \ref run() must be called before using this function.
   2.700 +    ///\warning If node \c v in unreachable from the root(s) the return value
   2.701 +    ///of this function is undefined.
   2.702 +    int dist(Node v) const { return (*_dist)[v]; }
   2.703 +
   2.704 +    ///Returns the 'previous arc' of the shortest path tree.
   2.705 +
   2.706 +    ///For a node \c v it returns the 'previous arc'
   2.707 +    ///of the shortest path tree,
   2.708 +    ///i.e. it returns the last arc of a shortest path from the root(s) to \c
   2.709 +    ///v. It is \ref INVALID
   2.710 +    ///if \c v is unreachable from the root(s) or \c v is a root. The
   2.711 +    ///shortest path tree used here is equal to the shortest path tree used in
   2.712 +    ///\ref predNode().
   2.713 +    ///\pre Either \ref run() or \ref start() must be called before using
   2.714 +    ///this function.
   2.715 +    Arc predArc(Node v) const { return (*_pred)[v];}
   2.716 +
   2.717 +    ///Returns the 'previous node' of the shortest path tree.
   2.718 +
   2.719 +    ///For a node \c v it returns the 'previous node'
   2.720 +    ///of the shortest path tree,
   2.721 +    ///i.e. it returns the last but one node from a shortest path from the
   2.722 +    ///root(a) to \c /v.
   2.723 +    ///It is INVALID if \c v is unreachable from the root(s) or
   2.724 +    ///if \c v itself a root.
   2.725 +    ///The shortest path tree used here is equal to the shortest path
   2.726 +    ///tree used in \ref predArc().
   2.727 +    ///\pre Either \ref run() or \ref start() must be called before
   2.728 +    ///using this function.
   2.729 +    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   2.730 +				  G->source((*_pred)[v]); }
   2.731 +    
   2.732 +    ///Returns a reference to the NodeMap of distances.
   2.733 +
   2.734 +    ///Returns a reference to the NodeMap of distances.
   2.735 +    ///\pre Either \ref run() or \ref init() must
   2.736 +    ///be called before using this function.
   2.737 +    const DistMap &distMap() const { return *_dist;}
   2.738 + 
   2.739 +    ///Returns a reference to the shortest path tree map.
   2.740 +
   2.741 +    ///Returns a reference to the NodeMap of the arcs of the
   2.742 +    ///shortest path tree.
   2.743 +    ///\pre Either \ref run() or \ref init()
   2.744 +    ///must be called before using this function.
   2.745 +    const PredMap &predMap() const { return *_pred;}
   2.746 + 
   2.747 +    ///Checks if a node is reachable from the root.
   2.748 +
   2.749 +    ///Returns \c true if \c v is reachable from the root.
   2.750 +    ///\warning The source nodes are indicated as unreached.
   2.751 +    ///\pre Either \ref run() or \ref start()
   2.752 +    ///must be called before using this function.
   2.753 +    ///
   2.754 +    bool reached(Node v) { return (*_reached)[v]; }
   2.755 +    
   2.756 +    ///@}
   2.757 +  };
   2.758 +
   2.759 +  ///Default traits class of Bfs function.
   2.760 +
   2.761 +  ///Default traits class of Bfs function.
   2.762 +  ///\param GR Digraph type.
   2.763 +  template<class GR>
   2.764 +  struct BfsWizardDefaultTraits
   2.765 +  {
   2.766 +    ///The digraph type the algorithm runs on. 
   2.767 +    typedef GR Digraph;
   2.768 +    ///\brief The type of the map that stores the last
   2.769 +    ///arcs of the shortest paths.
   2.770 +    /// 
   2.771 +    ///The type of the map that stores the last
   2.772 +    ///arcs of the shortest paths.
   2.773 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   2.774 +    ///
   2.775 +    typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
   2.776 +    ///Instantiates a PredMap.
   2.777 + 
   2.778 +    ///This function instantiates a \ref PredMap. 
   2.779 +    ///\param g is the digraph, to which we would like to define the PredMap.
   2.780 +    ///\todo The digraph alone may be insufficient to initialize
   2.781 +#ifdef DOXYGEN
   2.782 +    static PredMap *createPredMap(const GR &g) 
   2.783 +#else
   2.784 +    static PredMap *createPredMap(const GR &) 
   2.785 +#endif
   2.786 +    {
   2.787 +      return new PredMap();
   2.788 +    }
   2.789 +
   2.790 +    ///The type of the map that indicates which nodes are processed.
   2.791 + 
   2.792 +    ///The type of the map that indicates which nodes are processed.
   2.793 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   2.794 +    ///\todo named parameter to set this type, function to read and write.
   2.795 +    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
   2.796 +    ///Instantiates a ProcessedMap.
   2.797 + 
   2.798 +    ///This function instantiates a \ref ProcessedMap. 
   2.799 +    ///\param g is the digraph, to which
   2.800 +    ///we would like to define the \ref ProcessedMap
   2.801 +#ifdef DOXYGEN
   2.802 +    static ProcessedMap *createProcessedMap(const GR &g)
   2.803 +#else
   2.804 +    static ProcessedMap *createProcessedMap(const GR &)
   2.805 +#endif
   2.806 +    {
   2.807 +      return new ProcessedMap();
   2.808 +    }
   2.809 +    ///The type of the map that indicates which nodes are reached.
   2.810 + 
   2.811 +    ///The type of the map that indicates which nodes are reached.
   2.812 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   2.813 +    ///\todo named parameter to set this type, function to read and write.
   2.814 +    typedef typename Digraph::template NodeMap<bool> ReachedMap;
   2.815 +    ///Instantiates a ReachedMap.
   2.816 + 
   2.817 +    ///This function instantiates a \ref ReachedMap. 
   2.818 +    ///\param G is the digraph, to which
   2.819 +    ///we would like to define the \ref ReachedMap.
   2.820 +    static ReachedMap *createReachedMap(const GR &G)
   2.821 +    {
   2.822 +      return new ReachedMap(G);
   2.823 +    }
   2.824 +    ///The type of the map that stores the dists of the nodes.
   2.825 + 
   2.826 +    ///The type of the map that stores the dists of the nodes.
   2.827 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   2.828 +    ///
   2.829 +    typedef NullMap<typename Digraph::Node,int> DistMap;
   2.830 +    ///Instantiates a DistMap.
   2.831 + 
   2.832 +    ///This function instantiates a \ref DistMap. 
   2.833 +    ///\param g is the digraph, to which we would like to define the \ref DistMap
   2.834 +#ifdef DOXYGEN
   2.835 +    static DistMap *createDistMap(const GR &g)
   2.836 +#else
   2.837 +    static DistMap *createDistMap(const GR &)
   2.838 +#endif
   2.839 +    {
   2.840 +      return new DistMap();
   2.841 +    }
   2.842 +  };
   2.843 +  
   2.844 +  /// Default traits used by \ref BfsWizard
   2.845 +
   2.846 +  /// To make it easier to use Bfs algorithm
   2.847 +  ///we have created a wizard class.
   2.848 +  /// This \ref BfsWizard class needs default traits,
   2.849 +  ///as well as the \ref Bfs class.
   2.850 +  /// The \ref BfsWizardBase is a class to be the default traits of the
   2.851 +  /// \ref BfsWizard class.
   2.852 +  template<class GR>
   2.853 +  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
   2.854 +  {
   2.855 +
   2.856 +    typedef BfsWizardDefaultTraits<GR> Base;
   2.857 +  protected:
   2.858 +    /// Type of the nodes in the digraph.
   2.859 +    typedef typename Base::Digraph::Node Node;
   2.860 +
   2.861 +    /// Pointer to the underlying digraph.
   2.862 +    void *_g;
   2.863 +    ///Pointer to the map of reached nodes.
   2.864 +    void *_reached;
   2.865 +    ///Pointer to the map of processed nodes.
   2.866 +    void *_processed;
   2.867 +    ///Pointer to the map of predecessors arcs.
   2.868 +    void *_pred;
   2.869 +    ///Pointer to the map of distances.
   2.870 +    void *_dist;
   2.871 +    ///Pointer to the source node.
   2.872 +    Node _source;
   2.873 +    
   2.874 +    public:
   2.875 +    /// Constructor.
   2.876 +    
   2.877 +    /// This constructor does not require parameters, therefore it initiates
   2.878 +    /// all of the attributes to default values (0, INVALID).
   2.879 +    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
   2.880 +			   _dist(0), _source(INVALID) {}
   2.881 +
   2.882 +    /// Constructor.
   2.883 +    
   2.884 +    /// This constructor requires some parameters,
   2.885 +    /// listed in the parameters list.
   2.886 +    /// Others are initiated to 0.
   2.887 +    /// \param g is the initial value of  \ref _g
   2.888 +    /// \param s is the initial value of  \ref _source
   2.889 +    BfsWizardBase(const GR &g, Node s=INVALID) :
   2.890 +      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
   2.891 +      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
   2.892 +
   2.893 +  };
   2.894 +  
   2.895 +  /// A class to make the usage of Bfs algorithm easier
   2.896 +
   2.897 +  /// This class is created to make it easier to use Bfs algorithm.
   2.898 +  /// It uses the functions and features of the plain \ref Bfs,
   2.899 +  /// but it is much simpler to use it.
   2.900 +  ///
   2.901 +  /// Simplicity means that the way to change the types defined
   2.902 +  /// in the traits class is based on functions that returns the new class
   2.903 +  /// and not on templatable built-in classes.
   2.904 +  /// When using the plain \ref Bfs
   2.905 +  /// the new class with the modified type comes from
   2.906 +  /// the original class by using the ::
   2.907 +  /// operator. In the case of \ref BfsWizard only
   2.908 +  /// a function have to be called and it will
   2.909 +  /// return the needed class.
   2.910 +  ///
   2.911 +  /// It does not have own \ref run method. When its \ref run method is called
   2.912 +  /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
   2.913 +  /// method of it.
   2.914 +  template<class TR>
   2.915 +  class BfsWizard : public TR
   2.916 +  {
   2.917 +    typedef TR Base;
   2.918 +
   2.919 +    ///The type of the underlying digraph.
   2.920 +    typedef typename TR::Digraph Digraph;
   2.921 +    //\e
   2.922 +    typedef typename Digraph::Node Node;
   2.923 +    //\e
   2.924 +    typedef typename Digraph::NodeIt NodeIt;
   2.925 +    //\e
   2.926 +    typedef typename Digraph::Arc Arc;
   2.927 +    //\e
   2.928 +    typedef typename Digraph::OutArcIt OutArcIt;
   2.929 +    
   2.930 +    ///\brief The type of the map that stores
   2.931 +    ///the reached nodes
   2.932 +    typedef typename TR::ReachedMap ReachedMap;
   2.933 +    ///\brief The type of the map that stores
   2.934 +    ///the processed nodes
   2.935 +    typedef typename TR::ProcessedMap ProcessedMap;
   2.936 +    ///\brief The type of the map that stores the last
   2.937 +    ///arcs of the shortest paths.
   2.938 +    typedef typename TR::PredMap PredMap;
   2.939 +    ///The type of the map that stores the dists of the nodes.
   2.940 +    typedef typename TR::DistMap DistMap;
   2.941 +
   2.942 +  public:
   2.943 +    /// Constructor.
   2.944 +    BfsWizard() : TR() {}
   2.945 +
   2.946 +    /// Constructor that requires parameters.
   2.947 +
   2.948 +    /// Constructor that requires parameters.
   2.949 +    /// These parameters will be the default values for the traits class.
   2.950 +    BfsWizard(const Digraph &g, Node s=INVALID) :
   2.951 +      TR(g,s) {}
   2.952 +
   2.953 +    ///Copy constructor
   2.954 +    BfsWizard(const TR &b) : TR(b) {}
   2.955 +
   2.956 +    ~BfsWizard() {}
   2.957 +
   2.958 +    ///Runs Bfs algorithm from a given node.
   2.959 +    
   2.960 +    ///Runs Bfs algorithm from a given node.
   2.961 +    ///The node can be given by the \ref source function.
   2.962 +    void run()
   2.963 +    {
   2.964 +      if(Base::_source==INVALID) throw UninitializedParameter();
   2.965 +      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   2.966 +      if(Base::_reached)
   2.967 +	alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   2.968 +      if(Base::_processed) 
   2.969 +        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   2.970 +      if(Base::_pred) 
   2.971 +        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   2.972 +      if(Base::_dist) 
   2.973 +        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   2.974 +      alg.run(Base::_source);
   2.975 +    }
   2.976 +
   2.977 +    ///Runs Bfs algorithm from the given node.
   2.978 +
   2.979 +    ///Runs Bfs algorithm from the given node.
   2.980 +    ///\param s is the given source.
   2.981 +    void run(Node s)
   2.982 +    {
   2.983 +      Base::_source=s;
   2.984 +      run();
   2.985 +    }
   2.986 +
   2.987 +    template<class T>
   2.988 +    struct DefPredMapBase : public Base {
   2.989 +      typedef T PredMap;
   2.990 +      static PredMap *createPredMap(const Digraph &) { return 0; };
   2.991 +      DefPredMapBase(const TR &b) : TR(b) {}
   2.992 +    };
   2.993 +    
   2.994 +    ///\brief \ref named-templ-param "Named parameter"
   2.995 +    ///function for setting PredMap
   2.996 +    ///
   2.997 +    /// \ref named-templ-param "Named parameter"
   2.998 +    ///function for setting PredMap
   2.999 +    ///
  2.1000 +    template<class T>
  2.1001 +    BfsWizard<DefPredMapBase<T> > predMap(const T &t) 
  2.1002 +    {
  2.1003 +      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  2.1004 +      return BfsWizard<DefPredMapBase<T> >(*this);
  2.1005 +    }
  2.1006 +    
  2.1007 + 
  2.1008 +    template<class T>
  2.1009 +    struct DefReachedMapBase : public Base {
  2.1010 +      typedef T ReachedMap;
  2.1011 +      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
  2.1012 +      DefReachedMapBase(const TR &b) : TR(b) {}
  2.1013 +    };
  2.1014 +    
  2.1015 +    ///\brief \ref named-templ-param "Named parameter"
  2.1016 +    ///function for setting ReachedMap
  2.1017 +    ///
  2.1018 +    /// \ref named-templ-param "Named parameter"
  2.1019 +    ///function for setting ReachedMap
  2.1020 +    ///
  2.1021 +    template<class T>
  2.1022 +    BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
  2.1023 +    {
  2.1024 +      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  2.1025 +      return BfsWizard<DefReachedMapBase<T> >(*this);
  2.1026 +    }
  2.1027 +    
  2.1028 +
  2.1029 +    template<class T>
  2.1030 +    struct DefProcessedMapBase : public Base {
  2.1031 +      typedef T ProcessedMap;
  2.1032 +      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
  2.1033 +      DefProcessedMapBase(const TR &b) : TR(b) {}
  2.1034 +    };
  2.1035 +    
  2.1036 +    ///\brief \ref named-templ-param "Named parameter"
  2.1037 +    ///function for setting ProcessedMap
  2.1038 +    ///
  2.1039 +    /// \ref named-templ-param "Named parameter"
  2.1040 +    ///function for setting ProcessedMap
  2.1041 +    ///
  2.1042 +    template<class T>
  2.1043 +    BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
  2.1044 +    {
  2.1045 +      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  2.1046 +      return BfsWizard<DefProcessedMapBase<T> >(*this);
  2.1047 +    }
  2.1048 +    
  2.1049 +   
  2.1050 +    template<class T>
  2.1051 +    struct DefDistMapBase : public Base {
  2.1052 +      typedef T DistMap;
  2.1053 +      static DistMap *createDistMap(const Digraph &) { return 0; };
  2.1054 +      DefDistMapBase(const TR &b) : TR(b) {}
  2.1055 +    };
  2.1056 +    
  2.1057 +    ///\brief \ref named-templ-param "Named parameter"
  2.1058 +    ///function for setting DistMap type
  2.1059 +    ///
  2.1060 +    /// \ref named-templ-param "Named parameter"
  2.1061 +    ///function for setting DistMap type
  2.1062 +    ///
  2.1063 +    template<class T>
  2.1064 +    BfsWizard<DefDistMapBase<T> > distMap(const T &t) 
  2.1065 +    {
  2.1066 +      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
  2.1067 +      return BfsWizard<DefDistMapBase<T> >(*this);
  2.1068 +    }
  2.1069 +    
  2.1070 +    /// Sets the source node, from which the Bfs algorithm runs.
  2.1071 +
  2.1072 +    /// Sets the source node, from which the Bfs algorithm runs.
  2.1073 +    /// \param s is the source node.
  2.1074 +    BfsWizard<TR> &source(Node s) 
  2.1075 +    {
  2.1076 +      Base::_source=s;
  2.1077 +      return *this;
  2.1078 +    }
  2.1079 +    
  2.1080 +  };
  2.1081 +  
  2.1082 +  ///Function type interface for Bfs algorithm.
  2.1083 +
  2.1084 +  /// \ingroup search
  2.1085 +  ///Function type interface for Bfs algorithm.
  2.1086 +  ///
  2.1087 +  ///This function also has several
  2.1088 +  ///\ref named-templ-func-param "named parameters",
  2.1089 +  ///they are declared as the members of class \ref BfsWizard.
  2.1090 +  ///The following
  2.1091 +  ///example shows how to use these parameters.
  2.1092 +  ///\code
  2.1093 +  ///  bfs(g,source).predMap(preds).run();
  2.1094 +  ///\endcode
  2.1095 +  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
  2.1096 +  ///to the end of the parameter list.
  2.1097 +  ///\sa BfsWizard
  2.1098 +  ///\sa Bfs
  2.1099 +  template<class GR>
  2.1100 +  BfsWizard<BfsWizardBase<GR> >
  2.1101 +  bfs(const GR &g,typename GR::Node s=INVALID)
  2.1102 +  {
  2.1103 +    return BfsWizard<BfsWizardBase<GR> >(g,s);
  2.1104 +  }
  2.1105 +
  2.1106 +#ifdef DOXYGEN
  2.1107 +  /// \brief Visitor class for bfs.
  2.1108 +  ///  
  2.1109 +  /// This class defines the interface of the BfsVisit events, and
  2.1110 +  /// it could be the base of a real Visitor class.
  2.1111 +  template <typename _Digraph>
  2.1112 +  struct BfsVisitor {
  2.1113 +    typedef _Digraph Digraph;
  2.1114 +    typedef typename Digraph::Arc Arc;
  2.1115 +    typedef typename Digraph::Node Node;
  2.1116 +    /// \brief Called when the arc reach a node.
  2.1117 +    /// 
  2.1118 +    /// It is called when the bfs find an arc which target is not
  2.1119 +    /// reached yet.
  2.1120 +    void discover(const Arc& arc) {}
  2.1121 +    /// \brief Called when the node reached first time.
  2.1122 +    /// 
  2.1123 +    /// It is Called when the node reached first time.
  2.1124 +    void reach(const Node& node) {}
  2.1125 +    /// \brief Called when the arc examined but target of the arc 
  2.1126 +    /// already discovered.
  2.1127 +    /// 
  2.1128 +    /// It called when the arc examined but the target of the arc 
  2.1129 +    /// already discovered.
  2.1130 +    void examine(const Arc& arc) {}
  2.1131 +    /// \brief Called for the source node of the bfs.
  2.1132 +    /// 
  2.1133 +    /// It is called for the source node of the bfs.
  2.1134 +    void start(const Node& node) {}
  2.1135 +    /// \brief Called when the node processed.
  2.1136 +    /// 
  2.1137 +    /// It is Called when the node processed.
  2.1138 +    void process(const Node& node) {}
  2.1139 +  };
  2.1140 +#else
  2.1141 +  template <typename _Digraph>
  2.1142 +  struct BfsVisitor {
  2.1143 +    typedef _Digraph Digraph;
  2.1144 +    typedef typename Digraph::Arc Arc;
  2.1145 +    typedef typename Digraph::Node Node;
  2.1146 +    void discover(const Arc&) {}
  2.1147 +    void reach(const Node&) {}
  2.1148 +    void examine(const Arc&) {}
  2.1149 +    void start(const Node&) {}
  2.1150 +    void process(const Node&) {}
  2.1151 +
  2.1152 +    template <typename _Visitor>
  2.1153 +    struct Constraints {
  2.1154 +      void constraints() {
  2.1155 +	Arc arc;
  2.1156 +	Node node;
  2.1157 +	visitor.discover(arc);
  2.1158 +	visitor.reach(node);
  2.1159 +	visitor.examine(arc);
  2.1160 +	visitor.start(node);
  2.1161 +        visitor.process(node);
  2.1162 +      }
  2.1163 +      _Visitor& visitor;
  2.1164 +    };
  2.1165 +  };
  2.1166 +#endif
  2.1167 +
  2.1168 +  /// \brief Default traits class of BfsVisit class.
  2.1169 +  ///
  2.1170 +  /// Default traits class of BfsVisit class.
  2.1171 +  /// \param _Digraph Digraph type.
  2.1172 +  template<class _Digraph>
  2.1173 +  struct BfsVisitDefaultTraits {
  2.1174 +
  2.1175 +    /// \brief The digraph type the algorithm runs on. 
  2.1176 +    typedef _Digraph Digraph;
  2.1177 +
  2.1178 +    /// \brief The type of the map that indicates which nodes are reached.
  2.1179 +    /// 
  2.1180 +    /// The type of the map that indicates which nodes are reached.
  2.1181 +    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
  2.1182 +    /// \todo named parameter to set this type, function to read and write.
  2.1183 +    typedef typename Digraph::template NodeMap<bool> ReachedMap;
  2.1184 +
  2.1185 +    /// \brief Instantiates a ReachedMap.
  2.1186 +    ///
  2.1187 +    /// This function instantiates a \ref ReachedMap. 
  2.1188 +    /// \param digraph is the digraph, to which
  2.1189 +    /// we would like to define the \ref ReachedMap.
  2.1190 +    static ReachedMap *createReachedMap(const Digraph &digraph) {
  2.1191 +      return new ReachedMap(digraph);
  2.1192 +    }
  2.1193 +
  2.1194 +  };
  2.1195 +
  2.1196 +  /// \ingroup search
  2.1197 +  ///  
  2.1198 +  /// \brief %BFS Visit algorithm class.
  2.1199 +  ///  
  2.1200 +  /// This class provides an efficient implementation of the %BFS algorithm
  2.1201 +  /// with visitor interface.
  2.1202 +  ///
  2.1203 +  /// The %BfsVisit class provides an alternative interface to the Bfs
  2.1204 +  /// class. It works with callback mechanism, the BfsVisit object calls
  2.1205 +  /// on every bfs event the \c Visitor class member functions. 
  2.1206 +  ///
  2.1207 +  /// \param _Digraph The digraph type the algorithm runs on. The default value is
  2.1208 +  /// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it
  2.1209 +  /// is only passed to \ref BfsDefaultTraits.
  2.1210 +  /// \param _Visitor The Visitor object for the algorithm. The 
  2.1211 +  /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which
  2.1212 +  /// does not observe the Bfs events. If you want to observe the bfs
  2.1213 +  /// events you should implement your own Visitor class.
  2.1214 +  /// \param _Traits Traits class to set various data types used by the 
  2.1215 +  /// algorithm. The default traits class is
  2.1216 +  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
  2.1217 +  /// See \ref BfsVisitDefaultTraits for the documentation of
  2.1218 +  /// a Bfs visit traits class.
  2.1219 +  ///
  2.1220 +  /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
  2.1221 +#ifdef DOXYGEN
  2.1222 +  template <typename _Digraph, typename _Visitor, typename _Traits>
  2.1223 +#else
  2.1224 +  template <typename _Digraph = ListDigraph,
  2.1225 +	    typename _Visitor = BfsVisitor<_Digraph>,
  2.1226 +	    typename _Traits = BfsDefaultTraits<_Digraph> >
  2.1227 +#endif
  2.1228 +  class BfsVisit {
  2.1229 +  public:
  2.1230 +    
  2.1231 +    /// \brief \ref Exception for uninitialized parameters.
  2.1232 +    ///
  2.1233 +    /// This error represents problems in the initialization
  2.1234 +    /// of the parameters of the algorithms.
  2.1235 +    class UninitializedParameter : public lemon::UninitializedParameter {
  2.1236 +    public:
  2.1237 +      virtual const char* what() const throw() 
  2.1238 +      {
  2.1239 +	return "lemon::BfsVisit::UninitializedParameter";
  2.1240 +      }
  2.1241 +    };
  2.1242 +
  2.1243 +    typedef _Traits Traits;
  2.1244 +
  2.1245 +    typedef typename Traits::Digraph Digraph;
  2.1246 +
  2.1247 +    typedef _Visitor Visitor;
  2.1248 +
  2.1249 +    ///The type of the map indicating which nodes are reached.
  2.1250 +    typedef typename Traits::ReachedMap ReachedMap;
  2.1251 +
  2.1252 +  private:
  2.1253 +
  2.1254 +    typedef typename Digraph::Node Node;
  2.1255 +    typedef typename Digraph::NodeIt NodeIt;
  2.1256 +    typedef typename Digraph::Arc Arc;
  2.1257 +    typedef typename Digraph::OutArcIt OutArcIt;
  2.1258 +
  2.1259 +    /// Pointer to the underlying digraph.
  2.1260 +    const Digraph *_digraph;
  2.1261 +    /// Pointer to the visitor object.
  2.1262 +    Visitor *_visitor;
  2.1263 +    ///Pointer to the map of reached status of the nodes.
  2.1264 +    ReachedMap *_reached;
  2.1265 +    ///Indicates if \ref _reached is locally allocated (\c true) or not.
  2.1266 +    bool local_reached;
  2.1267 +
  2.1268 +    std::vector<typename Digraph::Node> _list;
  2.1269 +    int _list_front, _list_back;
  2.1270 +
  2.1271 +    /// \brief Creates the maps if necessary.
  2.1272 +    ///
  2.1273 +    /// Creates the maps if necessary.
  2.1274 +    void create_maps() {
  2.1275 +      if(!_reached) {
  2.1276 +	local_reached = true;
  2.1277 +	_reached = Traits::createReachedMap(*_digraph);
  2.1278 +      }
  2.1279 +    }
  2.1280 +
  2.1281 +  protected:
  2.1282 +
  2.1283 +    BfsVisit() {}
  2.1284 +    
  2.1285 +  public:
  2.1286 +
  2.1287 +    typedef BfsVisit Create;
  2.1288 +
  2.1289 +    /// \name Named template parameters
  2.1290 +
  2.1291 +    ///@{
  2.1292 +    template <class T>
  2.1293 +    struct DefReachedMapTraits : public Traits {
  2.1294 +      typedef T ReachedMap;
  2.1295 +      static ReachedMap *createReachedMap(const Digraph &digraph) {
  2.1296 +	throw UninitializedParameter();
  2.1297 +      }
  2.1298 +    };
  2.1299 +    /// \brief \ref named-templ-param "Named parameter" for setting 
  2.1300 +    /// ReachedMap type
  2.1301 +    ///
  2.1302 +    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
  2.1303 +    template <class T>
  2.1304 +    struct DefReachedMap : public BfsVisit< Digraph, Visitor,
  2.1305 +					    DefReachedMapTraits<T> > {
  2.1306 +      typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
  2.1307 +    };
  2.1308 +    ///@}
  2.1309 +
  2.1310 +  public:      
  2.1311 +    
  2.1312 +    /// \brief Constructor.
  2.1313 +    ///
  2.1314 +    /// Constructor.
  2.1315 +    ///
  2.1316 +    /// \param digraph the digraph the algorithm will run on.
  2.1317 +    /// \param visitor The visitor of the algorithm.
  2.1318 +    ///
  2.1319 +    BfsVisit(const Digraph& digraph, Visitor& visitor) 
  2.1320 +      : _digraph(&digraph), _visitor(&visitor),
  2.1321 +	_reached(0), local_reached(false) {}
  2.1322 +    
  2.1323 +    /// \brief Destructor.
  2.1324 +    ///
  2.1325 +    /// Destructor.
  2.1326 +    ~BfsVisit() {
  2.1327 +      if(local_reached) delete _reached;
  2.1328 +    }
  2.1329 +
  2.1330 +    /// \brief Sets the map indicating if a node is reached.
  2.1331 +    ///
  2.1332 +    /// Sets the map indicating if a node is reached.
  2.1333 +    /// If you don't use this function before calling \ref run(),
  2.1334 +    /// it will allocate one. The destuctor deallocates this
  2.1335 +    /// automatically allocated map, of course.
  2.1336 +    /// \return <tt> (*this) </tt>
  2.1337 +    BfsVisit &reachedMap(ReachedMap &m) {
  2.1338 +      if(local_reached) {
  2.1339 +	delete _reached;
  2.1340 +	local_reached = false;
  2.1341 +      }
  2.1342 +      _reached = &m;
  2.1343 +      return *this;
  2.1344 +    }
  2.1345 +
  2.1346 +  public:
  2.1347 +    /// \name Execution control
  2.1348 +    /// The simplest way to execute the algorithm is to use
  2.1349 +    /// one of the member functions called \c run(...).
  2.1350 +    /// \n
  2.1351 +    /// If you need more control on the execution,
  2.1352 +    /// first you must call \ref init(), then you can adda source node
  2.1353 +    /// with \ref addSource().
  2.1354 +    /// Finally \ref start() will perform the actual path
  2.1355 +    /// computation.
  2.1356 +
  2.1357 +    /// @{
  2.1358 +    /// \brief Initializes the internal data structures.
  2.1359 +    ///
  2.1360 +    /// Initializes the internal data structures.
  2.1361 +    ///
  2.1362 +    void init() {
  2.1363 +      create_maps();
  2.1364 +      _list.resize(countNodes(*_digraph));
  2.1365 +      _list_front = _list_back = -1;
  2.1366 +      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
  2.1367 +	_reached->set(u, false);
  2.1368 +      }
  2.1369 +    }
  2.1370 +    
  2.1371 +    /// \brief Adds a new source node.
  2.1372 +    ///
  2.1373 +    /// Adds a new source node to the set of nodes to be processed.
  2.1374 +    void addSource(Node s) {
  2.1375 +      if(!(*_reached)[s]) {
  2.1376 +	  _reached->set(s,true);
  2.1377 +	  _visitor->start(s);
  2.1378 +	  _visitor->reach(s);
  2.1379 +          _list[++_list_back] = s;
  2.1380 +	}
  2.1381 +    }
  2.1382 +    
  2.1383 +    /// \brief Processes the next node.
  2.1384 +    ///
  2.1385 +    /// Processes the next node.
  2.1386 +    ///
  2.1387 +    /// \return The processed node.
  2.1388 +    ///
  2.1389 +    /// \pre The queue must not be empty!
  2.1390 +    Node processNextNode() { 
  2.1391 +      Node n = _list[++_list_front];
  2.1392 +      _visitor->process(n);
  2.1393 +      Arc e;
  2.1394 +      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
  2.1395 +        Node m = _digraph->target(e);
  2.1396 +        if (!(*_reached)[m]) {
  2.1397 +          _visitor->discover(e);
  2.1398 +          _visitor->reach(m);
  2.1399 +          _reached->set(m, true);
  2.1400 +          _list[++_list_back] = m;
  2.1401 +        } else {
  2.1402 +          _visitor->examine(e);
  2.1403 +        }
  2.1404 +      }
  2.1405 +      return n;
  2.1406 +    }
  2.1407 +
  2.1408 +    /// \brief Processes the next node.
  2.1409 +    ///
  2.1410 +    /// Processes the next node. And checks that the given target node
  2.1411 +    /// is reached. If the target node is reachable from the processed
  2.1412 +    /// node then the reached parameter will be set true. The reached
  2.1413 +    /// parameter should be initially false.
  2.1414 +    ///
  2.1415 +    /// \param target The target node.
  2.1416 +    /// \retval reach Indicates that the target node is reached.
  2.1417 +    /// \return The processed node.
  2.1418 +    ///
  2.1419 +    /// \warning The queue must not be empty!
  2.1420 +    Node processNextNode(Node target, bool& reach) {
  2.1421 +      Node n = _list[++_list_front];
  2.1422 +      _visitor->process(n);
  2.1423 +      Arc e;
  2.1424 +      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
  2.1425 +        Node m = _digraph->target(e);
  2.1426 +        if (!(*_reached)[m]) {
  2.1427 +          _visitor->discover(e);
  2.1428 +          _visitor->reach(m);
  2.1429 +          _reached->set(m, true);
  2.1430 +          _list[++_list_back] = m;
  2.1431 +          reach = reach || (target == m);
  2.1432 +        } else {
  2.1433 +          _visitor->examine(e);
  2.1434 +        }
  2.1435 +      }
  2.1436 +      return n;
  2.1437 +    }
  2.1438 +
  2.1439 +    /// \brief Processes the next node.
  2.1440 +    ///
  2.1441 +    /// Processes the next node. And checks that at least one of
  2.1442 +    /// reached node has true value in the \c nm node map. If one node
  2.1443 +    /// with true value is reachable from the processed node then the
  2.1444 +    /// rnode parameter will be set to the first of such nodes.
  2.1445 +    ///
  2.1446 +    /// \param nm The node map of possible targets.
  2.1447 +    /// \retval rnode The reached target node.
  2.1448 +    /// \return The processed node.
  2.1449 +    ///
  2.1450 +    /// \warning The queue must not be empty!
  2.1451 +    template <typename NM>
  2.1452 +    Node processNextNode(const NM& nm, Node& rnode) {
  2.1453 +      Node n = _list[++_list_front];
  2.1454 +      _visitor->process(n);
  2.1455 +      Arc e;
  2.1456 +      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
  2.1457 +        Node m = _digraph->target(e);
  2.1458 +        if (!(*_reached)[m]) {
  2.1459 +          _visitor->discover(e);
  2.1460 +          _visitor->reach(m);
  2.1461 +          _reached->set(m, true);
  2.1462 +          _list[++_list_back] = m;
  2.1463 +          if (nm[m] && rnode == INVALID) rnode = m;
  2.1464 +        } else {
  2.1465 +          _visitor->examine(e);
  2.1466 +        }
  2.1467 +      }
  2.1468 +      return n;
  2.1469 +    }
  2.1470 +
  2.1471 +    /// \brief Next node to be processed.
  2.1472 +    ///
  2.1473 +    /// Next node to be processed.
  2.1474 +    ///
  2.1475 +    /// \return The next node to be processed or INVALID if the stack is
  2.1476 +    /// empty.
  2.1477 +    Node nextNode() { 
  2.1478 +      return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
  2.1479 +    }
  2.1480 +
  2.1481 +    /// \brief Returns \c false if there are nodes
  2.1482 +    /// to be processed in the queue
  2.1483 +    ///
  2.1484 +    /// Returns \c false if there are nodes
  2.1485 +    /// to be processed in the queue
  2.1486 +    bool emptyQueue() { return _list_front == _list_back; }
  2.1487 +
  2.1488 +    /// \brief Returns the number of the nodes to be processed.
  2.1489 +    ///
  2.1490 +    /// Returns the number of the nodes to be processed in the queue.
  2.1491 +    int queueSize() { return _list_back - _list_front; }
  2.1492 +    
  2.1493 +    /// \brief Executes the algorithm.
  2.1494 +    ///
  2.1495 +    /// Executes the algorithm.
  2.1496 +    ///
  2.1497 +    /// \pre init() must be called and at least one node should be added
  2.1498 +    /// with addSource() before using this function.
  2.1499 +    void start() {
  2.1500 +      while ( !emptyQueue() ) processNextNode();
  2.1501 +    }
  2.1502 +    
  2.1503 +    /// \brief Executes the algorithm until \c dest is reached.
  2.1504 +    ///
  2.1505 +    /// Executes the algorithm until \c dest is reached.
  2.1506 +    ///
  2.1507 +    /// \pre init() must be called and at least one node should be added
  2.1508 +    /// with addSource() before using this function.
  2.1509 +    void start(Node dest) {
  2.1510 +      bool reach = false;
  2.1511 +      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
  2.1512 +    }
  2.1513 +    
  2.1514 +    /// \brief Executes the algorithm until a condition is met.
  2.1515 +    ///
  2.1516 +    /// Executes the algorithm until a condition is met.
  2.1517 +    ///
  2.1518 +    /// \pre init() must be called and at least one node should be added
  2.1519 +    /// with addSource() before using this function.
  2.1520 +    ///
  2.1521 +    ///\param nm must be a bool (or convertible) node map. The
  2.1522 +    ///algorithm will stop when it reaches a node \c v with
  2.1523 +    /// <tt>nm[v]</tt> true.
  2.1524 +    ///
  2.1525 +    ///\return The reached node \c v with <tt>nm[v]</tt> true or
  2.1526 +    ///\c INVALID if no such node was found.
  2.1527 +    template <typename NM>
  2.1528 +    Node start(const NM &nm) {
  2.1529 +      Node rnode = INVALID;
  2.1530 +      while ( !emptyQueue() && rnode == INVALID ) {
  2.1531 +	processNextNode(nm, rnode);
  2.1532 +      }
  2.1533 +      return rnode;
  2.1534 +    }
  2.1535 +
  2.1536 +    /// \brief Runs %BFSVisit algorithm from node \c s.
  2.1537 +    ///
  2.1538 +    /// This method runs the %BFS algorithm from a root node \c s.
  2.1539 +    /// \note b.run(s) is just a shortcut of the following code.
  2.1540 +    ///\code
  2.1541 +    ///   b.init();
  2.1542 +    ///   b.addSource(s);
  2.1543 +    ///   b.start();
  2.1544 +    ///\endcode
  2.1545 +    void run(Node s) {
  2.1546 +      init();
  2.1547 +      addSource(s);
  2.1548 +      start();
  2.1549 +    }
  2.1550 +
  2.1551 +    /// \brief Runs %BFSVisit algorithm to visit all nodes in the digraph.
  2.1552 +    ///    
  2.1553 +    /// This method runs the %BFS algorithm in order to
  2.1554 +    /// compute the %BFS path to each node. The algorithm computes
  2.1555 +    /// - The %BFS tree.
  2.1556 +    /// - The distance of each node from the root in the %BFS tree.
  2.1557 +    ///
  2.1558 +    ///\note b.run() is just a shortcut of the following code.
  2.1559 +    ///\code
  2.1560 +    ///  b.init();
  2.1561 +    ///  for (NodeIt it(digraph); it != INVALID; ++it) {
  2.1562 +    ///    if (!b.reached(it)) {
  2.1563 +    ///      b.addSource(it);
  2.1564 +    ///      b.start();
  2.1565 +    ///    }
  2.1566 +    ///  }
  2.1567 +    ///\endcode
  2.1568 +    void run() {
  2.1569 +      init();
  2.1570 +      for (NodeIt it(*_digraph); it != INVALID; ++it) {
  2.1571 +        if (!reached(it)) {
  2.1572 +          addSource(it);
  2.1573 +          start();
  2.1574 +        }
  2.1575 +      }
  2.1576 +    }
  2.1577 +    ///@}
  2.1578 +
  2.1579 +    /// \name Query Functions
  2.1580 +    /// The result of the %BFS algorithm can be obtained using these
  2.1581 +    /// functions.\n
  2.1582 +    /// Before the use of these functions,
  2.1583 +    /// either run() or start() must be called.
  2.1584 +    ///@{
  2.1585 +
  2.1586 +    /// \brief Checks if a node is reachable from the root.
  2.1587 +    ///
  2.1588 +    /// Returns \c true if \c v is reachable from the root(s).
  2.1589 +    /// \warning The source nodes are inditated as unreachable.
  2.1590 +    /// \pre Either \ref run() or \ref start()
  2.1591 +    /// must be called before using this function.
  2.1592 +    ///
  2.1593 +    bool reached(Node v) { return (*_reached)[v]; }
  2.1594 +    ///@}
  2.1595 +  };
  2.1596 +
  2.1597 +} //END OF NAMESPACE LEMON
  2.1598 +
  2.1599 +#endif
  2.1600 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/lemon/bin_heap.h	Thu Mar 20 12:12:24 2008 +0000
     3.3 @@ -0,0 +1,346 @@
     3.4 +/* -*- C++ -*-
     3.5 + *
     3.6 + * This file is a part of LEMON, a generic C++ optimization library
     3.7 + *
     3.8 + * Copyright (C) 2003-2008
     3.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    3.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    3.11 + *
    3.12 + * Permission to use, modify and distribute this software is granted
    3.13 + * provided that this copyright notice appears in all copies. For
    3.14 + * precise terms see the accompanying LICENSE file.
    3.15 + *
    3.16 + * This software is provided "AS IS" with no warranty of any kind,
    3.17 + * express or implied, and with no claim as to its suitability for any
    3.18 + * purpose.
    3.19 + *
    3.20 + */
    3.21 +
    3.22 +#ifndef LEMON_BIN_HEAP_H
    3.23 +#define LEMON_BIN_HEAP_H
    3.24 +
    3.25 +///\ingroup auxdat
    3.26 +///\file
    3.27 +///\brief Binary Heap implementation.
    3.28 +
    3.29 +#include <vector>
    3.30 +#include <utility>
    3.31 +#include <functional>
    3.32 +
    3.33 +namespace lemon {
    3.34 +
    3.35 +  ///\ingroup auxdat
    3.36 +  ///
    3.37 +  ///\brief A Binary Heap implementation.
    3.38 +  ///
    3.39 +  ///This class implements the \e binary \e heap data structure. A \e heap
    3.40 +  ///is a data structure for storing items with specified values called \e
    3.41 +  ///priorities in such a way that finding the item with minimum priority is
    3.42 +  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
    3.43 +  ///one can change the priority of an item, add or erase an item, etc.
    3.44 +  ///
    3.45 +  ///\param _Prio Type of the priority of the items.
    3.46 +  ///\param _ItemIntMap A read and writable Item int map, used internally
    3.47 +  ///to handle the cross references.
    3.48 +  ///\param _Compare A class for the ordering of the priorities. The
    3.49 +  ///default is \c std::less<_Prio>.
    3.50 +  ///
    3.51 +  ///\sa FibHeap
    3.52 +  ///\sa Dijkstra
    3.53 +  template <typename _Prio, typename _ItemIntMap,
    3.54 +	    typename _Compare = std::less<_Prio> >
    3.55 +  class BinHeap {
    3.56 +
    3.57 +  public:
    3.58 +    ///\e
    3.59 +    typedef _ItemIntMap ItemIntMap;
    3.60 +    ///\e
    3.61 +    typedef _Prio Prio;
    3.62 +    ///\e
    3.63 +    typedef typename ItemIntMap::Key Item;
    3.64 +    ///\e
    3.65 +    typedef std::pair<Item,Prio> Pair;
    3.66 +    ///\e
    3.67 +    typedef _Compare Compare;
    3.68 +
    3.69 +    /// \brief Type to represent the items states.
    3.70 +    ///
    3.71 +    /// Each Item element have a state associated to it. It may be "in heap",
    3.72 +    /// "pre heap" or "post heap". The latter two are indifferent from the
    3.73 +    /// heap's point of view, but may be useful to the user.
    3.74 +    ///
    3.75 +    /// The ItemIntMap \e should be initialized in such way that it maps
    3.76 +    /// PRE_HEAP (-1) to any element to be put in the heap...
    3.77 +    enum State {
    3.78 +      IN_HEAP = 0,
    3.79 +      PRE_HEAP = -1,
    3.80 +      POST_HEAP = -2
    3.81 +    };
    3.82 +
    3.83 +  private:
    3.84 +    std::vector<Pair> data;
    3.85 +    Compare comp;
    3.86 +    ItemIntMap &iim;
    3.87 +
    3.88 +  public:
    3.89 +    /// \brief The constructor.
    3.90 +    ///
    3.91 +    /// The constructor.
    3.92 +    /// \param _iim should be given to the constructor, since it is used
    3.93 +    /// internally to handle the cross references. The value of the map
    3.94 +    /// should be PRE_HEAP (-1) for each element.
    3.95 +    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
    3.96 +    
    3.97 +    /// \brief The constructor.
    3.98 +    ///
    3.99 +    /// The constructor.
   3.100 +    /// \param _iim should be given to the constructor, since it is used
   3.101 +    /// internally to handle the cross references. The value of the map
   3.102 +    /// should be PRE_HEAP (-1) for each element.
   3.103 +    ///
   3.104 +    /// \param _comp The comparator function object.
   3.105 +    BinHeap(ItemIntMap &_iim, const Compare &_comp) 
   3.106 +      : iim(_iim), comp(_comp) {}
   3.107 +
   3.108 +
   3.109 +    /// The number of items stored in the heap.
   3.110 +    ///
   3.111 +    /// \brief Returns the number of items stored in the heap.
   3.112 +    int size() const { return data.size(); }
   3.113 +    
   3.114 +    /// \brief Checks if the heap stores no items.
   3.115 +    ///
   3.116 +    /// Returns \c true if and only if the heap stores no items.
   3.117 +    bool empty() const { return data.empty(); }
   3.118 +
   3.119 +    /// \brief Make empty this heap.
   3.120 +    /// 
   3.121 +    /// Make empty this heap. It does not change the cross reference map.
   3.122 +    /// If you want to reuse what is not surely empty you should first clear
   3.123 +    /// the heap and after that you should set the cross reference map for
   3.124 +    /// each item to \c PRE_HEAP.
   3.125 +    void clear() { 
   3.126 +      data.clear(); 
   3.127 +    }
   3.128 +
   3.129 +  private:
   3.130 +    static int parent(int i) { return (i-1)/2; }
   3.131 +
   3.132 +    static int second_child(int i) { return 2*i+2; }
   3.133 +    bool less(const Pair &p1, const Pair &p2) const {
   3.134 +      return comp(p1.second, p2.second);
   3.135 +    }
   3.136 +
   3.137 +    int bubble_up(int hole, Pair p) {
   3.138 +      int par = parent(hole);
   3.139 +      while( hole>0 && less(p,data[par]) ) {
   3.140 +	move(data[par],hole);
   3.141 +	hole = par;
   3.142 +	par = parent(hole);
   3.143 +      }
   3.144 +      move(p, hole);
   3.145 +      return hole;
   3.146 +    }
   3.147 +
   3.148 +    int bubble_down(int hole, Pair p, int length) {
   3.149 +      int child = second_child(hole);
   3.150 +      while(child < length) {
   3.151 +	if( less(data[child-1], data[child]) ) {
   3.152 +	  --child;
   3.153 +	}
   3.154 +	if( !less(data[child], p) )
   3.155 +	  goto ok;
   3.156 +	move(data[child], hole);
   3.157 +	hole = child;
   3.158 +	child = second_child(hole);
   3.159 +      }
   3.160 +      child--;
   3.161 +      if( child<length && less(data[child], p) ) {
   3.162 +	move(data[child], hole);
   3.163 +	hole=child;
   3.164 +      }
   3.165 +    ok:
   3.166 +      move(p, hole);
   3.167 +      return hole;
   3.168 +    }
   3.169 +
   3.170 +    void move(const Pair &p, int i) {
   3.171 +      data[i] = p;
   3.172 +      iim.set(p.first, i);
   3.173 +    }
   3.174 +
   3.175 +  public:
   3.176 +    /// \brief Insert a pair of item and priority into the heap.
   3.177 +    ///
   3.178 +    /// Adds \c p.first to the heap with priority \c p.second.
   3.179 +    /// \param p The pair to insert.
   3.180 +    void push(const Pair &p) {
   3.181 +      int n = data.size();
   3.182 +      data.resize(n+1);
   3.183 +      bubble_up(n, p);
   3.184 +    }
   3.185 +
   3.186 +    /// \brief Insert an item into the heap with the given heap.
   3.187 +    ///    
   3.188 +    /// Adds \c i to the heap with priority \c p. 
   3.189 +    /// \param i The item to insert.
   3.190 +    /// \param p The priority of the item.
   3.191 +    void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
   3.192 +
   3.193 +    /// \brief Returns the item with minimum priority relative to \c Compare.
   3.194 +    ///
   3.195 +    /// This method returns the item with minimum priority relative to \c
   3.196 +    /// Compare.  
   3.197 +    /// \pre The heap must be nonempty.  
   3.198 +    Item top() const {
   3.199 +      return data[0].first;
   3.200 +    }
   3.201 +
   3.202 +    /// \brief Returns the minimum priority relative to \c Compare.
   3.203 +    ///
   3.204 +    /// It returns the minimum priority relative to \c Compare.
   3.205 +    /// \pre The heap must be nonempty.
   3.206 +    Prio prio() const {
   3.207 +      return data[0].second;
   3.208 +    }
   3.209 +
   3.210 +    /// \brief Deletes the item with minimum priority relative to \c Compare.
   3.211 +    ///
   3.212 +    /// This method deletes the item with minimum priority relative to \c
   3.213 +    /// Compare from the heap.  
   3.214 +    /// \pre The heap must be non-empty.  
   3.215 +    void pop() {
   3.216 +      int n = data.size()-1;
   3.217 +      iim.set(data[0].first, POST_HEAP);
   3.218 +      if (n > 0) {
   3.219 +	bubble_down(0, data[n], n);
   3.220 +      }
   3.221 +      data.pop_back();
   3.222 +    }
   3.223 +
   3.224 +    /// \brief Deletes \c i from the heap.
   3.225 +    ///
   3.226 +    /// This method deletes item \c i from the heap.
   3.227 +    /// \param i The item to erase.
   3.228 +    /// \pre The item should be in the heap.
   3.229 +    void erase(const Item &i) {
   3.230 +      int h = iim[i];
   3.231 +      int n = data.size()-1;
   3.232 +      iim.set(data[h].first, POST_HEAP);
   3.233 +      if( h < n ) {
   3.234 +	if ( bubble_up(h, data[n]) == h) {
   3.235 +	  bubble_down(h, data[n], n);
   3.236 +	}
   3.237 +      }
   3.238 +      data.pop_back();
   3.239 +    }
   3.240 +
   3.241 +    
   3.242 +    /// \brief Returns the priority of \c i.
   3.243 +    ///
   3.244 +    /// This function returns the priority of item \c i.  
   3.245 +    /// \pre \c i must be in the heap.
   3.246 +    /// \param i The item.
   3.247 +    Prio operator[](const Item &i) const {
   3.248 +      int idx = iim[i];
   3.249 +      return data[idx].second;
   3.250 +    }
   3.251 +
   3.252 +    /// \brief \c i gets to the heap with priority \c p independently 
   3.253 +    /// if \c i was already there.
   3.254 +    ///
   3.255 +    /// This method calls \ref push(\c i, \c p) if \c i is not stored
   3.256 +    /// in the heap and sets the priority of \c i to \c p otherwise.
   3.257 +    /// \param i The item.
   3.258 +    /// \param p The priority.
   3.259 +    void set(const Item &i, const Prio &p) {
   3.260 +      int idx = iim[i];
   3.261 +      if( idx < 0 ) {
   3.262 +	push(i,p);
   3.263 +      }
   3.264 +      else if( comp(p, data[idx].second) ) {
   3.265 +	bubble_up(idx, Pair(i,p));
   3.266 +      }
   3.267 +      else {
   3.268 +	bubble_down(idx, Pair(i,p), data.size());
   3.269 +      }
   3.270 +    }
   3.271 +
   3.272 +    /// \brief Decreases the priority of \c i to \c p.
   3.273 +    ///
   3.274 +    /// This method decreases the priority of item \c i to \c p.
   3.275 +    /// \pre \c i must be stored in the heap with priority at least \c
   3.276 +    /// p relative to \c Compare.
   3.277 +    /// \param i The item.
   3.278 +    /// \param p The priority.
   3.279 +    void decrease(const Item &i, const Prio &p) {
   3.280 +      int idx = iim[i];
   3.281 +      bubble_up(idx, Pair(i,p));
   3.282 +    }
   3.283 +    
   3.284 +    /// \brief Increases the priority of \c i to \c p.
   3.285 +    ///
   3.286 +    /// This method sets the priority of item \c i to \c p. 
   3.287 +    /// \pre \c i must be stored in the heap with priority at most \c
   3.288 +    /// p relative to \c Compare.
   3.289 +    /// \param i The item.
   3.290 +    /// \param p The priority.
   3.291 +    void increase(const Item &i, const Prio &p) {
   3.292 +      int idx = iim[i];
   3.293 +      bubble_down(idx, Pair(i,p), data.size());
   3.294 +    }
   3.295 +
   3.296 +    /// \brief Returns if \c item is in, has already been in, or has 
   3.297 +    /// never been in the heap.
   3.298 +    ///
   3.299 +    /// This method returns PRE_HEAP if \c item has never been in the
   3.300 +    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   3.301 +    /// otherwise. In the latter case it is possible that \c item will
   3.302 +    /// get back to the heap again.
   3.303 +    /// \param i The item.
   3.304 +    State state(const Item &i) const {
   3.305 +      int s = iim[i];
   3.306 +      if( s>=0 )
   3.307 +	s=0;
   3.308 +      return State(s);
   3.309 +    }
   3.310 +
   3.311 +    /// \brief Sets the state of the \c item in the heap.
   3.312 +    ///
   3.313 +    /// Sets the state of the \c item in the heap. It can be used to
   3.314 +    /// manually clear the heap when it is important to achive the
   3.315 +    /// better time complexity.
   3.316 +    /// \param i The item.
   3.317 +    /// \param st The state. It should not be \c IN_HEAP. 
   3.318 +    void state(const Item& i, State st) {
   3.319 +      switch (st) {
   3.320 +      case POST_HEAP:
   3.321 +      case PRE_HEAP:
   3.322 +        if (state(i) == IN_HEAP) {
   3.323 +          erase(i);
   3.324 +        }
   3.325 +        iim[i] = st;
   3.326 +        break;
   3.327 +      case IN_HEAP:
   3.328 +        break;
   3.329 +      }
   3.330 +    }
   3.331 +
   3.332 +    /// \brief Replaces an item in the heap.
   3.333 +    ///
   3.334 +    /// The \c i item is replaced with \c j item. The \c i item should
   3.335 +    /// be in the heap, while the \c j should be out of the heap. The
   3.336 +    /// \c i item will out of the heap and \c j will be in the heap
   3.337 +    /// with the same prioriority as prevoiusly the \c i item.
   3.338 +    void replace(const Item& i, const Item& j) {
   3.339 +      int idx = iim[i];
   3.340 +      iim.set(i, iim[j]);
   3.341 +      iim.set(j, idx);
   3.342 +      data[idx].first = j;
   3.343 +    }
   3.344 +
   3.345 +  }; // class BinHeap
   3.346 +  
   3.347 +} // namespace lemon
   3.348 +
   3.349 +#endif // LEMON_BIN_HEAP_H
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/lemon/bits/path_dump.h	Thu Mar 20 12:12:24 2008 +0000
     4.3 @@ -0,0 +1,174 @@
     4.4 +/* -*- C++ -*-
     4.5 + *
     4.6 + * This file is a part of LEMON, a generic C++ optimization library
     4.7 + *
     4.8 + * Copyright (C) 2003-2008
     4.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    4.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    4.11 + *
    4.12 + * Permission to use, modify and distribute this software is granted
    4.13 + * provided that this copyright notice appears in all copies. For
    4.14 + * precise terms see the accompanying LICENSE file.
    4.15 + *
    4.16 + * This software is provided "AS IS" with no warranty of any kind,
    4.17 + * express or implied, and with no claim as to its suitability for any
    4.18 + * purpose.
    4.19 + *
    4.20 + */
    4.21 +
    4.22 +#ifndef LEMON_BITS_PRED_MAP_PATH_H
    4.23 +#define LEMON_BITS_PRED_MAP_PATH_H
    4.24 +
    4.25 +namespace lemon {
    4.26 +
    4.27 +  template <typename _Digraph, typename _PredMap>
    4.28 +  class PredMapPath {
    4.29 +  public:
    4.30 +    typedef True RevPathTag;
    4.31 +
    4.32 +    typedef _Digraph Digraph;
    4.33 +    typedef typename Digraph::Arc Arc;
    4.34 +    typedef _PredMap PredMap;
    4.35 +
    4.36 +    PredMapPath(const Digraph& _digraph, const PredMap& _predMap,
    4.37 +                typename Digraph::Node _target)
    4.38 +      : digraph(_digraph), predMap(_predMap), target(_target) {}
    4.39 +
    4.40 +    int length() const {
    4.41 +      int len = 0;
    4.42 +      typename Digraph::Node node = target;
    4.43 +      typename Digraph::Arc arc;
    4.44 +      while ((arc = predMap[node]) != INVALID) {
    4.45 +        node = digraph.source(arc);
    4.46 +        ++len;
    4.47 +      }
    4.48 +      return len;
    4.49 +    }
    4.50 +
    4.51 +    bool empty() const {
    4.52 +      return predMap[target] != INVALID;
    4.53 +    }
    4.54 +
    4.55 +    class RevArcIt {
    4.56 +    public:
    4.57 +      RevArcIt() {}
    4.58 +      RevArcIt(Invalid) : path(0), current(INVALID) {}
    4.59 +      RevArcIt(const PredMapPath& _path) 
    4.60 +        : path(&_path), current(_path.target) {
    4.61 +        if (path->predMap[current] == INVALID) current = INVALID;
    4.62 +      }
    4.63 +
    4.64 +      operator const typename Digraph::Arc() const {
    4.65 +        return path->predMap[current];
    4.66 +      }
    4.67 +
    4.68 +      RevArcIt& operator++() {
    4.69 +        current = path->digraph.source(path->predMap[current]);
    4.70 +        if (path->predMap[current] == INVALID) current = INVALID;
    4.71 +        return *this;
    4.72 +      }
    4.73 +
    4.74 +      bool operator==(const RevArcIt& e) const { 
    4.75 +        return current == e.current; 
    4.76 +      }
    4.77 +
    4.78 +      bool operator!=(const RevArcIt& e) const {
    4.79 +        return current != e.current; 
    4.80 +      }
    4.81 +
    4.82 +      bool operator<(const RevArcIt& e) const { 
    4.83 +        return current < e.current; 
    4.84 +      }
    4.85 +      
    4.86 +    private:
    4.87 +      const PredMapPath* path;
    4.88 +      typename Digraph::Node current;
    4.89 +    };
    4.90 +
    4.91 +  private:
    4.92 +    const Digraph& digraph;
    4.93 +    const PredMap& predMap;
    4.94 +    typename Digraph::Node target;
    4.95 +  };
    4.96 +
    4.97 +
    4.98 +  template <typename _Digraph, typename _PredMatrixMap>
    4.99 +  class PredMatrixMapPath {
   4.100 +  public:
   4.101 +    typedef True RevPathTag;
   4.102 +
   4.103 +    typedef _Digraph Digraph;
   4.104 +    typedef typename Digraph::Arc Arc;
   4.105 +    typedef _PredMatrixMap PredMatrixMap;
   4.106 +
   4.107 +    PredMatrixMapPath(const Digraph& _digraph, 
   4.108 +                      const PredMatrixMap& _predMatrixMap,
   4.109 +                      typename Digraph::Node _source, 
   4.110 +                      typename Digraph::Node _target)
   4.111 +      : digraph(_digraph), predMatrixMap(_predMatrixMap), 
   4.112 +        source(_source), target(_target) {}
   4.113 +
   4.114 +    int length() const {
   4.115 +      int len = 0;
   4.116 +      typename Digraph::Node node = target;
   4.117 +      typename Digraph::Arc arc;
   4.118 +      while ((arc = predMatrixMap(source, node)) != INVALID) {
   4.119 +        node = digraph.source(arc);
   4.120 +        ++len;
   4.121 +      }
   4.122 +      return len;
   4.123 +    }
   4.124 +
   4.125 +    bool empty() const {
   4.126 +      return source != target;
   4.127 +    }
   4.128 +
   4.129 +    class RevArcIt {
   4.130 +    public:
   4.131 +      RevArcIt() {}
   4.132 +      RevArcIt(Invalid) : path(0), current(INVALID) {}
   4.133 +      RevArcIt(const PredMatrixMapPath& _path) 
   4.134 +        : path(&_path), current(_path.target) {
   4.135 +        if (path->predMatrixMap(path->source, current) == INVALID) 
   4.136 +          current = INVALID;
   4.137 +      }
   4.138 +
   4.139 +      operator const typename Digraph::Arc() const {
   4.140 +        return path->predMatrixMap(path->source, current);
   4.141 +      }
   4.142 +
   4.143 +      RevArcIt& operator++() {
   4.144 +        current = 
   4.145 +          path->digraph.source(path->predMatrixMap(path->source, current));
   4.146 +        if (path->predMatrixMap(path->source, current) == INVALID) 
   4.147 +          current = INVALID;
   4.148 +        return *this;
   4.149 +      }
   4.150 +
   4.151 +      bool operator==(const RevArcIt& e) const { 
   4.152 +        return current == e.current; 
   4.153 +      }
   4.154 +
   4.155 +      bool operator!=(const RevArcIt& e) const {
   4.156 +        return current != e.current; 
   4.157 +      }
   4.158 +
   4.159 +      bool operator<(const RevArcIt& e) const { 
   4.160 +        return current < e.current; 
   4.161 +      }
   4.162 +      
   4.163 +    private:
   4.164 +      const PredMatrixMapPath* path;
   4.165 +      typename Digraph::Node current;
   4.166 +    };
   4.167 +
   4.168 +  private:
   4.169 +    const Digraph& digraph;
   4.170 +    const PredMatrixMap& predMatrixMap;
   4.171 +    typename Digraph::Node source;
   4.172 +    typename Digraph::Node target;
   4.173 +  };
   4.174 +
   4.175 +}
   4.176 +
   4.177 +#endif
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/lemon/concepts/heap.h	Thu Mar 20 12:12:24 2008 +0000
     5.3 @@ -0,0 +1,226 @@
     5.4 +/* -*- C++ -*-
     5.5 + *
     5.6 + * This file is a part of LEMON, a generic C++ optimization library
     5.7 + *
     5.8 + * Copyright (C) 2003-2008
     5.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    5.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    5.11 + *
    5.12 + * Permission to use, modify and distribute this software is granted
    5.13 + * provided that this copyright notice appears in all copies. For
    5.14 + * precise terms see the accompanying LICENSE file.
    5.15 + *
    5.16 + * This software is provided "AS IS" with no warranty of any kind,
    5.17 + * express or implied, and with no claim as to its suitability for any
    5.18 + * purpose.
    5.19 + *
    5.20 + */
    5.21 +
    5.22 +///\ingroup concept
    5.23 +///\file
    5.24 +///\brief Classes for representing heaps.
    5.25 +///
    5.26 +
    5.27 +#ifndef LEMON_CONCEPT_HEAP_H
    5.28 +#define LEMON_CONCEPT_HEAP_H
    5.29 +
    5.30 +#include <lemon/bits/invalid.h>
    5.31 +
    5.32 +namespace lemon {
    5.33 +  namespace concepts {
    5.34 +    /// \addtogroup concept
    5.35 +    /// @{
    5.36 +
    5.37 +
    5.38 +    /// \brief A concept structure describes the main interface of heaps.
    5.39 +    ///
    5.40 +    /// A concept structure describes the main interface of heaps.
    5.41 +    ///
    5.42 +    template <typename Prio, typename ItemIntMap>
    5.43 +    class Heap {
    5.44 +    public:
    5.45 +
    5.46 +      ///\brief Type of the items stored in the heap.
    5.47 +      typedef typename ItemIntMap::Key  Item;
    5.48 +  
    5.49 +
    5.50 +      /// \brief Type to represent the items states.
    5.51 +      ///
    5.52 +      /// Each Item element have a state associated to it. It may be "in heap",
    5.53 +      /// "pre heap" or "post heap". The later two are indifferent from the
    5.54 +      /// heap's point of view, but may be useful to the user.
    5.55 +      ///
    5.56 +      /// The ItemIntMap _should_ be initialized in such way, that it maps
    5.57 +      /// PRE_HEAP (-1) to any element to be put in the heap...
    5.58 +      enum State {
    5.59 +	IN_HEAP = 0,
    5.60 +	PRE_HEAP = -1,
    5.61 +	POST_HEAP = -2
    5.62 +      };
    5.63 +      
    5.64 +      /// \brief The constructor.
    5.65 +      ///
    5.66 +      /// The constructor.
    5.67 +      /// \param _iim should be given to the constructor, since it is used
    5.68 +      /// internally to handle the cross references. The value of the map
    5.69 +      /// should be PRE_HEAP (-1) for each element.
    5.70 +      explicit Heap(ItemIntMap &_iim) {}
    5.71 +
    5.72 +      /// \brief The number of items stored in the heap.
    5.73 +      ///
    5.74 +      /// Returns the number of items stored in the heap.
    5.75 +      int size() const { return 0; }
    5.76 +
    5.77 +      /// \brief Checks if the heap stores no items.
    5.78 +      ///
    5.79 +      /// Returns \c true if and only if the heap stores no items.
    5.80 +      bool empty() const { return false; }
    5.81 +
    5.82 +      /// \brief Makes empty this heap.
    5.83 +      ///
    5.84 +      /// Makes this heap empty.
    5.85 +      void clear();
    5.86 +
    5.87 +      /// \brief Insert an item into the heap with the given heap.
    5.88 +      ///    
    5.89 +      /// Adds \c i to the heap with priority \c p. 
    5.90 +      /// \param i The item to insert.
    5.91 +      /// \param p The priority of the item.
    5.92 +      void push(const Item &i, const Prio &p) {}
    5.93 +
    5.94 +      /// \brief Returns the item with minimum priority.
    5.95 +      ///
    5.96 +      /// This method returns the item with minimum priority.  
    5.97 +      /// \pre The heap must be nonempty.  
    5.98 +      Item top() const {}
    5.99 +
   5.100 +      /// \brief Returns the minimum priority.
   5.101 +      ///
   5.102 +      /// It returns the minimum priority.
   5.103 +      /// \pre The heap must be nonempty.
   5.104 +      Prio prio() const {}
   5.105 +
   5.106 +      /// \brief Deletes the item with minimum priority.
   5.107 +      ///
   5.108 +      /// This method deletes the item with minimum priority.
   5.109 +      /// \pre The heap must be non-empty.  
   5.110 +      void pop() {}
   5.111 +
   5.112 +      /// \brief Deletes \c i from the heap.
   5.113 +      ///
   5.114 +      /// This method deletes item \c i from the heap, if \c i was
   5.115 +      /// already stored in the heap.
   5.116 +      /// \param i The item to erase. 
   5.117 +      void erase(const Item &i) {}
   5.118 +
   5.119 +      /// \brief Returns the priority of \c i.
   5.120 +      ///
   5.121 +      /// This function returns the priority of item \c i.  
   5.122 +      /// \pre \c i must be in the heap.
   5.123 +      /// \param i The item.
   5.124 +      Prio operator[](const Item &i) const {}
   5.125 +
   5.126 +      /// \brief \c i gets to the heap with priority \c p independently 
   5.127 +      /// if \c i was already there.
   5.128 +      ///
   5.129 +      /// This method calls \ref push(\c i, \c p) if \c i is not stored
   5.130 +      /// in the heap and sets the priority of \c i to \c p otherwise.
   5.131 +      /// It may throw an \e UnderFlowPriorityException. 
   5.132 +      /// \param i The item.
   5.133 +      /// \param p The priority.
   5.134 +      void set(const Item &i, const Prio &p) {}
   5.135 +      
   5.136 +      /// \brief Decreases the priority of \c i to \c p.
   5.137 +      ///
   5.138 +      /// This method decreases the priority of item \c i to \c p.
   5.139 +      /// \pre \c i must be stored in the heap with priority at least \c p.
   5.140 +      /// \param i The item.
   5.141 +      /// \param p The priority.
   5.142 +      void decrease(const Item &i, const Prio &p) {}
   5.143 +
   5.144 +      /// \brief Increases the priority of \c i to \c p.
   5.145 +      ///
   5.146 +      /// This method sets the priority of item \c i to \c p. 
   5.147 +      /// \pre \c i must be stored in the heap with priority at most \c
   5.148 +      /// p relative to \c Compare.
   5.149 +      /// \param i The item.
   5.150 +      /// \param p The priority.
   5.151 +      void increase(const Item &i, const Prio &p) {}
   5.152 +
   5.153 +      /// \brief Returns if \c item is in, has already been in, or has 
   5.154 +      /// never been in the heap.
   5.155 +      ///
   5.156 +      /// This method returns PRE_HEAP if \c item has never been in the
   5.157 +      /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   5.158 +      /// otherwise. In the latter case it is possible that \c item will
   5.159 +      /// get back to the heap again.
   5.160 +      /// \param i The item.
   5.161 +      State state(const Item &i) const {}
   5.162 +
   5.163 +      /// \brief Sets the state of the \c item in the heap.
   5.164 +      ///
   5.165 +      /// Sets the state of the \c item in the heap. It can be used to
   5.166 +      /// manually clear the heap when it is important to achive the
   5.167 +      /// better time complexity.
   5.168 +      /// \param i The item.
   5.169 +      /// \param st The state. It should not be \c IN_HEAP. 
   5.170 +      void state(const Item& i, State st) {}
   5.171 +
   5.172 +
   5.173 +      template <typename _Heap>
   5.174 +      struct Constraints {
   5.175 +      public:
   5.176 +    
   5.177 +	void constraints() {
   5.178 +	  Item item;
   5.179 +	  Prio prio;
   5.180 +
   5.181 +	  item=Item();
   5.182 +	  prio=Prio();
   5.183 +
   5.184 +	  ignore_unused_variable_warning(item);
   5.185 +	  ignore_unused_variable_warning(prio);
   5.186 +
   5.187 +	  typedef typename _Heap::State State;
   5.188 +	  State state;
   5.189 +
   5.190 +	  ignore_unused_variable_warning(state);
   5.191 +      
   5.192 +	  _Heap heap1 = _Heap(map);
   5.193 +
   5.194 +	  ignore_unused_variable_warning(heap1);
   5.195 +      
   5.196 +	  heap.push(item, prio);
   5.197 +
   5.198 +	  prio = heap.prio();
   5.199 +	  item = heap.top();
   5.200 +
   5.201 +	  heap.pop();
   5.202 +
   5.203 +	  heap.set(item, prio);
   5.204 +	  heap.decrease(item, prio);
   5.205 +	  heap.increase(item, prio);
   5.206 +	  prio = heap[item];
   5.207 +
   5.208 +	  heap.erase(item);
   5.209 +
   5.210 +	  state = heap.state(item);
   5.211 +
   5.212 +	  state = _Heap::PRE_HEAP;
   5.213 +	  state = _Heap::IN_HEAP;
   5.214 +	  state = _Heap::POST_HEAP;
   5.215 +
   5.216 +	  heap.clear();
   5.217 +	}
   5.218 +    
   5.219 +	_Heap& heap;
   5.220 +	ItemIntMap& map;
   5.221 +
   5.222 +	Constraints() : heap(0), map(0) {}
   5.223 +      };
   5.224 +    };
   5.225 +
   5.226 +    /// @}
   5.227 +  } // namespace lemon
   5.228 +}
   5.229 +#endif // LEMON_CONCEPT_PATH_H
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/lemon/concepts/path.h	Thu Mar 20 12:12:24 2008 +0000
     6.3 @@ -0,0 +1,307 @@
     6.4 +/* -*- C++ -*-
     6.5 + *
     6.6 + * This file is a part of LEMON, a generic C++ optimization library
     6.7 + *
     6.8 + * Copyright (C) 2003-2008
     6.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    6.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    6.11 + *
    6.12 + * Permission to use, modify and distribute this software is granted
    6.13 + * provided that this copyright notice appears in all copies. For
    6.14 + * precise terms see the accompanying LICENSE file.
    6.15 + *
    6.16 + * This software is provided "AS IS" with no warranty of any kind,
    6.17 + * express or implied, and with no claim as to its suitability for any
    6.18 + * purpose.
    6.19 + *
    6.20 + */
    6.21 +
    6.22 +///\ingroup concept
    6.23 +///\file
    6.24 +///\brief Classes for representing paths in digraphs.
    6.25 +///
    6.26 +///\todo Iterators have obsolete style
    6.27 +
    6.28 +#ifndef LEMON_CONCEPT_PATH_H
    6.29 +#define LEMON_CONCEPT_PATH_H
    6.30 +
    6.31 +#include <lemon/bits/invalid.h>
    6.32 +#include <lemon/bits/utility.h>
    6.33 +#include <lemon/concept_check.h>
    6.34 +
    6.35 +namespace lemon {
    6.36 +  namespace concepts {
    6.37 +
    6.38 +    /// \addtogroup concept
    6.39 +    /// @{
    6.40 +
    6.41 +    /// \brief A skeleton structure for representing directed paths in
    6.42 +    /// a digraph.
    6.43 +    ///
    6.44 +    /// A skeleton structure for representing directed paths in a
    6.45 +    /// digraph.  
    6.46 +    /// \param _Digraph The digraph type in which the path is.
    6.47 +    ///
    6.48 +    /// In a sense, the path can be treated as a list of arcs. The
    6.49 +    /// lemon path type stores just this list. As a consequence it
    6.50 +    /// cannot enumerate the nodes in the path and the zero length
    6.51 +    /// paths cannot store the source.
    6.52 +    ///
    6.53 +    template <typename _Digraph>
    6.54 +    class Path {
    6.55 +    public:
    6.56 +
    6.57 +      /// Type of the underlying digraph.
    6.58 +      typedef _Digraph Digraph;
    6.59 +      /// Arc type of the underlying digraph.
    6.60 +      typedef typename Digraph::Arc Arc;
    6.61 +
    6.62 +      class ArcIt;
    6.63 +
    6.64 +      /// \brief Default constructor
    6.65 +      Path() {}
    6.66 +
    6.67 +      /// \brief Template constructor
    6.68 +      template <typename CPath>
    6.69 +      Path(const CPath& cpath) {}
    6.70 +
    6.71 +      /// \brief Template assigment
    6.72 +      template <typename CPath>
    6.73 +      Path& operator=(const CPath& cpath) {}
    6.74 +
    6.75 +      /// Length of the path ie. the number of arcs in the path.
    6.76 +      int length() const { return 0;}
    6.77 +
    6.78 +      /// Returns whether the path is empty.
    6.79 +      bool empty() const { return true;}
    6.80 +
    6.81 +      /// Resets the path to an empty path.
    6.82 +      void clear() {}
    6.83 +
    6.84 +      /// \brief Lemon style iterator for path arcs
    6.85 +      ///
    6.86 +      /// This class is used to iterate on the arcs of the paths.
    6.87 +      class ArcIt {
    6.88 +      public:
    6.89 +	/// Default constructor
    6.90 +	ArcIt() {}
    6.91 +	/// Invalid constructor
    6.92 +	ArcIt(Invalid) {}
    6.93 +	/// Constructor for first arc
    6.94 +	ArcIt(const Path &) {}
    6.95 +
    6.96 +        /// Conversion to Arc
    6.97 +	operator Arc() const { return INVALID; }
    6.98 +
    6.99 +	/// Next arc
   6.100 +	ArcIt& operator++() {return *this;}
   6.101 +
   6.102 +	/// Comparison operator
   6.103 +	bool operator==(const ArcIt&) const {return true;}
   6.104 +	/// Comparison operator
   6.105 +	bool operator!=(const ArcIt&) const {return true;}
   6.106 + 	/// Comparison operator
   6.107 + 	bool operator<(const ArcIt&) const {return false;}
   6.108 +
   6.109 +      };
   6.110 +
   6.111 +      template <typename _Path>
   6.112 +      struct Constraints {
   6.113 +        void constraints() {
   6.114 +          Path<Digraph> pc;
   6.115 +          _Path p, pp(pc);
   6.116 +          int l = p.length();
   6.117 +          int e = p.empty();
   6.118 +          p.clear();
   6.119 +
   6.120 +          p = pc;
   6.121 +
   6.122 +          typename _Path::ArcIt id, ii(INVALID), i(p);
   6.123 +
   6.124 +          ++i;
   6.125 +          typename Digraph::Arc ed = i;
   6.126 +
   6.127 +          e = (i == ii);
   6.128 +          e = (i != ii);
   6.129 +          e = (i < ii);
   6.130 +
   6.131 +          ignore_unused_variable_warning(l);
   6.132 +          ignore_unused_variable_warning(pp);
   6.133 +          ignore_unused_variable_warning(e);
   6.134 +          ignore_unused_variable_warning(id);
   6.135 +          ignore_unused_variable_warning(ii);
   6.136 +          ignore_unused_variable_warning(ed);
   6.137 +        }
   6.138 +      };
   6.139 +
   6.140 +    };
   6.141 +
   6.142 +    namespace _path_bits {
   6.143 +      
   6.144 +      template <typename _Digraph, typename _Path, typename RevPathTag = void>
   6.145 +      struct PathDumperConstraints {
   6.146 +        void constraints() {
   6.147 +          int l = p.length();
   6.148 +          int e = p.empty();
   6.149 +
   6.150 +          typename _Path::ArcIt id, i(p);
   6.151 +
   6.152 +          ++i;
   6.153 +          typename _Digraph::Arc ed = i;
   6.154 +
   6.155 +          e = (i == INVALID);
   6.156 +          e = (i != INVALID);
   6.157 +
   6.158 +          ignore_unused_variable_warning(l);
   6.159 +          ignore_unused_variable_warning(e);
   6.160 +          ignore_unused_variable_warning(id);
   6.161 +          ignore_unused_variable_warning(ed);
   6.162 +        }
   6.163 +        _Path& p;
   6.164 +      };
   6.165 +
   6.166 +      template <typename _Digraph, typename _Path>
   6.167 +      struct PathDumperConstraints<
   6.168 +        _Digraph, _Path, 
   6.169 +        typename enable_if<typename _Path::RevPathTag, void>::type
   6.170 +      > {
   6.171 +        void constraints() {
   6.172 +          int l = p.length();
   6.173 +          int e = p.empty();
   6.174 +
   6.175 +          typename _Path::RevArcIt id, i(p);
   6.176 +
   6.177 +          ++i;
   6.178 +          typename _Digraph::Arc ed = i;
   6.179 +
   6.180 +          e = (i == INVALID);
   6.181 +          e = (i != INVALID);
   6.182 +
   6.183 +          ignore_unused_variable_warning(l);
   6.184 +          ignore_unused_variable_warning(e);
   6.185 +          ignore_unused_variable_warning(id);
   6.186 +          ignore_unused_variable_warning(ed);
   6.187 +        }
   6.188 +        _Path& p;
   6.189 +      };
   6.190 +    
   6.191 +    }
   6.192 +
   6.193 +
   6.194 +    /// \brief A skeleton structure for path dumpers.
   6.195 +    ///
   6.196 +    /// A skeleton structure for path dumpers. The path dumpers are
   6.197 +    /// the generalization of the paths. The path dumpers can
   6.198 +    /// enumerate the arcs of the path wheter in forward or in
   6.199 +    /// backward order.  In most time these classes are not used
   6.200 +    /// directly rather it used to assign a dumped class to a real
   6.201 +    /// path type.
   6.202 +    ///
   6.203 +    /// The main purpose of this concept is that the shortest path
   6.204 +    /// algorithms can enumerate easily the arcs in reverse order.
   6.205 +    /// If we would like to give back a real path from these
   6.206 +    /// algorithms then we should create a temporarly path object. In
   6.207 +    /// Lemon such algorithms gives back a path dumper what can
   6.208 +    /// assigned to a real path and the dumpers can be implemented as
   6.209 +    /// an adaptor class to the predecessor map.
   6.210 +
   6.211 +    /// \param _Digraph  The digraph type in which the path is.
   6.212 +    ///
   6.213 +    /// The paths can be constructed from any path type by a
   6.214 +    /// template constructor or a template assignment operator.
   6.215 +    /// 
   6.216 +    template <typename _Digraph>
   6.217 +    class PathDumper {
   6.218 +    public:
   6.219 +
   6.220 +      /// Type of the underlying digraph.
   6.221 +      typedef _Digraph Digraph;
   6.222 +      /// Arc type of the underlying digraph.
   6.223 +      typedef typename Digraph::Arc Arc;
   6.224 +
   6.225 +      /// Length of the path ie. the number of arcs in the path.
   6.226 +      int length() const { return 0;}
   6.227 +
   6.228 +      /// Returns whether the path is empty.
   6.229 +      bool empty() const { return true;}
   6.230 +
   6.231 +      /// \brief Forward or reverse dumping
   6.232 +      ///
   6.233 +      /// If the RevPathTag is defined and true then reverse dumping
   6.234 +      /// is provided in the path dumper. In this case instead of the
   6.235 +      /// ArcIt the RevArcIt iterator should be implemented in the
   6.236 +      /// dumper.
   6.237 +      typedef False RevPathTag;
   6.238 +
   6.239 +      /// \brief Lemon style iterator for path arcs
   6.240 +      ///
   6.241 +      /// This class is used to iterate on the arcs of the paths.
   6.242 +      class ArcIt {
   6.243 +      public:
   6.244 +	/// Default constructor
   6.245 +	ArcIt() {}
   6.246 +	/// Invalid constructor
   6.247 +	ArcIt(Invalid) {}
   6.248 +	/// Constructor for first arc
   6.249 +	ArcIt(const PathDumper&) {}
   6.250 +
   6.251 +        /// Conversion to Arc
   6.252 +	operator Arc() const { return INVALID; }
   6.253 +
   6.254 +	/// Next arc
   6.255 +	ArcIt& operator++() {return *this;}
   6.256 +
   6.257 +	/// Comparison operator
   6.258 +	bool operator==(const ArcIt&) const {return true;}
   6.259 +	/// Comparison operator
   6.260 +	bool operator!=(const ArcIt&) const {return true;}
   6.261 + 	/// Comparison operator
   6.262 + 	bool operator<(const ArcIt&) const {return false;}
   6.263 +
   6.264 +      };
   6.265 +
   6.266 +      /// \brief Lemon style iterator for path arcs
   6.267 +      ///
   6.268 +      /// This class is used to iterate on the arcs of the paths in
   6.269 +      /// reverse direction.
   6.270 +      class RevArcIt {
   6.271 +      public:
   6.272 +	/// Default constructor
   6.273 +	RevArcIt() {}
   6.274 +	/// Invalid constructor
   6.275 +	RevArcIt(Invalid) {}
   6.276 +	/// Constructor for first arc
   6.277 +	RevArcIt(const PathDumper &) {}
   6.278 +
   6.279 +        /// Conversion to Arc
   6.280 +	operator Arc() const { return INVALID; }
   6.281 +
   6.282 +	/// Next arc
   6.283 +	RevArcIt& operator++() {return *this;}
   6.284 +
   6.285 +	/// Comparison operator
   6.286 +	bool operator==(const RevArcIt&) const {return true;}
   6.287 +	/// Comparison operator
   6.288 +	bool operator!=(const RevArcIt&) const {return true;}
   6.289 + 	/// Comparison operator
   6.290 + 	bool operator<(const RevArcIt&) const {return false;}
   6.291 +
   6.292 +      };
   6.293 +
   6.294 +      template <typename _Path>
   6.295 +      struct Constraints {
   6.296 +        void constraints() {
   6.297 +          function_requires<_path_bits::
   6.298 +            PathDumperConstraints<Digraph, _Path> >();
   6.299 +        }
   6.300 +      };
   6.301 +
   6.302 +    };
   6.303 +
   6.304 +
   6.305 +    ///@}
   6.306 +  }
   6.307 +
   6.308 +} // namespace lemon
   6.309 +
   6.310 +#endif // LEMON_CONCEPT_PATH_H
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/lemon/dfs.h	Thu Mar 20 12:12:24 2008 +0000
     7.3 @@ -0,0 +1,1543 @@
     7.4 +/* -*- C++ -*-
     7.5 + *
     7.6 + * This file is a part of LEMON, a generic C++ optimization library
     7.7 + *
     7.8 + * Copyright (C) 2003-2008
     7.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    7.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    7.11 + *
    7.12 + * Permission to use, modify and distribute this software is granted
    7.13 + * provided that this copyright notice appears in all copies. For
    7.14 + * precise terms see the accompanying LICENSE file.
    7.15 + *
    7.16 + * This software is provided "AS IS" with no warranty of any kind,
    7.17 + * express or implied, and with no claim as to its suitability for any
    7.18 + * purpose.
    7.19 + *
    7.20 + */
    7.21 +
    7.22 +#ifndef LEMON_DFS_H
    7.23 +#define LEMON_DFS_H
    7.24 +
    7.25 +///\ingroup search
    7.26 +///\file
    7.27 +///\brief Dfs algorithm.
    7.28 +
    7.29 +#include <lemon/list_graph.h>
    7.30 +#include <lemon/graph_utils.h>
    7.31 +#include <lemon/bits/path_dump.h>
    7.32 +#include <lemon/bits/invalid.h>
    7.33 +#include <lemon/error.h>
    7.34 +#include <lemon/maps.h>
    7.35 +
    7.36 +#include <lemon/concept_check.h>
    7.37 +
    7.38 +namespace lemon {
    7.39 +
    7.40 +  
    7.41 +  ///Default traits class of Dfs class.
    7.42 +
    7.43 +  ///Default traits class of Dfs class.
    7.44 +  ///\param GR Digraph type.
    7.45 +  template<class GR>
    7.46 +  struct DfsDefaultTraits
    7.47 +  {
    7.48 +    ///The digraph type the algorithm runs on. 
    7.49 +    typedef GR Digraph;
    7.50 +    ///\brief The type of the map that stores the last
    7.51 +    ///arcs of the %DFS paths.
    7.52 +    /// 
    7.53 +    ///The type of the map that stores the last
    7.54 +    ///arcs of the %DFS paths.
    7.55 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    7.56 +    ///
    7.57 +    typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap;
    7.58 +    ///Instantiates a PredMap.
    7.59 + 
    7.60 +    ///This function instantiates a \ref PredMap. 
    7.61 +    ///\param G is the digraph, to which we would like to define the PredMap.
    7.62 +    ///\todo The digraph alone may be insufficient to initialize
    7.63 +    static PredMap *createPredMap(const GR &G) 
    7.64 +    {
    7.65 +      return new PredMap(G);
    7.66 +    }
    7.67 +
    7.68 +    ///The type of the map that indicates which nodes are processed.
    7.69 + 
    7.70 +    ///The type of the map that indicates which nodes are processed.
    7.71 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    7.72 +    ///\todo named parameter to set this type, function to read and write.
    7.73 +    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    7.74 +    ///Instantiates a ProcessedMap.
    7.75 + 
    7.76 +    ///This function instantiates a \ref ProcessedMap. 
    7.77 +    ///\param g is the digraph, to which
    7.78 +    ///we would like to define the \ref ProcessedMap
    7.79 +#ifdef DOXYGEN
    7.80 +    static ProcessedMap *createProcessedMap(const GR &g)
    7.81 +#else
    7.82 +    static ProcessedMap *createProcessedMap(const GR &)
    7.83 +#endif
    7.84 +    {
    7.85 +      return new ProcessedMap();
    7.86 +    }
    7.87 +    ///The type of the map that indicates which nodes are reached.
    7.88 + 
    7.89 +    ///The type of the map that indicates which nodes are reached.
    7.90 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    7.91 +    ///\todo named parameter to set this type, function to read and write.
    7.92 +    typedef typename Digraph::template NodeMap<bool> ReachedMap;
    7.93 +    ///Instantiates a ReachedMap.
    7.94 + 
    7.95 +    ///This function instantiates a \ref ReachedMap. 
    7.96 +    ///\param G is the digraph, to which
    7.97 +    ///we would like to define the \ref ReachedMap.
    7.98 +    static ReachedMap *createReachedMap(const GR &G)
    7.99 +    {
   7.100 +      return new ReachedMap(G);
   7.101 +    }
   7.102 +    ///The type of the map that stores the dists of the nodes.
   7.103 + 
   7.104 +    ///The type of the map that stores the dists of the nodes.
   7.105 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   7.106 +    ///
   7.107 +    typedef typename Digraph::template NodeMap<int> DistMap;
   7.108 +    ///Instantiates a DistMap.
   7.109 + 
   7.110 +    ///This function instantiates a \ref DistMap. 
   7.111 +    ///\param G is the digraph, to which we would like to define the \ref DistMap
   7.112 +    static DistMap *createDistMap(const GR &G)
   7.113 +    {
   7.114 +      return new DistMap(G);
   7.115 +    }
   7.116 +  };
   7.117 +  
   7.118 +  ///%DFS algorithm class.
   7.119 +  
   7.120 +  ///\ingroup search
   7.121 +  ///This class provides an efficient implementation of the %DFS algorithm.
   7.122 +  ///
   7.123 +  ///\param GR The digraph type the algorithm runs on. The default value is
   7.124 +  ///\ref ListDigraph. The value of GR is not used directly by Dfs, it
   7.125 +  ///is only passed to \ref DfsDefaultTraits.
   7.126 +  ///\param TR Traits class to set various data types used by the algorithm.
   7.127 +  ///The default traits class is
   7.128 +  ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
   7.129 +  ///See \ref DfsDefaultTraits for the documentation of
   7.130 +  ///a Dfs traits class.
   7.131 +  ///
   7.132 +  ///\author Jacint Szabo and Alpar Juttner
   7.133 +#ifdef DOXYGEN
   7.134 +  template <typename GR,
   7.135 +	    typename TR>
   7.136 +#else
   7.137 +  template <typename GR=ListDigraph,
   7.138 +	    typename TR=DfsDefaultTraits<GR> >
   7.139 +#endif
   7.140 +  class Dfs {
   7.141 +  public:
   7.142 +    /**
   7.143 +     * \brief \ref Exception for uninitialized parameters.
   7.144 +     *
   7.145 +     * This error represents problems in the initialization
   7.146 +     * of the parameters of the algorithms.
   7.147 +     */
   7.148 +    class UninitializedParameter : public lemon::UninitializedParameter {
   7.149 +    public:
   7.150 +      virtual const char* what() const throw() {
   7.151 +	return "lemon::Dfs::UninitializedParameter";
   7.152 +      }
   7.153 +    };
   7.154 +
   7.155 +    typedef TR Traits;
   7.156 +    ///The type of the underlying digraph.
   7.157 +    typedef typename TR::Digraph Digraph;
   7.158 +    ///\e
   7.159 +    typedef typename Digraph::Node Node;
   7.160 +    ///\e
   7.161 +    typedef typename Digraph::NodeIt NodeIt;
   7.162 +    ///\e
   7.163 +    typedef typename Digraph::Arc Arc;
   7.164 +    ///\e
   7.165 +    typedef typename Digraph::OutArcIt OutArcIt;
   7.166 +    
   7.167 +    ///\brief The type of the map that stores the last
   7.168 +    ///arcs of the %DFS paths.
   7.169 +    typedef typename TR::PredMap PredMap;
   7.170 +    ///The type of the map indicating which nodes are reached.
   7.171 +    typedef typename TR::ReachedMap ReachedMap;
   7.172 +    ///The type of the map indicating which nodes are processed.
   7.173 +    typedef typename TR::ProcessedMap ProcessedMap;
   7.174 +    ///The type of the map that stores the dists of the nodes.
   7.175 +    typedef typename TR::DistMap DistMap;
   7.176 +  private:
   7.177 +    /// Pointer to the underlying digraph.
   7.178 +    const Digraph *G;
   7.179 +    ///Pointer to the map of predecessors arcs.
   7.180 +    PredMap *_pred;
   7.181 +    ///Indicates if \ref _pred is locally allocated (\c true) or not.
   7.182 +    bool local_pred;
   7.183 +    ///Pointer to the map of distances.
   7.184 +    DistMap *_dist;
   7.185 +    ///Indicates if \ref _dist is locally allocated (\c true) or not.
   7.186 +    bool local_dist;
   7.187 +    ///Pointer to the map of reached status of the nodes.
   7.188 +    ReachedMap *_reached;
   7.189 +    ///Indicates if \ref _reached is locally allocated (\c true) or not.
   7.190 +    bool local_reached;
   7.191 +    ///Pointer to the map of processed status of the nodes.
   7.192 +    ProcessedMap *_processed;
   7.193 +    ///Indicates if \ref _processed is locally allocated (\c true) or not.
   7.194 +    bool local_processed;
   7.195 +
   7.196 +    std::vector<typename Digraph::OutArcIt> _stack;
   7.197 +    int _stack_head;
   7.198 +
   7.199 +    ///Creates the maps if necessary.
   7.200 +    
   7.201 +    ///\todo Better memory allocation (instead of new).
   7.202 +    void create_maps() 
   7.203 +    {
   7.204 +      if(!_pred) {
   7.205 +	local_pred = true;
   7.206 +	_pred = Traits::createPredMap(*G);
   7.207 +      }
   7.208 +      if(!_dist) {
   7.209 +	local_dist = true;
   7.210 +	_dist = Traits::createDistMap(*G);
   7.211 +      }
   7.212 +      if(!_reached) {
   7.213 +	local_reached = true;
   7.214 +	_reached = Traits::createReachedMap(*G);
   7.215 +      }
   7.216 +      if(!_processed) {
   7.217 +	local_processed = true;
   7.218 +	_processed = Traits::createProcessedMap(*G);
   7.219 +      }
   7.220 +    }
   7.221 +
   7.222 +  protected:
   7.223 +
   7.224 +    Dfs() {}
   7.225 +    
   7.226 +  public:
   7.227 +
   7.228 +    typedef Dfs Create;
   7.229 +
   7.230 +    ///\name Named template parameters
   7.231 +
   7.232 +    ///@{
   7.233 +
   7.234 +    template <class T>
   7.235 +    struct DefPredMapTraits : public Traits {
   7.236 +      typedef T PredMap;
   7.237 +      static PredMap *createPredMap(const Digraph &G) 
   7.238 +      {
   7.239 +	throw UninitializedParameter();
   7.240 +      }
   7.241 +    };
   7.242 +    ///\brief \ref named-templ-param "Named parameter" for setting
   7.243 +    ///PredMap type
   7.244 +    ///
   7.245 +    ///\ref named-templ-param "Named parameter" for setting PredMap type
   7.246 +    ///
   7.247 +    template <class T>
   7.248 +    struct DefPredMap : public Dfs<Digraph, DefPredMapTraits<T> > {
   7.249 +      typedef Dfs<Digraph, DefPredMapTraits<T> > Create;
   7.250 +    };
   7.251 +    
   7.252 +    
   7.253 +    template <class T>
   7.254 +    struct DefDistMapTraits : public Traits {
   7.255 +      typedef T DistMap;
   7.256 +      static DistMap *createDistMap(const Digraph &) 
   7.257 +      {
   7.258 +	throw UninitializedParameter();
   7.259 +      }
   7.260 +    };
   7.261 +    ///\brief \ref named-templ-param "Named parameter" for setting
   7.262 +    ///DistMap type
   7.263 +    ///
   7.264 +    ///\ref named-templ-param "Named parameter" for setting DistMap
   7.265 +    ///type
   7.266 +    template <class T>
   7.267 +    struct DefDistMap {
   7.268 +      typedef Dfs<Digraph, DefDistMapTraits<T> > Create;
   7.269 +    };
   7.270 +    
   7.271 +    template <class T>
   7.272 +    struct DefReachedMapTraits : public Traits {
   7.273 +      typedef T ReachedMap;
   7.274 +      static ReachedMap *createReachedMap(const Digraph &) 
   7.275 +      {
   7.276 +	throw UninitializedParameter();
   7.277 +      }
   7.278 +    };
   7.279 +    ///\brief \ref named-templ-param "Named parameter" for setting
   7.280 +    ///ReachedMap type
   7.281 +    ///
   7.282 +    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
   7.283 +    ///
   7.284 +    template <class T>
   7.285 +    struct DefReachedMap : public Dfs< Digraph, DefReachedMapTraits<T> > {
   7.286 +      typedef Dfs< Digraph, DefReachedMapTraits<T> > Create;
   7.287 +    };
   7.288 +
   7.289 +    template <class T>
   7.290 +    struct DefProcessedMapTraits : public Traits {
   7.291 +      typedef T ProcessedMap;
   7.292 +      static ProcessedMap *createProcessedMap(const Digraph &) 
   7.293 +      {
   7.294 +	throw UninitializedParameter();
   7.295 +      }
   7.296 +    };
   7.297 +    ///\brief \ref named-templ-param "Named parameter" for setting
   7.298 +    ///ProcessedMap type
   7.299 +    ///
   7.300 +    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   7.301 +    ///
   7.302 +    template <class T>
   7.303 +    struct DefProcessedMap : public Dfs< Digraph, DefProcessedMapTraits<T> > { 
   7.304 +      typedef Dfs< Digraph, DefProcessedMapTraits<T> > Create;
   7.305 +    };
   7.306 +    
   7.307 +    struct DefDigraphProcessedMapTraits : public Traits {
   7.308 +      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
   7.309 +      static ProcessedMap *createProcessedMap(const Digraph &G) 
   7.310 +      {
   7.311 +	return new ProcessedMap(G);
   7.312 +      }
   7.313 +    };
   7.314 +    ///\brief \ref named-templ-param "Named parameter"
   7.315 +    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
   7.316 +    ///
   7.317 +    ///\ref named-templ-param "Named parameter"
   7.318 +    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
   7.319 +    ///If you don't set it explicitely, it will be automatically allocated.
   7.320 +    template <class T>
   7.321 +    class DefProcessedMapToBeDefaultMap :
   7.322 +      public Dfs< Digraph, DefDigraphProcessedMapTraits> { 
   7.323 +      typedef Dfs< Digraph, DefDigraphProcessedMapTraits> Create;
   7.324 +    };
   7.325 +    
   7.326 +    ///@}
   7.327 +
   7.328 +  public:      
   7.329 +    
   7.330 +    ///Constructor.
   7.331 +    
   7.332 +    ///\param _G the digraph the algorithm will run on.
   7.333 +    ///
   7.334 +    Dfs(const Digraph& _G) :
   7.335 +      G(&_G),
   7.336 +      _pred(NULL), local_pred(false),
   7.337 +      _dist(NULL), local_dist(false),
   7.338 +      _reached(NULL), local_reached(false),
   7.339 +      _processed(NULL), local_processed(false)
   7.340 +    { }
   7.341 +    
   7.342 +    ///Destructor.
   7.343 +    ~Dfs() 
   7.344 +    {
   7.345 +      if(local_pred) delete _pred;
   7.346 +      if(local_dist) delete _dist;
   7.347 +      if(local_reached) delete _reached;
   7.348 +      if(local_processed) delete _processed;
   7.349 +    }
   7.350 +
   7.351 +    ///Sets the map storing the predecessor arcs.
   7.352 +
   7.353 +    ///Sets the map storing the predecessor arcs.
   7.354 +    ///If you don't use this function before calling \ref run(),
   7.355 +    ///it will allocate one. The destuctor deallocates this
   7.356 +    ///automatically allocated map, of course.
   7.357 +    ///\return <tt> (*this) </tt>
   7.358 +    Dfs &predMap(PredMap &m) 
   7.359 +    {
   7.360 +      if(local_pred) {
   7.361 +	delete _pred;
   7.362 +	local_pred=false;
   7.363 +      }
   7.364 +      _pred = &m;
   7.365 +      return *this;
   7.366 +    }
   7.367 +
   7.368 +    ///Sets the map storing the distances calculated by the algorithm.
   7.369 +
   7.370 +    ///Sets the map storing the distances calculated by the algorithm.
   7.371 +    ///If you don't use this function before calling \ref run(),
   7.372 +    ///it will allocate one. The destuctor deallocates this
   7.373 +    ///automatically allocated map, of course.
   7.374 +    ///\return <tt> (*this) </tt>
   7.375 +    Dfs &distMap(DistMap &m) 
   7.376 +    {
   7.377 +      if(local_dist) {
   7.378 +	delete _dist;
   7.379 +	local_dist=false;
   7.380 +      }
   7.381 +      _dist = &m;
   7.382 +      return *this;
   7.383 +    }
   7.384 +
   7.385 +    ///Sets the map indicating if a node is reached.
   7.386 +
   7.387 +    ///Sets the map indicating if a node is reached.
   7.388 +    ///If you don't use this function before calling \ref run(),
   7.389 +    ///it will allocate one. The destuctor deallocates this
   7.390 +    ///automatically allocated map, of course.
   7.391 +    ///\return <tt> (*this) </tt>
   7.392 +    Dfs &reachedMap(ReachedMap &m) 
   7.393 +    {
   7.394 +      if(local_reached) {
   7.395 +	delete _reached;
   7.396 +	local_reached=false;
   7.397 +      }
   7.398 +      _reached = &m;
   7.399 +      return *this;
   7.400 +    }
   7.401 +
   7.402 +    ///Sets the map indicating if a node is processed.
   7.403 +
   7.404 +    ///Sets the map indicating if a node is processed.
   7.405 +    ///If you don't use this function before calling \ref run(),
   7.406 +    ///it will allocate one. The destuctor deallocates this
   7.407 +    ///automatically allocated map, of course.
   7.408 +    ///\return <tt> (*this) </tt>
   7.409 +    Dfs &processedMap(ProcessedMap &m) 
   7.410 +    {
   7.411 +      if(local_processed) {
   7.412 +	delete _processed;
   7.413 +	local_processed=false;
   7.414 +      }
   7.415 +      _processed = &m;
   7.416 +      return *this;
   7.417 +    }
   7.418 +
   7.419 +  public:
   7.420 +    ///\name Execution control
   7.421 +    ///The simplest way to execute the algorithm is to use
   7.422 +    ///one of the member functions called \c run(...).
   7.423 +    ///\n
   7.424 +    ///If you need more control on the execution,
   7.425 +    ///first you must call \ref init(), then you can add a source node
   7.426 +    ///with \ref addSource().
   7.427 +    ///Finally \ref start() will perform the actual path
   7.428 +    ///computation.
   7.429 +
   7.430 +    ///@{
   7.431 +
   7.432 +    ///Initializes the internal data structures.
   7.433 +
   7.434 +    ///Initializes the internal data structures.
   7.435 +    ///
   7.436 +    void init()
   7.437 +    {
   7.438 +      create_maps();
   7.439 +      _stack.resize(countNodes(*G));
   7.440 +      _stack_head=-1;
   7.441 +      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   7.442 +	_pred->set(u,INVALID);
   7.443 +	// _predNode->set(u,INVALID);
   7.444 +	_reached->set(u,false);
   7.445 +	_processed->set(u,false);
   7.446 +      }
   7.447 +    }
   7.448 +    
   7.449 +    ///Adds a new source node.
   7.450 +
   7.451 +    ///Adds a new source node to the set of nodes to be processed.
   7.452 +    ///
   7.453 +    ///\warning dists are wrong (or at least strange)
   7.454 +    ///in case of multiple sources.
   7.455 +    void addSource(Node s)
   7.456 +    {
   7.457 +      if(!(*_reached)[s])
   7.458 +	{
   7.459 +	  _reached->set(s,true);
   7.460 +	  _pred->set(s,INVALID);
   7.461 +	  OutArcIt e(*G,s);
   7.462 +	  if(e!=INVALID) {
   7.463 +	    _stack[++_stack_head]=e;
   7.464 +	    _dist->set(s,_stack_head);
   7.465 +	  }
   7.466 +	  else {
   7.467 +	    _processed->set(s,true);
   7.468 +	    _dist->set(s,0);
   7.469 +	  }
   7.470 +	}
   7.471 +    }
   7.472 +    
   7.473 +    ///Processes the next arc.
   7.474 +
   7.475 +    ///Processes the next arc.
   7.476 +    ///
   7.477 +    ///\return The processed arc.
   7.478 +    ///
   7.479 +    ///\pre The stack must not be empty!
   7.480 +    Arc processNextArc()
   7.481 +    { 
   7.482 +      Node m;
   7.483 +      Arc e=_stack[_stack_head];
   7.484 +      if(!(*_reached)[m=G->target(e)]) {
   7.485 +	_pred->set(m,e);
   7.486 +	_reached->set(m,true);
   7.487 +	++_stack_head;
   7.488 +	_stack[_stack_head] = OutArcIt(*G, m);
   7.489 +	_dist->set(m,_stack_head);
   7.490 +      }
   7.491 +      else {
   7.492 +	m=G->source(e);
   7.493 +	++_stack[_stack_head];
   7.494 +      }
   7.495 +      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
   7.496 +	_processed->set(m,true);
   7.497 +	--_stack_head;
   7.498 +	if(_stack_head>=0) {
   7.499 +	  m=G->source(_stack[_stack_head]);
   7.500 +	  ++_stack[_stack_head];
   7.501 +	}
   7.502 +      }
   7.503 +      return e;
   7.504 +    }
   7.505 +    ///Next arc to be processed.
   7.506 +
   7.507 +    ///Next arc to be processed.
   7.508 +    ///
   7.509 +    ///\return The next arc to be processed or INVALID if the stack is
   7.510 +    /// empty.
   7.511 +    OutArcIt nextArc()
   7.512 +    { 
   7.513 +      return _stack_head>=0?_stack[_stack_head]:INVALID;
   7.514 +    }
   7.515 +
   7.516 +    ///\brief Returns \c false if there are nodes
   7.517 +    ///to be processed in the queue
   7.518 +    ///
   7.519 +    ///Returns \c false if there are nodes
   7.520 +    ///to be processed in the queue
   7.521 +    bool emptyQueue() { return _stack_head<0; }
   7.522 +    ///Returns the number of the nodes to be processed.
   7.523 +    
   7.524 +    ///Returns the number of the nodes to be processed in the queue.
   7.525 +    int queueSize() { return _stack_head+1; }
   7.526 +    
   7.527 +    ///Executes the algorithm.
   7.528 +
   7.529 +    ///Executes the algorithm.
   7.530 +    ///
   7.531 +    ///\pre init() must be called and at least one node should be added
   7.532 +    ///with addSource() before using this function.
   7.533 +    ///
   7.534 +    ///This method runs the %DFS algorithm from the root node(s)
   7.535 +    ///in order to
   7.536 +    ///compute the
   7.537 +    ///%DFS path to each node. The algorithm computes
   7.538 +    ///- The %DFS tree.
   7.539 +    ///- The distance of each node from the root(s) in the %DFS tree.
   7.540 +    ///
   7.541 +    void start()
   7.542 +    {
   7.543 +      while ( !emptyQueue() ) processNextArc();
   7.544 +    }
   7.545 +    
   7.546 +    ///Executes the algorithm until \c dest is reached.
   7.547 +
   7.548 +    ///Executes the algorithm until \c dest is reached.
   7.549 +    ///
   7.550 +    ///\pre init() must be called and at least one node should be added
   7.551 +    ///with addSource() before using this function.
   7.552 +    ///
   7.553 +    ///This method runs the %DFS algorithm from the root node(s)
   7.554 +    ///in order to
   7.555 +    ///compute the
   7.556 +    ///%DFS path to \c dest. The algorithm computes
   7.557 +    ///- The %DFS path to \c  dest.
   7.558 +    ///- The distance of \c dest from the root(s) in the %DFS tree.
   7.559 +    ///
   7.560 +    void start(Node dest)
   7.561 +    {
   7.562 +      while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) 
   7.563 +	processNextArc();
   7.564 +    }
   7.565 +    
   7.566 +    ///Executes the algorithm until a condition is met.
   7.567 +
   7.568 +    ///Executes the algorithm until a condition is met.
   7.569 +    ///
   7.570 +    ///\pre init() must be called and at least one node should be added
   7.571 +    ///with addSource() before using this function.
   7.572 +    ///
   7.573 +    ///\param em must be a bool (or convertible) arc map. The algorithm
   7.574 +    ///will stop when it reaches an arc \c e with <tt>em[e]</tt> true.
   7.575 +    ///
   7.576 +    ///\return The reached arc \c e with <tt>em[e]</tt> true or
   7.577 +    ///\c INVALID if no such arc was found.
   7.578 +    ///
   7.579 +    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map,
   7.580 +    ///not a node map.
   7.581 +    template<class EM>
   7.582 +    Arc start(const EM &em)
   7.583 +    {
   7.584 +      while ( !emptyQueue() && !em[_stack[_stack_head]] )
   7.585 +        processNextArc();
   7.586 +      return emptyQueue() ? INVALID : _stack[_stack_head];
   7.587 +    }
   7.588 +
   7.589 +    ///Runs %DFS algorithm to visit all nodes in the digraph.
   7.590 +    
   7.591 +    ///This method runs the %DFS algorithm in order to
   7.592 +    ///compute the
   7.593 +    ///%DFS path to each node. The algorithm computes
   7.594 +    ///- The %DFS tree.
   7.595 +    ///- The distance of each node from the root in the %DFS tree.
   7.596 +    ///
   7.597 +    ///\note d.run() is just a shortcut of the following code.
   7.598 +    ///\code
   7.599 +    ///  d.init();
   7.600 +    ///  for (NodeIt it(digraph); it != INVALID; ++it) {
   7.601 +    ///    if (!d.reached(it)) {
   7.602 +    ///      d.addSource(it);
   7.603 +    ///      d.start();
   7.604 +    ///    }
   7.605 +    ///  }
   7.606 +    ///\endcode
   7.607 +    void run() {
   7.608 +      init();
   7.609 +      for (NodeIt it(*G); it != INVALID; ++it) {
   7.610 +        if (!reached(it)) {
   7.611 +          addSource(it);
   7.612 +          start();
   7.613 +        }
   7.614 +      }
   7.615 +    }
   7.616 +
   7.617 +    ///Runs %DFS algorithm from node \c s.
   7.618 +    
   7.619 +    ///This method runs the %DFS algorithm from a root node \c s
   7.620 +    ///in order to
   7.621 +    ///compute the
   7.622 +    ///%DFS path to each node. The algorithm computes
   7.623 +    ///- The %DFS tree.
   7.624 +    ///- The distance of each node from the root in the %DFS tree.
   7.625 +    ///
   7.626 +    ///\note d.run(s) is just a shortcut of the following code.
   7.627 +    ///\code
   7.628 +    ///  d.init();
   7.629 +    ///  d.addSource(s);
   7.630 +    ///  d.start();
   7.631 +    ///\endcode
   7.632 +    void run(Node s) {
   7.633 +      init();
   7.634 +      addSource(s);
   7.635 +      start();
   7.636 +    }
   7.637 +    
   7.638 +    ///Finds the %DFS path between \c s and \c t.
   7.639 +    
   7.640 +    ///Finds the %DFS path between \c s and \c t.
   7.641 +    ///
   7.642 +    ///\return The length of the %DFS s---t path if there exists one,
   7.643 +    ///0 otherwise.
   7.644 +    ///\note Apart from the return value, d.run(s,t) is
   7.645 +    ///just a shortcut of the following code.
   7.646 +    ///\code
   7.647 +    ///  d.init();
   7.648 +    ///  d.addSource(s);
   7.649 +    ///  d.start(t);
   7.650 +    ///\endcode
   7.651 +    int run(Node s,Node t) {
   7.652 +      init();
   7.653 +      addSource(s);
   7.654 +      start(t);
   7.655 +      return reached(t)?_stack_head+1:0;
   7.656 +    }
   7.657 +    
   7.658 +    ///@}
   7.659 +
   7.660 +    ///\name Query Functions
   7.661 +    ///The result of the %DFS algorithm can be obtained using these
   7.662 +    ///functions.\n
   7.663 +    ///Before the use of these functions,
   7.664 +    ///either run() or start() must be called.
   7.665 +    
   7.666 +    ///@{
   7.667 +
   7.668 +    typedef PredMapPath<Digraph, PredMap> Path;
   7.669 +
   7.670 +    ///Gives back the shortest path.
   7.671 +    
   7.672 +    ///Gives back the shortest path.
   7.673 +    ///\pre The \c t should be reachable from the source.
   7.674 +    Path path(Node t) 
   7.675 +    {
   7.676 +      return Path(*G, *_pred, t);
   7.677 +    }
   7.678 +
   7.679 +    ///The distance of a node from the root(s).
   7.680 +
   7.681 +    ///Returns the distance of a node from the root(s).
   7.682 +    ///\pre \ref run() must be called before using this function.
   7.683 +    ///\warning If node \c v is unreachable from the root(s) then the return 
   7.684 +    ///value of this funcion is undefined.
   7.685 +    int dist(Node v) const { return (*_dist)[v]; }
   7.686 +
   7.687 +    ///Returns the 'previous arc' of the %DFS tree.
   7.688 +
   7.689 +    ///For a node \c v it returns the 'previous arc'
   7.690 +    ///of the %DFS path,
   7.691 +    ///i.e. it returns the last arc of a %DFS path from the root(s) to \c
   7.692 +    ///v. It is \ref INVALID
   7.693 +    ///if \c v is unreachable from the root(s) or \c v is a root. The
   7.694 +    ///%DFS tree used here is equal to the %DFS tree used in
   7.695 +    ///\ref predNode().
   7.696 +    ///\pre Either \ref run() or \ref start() must be called before using
   7.697 +    ///this function.
   7.698 +    Arc predArc(Node v) const { return (*_pred)[v];}
   7.699 +
   7.700 +    ///Returns the 'previous node' of the %DFS tree.
   7.701 +
   7.702 +    ///For a node \c v it returns the 'previous node'
   7.703 +    ///of the %DFS tree,
   7.704 +    ///i.e. it returns the last but one node from a %DFS path from the
   7.705 +    ///root(s) to \c v.
   7.706 +    ///It is INVALID if \c v is unreachable from the root(s) or
   7.707 +    ///if \c v itself a root.
   7.708 +    ///The %DFS tree used here is equal to the %DFS
   7.709 +    ///tree used in \ref predArc().
   7.710 +    ///\pre Either \ref run() or \ref start() must be called before
   7.711 +    ///using this function.
   7.712 +    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   7.713 +				  G->source((*_pred)[v]); }
   7.714 +    
   7.715 +    ///Returns a reference to the NodeMap of distances.
   7.716 +
   7.717 +    ///Returns a reference to the NodeMap of distances.
   7.718 +    ///\pre Either \ref run() or \ref init() must
   7.719 +    ///be called before using this function.
   7.720 +    const DistMap &distMap() const { return *_dist;}
   7.721 + 
   7.722 +    ///Returns a reference to the %DFS arc-tree map.
   7.723 +
   7.724 +    ///Returns a reference to the NodeMap of the arcs of the
   7.725 +    ///%DFS tree.
   7.726 +    ///\pre Either \ref run() or \ref init()
   7.727 +    ///must be called before using this function.
   7.728 +    const PredMap &predMap() const { return *_pred;}
   7.729 + 
   7.730 +    ///Checks if a node is reachable from the root.
   7.731 +
   7.732 +    ///Returns \c true if \c v is reachable from the root(s).
   7.733 +    ///\warning The source nodes are inditated as unreachable.
   7.734 +    ///\pre Either \ref run() or \ref start()
   7.735 +    ///must be called before using this function.
   7.736 +    ///
   7.737 +    bool reached(Node v) { return (*_reached)[v]; }
   7.738 +    
   7.739 +    ///@}
   7.740 +  };
   7.741 +
   7.742 +  ///Default traits class of Dfs function.
   7.743 +
   7.744 +  ///Default traits class of Dfs function.
   7.745 +  ///\param GR Digraph type.
   7.746 +  template<class GR>
   7.747 +  struct DfsWizardDefaultTraits
   7.748 +  {
   7.749 +    ///The digraph type the algorithm runs on. 
   7.750 +    typedef GR Digraph;
   7.751 +    ///\brief The type of the map that stores the last
   7.752 +    ///arcs of the %DFS paths.
   7.753 +    /// 
   7.754 +    ///The type of the map that stores the last
   7.755 +    ///arcs of the %DFS paths.
   7.756 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   7.757 +    ///
   7.758 +    typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
   7.759 +    ///Instantiates a PredMap.
   7.760 + 
   7.761 +    ///This function instantiates a \ref PredMap. 
   7.762 +    ///\param g is the digraph, to which we would like to define the PredMap.
   7.763 +    ///\todo The digraph alone may be insufficient to initialize
   7.764 +#ifdef DOXYGEN
   7.765 +    static PredMap *createPredMap(const GR &g) 
   7.766 +#else
   7.767 +    static PredMap *createPredMap(const GR &) 
   7.768 +#endif
   7.769 +    {
   7.770 +      return new PredMap();
   7.771 +    }
   7.772 +
   7.773 +    ///The type of the map that indicates which nodes are processed.
   7.774 + 
   7.775 +    ///The type of the map that indicates which nodes are processed.
   7.776 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   7.777 +    ///\todo named parameter to set this type, function to read and write.
   7.778 +    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
   7.779 +    ///Instantiates a ProcessedMap.
   7.780 + 
   7.781 +    ///This function instantiates a \ref ProcessedMap. 
   7.782 +    ///\param g is the digraph, to which
   7.783 +    ///we would like to define the \ref ProcessedMap
   7.784 +#ifdef DOXYGEN
   7.785 +    static ProcessedMap *createProcessedMap(const GR &g)
   7.786 +#else
   7.787 +    static ProcessedMap *createProcessedMap(const GR &)
   7.788 +#endif
   7.789 +    {
   7.790 +      return new ProcessedMap();
   7.791 +    }
   7.792 +    ///The type of the map that indicates which nodes are reached.
   7.793 + 
   7.794 +    ///The type of the map that indicates which nodes are reached.
   7.795 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   7.796 +    ///\todo named parameter to set this type, function to read and write.
   7.797 +    typedef typename Digraph::template NodeMap<bool> ReachedMap;
   7.798 +    ///Instantiates a ReachedMap.
   7.799 + 
   7.800 +    ///This function instantiates a \ref ReachedMap. 
   7.801 +    ///\param G is the digraph, to which
   7.802 +    ///we would like to define the \ref ReachedMap.
   7.803 +    static ReachedMap *createReachedMap(const GR &G)
   7.804 +    {
   7.805 +      return new ReachedMap(G);
   7.806 +    }
   7.807 +    ///The type of the map that stores the dists of the nodes.
   7.808 + 
   7.809 +    ///The type of the map that stores the dists of the nodes.
   7.810 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   7.811 +    ///
   7.812 +    typedef NullMap<typename Digraph::Node,int> DistMap;
   7.813 +    ///Instantiates a DistMap.
   7.814 + 
   7.815 +    ///This function instantiates a \ref DistMap. 
   7.816 +    ///\param g is the digraph, to which we would like to define the \ref DistMap
   7.817 +#ifdef DOXYGEN
   7.818 +    static DistMap *createDistMap(const GR &g)
   7.819 +#else
   7.820 +    static DistMap *createDistMap(const GR &)
   7.821 +#endif
   7.822 +    {
   7.823 +      return new DistMap();
   7.824 +    }
   7.825 +  };
   7.826 +  
   7.827 +  /// Default traits used by \ref DfsWizard
   7.828 +
   7.829 +  /// To make it easier to use Dfs algorithm
   7.830 +  ///we have created a wizard class.
   7.831 +  /// This \ref DfsWizard class needs default traits,
   7.832 +  ///as well as the \ref Dfs class.
   7.833 +  /// The \ref DfsWizardBase is a class to be the default traits of the
   7.834 +  /// \ref DfsWizard class.
   7.835 +  template<class GR>
   7.836 +  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
   7.837 +  {
   7.838 +
   7.839 +    typedef DfsWizardDefaultTraits<GR> Base;
   7.840 +  protected:
   7.841 +    /// Type of the nodes in the digraph.
   7.842 +    typedef typename Base::Digraph::Node Node;
   7.843 +
   7.844 +    /// Pointer to the underlying digraph.
   7.845 +    void *_g;
   7.846 +    ///Pointer to the map of reached nodes.
   7.847 +    void *_reached;
   7.848 +    ///Pointer to the map of processed nodes.
   7.849 +    void *_processed;
   7.850 +    ///Pointer to the map of predecessors arcs.
   7.851 +    void *_pred;
   7.852 +    ///Pointer to the map of distances.
   7.853 +    void *_dist;
   7.854 +    ///Pointer to the source node.
   7.855 +    Node _source;
   7.856 +    
   7.857 +    public:
   7.858 +    /// Constructor.
   7.859 +    
   7.860 +    /// This constructor does not require parameters, therefore it initiates
   7.861 +    /// all of the attributes to default values (0, INVALID).
   7.862 +    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
   7.863 +			   _dist(0), _source(INVALID) {}
   7.864 +
   7.865 +    /// Constructor.
   7.866 +    
   7.867 +    /// This constructor requires some parameters,
   7.868 +    /// listed in the parameters list.
   7.869 +    /// Others are initiated to 0.
   7.870 +    /// \param g is the initial value of  \ref _g
   7.871 +    /// \param s is the initial value of  \ref _source
   7.872 +    DfsWizardBase(const GR &g, Node s=INVALID) :
   7.873 +      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
   7.874 +      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
   7.875 +
   7.876 +  };
   7.877 +  
   7.878 +  /// A class to make the usage of the Dfs algorithm easier
   7.879 +
   7.880 +  /// This class is created to make it easier to use the Dfs algorithm.
   7.881 +  /// It uses the functions and features of the plain \ref Dfs,
   7.882 +  /// but it is much simpler to use it.
   7.883 +  ///
   7.884 +  /// Simplicity means that the way to change the types defined
   7.885 +  /// in the traits class is based on functions that returns the new class
   7.886 +  /// and not on templatable built-in classes.
   7.887 +  /// When using the plain \ref Dfs
   7.888 +  /// the new class with the modified type comes from
   7.889 +  /// the original class by using the ::
   7.890 +  /// operator. In the case of \ref DfsWizard only
   7.891 +  /// a function have to be called and it will
   7.892 +  /// return the needed class.
   7.893 +  ///
   7.894 +  /// It does not have own \ref run method. When its \ref run method is called
   7.895 +  /// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run
   7.896 +  /// method of it.
   7.897 +  template<class TR>
   7.898 +  class DfsWizard : public TR
   7.899 +  {
   7.900 +    typedef TR Base;
   7.901 +
   7.902 +    ///The type of the underlying digraph.
   7.903 +    typedef typename TR::Digraph Digraph;
   7.904 +    //\e
   7.905 +    typedef typename Digraph::Node Node;
   7.906 +    //\e
   7.907 +    typedef typename Digraph::NodeIt NodeIt;
   7.908 +    //\e
   7.909 +    typedef typename Digraph::Arc Arc;
   7.910 +    //\e
   7.911 +    typedef typename Digraph::OutArcIt OutArcIt;
   7.912 +    
   7.913 +    ///\brief The type of the map that stores
   7.914 +    ///the reached nodes
   7.915 +    typedef typename TR::ReachedMap ReachedMap;
   7.916 +    ///\brief The type of the map that stores
   7.917 +    ///the processed nodes
   7.918 +    typedef typename TR::ProcessedMap ProcessedMap;
   7.919 +    ///\brief The type of the map that stores the last
   7.920 +    ///arcs of the %DFS paths.
   7.921 +    typedef typename TR::PredMap PredMap;
   7.922 +    ///The type of the map that stores the distances of the nodes.
   7.923 +    typedef typename TR::DistMap DistMap;
   7.924 +
   7.925 +  public:
   7.926 +    /// Constructor.
   7.927 +    DfsWizard() : TR() {}
   7.928 +
   7.929 +    /// Constructor that requires parameters.
   7.930 +
   7.931 +    /// Constructor that requires parameters.
   7.932 +    /// These parameters will be the default values for the traits class.
   7.933 +    DfsWizard(const Digraph &g, Node s=INVALID) :
   7.934 +      TR(g,s) {}
   7.935 +
   7.936 +    ///Copy constructor
   7.937 +    DfsWizard(const TR &b) : TR(b) {}
   7.938 +
   7.939 +    ~DfsWizard() {}
   7.940 +
   7.941 +    ///Runs Dfs algorithm from a given node.
   7.942 +    
   7.943 +    ///Runs Dfs algorithm from a given node.
   7.944 +    ///The node can be given by the \ref source function.
   7.945 +    void run()
   7.946 +    {
   7.947 +      if(Base::_source==INVALID) throw UninitializedParameter();
   7.948 +      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   7.949 +      if(Base::_reached) 
   7.950 +        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   7.951 +      if(Base::_processed) 
   7.952 +        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   7.953 +      if(Base::_pred) 
   7.954 +        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   7.955 +      if(Base::_dist) 
   7.956 +        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   7.957 +      alg.run(Base::_source);
   7.958 +    }
   7.959 +
   7.960 +    ///Runs Dfs algorithm from the given node.
   7.961 +
   7.962 +    ///Runs Dfs algorithm from the given node.
   7.963 +    ///\param s is the given source.
   7.964 +    void run(Node s)
   7.965 +    {
   7.966 +      Base::_source=s;
   7.967 +      run();
   7.968 +    }
   7.969 +
   7.970 +    template<class T>
   7.971 +    struct DefPredMapBase : public Base {
   7.972 +      typedef T PredMap;
   7.973 +      static PredMap *createPredMap(const Digraph &) { return 0; };
   7.974 +      DefPredMapBase(const TR &b) : TR(b) {}
   7.975 +    };
   7.976 +    
   7.977 +    ///\brief \ref named-templ-param "Named parameter"
   7.978 +    ///function for setting PredMap type
   7.979 +    ///
   7.980 +    /// \ref named-templ-param "Named parameter"
   7.981 +    ///function for setting PredMap type
   7.982 +    ///
   7.983 +    template<class T>
   7.984 +    DfsWizard<DefPredMapBase<T> > predMap(const T &t) 
   7.985 +    {
   7.986 +      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
   7.987 +      return DfsWizard<DefPredMapBase<T> >(*this);
   7.988 +    }
   7.989 +    
   7.990 + 
   7.991 +    template<class T>
   7.992 +    struct DefReachedMapBase : public Base {
   7.993 +      typedef T ReachedMap;
   7.994 +      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
   7.995 +      DefReachedMapBase(const TR &b) : TR(b) {}
   7.996 +    };
   7.997 +    
   7.998 +    ///\brief \ref named-templ-param "Named parameter"
   7.999 +    ///function for setting ReachedMap
  7.1000 +    ///
  7.1001 +    /// \ref named-templ-param "Named parameter"
  7.1002 +    ///function for setting ReachedMap
  7.1003 +    ///
  7.1004 +    template<class T>
  7.1005 +    DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
  7.1006 +    {
  7.1007 +      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  7.1008 +      return DfsWizard<DefReachedMapBase<T> >(*this);
  7.1009 +    }
  7.1010 +    
  7.1011 +
  7.1012 +    template<class T>
  7.1013 +    struct DefProcessedMapBase : public Base {
  7.1014 +      typedef T ProcessedMap;
  7.1015 +      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
  7.1016 +      DefProcessedMapBase(const TR &b) : TR(b) {}
  7.1017 +    };
  7.1018 +    
  7.1019 +    ///\brief \ref named-templ-param "Named parameter"
  7.1020 +    ///function for setting ProcessedMap
  7.1021 +    ///
  7.1022 +    /// \ref named-templ-param "Named parameter"
  7.1023 +    ///function for setting ProcessedMap
  7.1024 +    ///
  7.1025 +    template<class T>
  7.1026 +    DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
  7.1027 +    {
  7.1028 +      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  7.1029 +      return DfsWizard<DefProcessedMapBase<T> >(*this);
  7.1030 +    }
  7.1031 +    
  7.1032 +    template<class T>
  7.1033 +    struct DefDistMapBase : public Base {
  7.1034 +      typedef T DistMap;
  7.1035 +      static DistMap *createDistMap(const Digraph &) { return 0; };
  7.1036 +      DefDistMapBase(const TR &b) : TR(b) {}
  7.1037 +    };
  7.1038 +    
  7.1039 +    ///\brief \ref named-templ-param "Named parameter"
  7.1040 +    ///function for setting DistMap type
  7.1041 +    ///
  7.1042 +    /// \ref named-templ-param "Named parameter"
  7.1043 +    ///function for setting DistMap type
  7.1044 +    ///
  7.1045 +    template<class T>
  7.1046 +    DfsWizard<DefDistMapBase<T> > distMap(const T &t) 
  7.1047 +    {
  7.1048 +      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
  7.1049 +      return DfsWizard<DefDistMapBase<T> >(*this);
  7.1050 +    }
  7.1051 +    
  7.1052 +    /// Sets the source node, from which the Dfs algorithm runs.
  7.1053 +
  7.1054 +    /// Sets the source node, from which the Dfs algorithm runs.
  7.1055 +    /// \param s is the source node.
  7.1056 +    DfsWizard<TR> &source(Node s) 
  7.1057 +    {
  7.1058 +      Base::_source=s;
  7.1059 +      return *this;
  7.1060 +    }
  7.1061 +    
  7.1062 +  };
  7.1063 +  
  7.1064 +  ///Function type interface for Dfs algorithm.
  7.1065 +
  7.1066 +  ///\ingroup search
  7.1067 +  ///Function type interface for Dfs algorithm.
  7.1068 +  ///
  7.1069 +  ///This function also has several
  7.1070 +  ///\ref named-templ-func-param "named parameters",
  7.1071 +  ///they are declared as the members of class \ref DfsWizard.
  7.1072 +  ///The following
  7.1073 +  ///example shows how to use these parameters.
  7.1074 +  ///\code
  7.1075 +  ///  dfs(g,source).predMap(preds).run();
  7.1076 +  ///\endcode
  7.1077 +  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
  7.1078 +  ///to the end of the parameter list.
  7.1079 +  ///\sa DfsWizard
  7.1080 +  ///\sa Dfs
  7.1081 +  template<class GR>
  7.1082 +  DfsWizard<DfsWizardBase<GR> >
  7.1083 +  dfs(const GR &g,typename GR::Node s=INVALID)
  7.1084 +  {
  7.1085 +    return DfsWizard<DfsWizardBase<GR> >(g,s);
  7.1086 +  }
  7.1087 +
  7.1088 +#ifdef DOXYGEN
  7.1089 +  /// \brief Visitor class for dfs.
  7.1090 +  ///  
  7.1091 +  /// It gives a simple interface for a functional interface for dfs 
  7.1092 +  /// traversal. The traversal on a linear data structure. 
  7.1093 +  template <typename _Digraph>
  7.1094 +  struct DfsVisitor {
  7.1095 +    typedef _Digraph Digraph;
  7.1096 +    typedef typename Digraph::Arc Arc;
  7.1097 +    typedef typename Digraph::Node Node;
  7.1098 +    /// \brief Called when the arc reach a node.
  7.1099 +    /// 
  7.1100 +    /// It is called when the dfs find an arc which target is not
  7.1101 +    /// reached yet.
  7.1102 +    void discover(const Arc& arc) {}
  7.1103 +    /// \brief Called when the node reached first time.
  7.1104 +    /// 
  7.1105 +    /// It is Called when the node reached first time.
  7.1106 +    void reach(const Node& node) {}
  7.1107 +    /// \brief Called when we step back on an arc.
  7.1108 +    /// 
  7.1109 +    /// It is called when the dfs should step back on the arc.
  7.1110 +    void backtrack(const Arc& arc) {}
  7.1111 +    /// \brief Called when we step back from the node.
  7.1112 +    /// 
  7.1113 +    /// It is called when we step back from the node.
  7.1114 +    void leave(const Node& node) {}
  7.1115 +    /// \brief Called when the arc examined but target of the arc 
  7.1116 +    /// already discovered.
  7.1117 +    /// 
  7.1118 +    /// It called when the arc examined but the target of the arc 
  7.1119 +    /// already discovered.
  7.1120 +    void examine(const Arc& arc) {}
  7.1121 +    /// \brief Called for the source node of the dfs.
  7.1122 +    /// 
  7.1123 +    /// It is called for the source node of the dfs.
  7.1124 +    void start(const Node& node) {}
  7.1125 +    /// \brief Called when we leave the source node of the dfs.
  7.1126 +    /// 
  7.1127 +    /// It is called when we leave the source node of the dfs.
  7.1128 +    void stop(const Node& node) {}
  7.1129 +
  7.1130 +  };
  7.1131 +#else
  7.1132 +  template <typename _Digraph>
  7.1133 +  struct DfsVisitor {
  7.1134 +    typedef _Digraph Digraph;
  7.1135 +    typedef typename Digraph::Arc Arc;
  7.1136 +    typedef typename Digraph::Node Node;
  7.1137 +    void discover(const Arc&) {}
  7.1138 +    void reach(const Node&) {}
  7.1139 +    void backtrack(const Arc&) {}
  7.1140 +    void leave(const Node&) {}
  7.1141 +    void examine(const Arc&) {}
  7.1142 +    void start(const Node&) {}
  7.1143 +    void stop(const Node&) {}
  7.1144 +
  7.1145 +    template <typename _Visitor>
  7.1146 +    struct Constraints {
  7.1147 +      void constraints() {
  7.1148 +	Arc arc;
  7.1149 +	Node node;
  7.1150 +	visitor.discover(arc);
  7.1151 +	visitor.reach(node);
  7.1152 +	visitor.backtrack(arc);
  7.1153 +	visitor.leave(node);
  7.1154 +	visitor.examine(arc);
  7.1155 +	visitor.start(node);
  7.1156 +	visitor.stop(arc);
  7.1157 +      }
  7.1158 +      _Visitor& visitor;
  7.1159 +    };
  7.1160 +  };
  7.1161 +#endif
  7.1162 +
  7.1163 +  /// \brief Default traits class of DfsVisit class.
  7.1164 +  ///
  7.1165 +  /// Default traits class of DfsVisit class.
  7.1166 +  /// \param _Digraph Digraph type.
  7.1167 +  template<class _Digraph>
  7.1168 +  struct DfsVisitDefaultTraits {
  7.1169 +
  7.1170 +    /// \brief The digraph type the algorithm runs on. 
  7.1171 +    typedef _Digraph Digraph;
  7.1172 +
  7.1173 +    /// \brief The type of the map that indicates which nodes are reached.
  7.1174 +    /// 
  7.1175 +    /// The type of the map that indicates which nodes are reached.
  7.1176 +    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
  7.1177 +    /// \todo named parameter to set this type, function to read and write.
  7.1178 +    typedef typename Digraph::template NodeMap<bool> ReachedMap;
  7.1179 +
  7.1180 +    /// \brief Instantiates a ReachedMap.
  7.1181 +    ///
  7.1182 +    /// This function instantiates a \ref ReachedMap. 
  7.1183 +    /// \param digraph is the digraph, to which
  7.1184 +    /// we would like to define the \ref ReachedMap.
  7.1185 +    static ReachedMap *createReachedMap(const Digraph &digraph) {
  7.1186 +      return new ReachedMap(digraph);
  7.1187 +    }
  7.1188 +
  7.1189 +  };
  7.1190 +  
  7.1191 +  /// %DFS Visit algorithm class.
  7.1192 +  
  7.1193 +  /// \ingroup search
  7.1194 +  /// This class provides an efficient implementation of the %DFS algorithm
  7.1195 +  /// with visitor interface.
  7.1196 +  ///
  7.1197 +  /// The %DfsVisit class provides an alternative interface to the Dfs
  7.1198 +  /// class. It works with callback mechanism, the DfsVisit object calls
  7.1199 +  /// on every dfs event the \c Visitor class member functions. 
  7.1200 +  ///
  7.1201 +  /// \param _Digraph The digraph type the algorithm runs on. The default value is
  7.1202 +  /// \ref ListDigraph. The value of _Digraph is not used directly by Dfs, it
  7.1203 +  /// is only passed to \ref DfsDefaultTraits.
  7.1204 +  /// \param _Visitor The Visitor object for the algorithm. The 
  7.1205 +  /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty Visitor which
  7.1206 +  /// does not observe the Dfs events. If you want to observe the dfs
  7.1207 +  /// events you should implement your own Visitor class.
  7.1208 +  /// \param _Traits Traits class to set various data types used by the 
  7.1209 +  /// algorithm. The default traits class is
  7.1210 +  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>".
  7.1211 +  /// See \ref DfsVisitDefaultTraits for the documentation of
  7.1212 +  /// a Dfs visit traits class.
  7.1213 +  ///
  7.1214 +  /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
  7.1215 +#ifdef DOXYGEN
  7.1216 +  template <typename _Digraph, typename _Visitor, typename _Traits>
  7.1217 +#else
  7.1218 +  template <typename _Digraph = ListDigraph,
  7.1219 +	    typename _Visitor = DfsVisitor<_Digraph>,
  7.1220 +	    typename _Traits = DfsDefaultTraits<_Digraph> >
  7.1221 +#endif
  7.1222 +  class DfsVisit {
  7.1223 +  public:
  7.1224 +    
  7.1225 +    /// \brief \ref Exception for uninitialized parameters.
  7.1226 +    ///
  7.1227 +    /// This error represents problems in the initialization
  7.1228 +    /// of the parameters of the algorithms.
  7.1229 +    class UninitializedParameter : public lemon::UninitializedParameter {
  7.1230 +    public:
  7.1231 +      virtual const char* what() const throw() 
  7.1232 +      {
  7.1233 +	return "lemon::DfsVisit::UninitializedParameter";
  7.1234 +      }
  7.1235 +    };
  7.1236 +
  7.1237 +    typedef _Traits Traits;
  7.1238 +
  7.1239 +    typedef typename Traits::Digraph Digraph;
  7.1240 +
  7.1241 +    typedef _Visitor Visitor;
  7.1242 +
  7.1243 +    ///The type of the map indicating which nodes are reached.
  7.1244 +    typedef typename Traits::ReachedMap ReachedMap;
  7.1245 +
  7.1246 +  private:
  7.1247 +
  7.1248 +    typedef typename Digraph::Node Node;
  7.1249 +    typedef typename Digraph::NodeIt NodeIt;
  7.1250 +    typedef typename Digraph::Arc Arc;
  7.1251 +    typedef typename Digraph::OutArcIt OutArcIt;
  7.1252 +
  7.1253 +    /// Pointer to the underlying digraph.
  7.1254 +    const Digraph *_digraph;
  7.1255 +    /// Pointer to the visitor object.
  7.1256 +    Visitor *_visitor;
  7.1257 +    ///Pointer to the map of reached status of the nodes.
  7.1258 +    ReachedMap *_reached;
  7.1259 +    ///Indicates if \ref _reached is locally allocated (\c true) or not.
  7.1260 +    bool local_reached;
  7.1261 +
  7.1262 +    std::vector<typename Digraph::Arc> _stack;
  7.1263 +    int _stack_head;
  7.1264 +
  7.1265 +    /// \brief Creates the maps if necessary.
  7.1266 +    ///
  7.1267 +    /// Creates the maps if necessary.
  7.1268 +    void create_maps() {
  7.1269 +      if(!_reached) {
  7.1270 +	local_reached = true;
  7.1271 +	_reached = Traits::createReachedMap(*_digraph);
  7.1272 +      }
  7.1273 +    }
  7.1274 +
  7.1275 +  protected:
  7.1276 +
  7.1277 +    DfsVisit() {}
  7.1278 +    
  7.1279 +  public:
  7.1280 +
  7.1281 +    typedef DfsVisit Create;
  7.1282 +
  7.1283 +    /// \name Named template parameters
  7.1284 +
  7.1285 +    ///@{
  7.1286 +    template <class T>
  7.1287 +    struct DefReachedMapTraits : public Traits {
  7.1288 +      typedef T ReachedMap;
  7.1289 +      static ReachedMap *createReachedMap(const Digraph &digraph) {
  7.1290 +	throw UninitializedParameter();
  7.1291 +      }
  7.1292 +    };
  7.1293 +    /// \brief \ref named-templ-param "Named parameter" for setting 
  7.1294 +    /// ReachedMap type
  7.1295 +    ///
  7.1296 +    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
  7.1297 +    template <class T>
  7.1298 +    struct DefReachedMap : public DfsVisit< Digraph, Visitor,
  7.1299 +					    DefReachedMapTraits<T> > {
  7.1300 +      typedef DfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
  7.1301 +    };
  7.1302 +    ///@}
  7.1303 +
  7.1304 +  public:      
  7.1305 +    
  7.1306 +    /// \brief Constructor.
  7.1307 +    ///
  7.1308 +    /// Constructor.
  7.1309 +    ///
  7.1310 +    /// \param digraph the digraph the algorithm will run on.
  7.1311 +    /// \param visitor The visitor of the algorithm.
  7.1312 +    ///
  7.1313 +    DfsVisit(const Digraph& digraph, Visitor& visitor) 
  7.1314 +      : _digraph(&digraph), _visitor(&visitor),
  7.1315 +	_reached(0), local_reached(false) {}
  7.1316 +    
  7.1317 +    /// \brief Destructor.
  7.1318 +    ///
  7.1319 +    /// Destructor.
  7.1320 +    ~DfsVisit() {
  7.1321 +      if(local_reached) delete _reached;
  7.1322 +    }
  7.1323 +
  7.1324 +    /// \brief Sets the map indicating if a node is reached.
  7.1325 +    ///
  7.1326 +    /// Sets the map indicating if a node is reached.
  7.1327 +    /// If you don't use this function before calling \ref run(),
  7.1328 +    /// it will allocate one. The destuctor deallocates this
  7.1329 +    /// automatically allocated map, of course.
  7.1330 +    /// \return <tt> (*this) </tt>
  7.1331 +    DfsVisit &reachedMap(ReachedMap &m) {
  7.1332 +      if(local_reached) {
  7.1333 +	delete _reached;
  7.1334 +	local_reached=false;
  7.1335 +      }
  7.1336 +      _reached = &m;
  7.1337 +      return *this;
  7.1338 +    }
  7.1339 +
  7.1340 +  public:
  7.1341 +    /// \name Execution control
  7.1342 +    /// The simplest way to execute the algorithm is to use
  7.1343 +    /// one of the member functions called \c run(...).
  7.1344 +    /// \n
  7.1345 +    /// If you need more control on the execution,
  7.1346 +    /// first you must call \ref init(), then you can adda source node
  7.1347 +    /// with \ref addSource().
  7.1348 +    /// Finally \ref start() will perform the actual path
  7.1349 +    /// computation.
  7.1350 +
  7.1351 +    /// @{
  7.1352 +    /// \brief Initializes the internal data structures.
  7.1353 +    ///
  7.1354 +    /// Initializes the internal data structures.
  7.1355 +    ///
  7.1356 +    void init() {
  7.1357 +      create_maps();
  7.1358 +      _stack.resize(countNodes(*_digraph));
  7.1359 +      _stack_head = -1;
  7.1360 +      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
  7.1361 +	_reached->set(u, false);
  7.1362 +      }
  7.1363 +    }
  7.1364 +    
  7.1365 +    /// \brief Adds a new source node.
  7.1366 +    ///
  7.1367 +    /// Adds a new source node to the set of nodes to be processed.
  7.1368 +    void addSource(Node s) {
  7.1369 +      if(!(*_reached)[s]) {
  7.1370 +	  _reached->set(s,true);
  7.1371 +	  _visitor->start(s);
  7.1372 +	  _visitor->reach(s);
  7.1373 +	  Arc e; 
  7.1374 +	  _digraph->firstOut(e, s);
  7.1375 +	  if (e != INVALID) {
  7.1376 +	    _stack[++_stack_head] = e;
  7.1377 +	  } else {
  7.1378 +	    _visitor->leave(s);
  7.1379 +	  }
  7.1380 +	}
  7.1381 +    }
  7.1382 +    
  7.1383 +    /// \brief Processes the next arc.
  7.1384 +    ///
  7.1385 +    /// Processes the next arc.
  7.1386 +    ///
  7.1387 +    /// \return The processed arc.
  7.1388 +    ///
  7.1389 +    /// \pre The stack must not be empty!
  7.1390 +    Arc processNextArc() { 
  7.1391 +      Arc e = _stack[_stack_head];
  7.1392 +      Node m = _digraph->target(e);
  7.1393 +      if(!(*_reached)[m]) {
  7.1394 +	_visitor->discover(e);
  7.1395 +	_visitor->reach(m);
  7.1396 +	_reached->set(m, true);
  7.1397 +	_digraph->firstOut(_stack[++_stack_head], m);
  7.1398 +      } else {
  7.1399 +	_visitor->examine(e);
  7.1400 +	m = _digraph->source(e);
  7.1401 +	_digraph->nextOut(_stack[_stack_head]);
  7.1402 +      }
  7.1403 +      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
  7.1404 +	_visitor->leave(m);
  7.1405 +	--_stack_head;
  7.1406 +	if (_stack_head >= 0) {
  7.1407 +	  _visitor->backtrack(_stack[_stack_head]);
  7.1408 +	  m = _digraph->source(_stack[_stack_head]);
  7.1409 +	  _digraph->nextOut(_stack[_stack_head]);
  7.1410 +	} else {
  7.1411 +	  _visitor->stop(m);	  
  7.1412 +	}
  7.1413 +      }
  7.1414 +      return e;
  7.1415 +    }
  7.1416 +
  7.1417 +    /// \brief Next arc to be processed.
  7.1418 +    ///
  7.1419 +    /// Next arc to be processed.
  7.1420 +    ///
  7.1421 +    /// \return The next arc to be processed or INVALID if the stack is
  7.1422 +    /// empty.
  7.1423 +    Arc nextArc() { 
  7.1424 +      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
  7.1425 +    }
  7.1426 +
  7.1427 +    /// \brief Returns \c false if there are nodes
  7.1428 +    /// to be processed in the queue
  7.1429 +    ///
  7.1430 +    /// Returns \c false if there are nodes
  7.1431 +    /// to be processed in the queue
  7.1432 +    bool emptyQueue() { return _stack_head < 0; }
  7.1433 +
  7.1434 +    /// \brief Returns the number of the nodes to be processed.
  7.1435 +    ///
  7.1436 +    /// Returns the number of the nodes to be processed in the queue.
  7.1437 +    int queueSize() { return _stack_head + 1; }
  7.1438 +    
  7.1439 +    /// \brief Executes the algorithm.
  7.1440 +    ///
  7.1441 +    /// Executes the algorithm.
  7.1442 +    ///
  7.1443 +    /// \pre init() must be called and at least one node should be added
  7.1444 +    /// with addSource() before using this function.
  7.1445 +    void start() {
  7.1446 +      while ( !emptyQueue() ) processNextArc();
  7.1447 +    }
  7.1448 +    
  7.1449 +    /// \brief Executes the algorithm until \c dest is reached.
  7.1450 +    ///
  7.1451 +    /// Executes the algorithm until \c dest is reached.
  7.1452 +    ///
  7.1453 +    /// \pre init() must be called and at least one node should be added
  7.1454 +    /// with addSource() before using this function.
  7.1455 +    void start(Node dest) {
  7.1456 +      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest ) 
  7.1457 +	processNextArc();
  7.1458 +    }
  7.1459 +    
  7.1460 +    /// \brief Executes the algorithm until a condition is met.
  7.1461 +    ///
  7.1462 +    /// Executes the algorithm until a condition is met.
  7.1463 +    ///
  7.1464 +    /// \pre init() must be called and at least one node should be added
  7.1465 +    /// with addSource() before using this function.
  7.1466 +    ///
  7.1467 +    /// \param em must be a bool (or convertible) arc map. The algorithm
  7.1468 +    /// will stop when it reaches an arc \c e with <tt>em[e]</tt> true.
  7.1469 +    ///
  7.1470 +    ///\return The reached arc \c e with <tt>em[e]</tt> true or
  7.1471 +    ///\c INVALID if no such arc was found.
  7.1472 +    ///
  7.1473 +    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map,
  7.1474 +    /// not a node map.
  7.1475 +    template <typename EM>
  7.1476 +    Arc start(const EM &em) {
  7.1477 +      while ( !emptyQueue() && !em[_stack[_stack_head]] )
  7.1478 +        processNextArc();
  7.1479 +      return emptyQueue() ? INVALID : _stack[_stack_head];
  7.1480 +    }
  7.1481 +
  7.1482 +    /// \brief Runs %DFSVisit algorithm from node \c s.
  7.1483 +    ///
  7.1484 +    /// This method runs the %DFS algorithm from a root node \c s.
  7.1485 +    /// \note d.run(s) is just a shortcut of the following code.
  7.1486 +    ///\code
  7.1487 +    ///   d.init();
  7.1488 +    ///   d.addSource(s);
  7.1489 +    ///   d.start();
  7.1490 +    ///\endcode
  7.1491 +    void run(Node s) {
  7.1492 +      init();
  7.1493 +      addSource(s);
  7.1494 +      start();
  7.1495 +    }
  7.1496 +
  7.1497 +    /// \brief Runs %DFSVisit algorithm to visit all nodes in the digraph.
  7.1498 +    
  7.1499 +    /// This method runs the %DFS algorithm in order to
  7.1500 +    /// compute the %DFS path to each node. The algorithm computes
  7.1501 +    /// - The %DFS tree.
  7.1502 +    /// - The distance of each node from the root in the %DFS tree.
  7.1503 +    ///
  7.1504 +    ///\note d.run() is just a shortcut of the following code.
  7.1505 +    ///\code
  7.1506 +    ///  d.init();
  7.1507 +    ///  for (NodeIt it(digraph); it != INVALID; ++it) {
  7.1508 +    ///    if (!d.reached(it)) {
  7.1509 +    ///      d.addSource(it);
  7.1510 +    ///      d.start();
  7.1511 +    ///    }
  7.1512 +    ///  }
  7.1513 +    ///\endcode
  7.1514 +    void run() {
  7.1515 +      init();
  7.1516 +      for (NodeIt it(*_digraph); it != INVALID; ++it) {
  7.1517 +        if (!reached(it)) {
  7.1518 +          addSource(it);
  7.1519 +          start();
  7.1520 +        }
  7.1521 +      }
  7.1522 +    }
  7.1523 +    ///@}
  7.1524 +
  7.1525 +    /// \name Query Functions
  7.1526 +    /// The result of the %DFS algorithm can be obtained using these
  7.1527 +    /// functions.\n
  7.1528 +    /// Before the use of these functions,
  7.1529 +    /// either run() or start() must be called.
  7.1530 +    ///@{
  7.1531 +    /// \brief Checks if a node is reachable from the root.
  7.1532 +    ///
  7.1533 +    /// Returns \c true if \c v is reachable from the root(s).
  7.1534 +    /// \warning The source nodes are inditated as unreachable.
  7.1535 +    /// \pre Either \ref run() or \ref start()
  7.1536 +    /// must be called before using this function.
  7.1537 +    ///
  7.1538 +    bool reached(Node v) { return (*_reached)[v]; }
  7.1539 +    ///@}
  7.1540 +  };
  7.1541 +
  7.1542 +
  7.1543 +} //END OF NAMESPACE LEMON
  7.1544 +
  7.1545 +#endif
  7.1546 +
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/lemon/dijkstra.h	Thu Mar 20 12:12:24 2008 +0000
     8.3 @@ -0,0 +1,1209 @@
     8.4 +/* -*- C++ -*-
     8.5 + *
     8.6 + * This file is a part of LEMON, a generic C++ optimization library
     8.7 + *
     8.8 + * Copyright (C) 2003-2008
     8.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    8.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    8.11 + *
    8.12 + * Permission to use, modify and distribute this software is granted
    8.13 + * provided that this copyright notice appears in all copies. For
    8.14 + * precise terms see the accompanying LICENSE file.
    8.15 + *
    8.16 + * This software is provided "AS IS" with no warranty of any kind,
    8.17 + * express or implied, and with no claim as to its suitability for any
    8.18 + * purpose.
    8.19 + *
    8.20 + */
    8.21 +
    8.22 +#ifndef LEMON_DIJKSTRA_H
    8.23 +#define LEMON_DIJKSTRA_H
    8.24 +
    8.25 +///\ingroup shortest_path
    8.26 +///\file
    8.27 +///\brief Dijkstra algorithm.
    8.28 +///
    8.29 +
    8.30 +#include <lemon/list_digraph.h>
    8.31 +#include <lemon/bin_heap.h>
    8.32 +#include <lemon/bits/path_dump.h>
    8.33 +#include <lemon/bits/invalid.h>
    8.34 +#include <lemon/error.h>
    8.35 +#include <lemon/maps.h>
    8.36 +
    8.37 +
    8.38 +namespace lemon {
    8.39 +
    8.40 +  /// \brief Default OperationTraits for the Dijkstra algorithm class.
    8.41 +  ///  
    8.42 +  /// It defines all computational operations and constants which are
    8.43 +  /// used in the Dijkstra algorithm.
    8.44 +  template <typename Value>
    8.45 +  struct DijkstraDefaultOperationTraits {
    8.46 +    /// \brief Gives back the zero value of the type.
    8.47 +    static Value zero() {
    8.48 +      return static_cast<Value>(0);
    8.49 +    }
    8.50 +    /// \brief Gives back the sum of the given two elements.
    8.51 +    static Value plus(const Value& left, const Value& right) {
    8.52 +      return left + right;
    8.53 +    }
    8.54 +    /// \brief Gives back true only if the first value less than the second.
    8.55 +    static bool less(const Value& left, const Value& right) {
    8.56 +      return left < right;
    8.57 +    }
    8.58 +  };
    8.59 +
    8.60 +  /// \brief Widest path OperationTraits for the Dijkstra algorithm class.
    8.61 +  ///  
    8.62 +  /// It defines all computational operations and constants which are
    8.63 +  /// used in the Dijkstra algorithm for widest path computation.
    8.64 +  template <typename Value>
    8.65 +  struct DijkstraWidestPathOperationTraits {
    8.66 +    /// \brief Gives back the maximum value of the type.
    8.67 +    static Value zero() {
    8.68 +      return std::numeric_limits<Value>::max();
    8.69 +    }
    8.70 +    /// \brief Gives back the minimum of the given two elements.
    8.71 +    static Value plus(const Value& left, const Value& right) {
    8.72 +      return std::min(left, right);
    8.73 +    }
    8.74 +    /// \brief Gives back true only if the first value less than the second.
    8.75 +    static bool less(const Value& left, const Value& right) {
    8.76 +      return left < right;
    8.77 +    }
    8.78 +  };
    8.79 +  
    8.80 +  ///Default traits class of Dijkstra class.
    8.81 +
    8.82 +  ///Default traits class of Dijkstra class.
    8.83 +  ///\param GR Digraph type.
    8.84 +  ///\param LM Type of length map.
    8.85 +  template<class GR, class LM>
    8.86 +  struct DijkstraDefaultTraits
    8.87 +  {
    8.88 +    ///The digraph type the algorithm runs on. 
    8.89 +    typedef GR Digraph;
    8.90 +    ///The type of the map that stores the arc lengths.
    8.91 +
    8.92 +    ///The type of the map that stores the arc lengths.
    8.93 +    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
    8.94 +    typedef LM LengthMap;
    8.95 +    //The type of the length of the arcs.
    8.96 +    typedef typename LM::Value Value;
    8.97 +    /// Operation traits for Dijkstra algorithm.
    8.98 +
    8.99 +    /// It defines the used operation by the algorithm.
   8.100 +    /// \see DijkstraDefaultOperationTraits
   8.101 +    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
   8.102 +    /// The cross reference type used by heap.
   8.103 +
   8.104 +
   8.105 +    /// The cross reference type used by heap.
   8.106 +    /// Usually it is \c Digraph::NodeMap<int>.
   8.107 +    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
   8.108 +    ///Instantiates a HeapCrossRef.
   8.109 +
   8.110 +    ///This function instantiates a \c HeapCrossRef. 
   8.111 +    /// \param G is the digraph, to which we would like to define the 
   8.112 +    /// HeapCrossRef.
   8.113 +    static HeapCrossRef *createHeapCrossRef(const GR &G) 
   8.114 +    {
   8.115 +      return new HeapCrossRef(G);
   8.116 +    }
   8.117 +    
   8.118 +    ///The heap type used by Dijkstra algorithm.
   8.119 +
   8.120 +    ///The heap type used by Dijkstra algorithm.
   8.121 +    ///
   8.122 +    ///\sa BinHeap
   8.123 +    ///\sa Dijkstra
   8.124 +    typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
   8.125 +
   8.126 +    static Heap *createHeap(HeapCrossRef& R) 
   8.127 +    {
   8.128 +      return new Heap(R);
   8.129 +    }
   8.130 +
   8.131 +    ///\brief The type of the map that stores the last
   8.132 +    ///arcs of the shortest paths.
   8.133 +    /// 
   8.134 +    ///The type of the map that stores the last
   8.135 +    ///arcs of the shortest paths.
   8.136 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   8.137 +    ///
   8.138 +    typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap;
   8.139 +    ///Instantiates a PredMap.
   8.140 + 
   8.141 +    ///This function instantiates a \c PredMap. 
   8.142 +    ///\param G is the digraph, to which we would like to define the PredMap.
   8.143 +    ///\todo The digraph alone may be insufficient for the initialization
   8.144 +    static PredMap *createPredMap(const GR &G) 
   8.145 +    {
   8.146 +      return new PredMap(G);
   8.147 +    }
   8.148 +
   8.149 +    ///The type of the map that stores whether a nodes is processed.
   8.150 + 
   8.151 +    ///The type of the map that stores whether a nodes is processed.
   8.152 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   8.153 +    ///By default it is a NullMap.
   8.154 +    ///\todo If it is set to a real map,
   8.155 +    ///Dijkstra::processed() should read this.
   8.156 +    ///\todo named parameter to set this type, function to read and write.
   8.157 +    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
   8.158 +    ///Instantiates a ProcessedMap.
   8.159 + 
   8.160 +    ///This function instantiates a \c ProcessedMap. 
   8.161 +    ///\param g is the digraph, to which
   8.162 +    ///we would like to define the \c ProcessedMap
   8.163 +#ifdef DOXYGEN
   8.164 +    static ProcessedMap *createProcessedMap(const GR &g)
   8.165 +#else
   8.166 +    static ProcessedMap *createProcessedMap(const GR &)
   8.167 +#endif
   8.168 +    {
   8.169 +      return new ProcessedMap();
   8.170 +    }
   8.171 +    ///The type of the map that stores the dists of the nodes.
   8.172 + 
   8.173 +    ///The type of the map that stores the dists of the nodes.
   8.174 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   8.175 +    ///
   8.176 +    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
   8.177 +    ///Instantiates a DistMap.
   8.178 + 
   8.179 +    ///This function instantiates a \ref DistMap. 
   8.180 +    ///\param G is the digraph, to which we would like to define the \ref DistMap
   8.181 +    static DistMap *createDistMap(const GR &G)
   8.182 +    {
   8.183 +      return new DistMap(G);
   8.184 +    }
   8.185 +  };
   8.186 +  
   8.187 +  ///%Dijkstra algorithm class.
   8.188 +  
   8.189 +  /// \ingroup shortest_path
   8.190 +  ///This class provides an efficient implementation of %Dijkstra algorithm.
   8.191 +  ///The arc lengths are passed to the algorithm using a
   8.192 +  ///\ref concepts::ReadMap "ReadMap",
   8.193 +  ///so it is easy to change it to any kind of length.
   8.194 +  ///
   8.195 +  ///The type of the length is determined by the
   8.196 +  ///\ref concepts::ReadMap::Value "Value" of the length map.
   8.197 +  ///
   8.198 +  ///It is also possible to change the underlying priority heap.
   8.199 +  ///
   8.200 +  ///\param GR The digraph type the algorithm runs on. The default value
   8.201 +  ///is \ref ListDigraph. The value of GR is not used directly by
   8.202 +  ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
   8.203 +  ///\param LM This read-only ArcMap determines the lengths of the
   8.204 +  ///arcs. It is read once for each arc, so the map may involve in
   8.205 +  ///relatively time consuming process to compute the arc length if
   8.206 +  ///it is necessary. The default map type is \ref
   8.207 +  ///concepts::Digraph::ArcMap "Digraph::ArcMap<int>".  The value
   8.208 +  ///of LM is not used directly by Dijkstra, it is only passed to \ref
   8.209 +  ///DijkstraDefaultTraits.  \param TR Traits class to set
   8.210 +  ///various data types used by the algorithm.  The default traits
   8.211 +  ///class is \ref DijkstraDefaultTraits
   8.212 +  ///"DijkstraDefaultTraits<GR,LM>".  See \ref
   8.213 +  ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
   8.214 +  ///class.
   8.215 +  ///
   8.216 +  ///\author Jacint Szabo and Alpar Juttner
   8.217 +
   8.218 +#ifdef DOXYGEN
   8.219 +  template <typename GR, typename LM, typename TR>
   8.220 +#else
   8.221 +  template <typename GR=ListDigraph,
   8.222 +	    typename LM=typename GR::template ArcMap<int>,
   8.223 +	    typename TR=DijkstraDefaultTraits<GR,LM> >
   8.224 +#endif
   8.225 +  class Dijkstra {
   8.226 +  public:
   8.227 +    /**
   8.228 +     * \brief \ref Exception for uninitialized parameters.
   8.229 +     *
   8.230 +     * This error represents problems in the initialization
   8.231 +     * of the parameters of the algorithms.
   8.232 +     */
   8.233 +    class UninitializedParameter : public lemon::UninitializedParameter {
   8.234 +    public:
   8.235 +      virtual const char* what() const throw() {
   8.236 +	return "lemon::Dijkstra::UninitializedParameter";
   8.237 +      }
   8.238 +    };
   8.239 +
   8.240 +    typedef TR Traits;
   8.241 +    ///The type of the underlying digraph.
   8.242 +    typedef typename TR::Digraph Digraph;
   8.243 +    ///\e
   8.244 +    typedef typename Digraph::Node Node;
   8.245 +    ///\e
   8.246 +    typedef typename Digraph::NodeIt NodeIt;
   8.247 +    ///\e
   8.248 +    typedef typename Digraph::Arc Arc;
   8.249 +    ///\e
   8.250 +    typedef typename Digraph::OutArcIt OutArcIt;
   8.251 +    
   8.252 +    ///The type of the length of the arcs.
   8.253 +    typedef typename TR::LengthMap::Value Value;
   8.254 +    ///The type of the map that stores the arc lengths.
   8.255 +    typedef typename TR::LengthMap LengthMap;
   8.256 +    ///\brief The type of the map that stores the last
   8.257 +    ///arcs of the shortest paths.
   8.258 +    typedef typename TR::PredMap PredMap;
   8.259 +    ///The type of the map indicating if a node is processed.
   8.260 +    typedef typename TR::ProcessedMap ProcessedMap;
   8.261 +    ///The type of the map that stores the dists of the nodes.
   8.262 +    typedef typename TR::DistMap DistMap;
   8.263 +    ///The cross reference type used for the current heap.
   8.264 +    typedef typename TR::HeapCrossRef HeapCrossRef;
   8.265 +    ///The heap type used by the dijkstra algorithm.
   8.266 +    typedef typename TR::Heap Heap;
   8.267 +    ///The operation traits.
   8.268 +    typedef typename TR::OperationTraits OperationTraits;
   8.269 +  private:
   8.270 +    /// Pointer to the underlying digraph.
   8.271 +    const Digraph *G;
   8.272 +    /// Pointer to the length map
   8.273 +    const LengthMap *length;
   8.274 +    ///Pointer to the map of predecessors arcs.
   8.275 +    PredMap *_pred;
   8.276 +    ///Indicates if \ref _pred is locally allocated (\c true) or not.
   8.277 +    bool local_pred;
   8.278 +    ///Pointer to the map of distances.
   8.279 +    DistMap *_dist;
   8.280 +    ///Indicates if \ref _dist is locally allocated (\c true) or not.
   8.281 +    bool local_dist;
   8.282 +    ///Pointer to the map of processed status of the nodes.
   8.283 +    ProcessedMap *_processed;
   8.284 +    ///Indicates if \ref _processed is locally allocated (\c true) or not.
   8.285 +    bool local_processed;
   8.286 +    ///Pointer to the heap cross references.
   8.287 +    HeapCrossRef *_heap_cross_ref;
   8.288 +    ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
   8.289 +    bool local_heap_cross_ref;
   8.290 +    ///Pointer to the heap.
   8.291 +    Heap *_heap;
   8.292 +    ///Indicates if \ref _heap is locally allocated (\c true) or not.
   8.293 +    bool local_heap;
   8.294 +
   8.295 +    ///Creates the maps if necessary.
   8.296 +    
   8.297 +    ///\todo Better memory allocation (instead of new).
   8.298 +    void create_maps() 
   8.299 +    {
   8.300 +      if(!_pred) {
   8.301 +	local_pred = true;
   8.302 +	_pred = Traits::createPredMap(*G);
   8.303 +      }
   8.304 +      if(!_dist) {
   8.305 +	local_dist = true;
   8.306 +	_dist = Traits::createDistMap(*G);
   8.307 +      }
   8.308 +      if(!_processed) {
   8.309 +	local_processed = true;
   8.310 +	_processed = Traits::createProcessedMap(*G);
   8.311 +      }
   8.312 +      if (!_heap_cross_ref) {
   8.313 +	local_heap_cross_ref = true;
   8.314 +	_heap_cross_ref = Traits::createHeapCrossRef(*G);
   8.315 +      }
   8.316 +      if (!_heap) {
   8.317 +	local_heap = true;
   8.318 +	_heap = Traits::createHeap(*_heap_cross_ref);
   8.319 +      }
   8.320 +    }
   8.321 +    
   8.322 +  public :
   8.323 +
   8.324 +    typedef Dijkstra Create;
   8.325 + 
   8.326 +    ///\name Named template parameters
   8.327 +
   8.328 +    ///@{
   8.329 +
   8.330 +    template <class T>
   8.331 +    struct DefPredMapTraits : public Traits {
   8.332 +      typedef T PredMap;
   8.333 +      static PredMap *createPredMap(const Digraph &)
   8.334 +      {
   8.335 +	throw UninitializedParameter();
   8.336 +      }
   8.337 +    };
   8.338 +    ///\ref named-templ-param "Named parameter" for setting PredMap type
   8.339 +
   8.340 +    ///\ref named-templ-param "Named parameter" for setting PredMap type
   8.341 +    ///
   8.342 +    template <class T>
   8.343 +    struct DefPredMap 
   8.344 +      : public Dijkstra< Digraph,	LengthMap, DefPredMapTraits<T> > {
   8.345 +      typedef Dijkstra< Digraph,	LengthMap, DefPredMapTraits<T> > Create;
   8.346 +    };
   8.347 +    
   8.348 +    template <class T>
   8.349 +    struct DefDistMapTraits : public Traits {
   8.350 +      typedef T DistMap;
   8.351 +      static DistMap *createDistMap(const Digraph &)
   8.352 +      {
   8.353 +	throw UninitializedParameter();
   8.354 +      }
   8.355 +    };
   8.356 +    ///\ref named-templ-param "Named parameter" for setting DistMap type
   8.357 +
   8.358 +    ///\ref named-templ-param "Named parameter" for setting DistMap type
   8.359 +    ///
   8.360 +    template <class T>
   8.361 +    struct DefDistMap 
   8.362 +      : public Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > { 
   8.363 +      typedef Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > Create;
   8.364 +    };
   8.365 +    
   8.366 +    template <class T>
   8.367 +    struct DefProcessedMapTraits : public Traits {
   8.368 +      typedef T ProcessedMap;
   8.369 +      static ProcessedMap *createProcessedMap(const Digraph &G) 
   8.370 +      {
   8.371 +	throw UninitializedParameter();
   8.372 +      }
   8.373 +    };
   8.374 +    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   8.375 +
   8.376 +    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   8.377 +    ///
   8.378 +    template <class T>
   8.379 +    struct DefProcessedMap 
   8.380 +      : public Dijkstra< Digraph,	LengthMap, DefProcessedMapTraits<T> > { 
   8.381 +      typedef Dijkstra< Digraph,	LengthMap, DefProcessedMapTraits<T> > Create;
   8.382 +    };
   8.383 +    
   8.384 +    struct DefDigraphProcessedMapTraits : public Traits {
   8.385 +      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
   8.386 +      static ProcessedMap *createProcessedMap(const Digraph &G) 
   8.387 +      {
   8.388 +	return new ProcessedMap(G);
   8.389 +      }
   8.390 +    };
   8.391 +    ///\brief \ref named-templ-param "Named parameter"
   8.392 +    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
   8.393 +    ///
   8.394 +    ///\ref named-templ-param "Named parameter"
   8.395 +    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
   8.396 +    ///If you don't set it explicitely, it will be automatically allocated.
   8.397 +    template <class T>
   8.398 +    struct DefProcessedMapToBeDefaultMap 
   8.399 +      : public Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> {
   8.400 +      typedef Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> Create;
   8.401 +    };
   8.402 +
   8.403 +    template <class H, class CR>
   8.404 +    struct DefHeapTraits : public Traits {
   8.405 +      typedef CR HeapCrossRef;
   8.406 +      typedef H Heap;
   8.407 +      static HeapCrossRef *createHeapCrossRef(const Digraph &) {
   8.408 +	throw UninitializedParameter();
   8.409 +      }
   8.410 +      static Heap *createHeap(HeapCrossRef &) 
   8.411 +      {
   8.412 +	throw UninitializedParameter();
   8.413 +      }
   8.414 +    };
   8.415 +    ///\brief \ref named-templ-param "Named parameter" for setting
   8.416 +    ///heap and cross reference type
   8.417 +    ///
   8.418 +    ///\ref named-templ-param "Named parameter" for setting heap and cross 
   8.419 +    ///reference type
   8.420 +    ///
   8.421 +    template <class H, class CR = typename Digraph::template NodeMap<int> >
   8.422 +    struct DefHeap
   8.423 +      : public Dijkstra< Digraph,	LengthMap, DefHeapTraits<H, CR> > { 
   8.424 +      typedef Dijkstra< Digraph,	LengthMap, DefHeapTraits<H, CR> > Create;
   8.425 +    };
   8.426 +
   8.427 +    template <class H, class CR>
   8.428 +    struct DefStandardHeapTraits : public Traits {
   8.429 +      typedef CR HeapCrossRef;
   8.430 +      typedef H Heap;
   8.431 +      static HeapCrossRef *createHeapCrossRef(const Digraph &G) {
   8.432 +	return new HeapCrossRef(G);
   8.433 +      }
   8.434 +      static Heap *createHeap(HeapCrossRef &R) 
   8.435 +      {
   8.436 +	return new Heap(R);
   8.437 +      }
   8.438 +    };
   8.439 +    ///\brief \ref named-templ-param "Named parameter" for setting
   8.440 +    ///heap and cross reference type with automatic allocation
   8.441 +    ///
   8.442 +    ///\ref named-templ-param "Named parameter" for setting heap and cross 
   8.443 +    ///reference type. It can allocate the heap and the cross reference 
   8.444 +    ///object if the cross reference's constructor waits for the digraph as 
   8.445 +    ///parameter and the heap's constructor waits for the cross reference.
   8.446 +    template <class H, class CR = typename Digraph::template NodeMap<int> >
   8.447 +    struct DefStandardHeap
   8.448 +      : public Dijkstra< Digraph,	LengthMap, DefStandardHeapTraits<H, CR> > { 
   8.449 +      typedef Dijkstra< Digraph,	LengthMap, DefStandardHeapTraits<H, CR> > 
   8.450 +      Create;
   8.451 +    };
   8.452 +
   8.453 +    template <class T>
   8.454 +    struct DefOperationTraitsTraits : public Traits {
   8.455 +      typedef T OperationTraits;
   8.456 +    };
   8.457 +    
   8.458 +    /// \brief \ref named-templ-param "Named parameter" for setting 
   8.459 +    /// OperationTraits type
   8.460 +    ///
   8.461 +    /// \ref named-templ-param "Named parameter" for setting OperationTraits
   8.462 +    /// type
   8.463 +    template <class T>
   8.464 +    struct DefOperationTraits
   8.465 +      : public Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> > {
   8.466 +      typedef Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> >
   8.467 +      Create;
   8.468 +    };
   8.469 +    
   8.470 +    ///@}
   8.471 +
   8.472 +
   8.473 +  protected:
   8.474 +
   8.475 +    Dijkstra() {}
   8.476 +
   8.477 +  public:      
   8.478 +    
   8.479 +    ///Constructor.
   8.480 +    
   8.481 +    ///\param _G the digraph the algorithm will run on.
   8.482 +    ///\param _length the length map used by the algorithm.
   8.483 +    Dijkstra(const Digraph& _G, const LengthMap& _length) :
   8.484 +      G(&_G), length(&_length),
   8.485 +      _pred(NULL), local_pred(false),
   8.486 +      _dist(NULL), local_dist(false),
   8.487 +      _processed(NULL), local_processed(false),
   8.488 +      _heap_cross_ref(NULL), local_heap_cross_ref(false),
   8.489 +      _heap(NULL), local_heap(false)
   8.490 +    { }
   8.491 +    
   8.492 +    ///Destructor.
   8.493 +    ~Dijkstra() 
   8.494 +    {
   8.495 +      if(local_pred) delete _pred;
   8.496 +      if(local_dist) delete _dist;
   8.497 +      if(local_processed) delete _processed;
   8.498 +      if(local_heap_cross_ref) delete _heap_cross_ref;
   8.499 +      if(local_heap) delete _heap;
   8.500 +    }
   8.501 +
   8.502 +    ///Sets the length map.
   8.503 +
   8.504 +    ///Sets the length map.
   8.505 +    ///\return <tt> (*this) </tt>
   8.506 +    Dijkstra &lengthMap(const LengthMap &m) 
   8.507 +    {
   8.508 +      length = &m;
   8.509 +      return *this;
   8.510 +    }
   8.511 +
   8.512 +    ///Sets the map storing the predecessor arcs.
   8.513 +
   8.514 +    ///Sets the map storing the predecessor arcs.
   8.515 +    ///If you don't use this function before calling \ref run(),
   8.516 +    ///it will allocate one. The destuctor deallocates this
   8.517 +    ///automatically allocated map, of course.
   8.518 +    ///\return <tt> (*this) </tt>
   8.519 +    Dijkstra &predMap(PredMap &m) 
   8.520 +    {
   8.521 +      if(local_pred) {
   8.522 +	delete _pred;
   8.523 +	local_pred=false;
   8.524 +      }
   8.525 +      _pred = &m;
   8.526 +      return *this;
   8.527 +    }
   8.528 +
   8.529 +    ///Sets the map storing the distances calculated by the algorithm.
   8.530 +
   8.531 +    ///Sets the map storing the distances calculated by the algorithm.
   8.532 +    ///If you don't use this function before calling \ref run(),
   8.533 +    ///it will allocate one. The destuctor deallocates this
   8.534 +    ///automatically allocated map, of course.
   8.535 +    ///\return <tt> (*this) </tt>
   8.536 +    Dijkstra &distMap(DistMap &m) 
   8.537 +    {
   8.538 +      if(local_dist) {
   8.539 +	delete _dist;
   8.540 +	local_dist=false;
   8.541 +      }
   8.542 +      _dist = &m;
   8.543 +      return *this;
   8.544 +    }
   8.545 +
   8.546 +    ///Sets the heap and the cross reference used by algorithm.
   8.547 +
   8.548 +    ///Sets the heap and the cross reference used by algorithm.
   8.549 +    ///If you don't use this function before calling \ref run(),
   8.550 +    ///it will allocate one. The destuctor deallocates this
   8.551 +    ///automatically allocated heap and cross reference, of course.
   8.552 +    ///\return <tt> (*this) </tt>
   8.553 +    Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
   8.554 +    {
   8.555 +      if(local_heap_cross_ref) {
   8.556 +	delete _heap_cross_ref;
   8.557 +	local_heap_cross_ref=false;
   8.558 +      }
   8.559 +      _heap_cross_ref = &cr;
   8.560 +      if(local_heap) {
   8.561 +	delete _heap;
   8.562 +	local_heap=false;
   8.563 +      }
   8.564 +      _heap = &hp;
   8.565 +      return *this;
   8.566 +    }
   8.567 +
   8.568 +  private:
   8.569 +    void finalizeNodeData(Node v,Value dst)
   8.570 +    {
   8.571 +      _processed->set(v,true);
   8.572 +      _dist->set(v, dst);
   8.573 +    }
   8.574 +
   8.575 +  public:
   8.576 +
   8.577 +    typedef PredMapPath<Digraph, PredMap> Path;
   8.578 +
   8.579 +    ///\name Execution control
   8.580 +    ///The simplest way to execute the algorithm is to use
   8.581 +    ///one of the member functions called \c run(...).
   8.582 +    ///\n
   8.583 +    ///If you need more control on the execution,
   8.584 +    ///first you must call \ref init(), then you can add several source nodes
   8.585 +    ///with \ref addSource().
   8.586 +    ///Finally \ref start() will perform the actual path
   8.587 +    ///computation.
   8.588 +
   8.589 +    ///@{
   8.590 +
   8.591 +    ///Initializes the internal data structures.
   8.592 +
   8.593 +    ///Initializes the internal data structures.
   8.594 +    ///
   8.595 +    void init()
   8.596 +    {
   8.597 +      create_maps();
   8.598 +      _heap->clear();
   8.599 +      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   8.600 +	_pred->set(u,INVALID);
   8.601 +	_processed->set(u,false);
   8.602 +	_heap_cross_ref->set(u,Heap::PRE_HEAP);
   8.603 +      }
   8.604 +    }
   8.605 +    
   8.606 +    ///Adds a new source node.
   8.607 +
   8.608 +    ///Adds a new source node to the priority heap.
   8.609 +    ///
   8.610 +    ///The optional second parameter is the initial distance of the node.
   8.611 +    ///
   8.612 +    ///It checks if the node has already been added to the heap and
   8.613 +    ///it is pushed to the heap only if either it was not in the heap
   8.614 +    ///or the shortest path found till then is shorter than \c dst.
   8.615 +    void addSource(Node s,Value dst=OperationTraits::zero())
   8.616 +    {
   8.617 +      if(_heap->state(s) != Heap::IN_HEAP) {
   8.618 +	_heap->push(s,dst);
   8.619 +      } else if(OperationTraits::less((*_heap)[s], dst)) {
   8.620 +	_heap->set(s,dst);
   8.621 +	_pred->set(s,INVALID);
   8.622 +      }
   8.623 +    }
   8.624 +    
   8.625 +    ///Processes the next node in the priority heap
   8.626 +
   8.627 +    ///Processes the next node in the priority heap.
   8.628 +    ///
   8.629 +    ///\return The processed node.
   8.630 +    ///
   8.631 +    ///\warning The priority heap must not be empty!
   8.632 +    Node processNextNode()
   8.633 +    {
   8.634 +      Node v=_heap->top(); 
   8.635 +      Value oldvalue=_heap->prio();
   8.636 +      _heap->pop();
   8.637 +      finalizeNodeData(v,oldvalue);
   8.638 +      
   8.639 +      for(OutArcIt e(*G,v); e!=INVALID; ++e) {
   8.640 +	Node w=G->target(e); 
   8.641 +	switch(_heap->state(w)) {
   8.642 +	case Heap::PRE_HEAP:
   8.643 +	  _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); 
   8.644 +	  _pred->set(w,e);
   8.645 +	  break;
   8.646 +	case Heap::IN_HEAP:
   8.647 +	  {
   8.648 +	    Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]);
   8.649 +	    if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
   8.650 +	      _heap->decrease(w, newvalue); 
   8.651 +	      _pred->set(w,e);
   8.652 +	    }
   8.653 +	  }
   8.654 +	  break;
   8.655 +	case Heap::POST_HEAP:
   8.656 +	  break;
   8.657 +	}
   8.658 +      }
   8.659 +      return v;
   8.660 +    }
   8.661 +
   8.662 +    ///Next node to be processed.
   8.663 +    
   8.664 +    ///Next node to be processed.
   8.665 +    ///
   8.666 +    ///\return The next node to be processed or INVALID if the priority heap
   8.667 +    /// is empty.
   8.668 +    Node nextNode()
   8.669 +    { 
   8.670 +      return !_heap->empty()?_heap->top():INVALID;
   8.671 +    }
   8.672 + 
   8.673 +    ///\brief Returns \c false if there are nodes
   8.674 +    ///to be processed in the priority heap
   8.675 +    ///
   8.676 +    ///Returns \c false if there are nodes
   8.677 +    ///to be processed in the priority heap
   8.678 +    bool emptyQueue() { return _heap->empty(); }
   8.679 +    ///Returns the number of the nodes to be processed in the priority heap
   8.680 +
   8.681 +    ///Returns the number of the nodes to be processed in the priority heap
   8.682 +    ///
   8.683 +    int queueSize() { return _heap->size(); }
   8.684 +    
   8.685 +    ///Executes the algorithm.
   8.686 +
   8.687 +    ///Executes the algorithm.
   8.688 +    ///
   8.689 +    ///\pre init() must be called and at least one node should be added
   8.690 +    ///with addSource() before using this function.
   8.691 +    ///
   8.692 +    ///This method runs the %Dijkstra algorithm from the root node(s)
   8.693 +    ///in order to
   8.694 +    ///compute the
   8.695 +    ///shortest path to each node. The algorithm computes
   8.696 +    ///- The shortest path tree.
   8.697 +    ///- The distance of each node from the root(s).
   8.698 +    ///
   8.699 +    void start()
   8.700 +    {
   8.701 +      while ( !_heap->empty() ) processNextNode();
   8.702 +    }
   8.703 +    
   8.704 +    ///Executes the algorithm until \c dest is reached.
   8.705 +
   8.706 +    ///Executes the algorithm until \c dest is reached.
   8.707 +    ///
   8.708 +    ///\pre init() must be called and at least one node should be added
   8.709 +    ///with addSource() before using this function.
   8.710 +    ///
   8.711 +    ///This method runs the %Dijkstra algorithm from the root node(s)
   8.712 +    ///in order to
   8.713 +    ///compute the
   8.714 +    ///shortest path to \c dest. The algorithm computes
   8.715 +    ///- The shortest path to \c  dest.
   8.716 +    ///- The distance of \c dest from the root(s).
   8.717 +    ///
   8.718 +    void start(Node dest)
   8.719 +    {
   8.720 +      while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
   8.721 +      if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
   8.722 +    }
   8.723 +    
   8.724 +    ///Executes the algorithm until a condition is met.
   8.725 +
   8.726 +    ///Executes the algorithm until a condition is met.
   8.727 +    ///
   8.728 +    ///\pre init() must be called and at least one node should be added
   8.729 +    ///with addSource() before using this function.
   8.730 +    ///
   8.731 +    ///\param nm must be a bool (or convertible) node map. The algorithm
   8.732 +    ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
   8.733 +    ///
   8.734 +    ///\return The reached node \c v with <tt>nm[v]</tt> true or
   8.735 +    ///\c INVALID if no such node was found.
   8.736 +    template<class NodeBoolMap>
   8.737 +    Node start(const NodeBoolMap &nm)
   8.738 +    {
   8.739 +      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
   8.740 +      if ( _heap->empty() ) return INVALID;
   8.741 +      finalizeNodeData(_heap->top(),_heap->prio());
   8.742 +      return _heap->top();
   8.743 +    }
   8.744 +    
   8.745 +    ///Runs %Dijkstra algorithm from node \c s.
   8.746 +    
   8.747 +    ///This method runs the %Dijkstra algorithm from a root node \c s
   8.748 +    ///in order to
   8.749 +    ///compute the
   8.750 +    ///shortest path to each node. The algorithm computes
   8.751 +    ///- The shortest path tree.
   8.752 +    ///- The distance of each node from the root.
   8.753 +    ///
   8.754 +    ///\note d.run(s) is just a shortcut of the following code.
   8.755 +    ///\code
   8.756 +    ///  d.init();
   8.757 +    ///  d.addSource(s);
   8.758 +    ///  d.start();
   8.759 +    ///\endcode
   8.760 +    void run(Node s) {
   8.761 +      init();
   8.762 +      addSource(s);
   8.763 +      start();
   8.764 +    }
   8.765 +    
   8.766 +    ///Finds the shortest path between \c s and \c t.
   8.767 +    
   8.768 +    ///Finds the shortest path between \c s and \c t.
   8.769 +    ///
   8.770 +    ///\return The length of the shortest s---t path if there exists one,
   8.771 +    ///0 otherwise.
   8.772 +    ///\note Apart from the return value, d.run(s) is
   8.773 +    ///just a shortcut of the following code.
   8.774 +    ///\code
   8.775 +    ///  d.init();
   8.776 +    ///  d.addSource(s);
   8.777 +    ///  d.start(t);
   8.778 +    ///\endcode
   8.779 +    Value run(Node s,Node t) {
   8.780 +      init();
   8.781 +      addSource(s);
   8.782 +      start(t);
   8.783 +      return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t];
   8.784 +    }
   8.785 +    
   8.786 +    ///@}
   8.787 +
   8.788 +    ///\name Query Functions
   8.789 +    ///The result of the %Dijkstra algorithm can be obtained using these
   8.790 +    ///functions.\n
   8.791 +    ///Before the use of these functions,
   8.792 +    ///either run() or start() must be called.
   8.793 +    
   8.794 +    ///@{
   8.795 +
   8.796 +    ///Gives back the shortest path.
   8.797 +    
   8.798 +    ///Gives back the shortest path.
   8.799 +    ///\pre The \c t should be reachable from the source.
   8.800 +    Path path(Node t) 
   8.801 +    {
   8.802 +      return Path(*G, *_pred, t);
   8.803 +    }
   8.804 +
   8.805 +    ///The distance of a node from the root.
   8.806 +
   8.807 +    ///Returns the distance of a node from the root.
   8.808 +    ///\pre \ref run() must be called before using this function.
   8.809 +    ///\warning If node \c v in unreachable from the root the return value
   8.810 +    ///of this funcion is undefined.
   8.811 +    Value dist(Node v) const { return (*_dist)[v]; }
   8.812 +
   8.813 +    ///The current distance of a node from the root.
   8.814 +
   8.815 +    ///Returns the current distance of a node from the root.
   8.816 +    ///It may be decreased in the following processes.
   8.817 +    ///\pre \c node should be reached but not processed
   8.818 +    Value currentDist(Node v) const { return (*_heap)[v]; }
   8.819 +
   8.820 +    ///Returns the 'previous arc' of the shortest path tree.
   8.821 +
   8.822 +    ///For a node \c v it returns the 'previous arc' of the shortest path tree,
   8.823 +    ///i.e. it returns the last arc of a shortest path from the root to \c
   8.824 +    ///v. It is \ref INVALID
   8.825 +    ///if \c v is unreachable from the root or if \c v=s. The
   8.826 +    ///shortest path tree used here is equal to the shortest path tree used in
   8.827 +    ///\ref predNode().  \pre \ref run() must be called before using
   8.828 +    ///this function.
   8.829 +    Arc predArc(Node v) const { return (*_pred)[v]; }
   8.830 +
   8.831 +    ///Returns the 'previous node' of the shortest path tree.
   8.832 +
   8.833 +    ///For a node \c v it returns the 'previous node' of the shortest path tree,
   8.834 +    ///i.e. it returns the last but one node from a shortest path from the
   8.835 +    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
   8.836 +    ///\c v=s. The shortest path tree used here is equal to the shortest path
   8.837 +    ///tree used in \ref predArc().  \pre \ref run() must be called before
   8.838 +    ///using this function.
   8.839 +    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   8.840 +				  G->source((*_pred)[v]); }
   8.841 +    
   8.842 +    ///Returns a reference to the NodeMap of distances.
   8.843 +
   8.844 +    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
   8.845 +    ///be called before using this function.
   8.846 +    const DistMap &distMap() const { return *_dist;}
   8.847 + 
   8.848 +    ///Returns a reference to the shortest path tree map.
   8.849 +
   8.850 +    ///Returns a reference to the NodeMap of the arcs of the
   8.851 +    ///shortest path tree.
   8.852 +    ///\pre \ref run() must be called before using this function.
   8.853 +    const PredMap &predMap() const { return *_pred;}
   8.854 + 
   8.855 +    ///Checks if a node is reachable from the root.
   8.856 +
   8.857 +    ///Returns \c true if \c v is reachable from the root.
   8.858 +    ///\warning The source nodes are inditated as unreached.
   8.859 +    ///\pre \ref run() must be called before using this function.
   8.860 +    ///
   8.861 +    bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
   8.862 +
   8.863 +    ///Checks if a node is processed.
   8.864 +
   8.865 +    ///Returns \c true if \c v is processed, i.e. the shortest
   8.866 +    ///path to \c v has already found.
   8.867 +    ///\pre \ref run() must be called before using this function.
   8.868 +    ///
   8.869 +    bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
   8.870 +    
   8.871 +    ///@}
   8.872 +  };
   8.873 +
   8.874 +
   8.875 +
   8.876 +
   8.877 + 
   8.878 +  ///Default traits class of Dijkstra function.
   8.879 +
   8.880 +  ///Default traits class of Dijkstra function.
   8.881 +  ///\param GR Digraph type.
   8.882 +  ///\param LM Type of length map.
   8.883 +  template<class GR, class LM>
   8.884 +  struct DijkstraWizardDefaultTraits
   8.885 +  {
   8.886 +    ///The digraph type the algorithm runs on. 
   8.887 +    typedef GR Digraph;
   8.888 +    ///The type of the map that stores the arc lengths.
   8.889 +
   8.890 +    ///The type of the map that stores the arc lengths.
   8.891 +    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
   8.892 +    typedef LM LengthMap;
   8.893 +    //The type of the length of the arcs.
   8.894 +    typedef typename LM::Value Value;
   8.895 +    /// Operation traits for Dijkstra algorithm.
   8.896 +
   8.897 +    /// It defines the used operation by the algorithm.
   8.898 +    /// \see DijkstraDefaultOperationTraits
   8.899 +    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
   8.900 +    ///The heap type used by Dijkstra algorithm.
   8.901 +
   8.902 +    /// The cross reference type used by heap.
   8.903 +
   8.904 +    /// The cross reference type used by heap.
   8.905 +    /// Usually it is \c Digraph::NodeMap<int>.
   8.906 +    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
   8.907 +    ///Instantiates a HeapCrossRef.
   8.908 +
   8.909 +    ///This function instantiates a \ref HeapCrossRef. 
   8.910 +    /// \param G is the digraph, to which we would like to define the 
   8.911 +    /// HeapCrossRef.
   8.912 +    /// \todo The digraph alone may be insufficient for the initialization
   8.913 +    static HeapCrossRef *createHeapCrossRef(const GR &G) 
   8.914 +    {
   8.915 +      return new HeapCrossRef(G);
   8.916 +    }
   8.917 +    
   8.918 +    ///The heap type used by Dijkstra algorithm.
   8.919 +
   8.920 +    ///The heap type used by Dijkstra algorithm.
   8.921 +    ///
   8.922 +    ///\sa BinHeap
   8.923 +    ///\sa Dijkstra
   8.924 +    typedef BinHeap<typename LM::Value, typename GR::template NodeMap<int>,
   8.925 +		    std::less<Value> > Heap;
   8.926 +
   8.927 +    static Heap *createHeap(HeapCrossRef& R) 
   8.928 +    {
   8.929 +      return new Heap(R);
   8.930 +    }
   8.931 +
   8.932 +    ///\brief The type of the map that stores the last
   8.933 +    ///arcs of the shortest paths.
   8.934 +    /// 
   8.935 +    ///The type of the map that stores the last
   8.936 +    ///arcs of the shortest paths.
   8.937 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   8.938 +    ///
   8.939 +    typedef NullMap <typename GR::Node,typename GR::Arc> PredMap;
   8.940 +    ///Instantiates a PredMap.
   8.941 + 
   8.942 +    ///This function instantiates a \ref PredMap. 
   8.943 +    ///\param g is the digraph, to which we would like to define the PredMap.
   8.944 +    ///\todo The digraph alone may be insufficient for the initialization
   8.945 +#ifdef DOXYGEN
   8.946 +    static PredMap *createPredMap(const GR &g) 
   8.947 +#else
   8.948 +    static PredMap *createPredMap(const GR &) 
   8.949 +#endif
   8.950 +    {
   8.951 +      return new PredMap();
   8.952 +    }
   8.953 +    ///The type of the map that stores whether a nodes is processed.
   8.954 + 
   8.955 +    ///The type of the map that stores whether a nodes is processed.
   8.956 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   8.957 +    ///By default it is a NullMap.
   8.958 +    ///\todo If it is set to a real map,
   8.959 +    ///Dijkstra::processed() should read this.
   8.960 +    ///\todo named parameter to set this type, function to read and write.
   8.961 +    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
   8.962 +    ///Instantiates a ProcessedMap.
   8.963 + 
   8.964 +    ///This function instantiates a \ref ProcessedMap. 
   8.965 +    ///\param g is the digraph, to which
   8.966 +    ///we would like to define the \ref ProcessedMap
   8.967 +#ifdef DOXYGEN
   8.968 +    static ProcessedMap *createProcessedMap(const GR &g)
   8.969 +#else
   8.970 +    static ProcessedMap *createProcessedMap(const GR &)
   8.971 +#endif
   8.972 +    {
   8.973 +      return new ProcessedMap();
   8.974 +    }
   8.975 +    ///The type of the map that stores the dists of the nodes.
   8.976 + 
   8.977 +    ///The type of the map that stores the dists of the nodes.
   8.978 +    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   8.979 +    ///
   8.980 +    typedef NullMap<typename Digraph::Node,typename LM::Value> DistMap;
   8.981 +    ///Instantiates a DistMap.
   8.982 + 
   8.983 +    ///This function instantiates a \ref DistMap. 
   8.984 +    ///\param g is the digraph, to which we would like to define the \ref DistMap
   8.985 +#ifdef DOXYGEN
   8.986 +    static DistMap *createDistMap(const GR &g)
   8.987 +#else
   8.988 +    static DistMap *createDistMap(const GR &)
   8.989 +#endif
   8.990 +    {
   8.991 +      return new DistMap();
   8.992 +    }
   8.993 +  };
   8.994 +  
   8.995 +  /// Default traits used by \ref DijkstraWizard
   8.996 +
   8.997 +  /// To make it easier to use Dijkstra algorithm
   8.998 +  ///we have created a wizard class.
   8.999 +  /// This \ref DijkstraWizard class needs default traits,
  8.1000 +  ///as well as the \ref Dijkstra class.
  8.1001 +  /// The \ref DijkstraWizardBase is a class to be the default traits of the
  8.1002 +  /// \ref DijkstraWizard class.
  8.1003 +  /// \todo More named parameters are required...
  8.1004 +  template<class GR,class LM>
  8.1005 +  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
  8.1006 +  {
  8.1007 +
  8.1008 +    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
  8.1009 +  protected:
  8.1010 +    /// Type of the nodes in the digraph.
  8.1011 +    typedef typename Base::Digraph::Node Node;
  8.1012 +
  8.1013 +    /// Pointer to the underlying digraph.
  8.1014 +    void *_g;
  8.1015 +    /// Pointer to the length map
  8.1016 +    void *_length;
  8.1017 +    ///Pointer to the map of predecessors arcs.
  8.1018 +    void *_pred;
  8.1019 +    ///Pointer to the map of distances.
  8.1020 +    void *_dist;
  8.1021 +    ///Pointer to the source node.
  8.1022 +    Node _source;
  8.1023 +
  8.1024 +    public:
  8.1025 +    /// Constructor.
  8.1026 +    
  8.1027 +    /// This constructor does not require parameters, therefore it initiates
  8.1028 +    /// all of the attributes to default values (0, INVALID).
  8.1029 +    DijkstraWizardBase() : _g(0), _length(0), _pred(0),
  8.1030 +			   _dist(0), _source(INVALID) {}
  8.1031 +
  8.1032 +    /// Constructor.
  8.1033 +    
  8.1034 +    /// This constructor requires some parameters,
  8.1035 +    /// listed in the parameters list.
  8.1036 +    /// Others are initiated to 0.
  8.1037 +    /// \param g is the initial value of  \ref _g
  8.1038 +    /// \param l is the initial value of  \ref _length
  8.1039 +    /// \param s is the initial value of  \ref _source
  8.1040 +    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
  8.1041 +      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
  8.1042 +      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), 
  8.1043 +      _pred(0), _dist(0), _source(s) {}
  8.1044 +
  8.1045 +  };
  8.1046 +  
  8.1047 +  /// A class to make the usage of Dijkstra algorithm easier
  8.1048 +
  8.1049 +  /// This class is created to make it easier to use Dijkstra algorithm.
  8.1050 +  /// It uses the functions and features of the plain \ref Dijkstra,
  8.1051 +  /// but it is much simpler to use it.
  8.1052 +  ///
  8.1053 +  /// Simplicity means that the way to change the types defined
  8.1054 +  /// in the traits class is based on functions that returns the new class
  8.1055 +  /// and not on templatable built-in classes.
  8.1056 +  /// When using the plain \ref Dijkstra
  8.1057 +  /// the new class with the modified type comes from
  8.1058 +  /// the original class by using the ::
  8.1059 +  /// operator. In the case of \ref DijkstraWizard only
  8.1060 +  /// a function have to be called and it will
  8.1061 +  /// return the needed class.
  8.1062 +  ///
  8.1063 +  /// It does not have own \ref run method. When its \ref run method is called
  8.1064 +  /// it initiates a plain \ref Dijkstra class, and calls the \ref 
  8.1065 +  /// Dijkstra::run method of it.
  8.1066 +  template<class TR>
  8.1067 +  class DijkstraWizard : public TR
  8.1068 +  {
  8.1069 +    typedef TR Base;
  8.1070 +
  8.1071 +    ///The type of the underlying digraph.
  8.1072 +    typedef typename TR::Digraph Digraph;
  8.1073 +    //\e
  8.1074 +    typedef typename Digraph::Node Node;
  8.1075 +    //\e
  8.1076 +    typedef typename Digraph::NodeIt NodeIt;
  8.1077 +    //\e
  8.1078 +    typedef typename Digraph::Arc Arc;
  8.1079 +    //\e
  8.1080 +    typedef typename Digraph::OutArcIt OutArcIt;
  8.1081 +    
  8.1082 +    ///The type of the map that stores the arc lengths.
  8.1083 +    typedef typename TR::LengthMap LengthMap;
  8.1084 +    ///The type of the length of the arcs.
  8.1085 +    typedef typename LengthMap::Value Value;
  8.1086 +    ///\brief The type of the map that stores the last
  8.1087 +    ///arcs of the shortest paths.
  8.1088 +    typedef typename TR::PredMap PredMap;
  8.1089 +    ///The type of the map that stores the dists of the nodes.
  8.1090 +    typedef typename TR::DistMap DistMap;
  8.1091 +    ///The heap type used by the dijkstra algorithm.
  8.1092 +    typedef typename TR::Heap Heap;
  8.1093 +  public:
  8.1094 +    /// Constructor.
  8.1095 +    DijkstraWizard() : TR() {}
  8.1096 +
  8.1097 +    /// Constructor that requires parameters.
  8.1098 +
  8.1099 +    /// Constructor that requires parameters.
  8.1100 +    /// These parameters will be the default values for the traits class.
  8.1101 +    DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
  8.1102 +      TR(g,l,s) {}
  8.1103 +
  8.1104 +    ///Copy constructor
  8.1105 +    DijkstraWizard(const TR &b) : TR(b) {}
  8.1106 +
  8.1107 +    ~DijkstraWizard() {}
  8.1108 +
  8.1109 +    ///Runs Dijkstra algorithm from a given node.
  8.1110 +    
  8.1111 +    ///Runs Dijkstra algorithm from a given node.
  8.1112 +    ///The node can be given by the \ref source function.
  8.1113 +    void run()
  8.1114 +    {
  8.1115 +      if(Base::_source==INVALID) throw UninitializedParameter();
  8.1116 +      Dijkstra<Digraph,LengthMap,TR> 
  8.1117 +	dij(*reinterpret_cast<const Digraph*>(Base::_g),
  8.1118 +            *reinterpret_cast<const LengthMap*>(Base::_length));
  8.1119 +      if(Base::_pred) dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
  8.1120 +      if(Base::_dist) dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
  8.1121 +      dij.run(Base::_source);
  8.1122 +    }
  8.1123 +
  8.1124 +    ///Runs Dijkstra algorithm from the given node.
  8.1125 +
  8.1126 +    ///Runs Dijkstra algorithm from the given node.
  8.1127 +    ///\param s is the given source.
  8.1128 +    void run(Node s)
  8.1129 +    {
  8.1130 +      Base::_source=s;
  8.1131 +      run();
  8.1132 +    }
  8.1133 +
  8.1134 +    template<class T>
  8.1135 +    struct DefPredMapBase : public Base {
  8.1136 +      typedef T PredMap;
  8.1137 +      static PredMap *createPredMap(const Digraph &) { return 0; };
  8.1138 +      DefPredMapBase(const TR &b) : TR(b) {}
  8.1139 +    };
  8.1140 +    
  8.1141 +    ///\brief \ref named-templ-param "Named parameter"
  8.1142 +    ///function for setting PredMap type
  8.1143 +    ///
  8.1144 +    /// \ref named-templ-param "Named parameter"
  8.1145 +    ///function for setting PredMap type
  8.1146 +    ///
  8.1147 +    template<class T>
  8.1148 +    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
  8.1149 +    {
  8.1150 +      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  8.1151 +      return DijkstraWizard<DefPredMapBase<T> >(*this);
  8.1152 +    }
  8.1153 +    
  8.1154 +    template<class T>
  8.1155 +    struct DefDistMapBase : public Base {
  8.1156 +      typedef T DistMap;
  8.1157 +      static DistMap *createDistMap(const Digraph &) { return 0; };
  8.1158 +      DefDistMapBase(const TR &b) : TR(b) {}
  8.1159 +    };
  8.1160 +    
  8.1161 +    ///\brief \ref named-templ-param "Named parameter"
  8.1162 +    ///function for setting DistMap type
  8.1163 +    ///
  8.1164 +    /// \ref named-templ-param "Named parameter"
  8.1165 +    ///function for setting DistMap type
  8.1166 +    ///
  8.1167 +    template<class T>
  8.1168 +    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
  8.1169 +    {
  8.1170 +      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
  8.1171 +      return DijkstraWizard<DefDistMapBase<T> >(*this);
  8.1172 +    }
  8.1173 +    
  8.1174 +    /// Sets the source node, from which the Dijkstra algorithm runs.
  8.1175 +
  8.1176 +    /// Sets the source node, from which the Dijkstra algorithm runs.
  8.1177 +    /// \param s is the source node.
  8.1178 +    DijkstraWizard<TR> &source(Node s) 
  8.1179 +    {
  8.1180 +      Base::_source=s;
  8.1181 +      return *this;
  8.1182 +    }
  8.1183 +    
  8.1184 +  };
  8.1185 +  
  8.1186 +  ///Function type interface for Dijkstra algorithm.
  8.1187 +
  8.1188 +  /// \ingroup shortest_path
  8.1189 +  ///Function type interface for Dijkstra algorithm.
  8.1190 +  ///
  8.1191 +  ///This function also has several
  8.1192 +  ///\ref named-templ-func-param "named parameters",
  8.1193 +  ///they are declared as the members of class \ref DijkstraWizard.
  8.1194 +  ///The following
  8.1195 +  ///example shows how to use these parameters.
  8.1196 +  ///\code
  8.1197 +  ///  dijkstra(g,length,source).predMap(preds).run();
  8.1198 +  ///\endcode
  8.1199 +  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
  8.1200 +  ///to the end of the parameter list.
  8.1201 +  ///\sa DijkstraWizard
  8.1202 +  ///\sa Dijkstra
  8.1203 +  template<class GR, class LM>
  8.1204 +  DijkstraWizard<DijkstraWizardBase<GR,LM> >
  8.1205 +  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
  8.1206 +  {
  8.1207 +    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
  8.1208 +  }
  8.1209 +
  8.1210 +} //END OF NAMESPACE LEMON
  8.1211 +
  8.1212 +#endif
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/lemon/graph_utils.h	Thu Mar 20 12:12:24 2008 +0000
     9.3 @@ -0,0 +1,3179 @@
     9.4 +/* -*- C++ -*-
     9.5 + *
     9.6 + * This file is a part of LEMON, a generic C++ optimization library
     9.7 + *
     9.8 + * Copyright (C) 2003-2008
     9.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    9.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    9.11 + *
    9.12 + * Permission to use, modify and distribute this software is granted
    9.13 + * provided that this copyright notice appears in all copies. For
    9.14 + * precise terms see the accompanying LICENSE file.
    9.15 + *
    9.16 + * This software is provided "AS IS" with no warranty of any kind,
    9.17 + * express or implied, and with no claim as to its suitability for any
    9.18 + * purpose.
    9.19 + *
    9.20 + */
    9.21 +
    9.22 +#ifndef LEMON_GRAPH_UTILS_H
    9.23 +#define LEMON_GRAPH_UTILS_H
    9.24 +
    9.25 +#include <iterator>
    9.26 +#include <vector>
    9.27 +#include <map>
    9.28 +#include <cmath>
    9.29 +#include <algorithm>
    9.30 +
    9.31 +#include <lemon/bits/invalid.h>
    9.32 +#include <lemon/bits/utility.h>
    9.33 +#include <lemon/maps.h>
    9.34 +#include <lemon/bits/traits.h>
    9.35 +
    9.36 +#include <lemon/bits/alteration_notifier.h>
    9.37 +#include <lemon/bits/default_map.h>
    9.38 +
    9.39 +///\ingroup gutils
    9.40 +///\file
    9.41 +///\brief Digraph utilities.
    9.42 +
    9.43 +namespace lemon {
    9.44 +
    9.45 +  /// \addtogroup gutils
    9.46 +  /// @{
    9.47 +
    9.48 +  ///Creates convenience typedefs for the digraph types and iterators
    9.49 +
    9.50 +  ///This \c \#define creates convenience typedefs for the following types
    9.51 +  ///of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
    9.52 +  ///\c OutArcIt
    9.53 +  ///\note If \c G it a template parameter, it should be used in this way.
    9.54 +  ///\code
    9.55 +  ///  GRAPH_TYPEDEFS(typename G);
    9.56 +  ///\endcode
    9.57 +  ///
    9.58 +  ///\warning There are no typedefs for the digraph maps because of the lack of
    9.59 +  ///template typedefs in C++.
    9.60 +#define GRAPH_TYPEDEFS(Digraph)				\
    9.61 +  typedef Digraph::     Node      Node;			\
    9.62 +    typedef Digraph::   NodeIt    NodeIt;			\
    9.63 +    typedef Digraph::   Arc      Arc;			\
    9.64 +    typedef Digraph::   ArcIt    ArcIt;			\
    9.65 +    typedef Digraph:: InArcIt  InArcIt;			\
    9.66 +    typedef Digraph::OutArcIt OutArcIt
    9.67 +
    9.68 +  ///Creates convenience typedefs for the graph types and iterators
    9.69 +
    9.70 +  ///This \c \#define creates the same convenience typedefs as defined by
    9.71 +  ///\ref GRAPH_TYPEDEFS(Digraph) and three more, namely it creates
    9.72 +  ///\c Edge, \c EdgeIt, \c IncArcIt,
    9.73 +  ///
    9.74 +  ///\note If \c G it a template parameter, it should be used in this way.
    9.75 +  ///\code
    9.76 +  ///  UGRAPH_TYPEDEFS(typename G);
    9.77 +  ///\endcode
    9.78 +  ///
    9.79 +  ///\warning There are no typedefs for the digraph maps because of the lack of
    9.80 +  ///template typedefs in C++.
    9.81 +#define UGRAPH_TYPEDEFS(Digraph)				\
    9.82 +  GRAPH_TYPEDEFS(Digraph);				\
    9.83 +    typedef Digraph:: Edge   Edge;			\
    9.84 +    typedef Digraph:: EdgeIt EdgeIt;			\
    9.85 +    typedef Digraph:: IncArcIt   IncArcIt
    9.86 +
    9.87 +  ///\brief Creates convenience typedefs for the bipartite digraph 
    9.88 +  ///types and iterators
    9.89 +
    9.90 +  ///This \c \#define creates the same convenience typedefs as defined by
    9.91 +  ///\ref UGRAPH_TYPEDEFS(Digraph) and two more, namely it creates
    9.92 +  ///\c RedIt, \c BlueIt, 
    9.93 +  ///
    9.94 +  ///\note If \c G it a template parameter, it should be used in this way.
    9.95 +  ///\code
    9.96 +  ///  BPUGRAPH_TYPEDEFS(typename G);
    9.97 +  ///\endcode
    9.98 +  ///
    9.99 +  ///\warning There are no typedefs for the digraph maps because of the lack of
   9.100 +  ///template typedefs in C++.
   9.101 +#define BPUGRAPH_TYPEDEFS(Digraph)            \
   9.102 +  UGRAPH_TYPEDEFS(Digraph);		    \
   9.103 +    typedef Digraph::Red Red;             \
   9.104 +    typedef Digraph::Blue Blue;             \
   9.105 +    typedef Digraph::RedIt RedIt;	    \
   9.106 +    typedef Digraph::BlueIt BlueIt
   9.107 +
   9.108 +  /// \brief Function to count the items in the digraph.
   9.109 +  ///
   9.110 +  /// This function counts the items (nodes, arcs etc) in the digraph.
   9.111 +  /// The complexity of the function is O(n) because
   9.112 +  /// it iterates on all of the items.
   9.113 +
   9.114 +  template <typename Digraph, typename Item>
   9.115 +  inline int countItems(const Digraph& g) {
   9.116 +    typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
   9.117 +    int num = 0;
   9.118 +    for (ItemIt it(g); it != INVALID; ++it) {
   9.119 +      ++num;
   9.120 +    }
   9.121 +    return num;
   9.122 +  }
   9.123 +
   9.124 +  // Node counting:
   9.125 +
   9.126 +  namespace _digraph_utils_bits {
   9.127 +    
   9.128 +    template <typename Digraph, typename Enable = void>
   9.129 +    struct CountNodesSelector {
   9.130 +      static int count(const Digraph &g) {
   9.131 +        return countItems<Digraph, typename Digraph::Node>(g);
   9.132 +      }
   9.133 +    };
   9.134 +
   9.135 +    template <typename Digraph>
   9.136 +    struct CountNodesSelector<
   9.137 +      Digraph, typename 
   9.138 +      enable_if<typename Digraph::NodeNumTag, void>::type> 
   9.139 +    {
   9.140 +      static int count(const Digraph &g) {
   9.141 +        return g.nodeNum();
   9.142 +      }
   9.143 +    };    
   9.144 +  }
   9.145 +
   9.146 +  /// \brief Function to count the nodes in the digraph.
   9.147 +  ///
   9.148 +  /// This function counts the nodes in the digraph.
   9.149 +  /// The complexity of the function is O(n) but for some
   9.150 +  /// digraph structures it is specialized to run in O(1).
   9.151 +  ///
   9.152 +  /// If the digraph contains a \e nodeNum() member function and a 
   9.153 +  /// \e NodeNumTag tag then this function calls directly the member
   9.154 +  /// function to query the cardinality of the node set.
   9.155 +  template <typename Digraph>
   9.156 +  inline int countNodes(const Digraph& g) {
   9.157 +    return _digraph_utils_bits::CountNodesSelector<Digraph>::count(g);
   9.158 +  }
   9.159 +
   9.160 +  namespace _digraph_utils_bits {
   9.161 +    
   9.162 +    template <typename Digraph, typename Enable = void>
   9.163 +    struct CountRedsSelector {
   9.164 +      static int count(const Digraph &g) {
   9.165 +        return countItems<Digraph, typename Digraph::Red>(g);
   9.166 +      }
   9.167 +    };
   9.168 +
   9.169 +    template <typename Digraph>
   9.170 +    struct CountRedsSelector<
   9.171 +      Digraph, typename 
   9.172 +      enable_if<typename Digraph::NodeNumTag, void>::type> 
   9.173 +    {
   9.174 +      static int count(const Digraph &g) {
   9.175 +        return g.redNum();
   9.176 +      }
   9.177 +    };    
   9.178 +  }
   9.179 +
   9.180 +  /// \brief Function to count the reds in the digraph.
   9.181 +  ///
   9.182 +  /// This function counts the reds in the digraph.
   9.183 +  /// The complexity of the function is O(an) but for some
   9.184 +  /// digraph structures it is specialized to run in O(1).
   9.185 +  ///
   9.186 +  /// If the digraph contains an \e redNum() member function and a 
   9.187 +  /// \e NodeNumTag tag then this function calls directly the member
   9.188 +  /// function to query the cardinality of the A-node set.
   9.189 +  template <typename Digraph>
   9.190 +  inline int countReds(const Digraph& g) {
   9.191 +    return _digraph_utils_bits::CountRedsSelector<Digraph>::count(g);
   9.192 +  }
   9.193 +
   9.194 +  namespace _digraph_utils_bits {
   9.195 +    
   9.196 +    template <typename Digraph, typename Enable = void>
   9.197 +    struct CountBluesSelector {
   9.198 +      static int count(const Digraph &g) {
   9.199 +        return countItems<Digraph, typename Digraph::Blue>(g);
   9.200 +      }
   9.201 +    };
   9.202 +
   9.203 +    template <typename Digraph>
   9.204 +    struct CountBluesSelector<
   9.205 +      Digraph, typename 
   9.206 +      enable_if<typename Digraph::NodeNumTag, void>::type> 
   9.207 +    {
   9.208 +      static int count(const Digraph &g) {
   9.209 +        return g.blueNum();
   9.210 +      }
   9.211 +    };    
   9.212 +  }
   9.213 +
   9.214 +  /// \brief Function to count the blues in the digraph.
   9.215 +  ///
   9.216 +  /// This function counts the blues in the digraph.
   9.217 +  /// The complexity of the function is O(bn) but for some
   9.218 +  /// digraph structures it is specialized to run in O(1).
   9.219 +  ///
   9.220 +  /// If the digraph contains a \e blueNum() member function and a 
   9.221 +  /// \e NodeNumTag tag then this function calls directly the member
   9.222 +  /// function to query the cardinality of the B-node set.
   9.223 +  template <typename Digraph>
   9.224 +  inline int countBlues(const Digraph& g) {
   9.225 +    return _digraph_utils_bits::CountBluesSelector<Digraph>::count(g);
   9.226 +  }
   9.227 +
   9.228 +
   9.229 +  // Arc counting:
   9.230 +
   9.231 +  namespace _digraph_utils_bits {
   9.232 +    
   9.233 +    template <typename Digraph, typename Enable = void>
   9.234 +    struct CountArcsSelector {
   9.235 +      static int count(const Digraph &g) {
   9.236 +        return countItems<Digraph, typename Digraph::Arc>(g);
   9.237 +      }
   9.238 +    };
   9.239 +
   9.240 +    template <typename Digraph>
   9.241 +    struct CountArcsSelector<
   9.242 +      Digraph, 
   9.243 +      typename enable_if<typename Digraph::ArcNumTag, void>::type> 
   9.244 +    {
   9.245 +      static int count(const Digraph &g) {
   9.246 +        return g.arcNum();
   9.247 +      }
   9.248 +    };    
   9.249 +  }
   9.250 +
   9.251 +  /// \brief Function to count the arcs in the digraph.
   9.252 +  ///
   9.253 +  /// This function counts the arcs in the digraph.
   9.254 +  /// The complexity of the function is O(e) but for some
   9.255 +  /// digraph structures it is specialized to run in O(1).
   9.256 +  ///
   9.257 +  /// If the digraph contains a \e arcNum() member function and a 
   9.258 +  /// \e ArcNumTag tag then this function calls directly the member
   9.259 +  /// function to query the cardinality of the arc set.
   9.260 +  template <typename Digraph>
   9.261 +  inline int countArcs(const Digraph& g) {
   9.262 +    return _digraph_utils_bits::CountArcsSelector<Digraph>::count(g);
   9.263 +  }
   9.264 +
   9.265 +  // Undirected arc counting:
   9.266 +  namespace _digraph_utils_bits {
   9.267 +    
   9.268 +    template <typename Digraph, typename Enable = void>
   9.269 +    struct CountEdgesSelector {
   9.270 +      static int count(const Digraph &g) {
   9.271 +        return countItems<Digraph, typename Digraph::Edge>(g);
   9.272 +      }
   9.273 +    };
   9.274 +
   9.275 +    template <typename Digraph>
   9.276 +    struct CountEdgesSelector<
   9.277 +      Digraph, 
   9.278 +      typename enable_if<typename Digraph::ArcNumTag, void>::type> 
   9.279 +    {
   9.280 +      static int count(const Digraph &g) {
   9.281 +        return g.edgeNum();
   9.282 +      }
   9.283 +    };    
   9.284 +  }
   9.285 +
   9.286 +  /// \brief Function to count the edges in the digraph.
   9.287 +  ///
   9.288 +  /// This function counts the edges in the digraph.
   9.289 +  /// The complexity of the function is O(e) but for some
   9.290 +  /// digraph structures it is specialized to run in O(1).
   9.291 +  ///
   9.292 +  /// If the digraph contains a \e edgeNum() member function and a 
   9.293 +  /// \e ArcNumTag tag then this function calls directly the member
   9.294 +  /// function to query the cardinality of the edge set.
   9.295 +  template <typename Digraph>
   9.296 +  inline int countEdges(const Digraph& g) {
   9.297 +    return _digraph_utils_bits::CountEdgesSelector<Digraph>::count(g);
   9.298 +
   9.299 +  }
   9.300 +
   9.301 +
   9.302 +  template <typename Digraph, typename DegIt>
   9.303 +  inline int countNodeDegree(const Digraph& _g, const typename Digraph::Node& _n) {
   9.304 +    int num = 0;
   9.305 +    for (DegIt it(_g, _n); it != INVALID; ++it) {
   9.306 +      ++num;
   9.307 +    }
   9.308 +    return num;
   9.309 +  }
   9.310 +
   9.311 +  /// \brief Function to count the number of the out-arcs from node \c n.
   9.312 +  ///
   9.313 +  /// This function counts the number of the out-arcs from node \c n
   9.314 +  /// in the digraph.  
   9.315 +  template <typename Digraph>
   9.316 +  inline int countOutArcs(const Digraph& _g,  const typename Digraph::Node& _n) {
   9.317 +    return countNodeDegree<Digraph, typename Digraph::OutArcIt>(_g, _n);
   9.318 +  }
   9.319 +
   9.320 +  /// \brief Function to count the number of the in-arcs to node \c n.
   9.321 +  ///
   9.322 +  /// This function counts the number of the in-arcs to node \c n
   9.323 +  /// in the digraph.  
   9.324 +  template <typename Digraph>
   9.325 +  inline int countInArcs(const Digraph& _g,  const typename Digraph::Node& _n) {
   9.326 +    return countNodeDegree<Digraph, typename Digraph::InArcIt>(_g, _n);
   9.327 +  }
   9.328 +
   9.329 +  /// \brief Function to count the number of the inc-arcs to node \c n.
   9.330 +  ///
   9.331 +  /// This function counts the number of the inc-arcs to node \c n
   9.332 +  /// in the digraph.  
   9.333 +  template <typename Digraph>
   9.334 +  inline int countIncArcs(const Digraph& _g,  const typename Digraph::Node& _n) {
   9.335 +    return countNodeDegree<Digraph, typename Digraph::IncArcIt>(_g, _n);
   9.336 +  }
   9.337 +
   9.338 +  namespace _digraph_utils_bits {
   9.339 +    
   9.340 +    template <typename Digraph, typename Enable = void>
   9.341 +    struct FindArcSelector {
   9.342 +      typedef typename Digraph::Node Node;
   9.343 +      typedef typename Digraph::Arc Arc;
   9.344 +      static Arc find(const Digraph &g, Node u, Node v, Arc e) {
   9.345 +        if (e == INVALID) {
   9.346 +          g.firstOut(e, u);
   9.347 +        } else {
   9.348 +          g.nextOut(e);
   9.349 +        }
   9.350 +        while (e != INVALID && g.target(e) != v) {
   9.351 +          g.nextOut(e);
   9.352 +        }
   9.353 +        return e;
   9.354 +      }
   9.355 +    };
   9.356 +
   9.357 +    template <typename Digraph>
   9.358 +    struct FindArcSelector<
   9.359 +      Digraph, 
   9.360 +      typename enable_if<typename Digraph::FindArcTag, void>::type> 
   9.361 +    {
   9.362 +      typedef typename Digraph::Node Node;
   9.363 +      typedef typename Digraph::Arc Arc;
   9.364 +      static Arc find(const Digraph &g, Node u, Node v, Arc prev) {
   9.365 +        return g.findArc(u, v, prev);
   9.366 +      }
   9.367 +    };    
   9.368 +  }
   9.369 +
   9.370 +  /// \brief Finds an arc between two nodes of a digraph.
   9.371 +  ///
   9.372 +  /// Finds an arc from node \c u to node \c v in digraph \c g.
   9.373 +  ///
   9.374 +  /// If \c prev is \ref INVALID (this is the default value), then
   9.375 +  /// it finds the first arc from \c u to \c v. Otherwise it looks for
   9.376 +  /// the next arc from \c u to \c v after \c prev.
   9.377 +  /// \return The found arc or \ref INVALID if there is no such an arc.
   9.378 +  ///
   9.379 +  /// Thus you can iterate through each arc from \c u to \c v as it follows.
   9.380 +  ///\code
   9.381 +  /// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
   9.382 +  ///   ...
   9.383 +  /// }
   9.384 +  ///\endcode
   9.385 +  ///
   9.386 +  ///\sa ArcLookUp
   9.387 +  ///\sa AllArcLookUp
   9.388 +  ///\sa DynArcLookUp
   9.389 +  ///\sa ConArcIt
   9.390 +  template <typename Digraph>
   9.391 +  inline typename Digraph::Arc 
   9.392 +  findArc(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v,
   9.393 +           typename Digraph::Arc prev = INVALID) {
   9.394 +    return _digraph_utils_bits::FindArcSelector<Digraph>::find(g, u, v, prev);
   9.395 +  }
   9.396 +
   9.397 +  /// \brief Iterator for iterating on arcs connected the same nodes.
   9.398 +  ///
   9.399 +  /// Iterator for iterating on arcs connected the same nodes. It is 
   9.400 +  /// higher level interface for the findArc() function. You can
   9.401 +  /// use it the following way:
   9.402 +  ///\code
   9.403 +  /// for (ConArcIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
   9.404 +  ///   ...
   9.405 +  /// }
   9.406 +  ///\endcode
   9.407 +  /// 
   9.408 +  ///\sa findArc()
   9.409 +  ///\sa ArcLookUp
   9.410 +  ///\sa AllArcLookUp
   9.411 +  ///\sa DynArcLookUp
   9.412 +  ///
   9.413 +  /// \author Balazs Dezso 
   9.414 +  template <typename _Digraph>
   9.415 +  class ConArcIt : public _Digraph::Arc {
   9.416 +  public:
   9.417 +
   9.418 +    typedef _Digraph Digraph;
   9.419 +    typedef typename Digraph::Arc Parent;
   9.420 +
   9.421 +    typedef typename Digraph::Arc Arc;
   9.422 +    typedef typename Digraph::Node Node;
   9.423 +
   9.424 +    /// \brief Constructor.
   9.425 +    ///
   9.426 +    /// Construct a new ConArcIt iterating on the arcs which
   9.427 +    /// connects the \c u and \c v node.
   9.428 +    ConArcIt(const Digraph& g, Node u, Node v) : digraph(g) {
   9.429 +      Parent::operator=(findArc(digraph, u, v));
   9.430 +    }
   9.431 +
   9.432 +    /// \brief Constructor.
   9.433 +    ///
   9.434 +    /// Construct a new ConArcIt which continues the iterating from 
   9.435 +    /// the \c e arc.
   9.436 +    ConArcIt(const Digraph& g, Arc e) : Parent(e), digraph(g) {}
   9.437 +    
   9.438 +    /// \brief Increment operator.
   9.439 +    ///
   9.440 +    /// It increments the iterator and gives back the next arc.
   9.441 +    ConArcIt& operator++() {
   9.442 +      Parent::operator=(findArc(digraph, digraph.source(*this), 
   9.443 +				 digraph.target(*this), *this));
   9.444 +      return *this;
   9.445 +    }
   9.446 +  private:
   9.447 +    const Digraph& digraph;
   9.448 +  };
   9.449 +
   9.450 +  namespace _digraph_utils_bits {
   9.451 +    
   9.452 +    template <typename Digraph, typename Enable = void>
   9.453 +    struct FindEdgeSelector {
   9.454 +      typedef typename Digraph::Node Node;
   9.455 +      typedef typename Digraph::Edge Edge;
   9.456 +      static Edge find(const Digraph &g, Node u, Node v, Edge e) {
   9.457 +        bool b;
   9.458 +        if (u != v) {
   9.459 +          if (e == INVALID) {
   9.460 +            g.firstInc(e, b, u);
   9.461 +          } else {
   9.462 +            b = g.source(e) == u;
   9.463 +            g.nextInc(e, b);
   9.464 +          }
   9.465 +          while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
   9.466 +            g.nextInc(e, b);
   9.467 +          }
   9.468 +        } else {
   9.469 +          if (e == INVALID) {
   9.470 +            g.firstInc(e, b, u);
   9.471 +          } else {
   9.472 +            b = true;
   9.473 +            g.nextInc(e, b);
   9.474 +          }
   9.475 +          while (e != INVALID && (!b || g.target(e) != v)) {
   9.476 +            g.nextInc(e, b);
   9.477 +          }
   9.478 +        }
   9.479 +        return e;
   9.480 +      }
   9.481 +    };
   9.482 +
   9.483 +    template <typename Digraph>
   9.484 +    struct FindEdgeSelector<
   9.485 +      Digraph, 
   9.486 +      typename enable_if<typename Digraph::FindArcTag, void>::type> 
   9.487 +    {
   9.488 +      typedef typename Digraph::Node Node;
   9.489 +      typedef typename Digraph::Edge Edge;
   9.490 +      static Edge find(const Digraph &g, Node u, Node v, Edge prev) {
   9.491 +        return g.findEdge(u, v, prev);
   9.492 +      }
   9.493 +    };    
   9.494 +  }
   9.495 +
   9.496 +  /// \brief Finds an edge between two nodes of a digraph.
   9.497 +  ///
   9.498 +  /// Finds an edge from node \c u to node \c v in digraph \c g.
   9.499 +  /// If the node \c u and node \c v is equal then each loop arc
   9.500 +  /// will be enumerated.
   9.501 +  ///
   9.502 +  /// If \c prev is \ref INVALID (this is the default value), then
   9.503 +  /// it finds the first arc from \c u to \c v. Otherwise it looks for
   9.504 +  /// the next arc from \c u to \c v after \c prev.
   9.505 +  /// \return The found arc or \ref INVALID if there is no such an arc.
   9.506 +  ///
   9.507 +  /// Thus you can iterate through each arc from \c u to \c v as it follows.
   9.508 +  ///\code
   9.509 +  /// for(Edge e = findEdge(g,u,v); e != INVALID; 
   9.510 +  ///     e = findEdge(g,u,v,e)) {
   9.511 +  ///   ...
   9.512 +  /// }
   9.513 +  ///\endcode
   9.514 +  ///
   9.515 +  ///\sa ConArcIt
   9.516 +
   9.517 +  template <typename Digraph>
   9.518 +  inline typename Digraph::Edge 
   9.519 +  findEdge(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v,
   9.520 +            typename Digraph::Edge p = INVALID) {
   9.521 +    return _digraph_utils_bits::FindEdgeSelector<Digraph>::find(g, u, v, p);
   9.522 +  }
   9.523 +
   9.524 +  /// \brief Iterator for iterating on edges connected the same nodes.
   9.525 +  ///
   9.526 +  /// Iterator for iterating on edges connected the same nodes. It is 
   9.527 +  /// higher level interface for the findEdge() function. You can
   9.528 +  /// use it the following way:
   9.529 +  ///\code
   9.530 +  /// for (ConEdgeIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
   9.531 +  ///   ...
   9.532 +  /// }
   9.533 +  ///\endcode
   9.534 +  ///
   9.535 +  ///\sa findEdge()
   9.536 +  ///
   9.537 +  /// \author Balazs Dezso 
   9.538 +  template <typename _Digraph>
   9.539 +  class ConEdgeIt : public _Digraph::Edge {
   9.540 +  public:
   9.541 +
   9.542 +    typedef _Digraph Digraph;
   9.543 +    typedef typename Digraph::Edge Parent;
   9.544 +
   9.545 +    typedef typename Digraph::Edge Edge;
   9.546 +    typedef typename Digraph::Node Node;
   9.547 +
   9.548 +    /// \brief Constructor.
   9.549 +    ///
   9.550 +    /// Construct a new ConEdgeIt iterating on the arcs which
   9.551 +    /// connects the \c u and \c v node.
   9.552 +    ConEdgeIt(const Digraph& g, Node u, Node v) : digraph(g) {
   9.553 +      Parent::operator=(findEdge(digraph, u, v));
   9.554 +    }
   9.555 +
   9.556 +    /// \brief Constructor.
   9.557 +    ///
   9.558 +    /// Construct a new ConEdgeIt which continues the iterating from 
   9.559 +    /// the \c e arc.
   9.560 +    ConEdgeIt(const Digraph& g, Edge e) : Parent(e), digraph(g) {}
   9.561 +    
   9.562 +    /// \brief Increment operator.
   9.563 +    ///
   9.564 +    /// It increments the iterator and gives back the next arc.
   9.565 +    ConEdgeIt& operator++() {
   9.566 +      Parent::operator=(findEdge(digraph, digraph.source(*this), 
   9.567 +				      digraph.target(*this), *this));
   9.568 +      return *this;
   9.569 +    }
   9.570 +  private:
   9.571 +    const Digraph& digraph;
   9.572 +  };
   9.573 +
   9.574 +  /// \brief Copy a map.
   9.575 +  ///
   9.576 +  /// This function copies the \c from map to the \c to map. It uses the
   9.577 +  /// given iterator to iterate on the data structure and it uses the \c ref
   9.578 +  /// mapping to convert the from's keys to the to's keys.
   9.579 +  template <typename To, typename From, 
   9.580 +	    typename ItemIt, typename Ref>	    
   9.581 +  void copyMap(To& to, const From& from, 
   9.582 +	       ItemIt it, const Ref& ref) {
   9.583 +    for (; it != INVALID; ++it) {
   9.584 +      to[ref[it]] = from[it];
   9.585 +    }
   9.586 +  }
   9.587 +
   9.588 +  /// \brief Copy the from map to the to map.
   9.589 +  ///
   9.590 +  /// Copy the \c from map to the \c to map. It uses the given iterator
   9.591 +  /// to iterate on the data structure.
   9.592 +  template <typename To, typename From, typename ItemIt>	    
   9.593 +  void copyMap(To& to, const From& from, ItemIt it) {
   9.594 +    for (; it != INVALID; ++it) {
   9.595 +      to[it] = from[it];
   9.596 +    }
   9.597 +  }
   9.598 +
   9.599 +  namespace _digraph_utils_bits {
   9.600 +
   9.601 +    template <typename Digraph, typename Item, typename RefMap>
   9.602 +    class MapCopyBase {
   9.603 +    public:
   9.604 +      virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
   9.605 +      
   9.606 +      virtual ~MapCopyBase() {}
   9.607 +    };
   9.608 +
   9.609 +    template <typename Digraph, typename Item, typename RefMap, 
   9.610 +              typename ToMap, typename FromMap>
   9.611 +    class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
   9.612 +    public:
   9.613 +
   9.614 +      MapCopy(ToMap& tmap, const FromMap& map) 
   9.615 +        : _tmap(tmap), _map(map) {}
   9.616 +      
   9.617 +      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
   9.618 +        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
   9.619 +        for (ItemIt it(digraph); it != INVALID; ++it) {
   9.620 +          _tmap.set(refMap[it], _map[it]);
   9.621 +        }
   9.622 +      }
   9.623 +
   9.624 +    private:
   9.625 +      ToMap& _tmap;
   9.626 +      const FromMap& _map;
   9.627 +    };
   9.628 +
   9.629 +    template <typename Digraph, typename Item, typename RefMap, typename It>
   9.630 +    class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
   9.631 +    public:
   9.632 +
   9.633 +      ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
   9.634 +      
   9.635 +      virtual void copy(const Digraph&, const RefMap& refMap) {
   9.636 +        _it = refMap[_item];
   9.637 +      }
   9.638 +
   9.639 +    private:
   9.640 +      It& _it;
   9.641 +      Item _item;
   9.642 +    };
   9.643 +
   9.644 +    template <typename Digraph, typename Item, typename RefMap, typename Ref>
   9.645 +    class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
   9.646 +    public:
   9.647 +
   9.648 +      RefCopy(Ref& map) : _map(map) {}
   9.649 +      
   9.650 +      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
   9.651 +        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
   9.652 +        for (ItemIt it(digraph); it != INVALID; ++it) {
   9.653 +          _map.set(it, refMap[it]);
   9.654 +        }
   9.655 +      }
   9.656 +
   9.657 +    private:
   9.658 +      Ref& _map;
   9.659 +    };
   9.660 +
   9.661 +    template <typename Digraph, typename Item, typename RefMap, 
   9.662 +              typename CrossRef>
   9.663 +    class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
   9.664 +    public:
   9.665 +
   9.666 +      CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
   9.667 +      
   9.668 +      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
   9.669 +        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
   9.670 +        for (ItemIt it(digraph); it != INVALID; ++it) {
   9.671 +          _cmap.set(refMap[it], it);
   9.672 +        }
   9.673 +      }
   9.674 +
   9.675 +    private:
   9.676 +      CrossRef& _cmap;
   9.677 +    };
   9.678 +
   9.679 +    template <typename Digraph, typename Enable = void>
   9.680 +    struct DigraphCopySelector {
   9.681 +      template <typename From, typename NodeRefMap, typename ArcRefMap>
   9.682 +      static void copy(Digraph &to, const From& from,
   9.683 +                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
   9.684 +        for (typename From::NodeIt it(from); it != INVALID; ++it) {
   9.685 +          nodeRefMap[it] = to.addNode();
   9.686 +        }
   9.687 +        for (typename From::ArcIt it(from); it != INVALID; ++it) {
   9.688 +          arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], 
   9.689 +                                          nodeRefMap[from.target(it)]);
   9.690 +        }
   9.691 +      }
   9.692 +    };
   9.693 +
   9.694 +    template <typename Digraph>
   9.695 +    struct DigraphCopySelector<
   9.696 +      Digraph, 
   9.697 +      typename enable_if<typename Digraph::BuildTag, void>::type> 
   9.698 +    {
   9.699 +      template <typename From, typename NodeRefMap, typename ArcRefMap>
   9.700 +      static void copy(Digraph &to, const From& from,
   9.701 +                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
   9.702 +        to.build(from, nodeRefMap, arcRefMap);
   9.703 +      }
   9.704 +    };
   9.705 +
   9.706 +    template <typename Graph, typename Enable = void>
   9.707 +    struct GraphCopySelector {
   9.708 +      template <typename From, typename NodeRefMap, typename EdgeRefMap>
   9.709 +      static void copy(Graph &to, const From& from,
   9.710 +                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
   9.711 +        for (typename From::NodeIt it(from); it != INVALID; ++it) {
   9.712 +          nodeRefMap[it] = to.addNode();
   9.713 +        }
   9.714 +        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
   9.715 +          edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)], 
   9.716 +				       nodeRefMap[from.target(it)]);
   9.717 +        }
   9.718 +      }
   9.719 +    };
   9.720 +
   9.721 +    template <typename Graph>
   9.722 +    struct GraphCopySelector<
   9.723 +      Graph, 
   9.724 +      typename enable_if<typename Graph::BuildTag, void>::type> 
   9.725 +    {
   9.726 +      template <typename From, typename NodeRefMap, typename EdgeRefMap>
   9.727 +      static void copy(Graph &to, const From& from,
   9.728 +                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
   9.729 +        to.build(from, nodeRefMap, edgeRefMap);
   9.730 +      }
   9.731 +    };
   9.732 +
   9.733 +    template <typename BpGraph, typename Enable = void>
   9.734 +    struct BpGraphCopySelector {
   9.735 +      template <typename From, typename RedRefMap, 
   9.736 +                typename BlueRefMap, typename EdgeRefMap>
   9.737 +      static void copy(BpGraph &to, const From& from,
   9.738 +                       RedRefMap& redRefMap, BlueRefMap& blueRefMap,
   9.739 +                       EdgeRefMap& edgeRefMap) {
   9.740 +        for (typename From::RedIt it(from); it != INVALID; ++it) {
   9.741 +          redRefMap[it] = to.addRed();
   9.742 +        }
   9.743 +        for (typename From::BlueIt it(from); it != INVALID; ++it) {
   9.744 +          blueRefMap[it] = to.addBlue();
   9.745 +        }
   9.746 +        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
   9.747 +          edgeRefMap[it] = to.addArc(redRefMap[from.red(it)], 
   9.748 +                                           blueRefMap[from.blue(it)]);
   9.749 +        }
   9.750 +      }
   9.751 +    };
   9.752 +
   9.753 +    template <typename BpGraph>
   9.754 +    struct BpGraphCopySelector<
   9.755 +      BpGraph, 
   9.756 +      typename enable_if<typename BpGraph::BuildTag, void>::type> 
   9.757 +    {
   9.758 +      template <typename From, typename RedRefMap, 
   9.759 +                typename BlueRefMap, typename EdgeRefMap>
   9.760 +      static void copy(BpGraph &to, const From& from,
   9.761 +                       RedRefMap& redRefMap, BlueRefMap& blueRefMap,
   9.762 +                       EdgeRefMap& edgeRefMap) {
   9.763 +        to.build(from, redRefMap, blueRefMap, edgeRefMap);
   9.764 +      }
   9.765 +    };
   9.766 +    
   9.767 +
   9.768 +  }
   9.769 +
   9.770 +  /// \brief Class to copy a digraph.
   9.771 +  ///
   9.772 +  /// Class to copy a digraph to another digraph (duplicate a digraph). The
   9.773 +  /// simplest way of using it is through the \c copyDigraph() function.
   9.774 +  template <typename To, typename From>
   9.775 +  class DigraphCopy {
   9.776 +  private:
   9.777 +
   9.778 +    typedef typename From::Node Node;
   9.779 +    typedef typename From::NodeIt NodeIt;
   9.780 +    typedef typename From::Arc Arc;
   9.781 +    typedef typename From::ArcIt ArcIt;
   9.782 +
   9.783 +    typedef typename To::Node TNode;
   9.784 +    typedef typename To::Arc TArc;
   9.785 +
   9.786 +    typedef typename From::template NodeMap<TNode> NodeRefMap;
   9.787 +    typedef typename From::template ArcMap<TArc> ArcRefMap;
   9.788 +    
   9.789 +    
   9.790 +  public: 
   9.791 +
   9.792 +
   9.793 +    /// \brief Constructor for the DigraphCopy.
   9.794 +    ///
   9.795 +    /// It copies the content of the \c _from digraph into the
   9.796 +    /// \c _to digraph.
   9.797 +    DigraphCopy(To& _to, const From& _from) 
   9.798 +      : from(_from), to(_to) {}
   9.799 +
   9.800 +    /// \brief Destructor of the DigraphCopy
   9.801 +    ///
   9.802 +    /// Destructor of the DigraphCopy
   9.803 +    ~DigraphCopy() {
   9.804 +      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
   9.805 +        delete nodeMapCopies[i];
   9.806 +      }
   9.807 +      for (int i = 0; i < int(arcMapCopies.size()); ++i) {
   9.808 +        delete arcMapCopies[i];
   9.809 +      }
   9.810 +
   9.811 +    }
   9.812 +
   9.813 +    /// \brief Copies the node references into the given map.
   9.814 +    ///
   9.815 +    /// Copies the node references into the given map.
   9.816 +    template <typename NodeRef>
   9.817 +    DigraphCopy& nodeRef(NodeRef& map) {
   9.818 +      nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, 
   9.819 +                              NodeRefMap, NodeRef>(map));
   9.820 +      return *this;
   9.821 +    }
   9.822 +
   9.823 +    /// \brief Copies the node cross references into the given map.
   9.824 +    ///
   9.825 +    ///  Copies the node cross references (reverse references) into
   9.826 +    ///  the given map.
   9.827 +    template <typename NodeCrossRef>
   9.828 +    DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
   9.829 +      nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
   9.830 +                              NodeRefMap, NodeCrossRef>(map));
   9.831 +      return *this;
   9.832 +    }
   9.833 +
   9.834 +    /// \brief Make copy of the given map.
   9.835 +    ///
   9.836 +    /// Makes copy of the given map for the newly created digraph. 
   9.837 +    /// The new map's key type is the to digraph's node type,
   9.838 +    /// and the copied map's key type is the from digraph's node
   9.839 +    /// type.  
   9.840 +    template <typename ToMap, typename FromMap>
   9.841 +    DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
   9.842 +      nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, 
   9.843 +                              NodeRefMap, ToMap, FromMap>(tmap, map));
   9.844 +      return *this;
   9.845 +    }
   9.846 +
   9.847 +    /// \brief Make a copy of the given node.
   9.848 +    ///
   9.849 +    /// Make a copy of the given node.
   9.850 +    DigraphCopy& node(TNode& tnode, const Node& snode) {
   9.851 +      nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, 
   9.852 +                              NodeRefMap, TNode>(tnode, snode));
   9.853 +      return *this;
   9.854 +    }
   9.855 +
   9.856 +    /// \brief Copies the arc references into the given map.
   9.857 +    ///
   9.858 +    /// Copies the arc references into the given map.
   9.859 +    template <typename ArcRef>
   9.860 +    DigraphCopy& arcRef(ArcRef& map) {
   9.861 +      arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, 
   9.862 +                              ArcRefMap, ArcRef>(map));
   9.863 +      return *this;
   9.864 +    }
   9.865 +
   9.866 +    /// \brief Copies the arc cross references into the given map.
   9.867 +    ///
   9.868 +    ///  Copies the arc cross references (reverse references) into
   9.869 +    ///  the given map.
   9.870 +    template <typename ArcCrossRef>
   9.871 +    DigraphCopy& arcCrossRef(ArcCrossRef& map) {
   9.872 +      arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
   9.873 +                              ArcRefMap, ArcCrossRef>(map));
   9.874 +      return *this;
   9.875 +    }
   9.876 +
   9.877 +    /// \brief Make copy of the given map.
   9.878 +    ///
   9.879 +    /// Makes copy of the given map for the newly created digraph. 
   9.880 +    /// The new map's key type is the to digraph's arc type,
   9.881 +    /// and the copied map's key type is the from digraph's arc
   9.882 +    /// type.  
   9.883 +    template <typename ToMap, typename FromMap>
   9.884 +    DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
   9.885 +      arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, 
   9.886 +                              ArcRefMap, ToMap, FromMap>(tmap, map));
   9.887 +      return *this;
   9.888 +    }
   9.889 +
   9.890 +    /// \brief Make a copy of the given arc.
   9.891 +    ///
   9.892 +    /// Make a copy of the given arc.
   9.893 +    DigraphCopy& arc(TArc& tarc, const Arc& sarc) {
   9.894 +      arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, 
   9.895 +                              ArcRefMap, TArc>(tarc, sarc));
   9.896 +      return *this;
   9.897 +    }
   9.898 +
   9.899 +    /// \brief Executes the copies.
   9.900 +    ///
   9.901 +    /// Executes the copies.
   9.902 +    void run() {
   9.903 +      NodeRefMap nodeRefMap(from);
   9.904 +      ArcRefMap arcRefMap(from);
   9.905 +      _digraph_utils_bits::DigraphCopySelector<To>::
   9.906 +        copy(to, from, nodeRefMap, arcRefMap);
   9.907 +      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
   9.908 +        nodeMapCopies[i]->copy(from, nodeRefMap);
   9.909 +      }
   9.910 +      for (int i = 0; i < int(arcMapCopies.size()); ++i) {
   9.911 +        arcMapCopies[i]->copy(from, arcRefMap);
   9.912 +      }      
   9.913 +    }
   9.914 +
   9.915 +  protected:
   9.916 +
   9.917 +
   9.918 +    const From& from;
   9.919 +    To& to;
   9.920 +
   9.921 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > 
   9.922 +    nodeMapCopies;
   9.923 +
   9.924 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > 
   9.925 +    arcMapCopies;
   9.926 +
   9.927 +  };
   9.928 +
   9.929 +  /// \brief Copy a digraph to another digraph.
   9.930 +  ///
   9.931 +  /// Copy a digraph to another digraph.
   9.932 +  /// The usage of the function:
   9.933 +  /// 
   9.934 +  ///\code
   9.935 +  /// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
   9.936 +  ///\endcode
   9.937 +  /// 
   9.938 +  /// After the copy the \c nr map will contain the mapping from the
   9.939 +  /// nodes of the \c from digraph to the nodes of the \c to digraph and
   9.940 +  /// \c ecr will contain the mapping from the arcs of the \c to digraph
   9.941 +  /// to the arcs of the \c from digraph.
   9.942 +  ///
   9.943 +  /// \see DigraphCopy 
   9.944 +  template <typename To, typename From>
   9.945 +  DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
   9.946 +    return DigraphCopy<To, From>(to, from);
   9.947 +  }
   9.948 +
   9.949 +  /// \brief Class to copy an graph.
   9.950 +  ///
   9.951 +  /// Class to copy an graph to another digraph (duplicate a digraph).
   9.952 +  /// The simplest way of using it is through the \c copyGraph() function.
   9.953 +  template <typename To, typename From>
   9.954 +  class GraphCopy {
   9.955 +  private:
   9.956 +
   9.957 +    typedef typename From::Node Node;
   9.958 +    typedef typename From::NodeIt NodeIt;
   9.959 +    typedef typename From::Arc Arc;
   9.960 +    typedef typename From::ArcIt ArcIt;
   9.961 +    typedef typename From::Edge Edge;
   9.962 +    typedef typename From::EdgeIt EdgeIt;
   9.963 +
   9.964 +    typedef typename To::Node TNode;
   9.965 +    typedef typename To::Arc TArc;
   9.966 +    typedef typename To::Edge TEdge;
   9.967 +
   9.968 +    typedef typename From::template NodeMap<TNode> NodeRefMap;
   9.969 +    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
   9.970 +
   9.971 +    struct ArcRefMap {
   9.972 +      ArcRefMap(const To& _to, const From& _from,
   9.973 +                 const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref) 
   9.974 +        : to(_to), from(_from), 
   9.975 +          edge_ref(_edge_ref), node_ref(_node_ref) {}
   9.976 +
   9.977 +      typedef typename From::Arc Key;
   9.978 +      typedef typename To::Arc Value;
   9.979 +
   9.980 +      Value operator[](const Key& key) const {
   9.981 +        bool forward = 
   9.982 +          (from.direction(key) == 
   9.983 +           (node_ref[from.source(static_cast<const Edge&>(key))] == 
   9.984 +            to.source(edge_ref[static_cast<const Edge&>(key)])));
   9.985 +	return to.direct(edge_ref[key], forward); 
   9.986 +      }
   9.987 +      
   9.988 +      const To& to;
   9.989 +      const From& from;
   9.990 +      const EdgeRefMap& edge_ref;
   9.991 +      const NodeRefMap& node_ref;
   9.992 +    };
   9.993 +
   9.994 +    
   9.995 +  public: 
   9.996 +
   9.997 +
   9.998 +    /// \brief Constructor for the DigraphCopy.
   9.999 +    ///
  9.1000 +    /// It copies the content of the \c _from digraph into the
  9.1001 +    /// \c _to digraph.
  9.1002 +    GraphCopy(To& _to, const From& _from) 
  9.1003 +      : from(_from), to(_to) {}
  9.1004 +
  9.1005 +    /// \brief Destructor of the DigraphCopy
  9.1006 +    ///
  9.1007 +    /// Destructor of the DigraphCopy
  9.1008 +    ~GraphCopy() {
  9.1009 +      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
  9.1010 +        delete nodeMapCopies[i];
  9.1011 +      }
  9.1012 +      for (int i = 0; i < int(arcMapCopies.size()); ++i) {
  9.1013 +        delete arcMapCopies[i];
  9.1014 +      }
  9.1015 +      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
  9.1016 +        delete edgeMapCopies[i];
  9.1017 +      }
  9.1018 +
  9.1019 +    }
  9.1020 +
  9.1021 +    /// \brief Copies the node references into the given map.
  9.1022 +    ///
  9.1023 +    /// Copies the node references into the given map.
  9.1024 +    template <typename NodeRef>
  9.1025 +    GraphCopy& nodeRef(NodeRef& map) {
  9.1026 +      nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, 
  9.1027 +                              NodeRefMap, NodeRef>(map));
  9.1028 +      return *this;
  9.1029 +    }
  9.1030 +
  9.1031 +    /// \brief Copies the node cross references into the given map.
  9.1032 +    ///
  9.1033 +    ///  Copies the node cross references (reverse references) into
  9.1034 +    ///  the given map.
  9.1035 +    template <typename NodeCrossRef>
  9.1036 +    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
  9.1037 +      nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
  9.1038 +                              NodeRefMap, NodeCrossRef>(map));
  9.1039 +      return *this;
  9.1040 +    }
  9.1041 +
  9.1042 +    /// \brief Make copy of the given map.
  9.1043 +    ///
  9.1044 +    /// Makes copy of the given map for the newly created digraph. 
  9.1045 +    /// The new map's key type is the to digraph's node type,
  9.1046 +    /// and the copied map's key type is the from digraph's node
  9.1047 +    /// type.  
  9.1048 +    template <typename ToMap, typename FromMap>
  9.1049 +    GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
  9.1050 +      nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, 
  9.1051 +                              NodeRefMap, ToMap, FromMap>(tmap, map));
  9.1052 +      return *this;
  9.1053 +    }
  9.1054 +
  9.1055 +    /// \brief Make a copy of the given node.
  9.1056 +    ///
  9.1057 +    /// Make a copy of the given node.
  9.1058 +    GraphCopy& node(TNode& tnode, const Node& snode) {
  9.1059 +      nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, 
  9.1060 +                              NodeRefMap, TNode>(tnode, snode));
  9.1061 +      return *this;
  9.1062 +    }
  9.1063 +
  9.1064 +    /// \brief Copies the arc references into the given map.
  9.1065 +    ///
  9.1066 +    /// Copies the arc references into the given map.
  9.1067 +    template <typename ArcRef>
  9.1068 +    GraphCopy& arcRef(ArcRef& map) {
  9.1069 +      arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, 
  9.1070 +                              ArcRefMap, ArcRef>(map));
  9.1071 +      return *this;
  9.1072 +    }
  9.1073 +
  9.1074 +    /// \brief Copies the arc cross references into the given map.
  9.1075 +    ///
  9.1076 +    ///  Copies the arc cross references (reverse references) into
  9.1077 +    ///  the given map.
  9.1078 +    template <typename ArcCrossRef>
  9.1079 +    GraphCopy& arcCrossRef(ArcCrossRef& map) {
  9.1080 +      arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
  9.1081 +                              ArcRefMap, ArcCrossRef>(map));
  9.1082 +      return *this;
  9.1083 +    }
  9.1084 +
  9.1085 +    /// \brief Make copy of the given map.
  9.1086 +    ///
  9.1087 +    /// Makes copy of the given map for the newly created digraph. 
  9.1088 +    /// The new map's key type is the to digraph's arc type,
  9.1089 +    /// and the copied map's key type is the from digraph's arc
  9.1090 +    /// type.  
  9.1091 +    template <typename ToMap, typename FromMap>
  9.1092 +    GraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
  9.1093 +      arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, 
  9.1094 +                              ArcRefMap, ToMap, FromMap>(tmap, map));
  9.1095 +      return *this;
  9.1096 +    }
  9.1097 +
  9.1098 +    /// \brief Make a copy of the given arc.
  9.1099 +    ///
  9.1100 +    /// Make a copy of the given arc.
  9.1101 +    GraphCopy& arc(TArc& tarc, const Arc& sarc) {
  9.1102 +      arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, 
  9.1103 +                              ArcRefMap, TArc>(tarc, sarc));
  9.1104 +      return *this;
  9.1105 +    }
  9.1106 +
  9.1107 +    /// \brief Copies the edge references into the given map.
  9.1108 +    ///
  9.1109 +    /// Copies the edge references into the given map.
  9.1110 +    template <typename EdgeRef>
  9.1111 +    GraphCopy& edgeRef(EdgeRef& map) {
  9.1112 +      edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge, 
  9.1113 +                               EdgeRefMap, EdgeRef>(map));
  9.1114 +      return *this;
  9.1115 +    }
  9.1116 +
  9.1117 +    /// \brief Copies the edge cross references into the given map.
  9.1118 +    ///
  9.1119 +    /// Copies the edge cross references (reverse
  9.1120 +    /// references) into the given map.
  9.1121 +    template <typename EdgeCrossRef>
  9.1122 +    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
  9.1123 +      edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, 
  9.1124 +                               Edge, EdgeRefMap, EdgeCrossRef>(map));
  9.1125 +      return *this;
  9.1126 +    }
  9.1127 +
  9.1128 +    /// \brief Make copy of the given map.
  9.1129 +    ///
  9.1130 +    /// Makes copy of the given map for the newly created digraph. 
  9.1131 +    /// The new map's key type is the to digraph's edge type,
  9.1132 +    /// and the copied map's key type is the from digraph's edge
  9.1133 +    /// type.  
  9.1134 +    template <typename ToMap, typename FromMap>
  9.1135 +    GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
  9.1136 +      edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge, 
  9.1137 +                               EdgeRefMap, ToMap, FromMap>(tmap, map));
  9.1138 +      return *this;
  9.1139 +    }
  9.1140 +
  9.1141 +    /// \brief Make a copy of the given edge.
  9.1142 +    ///
  9.1143 +    /// Make a copy of the given edge.
  9.1144 +    GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
  9.1145 +      edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge, 
  9.1146 +                               EdgeRefMap, TEdge>(tedge, sedge));
  9.1147 +      return *this;
  9.1148 +    }
  9.1149 +
  9.1150 +    /// \brief Executes the copies.
  9.1151 +    ///
  9.1152 +    /// Executes the copies.
  9.1153 +    void run() {
  9.1154 +      NodeRefMap nodeRefMap(from);
  9.1155 +      EdgeRefMap edgeRefMap(from);
  9.1156 +      ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap);
  9.1157 +      _digraph_utils_bits::GraphCopySelector<To>::
  9.1158 +        copy(to, from, nodeRefMap, edgeRefMap);
  9.1159 +      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
  9.1160 +        nodeMapCopies[i]->copy(from, nodeRefMap);
  9.1161 +      }
  9.1162 +      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
  9.1163 +        edgeMapCopies[i]->copy(from, edgeRefMap);
  9.1164 +      }
  9.1165 +      for (int i = 0; i < int(arcMapCopies.size()); ++i) {
  9.1166 +        arcMapCopies[i]->copy(from, arcRefMap);
  9.1167 +      }
  9.1168 +    }
  9.1169 +
  9.1170 +  private:
  9.1171 +    
  9.1172 +    const From& from;
  9.1173 +    To& to;
  9.1174 +
  9.1175 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > 
  9.1176 +    nodeMapCopies;
  9.1177 +
  9.1178 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > 
  9.1179 +    arcMapCopies;
  9.1180 +
  9.1181 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > 
  9.1182 +    edgeMapCopies;
  9.1183 +
  9.1184 +  };
  9.1185 +
  9.1186 +  /// \brief Copy an graph to another digraph.
  9.1187 +  ///
  9.1188 +  /// Copy an graph to another digraph.
  9.1189 +  /// The usage of the function:
  9.1190 +  /// 
  9.1191 +  ///\code
  9.1192 +  /// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
  9.1193 +  ///\endcode
  9.1194 +  /// 
  9.1195 +  /// After the copy the \c nr map will contain the mapping from the
  9.1196 +  /// nodes of the \c from digraph to the nodes of the \c to digraph and
  9.1197 +  /// \c ecr will contain the mapping from the arcs of the \c to digraph
  9.1198 +  /// to the arcs of the \c from digraph.
  9.1199 +  ///
  9.1200 +  /// \see GraphCopy 
  9.1201 +  template <typename To, typename From>
  9.1202 +  GraphCopy<To, From> 
  9.1203 +  copyGraph(To& to, const From& from) {
  9.1204 +    return GraphCopy<To, From>(to, from);
  9.1205 +  }
  9.1206 +
  9.1207 +  /// \brief Class to copy a bipartite digraph.
  9.1208 +  ///
  9.1209 +  /// Class to copy a bipartite digraph to another digraph
  9.1210 +  /// (duplicate a digraph).  The simplest way of using it is through
  9.1211 +  /// the \c copyBpGraph() function.
  9.1212 +  template <typename To, typename From>
  9.1213 +  class BpGraphCopy {
  9.1214 +  private:
  9.1215 +
  9.1216 +    typedef typename From::Node Node;
  9.1217 +    typedef typename From::Red Red;
  9.1218 +    typedef typename From::Blue Blue;
  9.1219 +    typedef typename From::NodeIt NodeIt;
  9.1220 +    typedef typename From::Arc Arc;
  9.1221 +    typedef typename From::ArcIt ArcIt;
  9.1222 +    typedef typename From::Edge Edge;
  9.1223 +    typedef typename From::EdgeIt EdgeIt;
  9.1224 +
  9.1225 +    typedef typename To::Node TNode;
  9.1226 +    typedef typename To::Arc TArc;
  9.1227 +    typedef typename To::Edge TEdge;
  9.1228 +
  9.1229 +    typedef typename From::template RedMap<TNode> RedRefMap;
  9.1230 +    typedef typename From::template BlueMap<TNode> BlueRefMap;
  9.1231 +    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
  9.1232 +
  9.1233 +    struct NodeRefMap {
  9.1234 +      NodeRefMap(const From& _from, const RedRefMap& _red_ref,
  9.1235 +                 const BlueRefMap& _blue_ref)
  9.1236 +        : from(_from), red_ref(_red_ref), blue_ref(_blue_ref) {}
  9.1237 +
  9.1238 +      typedef typename From::Node Key;
  9.1239 +      typedef typename To::Node Value;
  9.1240 +
  9.1241 +      Value operator[](const Key& key) const {
  9.1242 +	return from.red(key) ? red_ref[key] : blue_ref[key]; 
  9.1243 +      }
  9.1244 +      
  9.1245 +      const From& from;
  9.1246 +      const RedRefMap& red_ref;
  9.1247 +      const BlueRefMap& blue_ref;
  9.1248 +    };
  9.1249 +
  9.1250 +    struct ArcRefMap {
  9.1251 +      ArcRefMap(const To& _to, const From& _from,
  9.1252 +                 const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref) 
  9.1253 +        : to(_to), from(_from), 
  9.1254 +          edge_ref(_edge_ref), node_ref(_node_ref) {}
  9.1255 +
  9.1256 +      typedef typename From::Arc Key;
  9.1257 +      typedef typename To::Arc Value;
  9.1258 +
  9.1259 +      Value operator[](const Key& key) const {
  9.1260 +        bool forward = 
  9.1261 +          (from.direction(key) == 
  9.1262 +           (node_ref[from.source(static_cast<const Edge&>(key))] == 
  9.1263 +            to.source(edge_ref[static_cast<const Edge&>(key)])));
  9.1264 +	return to.direct(edge_ref[key], forward); 
  9.1265 +      }
  9.1266 +      
  9.1267 +      const To& to;
  9.1268 +      const From& from;
  9.1269 +      const EdgeRefMap& edge_ref;
  9.1270 +      const NodeRefMap& node_ref;
  9.1271 +    };
  9.1272 +    
  9.1273 +  public: 
  9.1274 +
  9.1275 +
  9.1276 +    /// \brief Constructor for the DigraphCopy.
  9.1277 +    ///
  9.1278 +    /// It copies the content of the \c _from digraph into the
  9.1279 +    /// \c _to digraph.
  9.1280 +    BpGraphCopy(To& _to, const From& _from) 
  9.1281 +      : from(_from), to(_to) {}
  9.1282 +
  9.1283 +    /// \brief Destructor of the DigraphCopy
  9.1284 +    ///
  9.1285 +    /// Destructor of the DigraphCopy
  9.1286 +    ~BpGraphCopy() {
  9.1287 +      for (int i = 0; i < int(redMapCopies.size()); ++i) {
  9.1288 +        delete redMapCopies[i];
  9.1289 +      }
  9.1290 +      for (int i = 0; i < int(blueMapCopies.size()); ++i) {
  9.1291 +        delete blueMapCopies[i];
  9.1292 +      }
  9.1293 +      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
  9.1294 +        delete nodeMapCopies[i];
  9.1295 +      }
  9.1296 +      for (int i = 0; i < int(arcMapCopies.size()); ++i) {
  9.1297 +        delete arcMapCopies[i];
  9.1298 +      }
  9.1299 +      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
  9.1300 +        delete edgeMapCopies[i];
  9.1301 +      }
  9.1302 +
  9.1303 +    }
  9.1304 +
  9.1305 +    /// \brief Copies the A-node references into the given map.
  9.1306 +    ///
  9.1307 +    /// Copies the A-node references into the given map.
  9.1308 +    template <typename RedRef>
  9.1309 +    BpGraphCopy& redRef(RedRef& map) {
  9.1310 +      redMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Red, 
  9.1311 +                               RedRefMap, RedRef>(map));
  9.1312 +      return *this;
  9.1313 +    }
  9.1314 +
  9.1315 +    /// \brief Copies the A-node cross references into the given map.
  9.1316 +    ///
  9.1317 +    /// Copies the A-node cross references (reverse references) into
  9.1318 +    /// the given map.
  9.1319 +    template <typename RedCrossRef>
  9.1320 +    BpGraphCopy& redCrossRef(RedCrossRef& map) {
  9.1321 +      redMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, 
  9.1322 +                               Red, RedRefMap, RedCrossRef>(map));
  9.1323 +      return *this;
  9.1324 +    }
  9.1325 +
  9.1326 +    /// \brief Make copy of the given A-node map.
  9.1327 +    ///
  9.1328 +    /// Makes copy of the given map for the newly created digraph. 
  9.1329 +    /// The new map's key type is the to digraph's node type,
  9.1330 +    /// and the copied map's key type is the from digraph's node
  9.1331 +    /// type.  
  9.1332 +    template <typename ToMap, typename FromMap>
  9.1333 +    BpGraphCopy& redMap(ToMap& tmap, const FromMap& map) {
  9.1334 +      redMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Red, 
  9.1335 +                               RedRefMap, ToMap, FromMap>(tmap, map));
  9.1336 +      return *this;
  9.1337 +    }
  9.1338 +
  9.1339 +    /// \brief Copies the B-node references into the given map.
  9.1340 +    ///
  9.1341 +    /// Copies the B-node references into the given map.
  9.1342 +    template <typename BlueRef>
  9.1343 +    BpGraphCopy& blueRef(BlueRef& map) {
  9.1344 +      blueMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Blue, 
  9.1345 +                               BlueRefMap, BlueRef>(map));
  9.1346 +      return *this;
  9.1347 +    }
  9.1348 +
  9.1349 +    /// \brief Copies the B-node cross references into the given map.
  9.1350 +    ///
  9.1351 +    ///  Copies the B-node cross references (reverse references) into
  9.1352 +    ///  the given map.
  9.1353 +    template <typename BlueCrossRef>
  9.1354 +    BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
  9.1355 +      blueMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, 
  9.1356 +                              Blue, BlueRefMap, BlueCrossRef>(map));
  9.1357 +      return *this;
  9.1358 +    }
  9.1359 +
  9.1360 +    /// \brief Make copy of the given B-node map.
  9.1361 +    ///
  9.1362 +    /// Makes copy of the given map for the newly created digraph. 
  9.1363 +    /// The new map's key type is the to digraph's node type,
  9.1364 +    /// and the copied map's key type is the from digraph's node
  9.1365 +    /// type.  
  9.1366 +    template <typename ToMap, typename FromMap>
  9.1367 +    BpGraphCopy& blueMap(ToMap& tmap, const FromMap& map) {
  9.1368 +      blueMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Blue, 
  9.1369 +                               BlueRefMap, ToMap, FromMap>(tmap, map));
  9.1370 +      return *this;
  9.1371 +    }
  9.1372 +    /// \brief Copies the node references into the given map.
  9.1373 +    ///
  9.1374 +    /// Copies the node references into the given map.
  9.1375 +    template <typename NodeRef>
  9.1376 +    BpGraphCopy& nodeRef(NodeRef& map) {
  9.1377 +      nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, 
  9.1378 +                              NodeRefMap, NodeRef>(map));
  9.1379 +      return *this;
  9.1380 +    }
  9.1381 +
  9.1382 +    /// \brief Copies the node cross references into the given map.
  9.1383 +    ///
  9.1384 +    ///  Copies the node cross references (reverse references) into
  9.1385 +    ///  the given map.
  9.1386 +    template <typename NodeCrossRef>
  9.1387 +    BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
  9.1388 +      nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
  9.1389 +                              NodeRefMap, NodeCrossRef>(map));
  9.1390 +      return *this;
  9.1391 +    }
  9.1392 +
  9.1393 +    /// \brief Make copy of the given map.
  9.1394 +    ///
  9.1395 +    /// Makes copy of the given map for the newly created digraph. 
  9.1396 +    /// The new map's key type is the to digraph's node type,
  9.1397 +    /// and the copied map's key type is the from digraph's node
  9.1398 +    /// type.  
  9.1399 +    template <typename ToMap, typename FromMap>
  9.1400 +    BpGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
  9.1401 +      nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, 
  9.1402 +                              NodeRefMap, ToMap, FromMap>(tmap, map));
  9.1403 +      return *this;
  9.1404 +    }
  9.1405 +
  9.1406 +    /// \brief Make a copy of the given node.
  9.1407 +    ///
  9.1408 +    /// Make a copy of the given node.
  9.1409 +    BpGraphCopy& node(TNode& tnode, const Node& snode) {
  9.1410 +      nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, 
  9.1411 +                              NodeRefMap, TNode>(tnode, snode));
  9.1412 +      return *this;
  9.1413 +    }
  9.1414 +
  9.1415 +    /// \brief Copies the arc references into the given map.
  9.1416 +    ///
  9.1417 +    /// Copies the arc references into the given map.
  9.1418 +    template <typename ArcRef>
  9.1419 +    BpGraphCopy& arcRef(ArcRef& map) {
  9.1420 +      arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, 
  9.1421 +                              ArcRefMap, ArcRef>(map));
  9.1422 +      return *this;
  9.1423 +    }
  9.1424 +
  9.1425 +    /// \brief Copies the arc cross references into the given map.
  9.1426 +    ///
  9.1427 +    ///  Copies the arc cross references (reverse references) into
  9.1428 +    ///  the given map.
  9.1429 +    template <typename ArcCrossRef>
  9.1430 +    BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
  9.1431 +      arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
  9.1432 +                              ArcRefMap, ArcCrossRef>(map));
  9.1433 +      return *this;
  9.1434 +    }
  9.1435 +
  9.1436 +    /// \brief Make copy of the given map.
  9.1437 +    ///
  9.1438 +    /// Makes copy of the given map for the newly created digraph. 
  9.1439 +    /// The new map's key type is the to digraph's arc type,
  9.1440 +    /// and the copied map's key type is the from digraph's arc
  9.1441 +    /// type.  
  9.1442 +    template <typename ToMap, typename FromMap>
  9.1443 +    BpGraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
  9.1444 +      arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, 
  9.1445 +                              ArcRefMap, ToMap, FromMap>(tmap, map));
  9.1446 +      return *this;
  9.1447 +    }
  9.1448 +
  9.1449 +    /// \brief Make a copy of the given arc.
  9.1450 +    ///
  9.1451 +    /// Make a copy of the given arc.
  9.1452 +    BpGraphCopy& arc(TArc& tarc, const Arc& sarc) {
  9.1453 +      arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, 
  9.1454 +                              ArcRefMap, TArc>(tarc, sarc));
  9.1455 +      return *this;
  9.1456 +    }
  9.1457 +
  9.1458 +    /// \brief Copies the edge references into the given map.
  9.1459 +    ///
  9.1460 +    /// Copies the edge references into the given map.
  9.1461 +    template <typename EdgeRef>
  9.1462 +    BpGraphCopy& edgeRef(EdgeRef& map) {
  9.1463 +      edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge, 
  9.1464 +                               EdgeRefMap, EdgeRef>(map));
  9.1465 +      return *this;
  9.1466 +    }
  9.1467 +
  9.1468 +    /// \brief Copies the edge cross references into the given map.
  9.1469 +    ///
  9.1470 +    /// Copies the edge cross references (reverse
  9.1471 +    /// references) into the given map.
  9.1472 +    template <typename EdgeCrossRef>
  9.1473 +    BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
  9.1474 +      edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, 
  9.1475 +                               Edge, EdgeRefMap, EdgeCrossRef>(map));
  9.1476 +      return *this;
  9.1477 +    }
  9.1478 +
  9.1479 +    /// \brief Make copy of the given map.
  9.1480 +    ///
  9.1481 +    /// Makes copy of the given map for the newly created digraph. 
  9.1482 +    /// The new map's key type is the to digraph's edge type,
  9.1483 +    /// and the copied map's key type is the from digraph's edge
  9.1484 +    /// type.  
  9.1485 +    template <typename ToMap, typename FromMap>
  9.1486 +    BpGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
  9.1487 +      edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge, 
  9.1488 +                               EdgeRefMap, ToMap, FromMap>(tmap, map));
  9.1489 +      return *this;
  9.1490 +    }
  9.1491 +
  9.1492 +    /// \brief Make a copy of the given edge.
  9.1493 +    ///
  9.1494 +    /// Make a copy of the given edge.
  9.1495 +    BpGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
  9.1496 +      edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge, 
  9.1497 +                               EdgeRefMap, TEdge>(tedge, sedge));
  9.1498 +      return *this;
  9.1499 +    }
  9.1500 +
  9.1501 +    /// \brief Executes the copies.
  9.1502 +    ///
  9.1503 +    /// Executes the copies.
  9.1504 +    void run() {
  9.1505 +      RedRefMap redRefMap(from);
  9.1506 +      BlueRefMap blueRefMap(from);
  9.1507 +      NodeRefMap nodeRefMap(from, redRefMap, blueRefMap);
  9.1508 +      EdgeRefMap edgeRefMap(from);
  9.1509 +      ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap);
  9.1510 +      _digraph_utils_bits::BpGraphCopySelector<To>::
  9.1511 +        copy(to, from, redRefMap, blueRefMap, edgeRefMap);
  9.1512 +      for (int i = 0; i < int(redMapCopies.size()); ++i) {
  9.1513 +        redMapCopies[i]->copy(from, redRefMap);
  9.1514 +      }
  9.1515 +      for (int i = 0; i < int(blueMapCopies.size()); ++i) {
  9.1516 +        blueMapCopies[i]->copy(from, blueRefMap);
  9.1517 +      }
  9.1518 +      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
  9.1519 +        nodeMapCopies[i]->copy(from, nodeRefMap);
  9.1520 +      }
  9.1521 +      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
  9.1522 +        edgeMapCopies[i]->copy(from, edgeRefMap);
  9.1523 +      }
  9.1524 +      for (int i = 0; i < int(arcMapCopies.size()); ++i) {
  9.1525 +        arcMapCopies[i]->copy(from, arcRefMap);
  9.1526 +      }
  9.1527 +    }
  9.1528 +
  9.1529 +  private:
  9.1530 +    
  9.1531 +    const From& from;
  9.1532 +    To& to;
  9.1533 +
  9.1534 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Red, RedRefMap>* > 
  9.1535 +    redMapCopies;
  9.1536 +
  9.1537 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Blue, BlueRefMap>* > 
  9.1538 +    blueMapCopies;
  9.1539 +
  9.1540 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > 
  9.1541 +    nodeMapCopies;
  9.1542 +
  9.1543 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > 
  9.1544 +    arcMapCopies;
  9.1545 +
  9.1546 +    std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > 
  9.1547 +    edgeMapCopies;
  9.1548 +
  9.1549 +  };
  9.1550 +
  9.1551 +  /// \brief Copy a bipartite digraph to another digraph.
  9.1552 +  ///
  9.1553 +  /// Copy a bipartite digraph to another digraph.
  9.1554 +  /// The usage of the function:
  9.1555 +  /// 
  9.1556 +  ///\code
  9.1557 +  /// copyBpGraph(trg, src).redRef(anr).arcCrossRef(ecr).run();
  9.1558 +  ///\endcode
  9.1559 +  /// 
  9.1560 +  /// After the copy the \c nr map will contain the mapping from the
  9.1561 +  /// nodes of the \c from digraph to the nodes of the \c to digraph and
  9.1562 +  /// \c ecr will contain the mapping from the arcs of the \c to digraph
  9.1563 +  /// to the arcs of the \c from digraph.
  9.1564 +  ///
  9.1565 +  /// \see BpGraphCopy
  9.1566 +  template <typename To, typename From>
  9.1567 +  BpGraphCopy<To, From> 
  9.1568 +  copyBpGraph(To& to, const From& from) {
  9.1569 +    return BpGraphCopy<To, From>(to, from);
  9.1570 +  }
  9.1571 +
  9.1572 +
  9.1573 +  /// @}
  9.1574 +
  9.1575 +  /// \addtogroup digraph_maps
  9.1576 +  /// @{
  9.1577 +
  9.1578 +  /// Provides an immutable and unique id for each item in the digraph.
  9.1579 +
  9.1580 +  /// The IdMap class provides a unique and immutable id for each item of the
  9.1581 +  /// same type (e.g. node) in the digraph. This id is <ul><li>\b unique:
  9.1582 +  /// different items (nodes) get different ids <li>\b immutable: the id of an
  9.1583 +  /// item (node) does not change (even if you delete other nodes).  </ul>
  9.1584 +  /// Through this map you get access (i.e. can read) the inner id values of
  9.1585 +  /// the items stored in the digraph. This map can be inverted with its member
  9.1586 +  /// class \c InverseMap.
  9.1587 +  ///
  9.1588 +  template <typename _Digraph, typename _Item>
  9.1589 +  class IdMap {
  9.1590 +  public:
  9.1591 +    typedef _Digraph Digraph;
  9.1592 +    typedef int Value;
  9.1593 +    typedef _Item Item;
  9.1594 +    typedef _Item Key;
  9.1595 +
  9.1596 +    /// \brief Constructor.
  9.1597 +    ///
  9.1598 +    /// Constructor of the map.
  9.1599 +    explicit IdMap(const Digraph& _digraph) : digraph(&_digraph) {}
  9.1600 +
  9.1601 +    /// \brief Gives back the \e id of the item.
  9.1602 +    ///
  9.1603 +    /// Gives back the immutable and unique \e id of the item.
  9.1604 +    int operator[](const Item& item) const { return digraph->id(item);}
  9.1605 +
  9.1606 +    /// \brief Gives back the item by its id.
  9.1607 +    ///
  9.1608 +    /// Gives back the item by its id.
  9.1609 +    Item operator()(int id) { return digraph->fromId(id, Item()); }
  9.1610 +
  9.1611 +  private:
  9.1612 +    const Digraph* digraph;
  9.1613 +
  9.1614 +  public:
  9.1615 +
  9.1616 +    /// \brief The class represents the inverse of its owner (IdMap).
  9.1617 +    ///
  9.1618 +    /// The class represents the inverse of its owner (IdMap).
  9.1619 +    /// \see inverse()
  9.1620 +    class InverseMap {
  9.1621 +    public:
  9.1622 +
  9.1623 +      /// \brief Constructor.
  9.1624 +      ///
  9.1625 +      /// Constructor for creating an id-to-item map.
  9.1626 +      explicit InverseMap(const Digraph& _digraph) : digraph(&_digraph) {}
  9.1627 +
  9.1628 +      /// \brief Constructor.
  9.1629 +      ///
  9.1630 +      /// Constructor for creating an id-to-item map.
  9.1631 +      explicit InverseMap(const IdMap& idMap) : digraph(idMap.digraph) {}
  9.1632 +
  9.1633 +      /// \brief Gives back the given item from its id.
  9.1634 +      ///
  9.1635 +      /// Gives back the given item from its id.
  9.1636 +      /// 
  9.1637 +      Item operator[](int id) const { return digraph->fromId(id, Item());}
  9.1638 +
  9.1639 +    private:
  9.1640 +      const Digraph* digraph;
  9.1641 +    };
  9.1642 +
  9.1643 +    /// \brief Gives back the inverse of the map.
  9.1644 +    ///
  9.1645 +    /// Gives back the inverse of the IdMap.
  9.1646 +    InverseMap inverse() const { return InverseMap(*digraph);} 
  9.1647 +
  9.1648 +  };
  9.1649 +
  9.1650 +  
  9.1651 +  /// \brief General invertable digraph-map type.
  9.1652 +
  9.1653 +  /// This type provides simple invertable digraph-maps. 
  9.1654 +  /// The InvertableMap wraps an arbitrary ReadWriteMap 
  9.1655 +  /// and if a key is set to a new value then store it
  9.1656 +  /// in the inverse map.
  9.1657 +  ///
  9.1658 +  /// The values of the map can be accessed
  9.1659 +  /// with stl compatible forward iterator.
  9.1660 +  ///
  9.1661 +  /// \param _Digraph The digraph type.
  9.1662 +  /// \param _Item The item type of the digraph.
  9.1663 +  /// \param _Value The value type of the map.
  9.1664 +  ///
  9.1665 +  /// \see IterableValueMap
  9.1666 +  template <typename _Digraph, typename _Item, typename _Value>
  9.1667 +  class InvertableMap : protected DefaultMap<_Digraph, _Item, _Value> {
  9.1668 +  private:
  9.1669 +    
  9.1670 +    typedef DefaultMap<_Digraph, _Item, _Value> Map;
  9.1671 +    typedef _Digraph Digraph;
  9.1672 +
  9.1673 +    typedef std::map<_Value, _Item> Container;
  9.1674 +    Container invMap;    
  9.1675 +
  9.1676 +  public:
  9.1677 + 
  9.1678 +    /// The key type of InvertableMap (Node, Arc, Edge).
  9.1679 +    typedef typename Map::Key Key;
  9.1680 +    /// The value type of the InvertableMap.
  9.1681 +    typedef typename Map::Value Value;
  9.1682 +
  9.1683 +
  9.1684 +
  9.1685 +    /// \brief Constructor.
  9.1686 +    ///
  9.1687 +    /// Construct a new InvertableMap for the digraph.
  9.1688 +    ///
  9.1689 +    explicit InvertableMap(const Digraph& digraph) : Map(digraph) {} 
  9.1690 +
  9.1691 +    /// \brief Forward iterator for values.
  9.1692 +    ///
  9.1693 +    /// This iterator is an stl compatible forward
  9.1694 +    /// iterator on the values of the map. The values can
  9.1695 +    /// be accessed in the [beginValue, endValue) range.
  9.1696 +    ///
  9.1697 +    class ValueIterator 
  9.1698 +      : public std::iterator<std::forward_iterator_tag, Value> {
  9.1699 +      friend class InvertableMap;
  9.1700 +    private:
  9.1701 +      ValueIterator(typename Container::const_iterator _it) 
  9.1702 +        : it(_it) {}
  9.1703 +    public:
  9.1704 +      
  9.1705 +      ValueIterator() {}
  9.1706 +
  9.1707 +      ValueIterator& operator++() { ++it; return *this; }
  9.1708 +      ValueIterator operator++(int) { 
  9.1709 +        ValueIterator tmp(*this); 
  9.1710 +        operator++();
  9.1711 +        return tmp; 
  9.1712 +      }
  9.1713 +
  9.1714 +      const Value& operator*() const { return it->first; }
  9.1715 +      const Value* operator->() const { return &(it->first); }
  9.1716 +
  9.1717 +      bool operator==(ValueIterator jt) const { return it == jt.it; }
  9.1718 +      bool operator!=(ValueIterator jt) const { return it != jt.it; }
  9.1719 +      
  9.1720 +    private:
  9.1721 +      typename Container::const_iterator it;
  9.1722 +    };
  9.1723 +
  9.1724 +    /// \brief Returns an iterator to the first value.
  9.1725 +    ///
  9.1726 +    /// Returns an stl compatible iterator to the 
  9.1727 +    /// first value of the map. The values of the
  9.1728 +    /// map can be accessed in the [beginValue, endValue)
  9.1729 +    /// range.
  9.1730 +    ValueIterator beginValue() const {
  9.1731 +      return ValueIterator(invMap.begin());
  9.1732 +    }
  9.1733 +
  9.1734 +    /// \brief Returns an iterator after the last value.
  9.1735 +    ///
  9.1736 +    /// Returns an stl compatible iterator after the 
  9.1737 +    /// last value of the map. The values of the
  9.1738 +    /// map can be accessed in the [beginValue, endValue)
  9.1739 +    /// range.
  9.1740 +    ValueIterator endValue() const {
  9.1741 +      return ValueIterator(invMap.end());
  9.1742 +    }
  9.1743 +    
  9.1744 +    /// \brief The setter function of the map.
  9.1745 +    ///
  9.1746 +    /// Sets the mapped value.
  9.1747 +    void set(const Key& key, const Value& val) {
  9.1748 +      Value oldval = Map::operator[](key);
  9.1749 +      typename Container::iterator it = invMap.find(oldval);
  9.1750 +      if (it != invMap.end() && it->second == key) {
  9.1751 +	invMap.erase(it);
  9.1752 +      }      
  9.1753 +      invMap.insert(make_pair(val, key));
  9.1754 +      Map::set(key, val);
  9.1755 +    }
  9.1756 +
  9.1757 +    /// \brief The getter function of the map.
  9.1758 +    ///
  9.1759 +    /// It gives back the value associated with the key.
  9.1760 +    typename MapTraits<Map>::ConstReturnValue 
  9.1761 +    operator[](const Key& key) const {
  9.1762 +      return Map::operator[](key);
  9.1763 +    }
  9.1764 +
  9.1765 +    /// \brief Gives back the item by its value.
  9.1766 +    ///
  9.1767 +    /// Gives back the item by its value.
  9.1768 +    Key operator()(const Value& key) const {
  9.1769 +      typename Container::const_iterator it = invMap.find(key);
  9.1770 +      return it != invMap.end() ? it->second : INVALID;
  9.1771 +    }
  9.1772 +
  9.1773 +  protected:
  9.1774 +
  9.1775 +    /// \brief Erase the key from the map.
  9.1776 +    ///
  9.1777 +    /// Erase the key to the map. It is called by the
  9.1778 +    /// \c AlterationNotifier.
  9.1779 +    virtual void erase(const Key& key) {
  9.1780 +      Value val = Map::operator[](key);
  9.1781 +      typename Container::iterator it = invMap.find(val);
  9.1782 +      if (it != invMap.end() && it->second == key) {
  9.1783 +	invMap.erase(it);
  9.1784 +      }
  9.1785 +      Map::erase(key);
  9.1786 +    }
  9.1787 +
  9.1788 +    /// \brief Erase more keys from the map.
  9.1789 +    ///
  9.1790 +    /// Erase more keys from the map. It is called by the
  9.1791 +    /// \c AlterationNotifier.
  9.1792 +    virtual void erase(const std::vector<Key>& keys) {
  9.1793 +      for (int i = 0; i < int(keys.size()); ++i) {
  9.1794 +	Value val = Map::operator[](keys[i]);
  9.1795 +	typename Container::iterator it = invMap.find(val);
  9.1796 +	if (it != invMap.end() && it->second == keys[i]) {
  9.1797 +	  invMap.erase(it);
  9.1798 +	}
  9.1799 +      }
  9.1800 +      Map::erase(keys);
  9.1801 +    }
  9.1802 +
  9.1803 +    /// \brief Clear the keys from the map and inverse map.
  9.1804 +    ///
  9.1805 +    /// Clear the keys from the map and inverse map. It is called by the
  9.1806 +    /// \c AlterationNotifier.
  9.1807 +    virtual void clear() {
  9.1808 +      invMap.clear();
  9.1809 +      Map::clear();
  9.1810 +    }
  9.1811 +
  9.1812 +  public:
  9.1813 +
  9.1814 +    /// \brief The inverse map type.
  9.1815 +    ///
  9.1816 +    /// The inverse of this map. The subscript operator of the map
  9.1817 +    /// gives back always the item what was last assigned to the value. 
  9.1818 +    class InverseMap {
  9.1819 +    public:
  9.1820 +      /// \brief Constructor of the InverseMap.
  9.1821 +      ///
  9.1822 +      /// Constructor of the InverseMap.
  9.1823 +      explicit InverseMap(const InvertableMap& _inverted) 
  9.1824 +        : inverted(_inverted) {}
  9.1825 +
  9.1826 +      /// The value type of the InverseMap.
  9.1827 +      typedef typename InvertableMap::Key Value;
  9.1828 +      /// The key type of the InverseMap.
  9.1829 +      typedef typename InvertableMap::Value Key; 
  9.1830 +
  9.1831 +      /// \brief Subscript operator. 
  9.1832 +      ///
  9.1833 +      /// Subscript operator. It gives back always the item 
  9.1834 +      /// what was last assigned to the value.
  9.1835 +      Value operator[](const Key& key) const {
  9.1836 +	return inverted(key);
  9.1837 +      }
  9.1838 +      
  9.1839 +    private:
  9.1840 +      const InvertableMap& inverted;
  9.1841 +    };
  9.1842 +
  9.1843 +    /// \brief It gives back the just readable inverse map.
  9.1844 +    ///
  9.1845 +    /// It gives back the just readable inverse map.
  9.1846 +    InverseMap inverse() const {
  9.1847 +      return InverseMap(*this);
  9.1848 +    } 
  9.1849 +
  9.1850 +
  9.1851 +    
  9.1852 +  };
  9.1853 +
  9.1854 +  /// \brief Provides a mutable, continuous and unique descriptor for each 
  9.1855 +  /// item in the digraph.
  9.1856 +  ///
  9.1857 +  /// The DescriptorMap class provides a unique and continuous (but mutable)
  9.1858 +  /// descriptor (id) for each item of the same type (e.g. node) in the
  9.1859 +  /// digraph. This id is <ul><li>\b unique: different items (nodes) get
  9.1860 +  /// different ids <li>\b continuous: the range of the ids is the set of
  9.1861 +  /// integers between 0 and \c n-1, where \c n is the number of the items of
  9.1862 +  /// this type (e.g. nodes) (so the id of a node can change if you delete an
  9.1863 +  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
  9.1864 +  /// with its member class \c InverseMap.
  9.1865 +  ///
  9.1866 +  /// \param _Digraph The digraph class the \c DescriptorMap belongs to.
  9.1867 +  /// \param _Item The Item is the Key of the Map. It may be Node, Arc or 
  9.1868 +  /// Edge.
  9.1869 +  template <typename _Digraph, typename _Item>
  9.1870 +  class DescriptorMap : protected DefaultMap<_Digraph, _Item, int> {
  9.1871 +
  9.1872 +    typedef _Item Item;
  9.1873 +    typedef DefaultMap<_Digraph, _Item, int> Map;
  9.1874 +
  9.1875 +  public:
  9.1876 +    /// The digraph class of DescriptorMap.
  9.1877 +    typedef _Digraph Digraph;
  9.1878 +
  9.1879 +    /// The key type of DescriptorMap (Node, Arc, Edge).
  9.1880 +    typedef typename Map::Key Key;
  9.1881 +    /// The value type of DescriptorMap.
  9.1882 +    typedef typename Map::Value Value;
  9.1883 +
  9.1884 +    /// \brief Constructor.
  9.1885 +    ///
  9.1886 +    /// Constructor for descriptor map.
  9.1887 +    explicit DescriptorMap(const Digraph& _digraph) : Map(_digraph) {
  9.1888 +      Item it;
  9.1889 +      const typename Map::Notifier* nf = Map::notifier(); 
  9.1890 +      for (nf->first(it); it != INVALID; nf->next(it)) {
  9.1891 +	Map::set(it, invMap.size());
  9.1892 +	invMap.push_back(it);	
  9.1893 +      }      
  9.1894 +    }
  9.1895 +
  9.1896 +  protected:
  9.1897 +
  9.1898 +    /// \brief Add a new key to the map.
  9.1899 +    ///
  9.1900 +    /// Add a new key to the map. It is called by the
  9.1901 +    /// \c AlterationNotifier.
  9.1902 +    virtual void add(const Item& item) {
  9.1903 +      Map::add(item);
  9.1904 +      Map::set(item, invMap.size());
  9.1905 +      invMap.push_back(item);
  9.1906 +    }
  9.1907 +
  9.1908 +    /// \brief Add more new keys to the map.
  9.1909 +    ///
  9.1910 +    /// Add more new keys to the map. It is called by the
  9.1911 +    /// \c AlterationNotifier.
  9.1912 +    virtual void add(const std::vector<Item>& items) {
  9.1913 +      Map::add(items);
  9.1914 +      for (int i = 0; i < int(items.size()); ++i) {
  9.1915 +	Map::set(items[i], invMap.size());
  9.1916 +	invMap.push_back(items[i]);
  9.1917 +      }
  9.1918 +    }
  9.1919 +
  9.1920 +    /// \brief Erase the key from the map.
  9.1921 +    ///
  9.1922 +    /// Erase the key from the map. It is called by the
  9.1923 +    /// \c AlterationNotifier.
  9.1924 +    virtual void erase(const Item& item) {
  9.1925 +      Map::set(invMap.back(), Map::operator[](item));
  9.1926 +      invMap[Map::operator[](item)] = invMap.back();
  9.1927 +      invMap.pop_back();
  9.1928 +      Map::erase(item);
  9.1929 +    }
  9.1930 +
  9.1931 +    /// \brief Erase more keys from the map.
  9.1932 +    ///
  9.1933 +    /// Erase more keys from the map. It is called by the
  9.1934 +    /// \c AlterationNotifier.
  9.1935 +    virtual void erase(const std::vector<Item>& items) {
  9.1936 +      for (int i = 0; i < int(items.size()); ++i) {
  9.1937 +	Map::set(invMap.back(), Map::operator[](items[i]));
  9.1938 +	invMap[Map::operator[](items[i])] = invMap.back();
  9.1939 +	invMap.pop_back();
  9.1940 +      }
  9.1941 +      Map::erase(items);
  9.1942 +    }
  9.1943 +
  9.1944 +    /// \brief Build the unique map.
  9.1945 +    ///
  9.1946 +    /// Build the unique map. It is called by the
  9.1947 +    /// \c AlterationNotifier.
  9.1948 +    virtual void build() {
  9.1949 +      Map::build();
  9.1950 +      Item it;
  9.1951 +      const typename Map::Notifier* nf = Map::notifier(); 
  9.1952 +      for (nf->first(it); it != INVALID; nf->next(it)) {
  9.1953 +	Map::set(it, invMap.size());
  9.1954 +	invMap.push_back(it);	
  9.1955 +      }      
  9.1956 +    }
  9.1957 +    
  9.1958 +    /// \brief Clear the keys from the map.
  9.1959 +    ///
  9.1960 +    /// Clear the keys from the map. It is called by the
  9.1961 +    /// \c AlterationNotifier.
  9.1962 +    virtual void clear() {
  9.1963 +      invMap.clear();
  9.1964 +      Map::clear();
  9.1965 +    }
  9.1966 +
  9.1967 +  public:
  9.1968 +
  9.1969 +    /// \brief Returns the maximal value plus one.
  9.1970 +    ///
  9.1971 +    /// Returns the maximal value plus one in the map.
  9.1972 +    unsigned int size() const {
  9.1973 +      return invMap.size();
  9.1974 +    }
  9.1975 +
  9.1976 +    /// \brief Swaps the position of the two items in the map.
  9.1977 +    ///
  9.1978 +    /// Swaps the position of the two items in the map.
  9.1979 +    void swap(const Item& p, const Item& q) {
  9.1980 +      int pi = Map::operator[](p);
  9.1981 +      int qi = Map::operator[](q);
  9.1982 +      Map::set(p, qi);
  9.1983 +      invMap[qi] = p;
  9.1984 +      Map::set(q, pi);
  9.1985 +      invMap[pi] = q;
  9.1986 +    }
  9.1987 +
  9.1988 +    /// \brief Gives back the \e descriptor of the item.
  9.1989 +    ///
  9.1990 +    /// Gives back the mutable and unique \e descriptor of the map.
  9.1991 +    int operator[](const Item& item) const {
  9.1992 +      return Map::operator[](item);
  9.1993 +    }
  9.1994 +
  9.1995 +    /// \brief Gives back the item by its descriptor.
  9.1996 +    ///
  9.1997 +    /// Gives back th item by its descriptor.
  9.1998 +    Item operator()(int id) const {
  9.1999 +      return invMap[id];
  9.2000 +    }
  9.2001 +    
  9.2002 +  private:
  9.2003 +
  9.2004 +    typedef std::vector<Item> Container;
  9.2005 +    Container invMap;
  9.2006 +
  9.2007 +  public:
  9.2008 +    /// \brief The inverse map type of DescriptorMap.
  9.2009 +    ///
  9.2010 +    /// The inverse map type of DescriptorMap.
  9.2011 +    class InverseMap {
  9.2012 +    public:
  9.2013 +      /// \brief Constructor of the InverseMap.
  9.2014 +      ///
  9.2015 +      /// Constructor of the InverseMap.
  9.2016 +      explicit InverseMap(const DescriptorMap& _inverted) 
  9.2017 +	: inverted(_inverted) {}
  9.2018 +
  9.2019 +
  9.2020 +      /// The value type of the InverseMap.
  9.2021 +      typedef typename DescriptorMap::Key Value;
  9.2022 +      /// The key type of the InverseMap.
  9.2023 +      typedef typename DescriptorMap::Value Key; 
  9.2024 +
  9.2025 +      /// \brief Subscript operator. 
  9.2026 +      ///
  9.2027 +      /// Subscript operator. It gives back the item 
  9.2028 +      /// that the descriptor belongs to currently.
  9.2029 +      Value operator[](const Key& key) const {
  9.2030 +	return inverted(key);
  9.2031 +      }
  9.2032 +
  9.2033 +      /// \brief Size of the map.
  9.2034 +      ///
  9.2035 +      /// Returns the size of the map.
  9.2036 +      unsigned int size() const {
  9.2037 +	return inverted.size();
  9.2038 +      }
  9.2039 +      
  9.2040 +    private:
  9.2041 +      const DescriptorMap& inverted;
  9.2042 +    };
  9.2043 +
  9.2044 +    /// \brief Gives back the inverse of the map.
  9.2045 +    ///
  9.2046 +    /// Gives back the inverse of the map.
  9.2047 +    const InverseMap inverse() const {
  9.2048 +      return InverseMap(*this);
  9.2049 +    }
  9.2050 +  };
  9.2051 +
  9.2052 +  /// \brief Returns the source of the given arc.
  9.2053 +  ///
  9.2054 +  /// The SourceMap gives back the source Node of the given arc. 
  9.2055 +  /// \see TargetMap
  9.2056 +  /// \author Balazs Dezso
  9.2057 +  template <typename Digraph>
  9.2058 +  class SourceMap {
  9.2059 +  public:
  9.2060 +
  9.2061 +    typedef typename Digraph::Node Value;
  9.2062 +    typedef typename Digraph::Arc Key;
  9.2063 +
  9.2064 +    /// \brief Constructor
  9.2065 +    ///
  9.2066 +    /// Constructor
  9.2067 +    /// \param _digraph The digraph that the map belongs to.
  9.2068 +    explicit SourceMap(const Digraph& _digraph) : digraph(_digraph) {}
  9.2069 +
  9.2070 +    /// \brief The subscript operator.
  9.2071 +    ///
  9.2072 +    /// The subscript operator.
  9.2073 +    /// \param arc The arc 
  9.2074 +    /// \return The source of the arc 
  9.2075 +    Value operator[](const Key& arc) const {
  9.2076 +      return digraph.source(arc);
  9.2077 +    }
  9.2078 +
  9.2079 +  private:
  9.2080 +    const Digraph& digraph;
  9.2081 +  };
  9.2082 +
  9.2083 +  /// \brief Returns a \ref SourceMap class.
  9.2084 +  ///
  9.2085 +  /// This function just returns an \ref SourceMap class.
  9.2086 +  /// \relates SourceMap
  9.2087 +  template <typename Digraph>
  9.2088 +  inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
  9.2089 +    return SourceMap<Digraph>(digraph);
  9.2090 +  } 
  9.2091 +
  9.2092 +  /// \brief Returns the target of the given arc.
  9.2093 +  ///
  9.2094 +  /// The TargetMap gives back the target Node of the given arc. 
  9.2095 +  /// \see SourceMap
  9.2096 +  /// \author Balazs Dezso
  9.2097 +  template <typename Digraph>
  9.2098 +  class TargetMap {
  9.2099 +  public:
  9.2100 +
  9.2101 +    typedef typename Digraph::Node Value;
  9.2102 +    typedef typename Digraph::Arc Key;
  9.2103 +
  9.2104 +    /// \brief Constructor
  9.2105 +    ///
  9.2106 +    /// Constructor
  9.2107 +    /// \param _digraph The digraph that the map belongs to.
  9.2108 +    explicit TargetMap(const Digraph& _digraph) : digraph(_digraph) {}
  9.2109 +
  9.2110 +    /// \brief The subscript operator.
  9.2111 +    ///
  9.2112 +    /// The subscript operator.
  9.2113 +    /// \param e The arc 
  9.2114 +    /// \return The target of the arc 
  9.2115 +    Value operator[](const Key& e) const {
  9.2116 +      return digraph.target(e);
  9.2117 +    }
  9.2118 +
  9.2119 +  private:
  9.2120 +    const Digraph& digraph;
  9.2121 +  };
  9.2122 +
  9.2123 +  /// \brief Returns a \ref TargetMap class.
  9.2124 +  ///
  9.2125 +  /// This function just returns a \ref TargetMap class.
  9.2126 +  /// \relates TargetMap
  9.2127 +  template <typename Digraph>
  9.2128 +  inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
  9.2129 +    return TargetMap<Digraph>(digraph);
  9.2130 +  }
  9.2131 +
  9.2132 +  /// \brief Returns the "forward" directed arc view of an edge.
  9.2133 +  ///
  9.2134 +  /// Returns the "forward" directed arc view of an edge.
  9.2135 +  /// \see BackwardMap
  9.2136 +  /// \author Balazs Dezso
  9.2137 +  template <typename Digraph>
  9.2138 +  class ForwardMap {
  9.2139 +  public:
  9.2140 +
  9.2141 +    typedef typename Digraph::Arc Value;
  9.2142 +    typedef typename Digraph::Edge Key;
  9.2143 +
  9.2144 +    /// \brief Constructor
  9.2145 +    ///
  9.2146 +    /// Constructor
  9.2147 +    /// \param _digraph The digraph that the map belongs to.
  9.2148 +    explicit ForwardMap(const Digraph& _digraph) : digraph(_digraph) {}
  9.2149 +
  9.2150 +    /// \brief The subscript operator.
  9.2151 +    ///
  9.2152 +    /// The subscript operator.
  9.2153 +    /// \param key An edge 
  9.2154 +    /// \return The "forward" directed arc view of edge 
  9.2155 +    Value operator[](const Key& key) const {
  9.2156 +      return digraph.direct(key, true);
  9.2157 +    }
  9.2158 +
  9.2159 +  private:
  9.2160 +    const Digraph& digraph;
  9.2161 +  };
  9.2162 +
  9.2163 +  /// \brief Returns a \ref ForwardMap class.
  9.2164 +  ///
  9.2165 +  /// This function just returns an \ref ForwardMap class.
  9.2166 +  /// \relates ForwardMap
  9.2167 +  template <typename Digraph>
  9.2168 +  inline ForwardMap<Digraph> forwardMap(const Digraph& digraph) {
  9.2169 +    return ForwardMap<Digraph>(digraph);
  9.2170 +  }
  9.2171 +
  9.2172 +  /// \brief Returns the "backward" directed arc view of an edge.
  9.2173 +  ///
  9.2174 +  /// Returns the "backward" directed arc view of an edge.
  9.2175 +  /// \see ForwardMap
  9.2176 +  /// \author Balazs Dezso
  9.2177 +  template <typename Digraph>
  9.2178 +  class BackwardMap {
  9.2179 +  public:
  9.2180 +
  9.2181 +    typedef typename Digraph::Arc Value;
  9.2182 +    typedef typename Digraph::Edge Key;
  9.2183 +
  9.2184 +    /// \brief Constructor
  9.2185 +    ///
  9.2186 +    /// Constructor
  9.2187 +    /// \param _digraph The digraph that the map belongs to.
  9.2188 +    explicit BackwardMap(const Digraph& _digraph) : digraph(_digraph) {}
  9.2189 +
  9.2190 +    /// \brief The subscript operator.
  9.2191 +    ///
  9.2192 +    /// The subscript operator.
  9.2193 +    /// \param key An edge 
  9.2194 +    /// \return The "backward" directed arc view of edge 
  9.2195 +    Value operator[](const Key& key) const {
  9.2196 +      return digraph.direct(key, false);
  9.2197 +    }
  9.2198 +
  9.2199 +  private:
  9.2200 +    const Digraph& digraph;
  9.2201 +  };
  9.2202 +
  9.2203 +  /// \brief Returns a \ref BackwardMap class
  9.2204 +
  9.2205 +  /// This function just returns a \ref BackwardMap class.
  9.2206 +  /// \relates BackwardMap
  9.2207 +  template <typename Digraph>
  9.2208 +  inline BackwardMap<Digraph> backwardMap(const Digraph& digraph) {
  9.2209 +    return BackwardMap<Digraph>(digraph);
  9.2210 +  }
  9.2211 +
  9.2212 +  /// \brief Potential difference map
  9.2213 +  ///
  9.2214 +  /// If there is an potential map on the nodes then we
  9.2215 +  /// can get an arc map as we get the substraction of the
  9.2216 +  /// values of the target and source.
  9.2217 +  template <typename Digraph, typename NodeMap>
  9.2218 +  class PotentialDifferenceMap {
  9.2219 +  public:
  9.2220 +    typedef typename Digraph::Arc Key;
  9.2221 +    typedef typename NodeMap::Value Value;
  9.2222 +
  9.2223 +    /// \brief Constructor
  9.2224 +    ///
  9.2225 +    /// Contructor of the map
  9.2226 +    explicit PotentialDifferenceMap(const Digraph& _digraph, 
  9.2227 +                                    const NodeMap& _potential) 
  9.2228 +      : digraph(_digraph), potential(_potential) {}
  9.2229 +
  9.2230 +    /// \brief Const subscription operator
  9.2231 +    ///
  9.2232 +    /// Const subscription operator
  9.2233 +    Value operator[](const Key& arc) const {
  9.2234 +      return potential[digraph.target(arc)] - potential[digraph.source(arc)];
  9.2235 +    }
  9.2236 +
  9.2237 +  private:
  9.2238 +    const Digraph& digraph;
  9.2239 +    const NodeMap& potential;
  9.2240 +  };
  9.2241 +
  9.2242 +  /// \brief Returns a PotentialDifferenceMap.
  9.2243 +  ///
  9.2244 +  /// This function just returns a PotentialDifferenceMap.
  9.2245 +  /// \relates PotentialDifferenceMap
  9.2246 +  template <typename Digraph, typename NodeMap>
  9.2247 +  PotentialDifferenceMap<Digraph, NodeMap> 
  9.2248 +  potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
  9.2249 +    return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
  9.2250 +  }
  9.2251 +
  9.2252 +  /// \brief Map of the node in-degrees.
  9.2253 +  ///
  9.2254 +  /// This map returns the in-degree of a node. Once it is constructed,
  9.2255 +  /// the degrees are stored in a standard NodeMap, so each query is done
  9.2256 +  /// in constant time. On the other hand, the values are updated automatically
  9.2257 +  /// whenever the digraph changes.
  9.2258 +  ///
  9.2259 +  /// \warning Besides addNode() and addArc(), a digraph structure may provide
  9.2260 +  /// alternative ways to modify the digraph. The correct behavior of InDegMap
  9.2261 +  /// is not guarantied if these additional features are used. For example
  9.2262 +  /// the functions \ref ListDigraph::changeSource() "changeSource()",
  9.2263 +  /// \ref ListDigraph::changeTarget() "changeTarget()" and
  9.2264 +  /// \ref ListDigraph::reverseArc() "reverseArc()"
  9.2265 +  /// of \ref ListDigraph will \e not update the degree values correctly.
  9.2266 +  ///
  9.2267 +  /// \sa OutDegMap
  9.2268 +
  9.2269 +  template <typename _Digraph>
  9.2270 +  class InDegMap  
  9.2271 +    : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
  9.2272 +      ::ItemNotifier::ObserverBase {
  9.2273 +
  9.2274 +  public:
  9.2275 +    
  9.2276 +    typedef _Digraph Digraph;
  9.2277 +    typedef int Value;
  9.2278 +    typedef typename Digraph::Node Key;
  9.2279 +
  9.2280 +    typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc>
  9.2281 +    ::ItemNotifier::ObserverBase Parent;
  9.2282 +
  9.2283 +  private:
  9.2284 +
  9.2285 +    class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
  9.2286 +    public:
  9.2287 +
  9.2288 +      typedef DefaultMap<_Digraph, Key, int> Parent;
  9.2289 +      typedef typename Parent::Digraph Digraph;
  9.2290 +
  9.2291 +      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
  9.2292 +      
  9.2293 +      virtual void add(const Key& key) {
  9.2294 +	Parent::add(key);
  9.2295 +	Parent::set(key, 0);
  9.2296 +      }
  9.2297 +
  9.2298 +      virtual void add(const std::vector<Key>& keys) {
  9.2299 +	Parent::add(keys);
  9.2300 +	for (int i = 0; i < int(keys.size()); ++i) {
  9.2301 +	  Parent::set(keys[i], 0);
  9.2302 +	}
  9.2303 +      }
  9.2304 +
  9.2305 +      virtual void build() {
  9.2306 +	Parent::build();
  9.2307 +	Key it;
  9.2308 +	typename Parent::Notifier* nf = Parent::notifier();
  9.2309 +	for (nf->first(it); it != INVALID; nf->next(it)) {
  9.2310 +	  Parent::set(it, 0);
  9.2311 +	}
  9.2312 +      }
  9.2313 +    };
  9.2314 +
  9.2315 +  public:
  9.2316 +
  9.2317 +    /// \brief Constructor.
  9.2318 +    ///
  9.2319 +    /// Constructor for creating in-degree map.
  9.2320 +    explicit InDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
  9.2321 +      Parent::attach(digraph.notifier(typename _Digraph::Arc()));
  9.2322 +      
  9.2323 +      for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
  9.2324 +	deg[it] = countInArcs(digraph, it);
  9.2325 +      }
  9.2326 +    }
  9.2327 +    
  9.2328 +    /// Gives back the in-degree of a Node.
  9.2329 +    int operator[](const Key& key) const {
  9.2330 +      return deg[key];
  9.2331 +    }
  9.2332 +
  9.2333 +  protected:
  9.2334 +    
  9.2335 +    typedef typename Digraph::Arc Arc;
  9.2336 +
  9.2337 +    virtual void add(const Arc& arc) {
  9.2338 +      ++deg[digraph.target(arc)];
  9.2339 +    }
  9.2340 +
  9.2341 +    virtual void add(const std::vector<Arc>& arcs) {
  9.2342 +      for (int i = 0; i < int(arcs.size()); ++i) {
  9.2343 +        ++deg[digraph.target(arcs[i])];
  9.2344 +      }
  9.2345 +    }
  9.2346 +
  9.2347 +    virtual void erase(const Arc& arc) {
  9.2348 +      --deg[digraph.target(arc)];
  9.2349 +    }
  9.2350 +
  9.2351 +    virtual void erase(const std::vector<Arc>& arcs) {
  9.2352 +      for (int i = 0; i < int(arcs.size()); ++i) {
  9.2353 +        --deg[digraph.target(arcs[i])];
  9.2354 +      }
  9.2355 +    }
  9.2356 +
  9.2357 +    virtual void build() {
  9.2358 +      for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
  9.2359 +	deg[it] = countInArcs(digraph, it);
  9.2360 +      }      
  9.2361 +    }
  9.2362 +
  9.2363 +    virtual void clear() {
  9.2364 +      for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
  9.2365 +	deg[it] = 0;
  9.2366 +      }
  9.2367 +    }
  9.2368 +  private:
  9.2369 +    
  9.2370 +    const _Digraph& digraph;
  9.2371 +    AutoNodeMap deg;
  9.2372 +  };
  9.2373 +
  9.2374 +  /// \brief Map of the node out-degrees.
  9.2375 +  ///
  9.2376 +  /// This map returns the out-degree of a node. Once it is constructed,
  9.2377 +  /// the degrees are stored in a standard NodeMap, so each query is done
  9.2378 +  /// in constant time. On the other hand, the values are updated automatically
  9.2379 +  /// whenever the digraph changes.
  9.2380 +  ///
  9.2381 +  /// \warning Besides addNode() and addArc(), a digraph structure may provide
  9.2382 +  /// alternative ways to modify the digraph. The correct behavior of OutDegMap
  9.2383 +  /// is not guarantied if these additional features are used. For example
  9.2384 +  /// the functions \ref ListDigraph::changeSource() "changeSource()",
  9.2385 +  /// \ref ListDigraph::changeTarget() "changeTarget()" and
  9.2386 +  /// \ref ListDigraph::reverseArc() "reverseArc()"
  9.2387 +  /// of \ref ListDigraph will \e not update the degree values correctly.
  9.2388 +  ///
  9.2389 +  /// \sa InDegMap
  9.2390 +
  9.2391 +  template <typename _Digraph>
  9.2392 +  class OutDegMap  
  9.2393 +    : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
  9.2394 +      ::ItemNotifier::ObserverBase {
  9.2395 +
  9.2396 +  public:
  9.2397 +
  9.2398 +    typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc>
  9.2399 +    ::ItemNotifier::ObserverBase Parent;
  9.2400 +    
  9.2401 +    typedef _Digraph Digraph;
  9.2402 +    typedef int Value;
  9.2403 +    typedef typename Digraph::Node Key;
  9.2404 +
  9.2405 +  private:
  9.2406 +
  9.2407 +    class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
  9.2408 +    public:
  9.2409 +
  9.2410 +      typedef DefaultMap<_Digraph, Key, int> Parent;
  9.2411 +      typedef typename Parent::Digraph Digraph;
  9.2412 +
  9.2413 +      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
  9.2414 +      
  9.2415 +      virtual void add(const Key& key) {
  9.2416 +	Parent::add(key);
  9.2417 +	Parent::set(key, 0);
  9.2418 +      }
  9.2419 +      virtual void add(const std::vector<Key>& keys) {
  9.2420 +	Parent::add(keys);
  9.2421 +	for (int i = 0; i < int(keys.size()); ++i) {
  9.2422 +	  Parent::set(keys[i], 0);
  9.2423 +	}
  9.2424 +      }
  9.2425 +      virtual void build() {
  9.2426 +	Parent::build();
  9.2427 +	Key it;
  9.2428 +	typename Parent::Notifier* nf = Parent::notifier();
  9.2429 +	for (nf->first(it); it != INVALID; nf->next(it)) {
  9.2430 +	  Parent::set(it, 0);
  9.2431 +	}
  9.2432 +      }
  9.2433 +    };
  9.2434 +
  9.2435 +  public:
  9.2436 +
  9.2437 +    /// \brief Constructor.
  9.2438 +    ///
  9.2439 +    /// Constructor for creating out-degree map.
  9.2440 +    explicit OutDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
  9.2441 +      Parent::attach(digraph.notifier(typename _Digraph::Arc()));
  9.2442 +      
  9.2443 +      for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
  9.2444 +	deg[it] = countOutArcs(digraph, it);
  9.2445 +      }
  9.2446 +    }
  9.2447 +
  9.2448 +    /// Gives back the out-degree of a Node.
  9.2449 +    int operator[](const Key& key) const {
  9.2450 +      return deg[key];
  9.2451 +    }
  9.2452 +
  9.2453 +  protected:
  9.2454 +    
  9.2455 +    typedef typename Digraph::Arc Arc;
  9.2456 +
  9.2457 +    virtual void add(const Arc& arc) {
  9.2458 +      ++deg[digraph.source(arc)];
  9.2459 +    }
  9.2460 +
  9.2461 +    virtual void add(const std::vector<Arc>& arcs) {
  9.2462 +      for (int i = 0; i < int(arcs.size()); ++i) {
  9.2463 +        ++deg[digraph.source(arcs[i])];
  9.2464 +      }
  9.2465 +    }
  9.2466 +
  9.2467 +    virtual void erase(const Arc& arc) {
  9.2468 +      --deg[digraph.source(arc)];
  9.2469 +    }
  9.2470 +
  9.2471 +    virtual void erase(const std::vector<Arc>& arcs) {
  9.2472 +      for (int i = 0; i < int(arcs.size()); ++i) {
  9.2473 +        --deg[digraph.source(arcs[i])];
  9.2474 +      }
  9.2475 +    }
  9.2476 +
  9.2477 +    virtual void build() {
  9.2478 +      for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
  9.2479 +	deg[it] = countOutArcs(digraph, it);
  9.2480 +      }      
  9.2481 +    }
  9.2482 +
  9.2483 +    virtual void clear() {
  9.2484 +      for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
  9.2485 +	deg[it] = 0;
  9.2486 +      }
  9.2487 +    }
  9.2488 +  private:
  9.2489 +    
  9.2490 +    const _Digraph& digraph;
  9.2491 +    AutoNodeMap deg;
  9.2492 +  };
  9.2493 +
  9.2494 +
  9.2495 +  ///Dynamic arc look up between given endpoints.
  9.2496 +  
  9.2497 +  ///\ingroup gutils
  9.2498 +  ///Using this class, you can find an arc in a digraph from a given
  9.2499 +  ///source to a given target in amortized time <em>O(log d)</em>,
  9.2500 +  ///where <em>d</em> is the out-degree of the source node.
  9.2501 +  ///
  9.2502 +  ///It is possible to find \e all parallel arcs between two nodes with
  9.2503 +  ///the \c findFirst() and \c findNext() members.
  9.2504 +  ///
  9.2505 +  ///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
  9.2506 +  ///digraph do not changed so frequently.
  9.2507 +  ///
  9.2508 +  ///This class uses a self-adjusting binary search tree, Sleator's
  9.2509 +  ///and Tarjan's Splay tree for guarantee the logarithmic amortized
  9.2510 +  ///time bound for arc lookups. This class also guarantees the
  9.2511 +  ///optimal time bound in a constant factor for any distribution of
  9.2512 +  ///queries.
  9.2513 +  ///
  9.2514 +  ///\param G The type of the underlying digraph.  
  9.2515 +  ///
  9.2516 +  ///\sa ArcLookUp  
  9.2517 +  ///\sa AllArcLookUp  
  9.2518 +  template<class G>
  9.2519 +  class DynArcLookUp 
  9.2520 +    : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
  9.2521 +  {
  9.2522 +  public:
  9.2523 +    typedef typename ItemSetTraits<G, typename G::Arc>
  9.2524 +    ::ItemNotifier::ObserverBase Parent;
  9.2525 +
  9.2526 +    GRAPH_TYPEDEFS(typename G);
  9.2527 +    typedef G Digraph;
  9.2528 +
  9.2529 +  protected:
  9.2530 +
  9.2531 +    class AutoNodeMap : public DefaultMap<G, Node, Arc> {
  9.2532 +    public:
  9.2533 +
  9.2534 +      typedef DefaultMap<G, Node, Arc> Parent;
  9.2535 +
  9.2536 +      AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
  9.2537 +      
  9.2538 +      virtual void add(const Node& node) {
  9.2539 +	Parent::add(node);
  9.2540 +	Parent::set(node, INVALID);
  9.2541 +      }
  9.2542 +
  9.2543 +      virtual void add(const std::vector<Node>& nodes) {
  9.2544 +	Parent::add(nodes);
  9.2545 +	for (int i = 0; i < int(nodes.size()); ++i) {
  9.2546 +	  Parent::set(nodes[i], INVALID);
  9.2547 +	}
  9.2548 +      }
  9.2549 +
  9.2550 +      virtual void build() {
  9.2551 +	Parent::build();
  9.2552 +	Node it;
  9.2553 +	typename Parent::Notifier* nf = Parent::notifier();
  9.2554 +	for (nf->first(it); it != INVALID; nf->next(it)) {
  9.2555 +	  Parent::set(it, INVALID);
  9.2556 +	}
  9.2557 +      }
  9.2558 +    };
  9.2559 +
  9.2560 +    const Digraph &_g;
  9.2561 +    AutoNodeMap _head;
  9.2562 +    typename Digraph::template ArcMap<Arc> _parent;
  9.2563 +    typename Digraph::template ArcMap<Arc> _left;
  9.2564 +    typename Digraph::template ArcMap<Arc> _right;
  9.2565 +    
  9.2566 +    class ArcLess {
  9.2567 +      const Digraph &g;
  9.2568 +    public:
  9.2569 +      ArcLess(const Digraph &_g) : g(_g) {}
  9.2570 +      bool operator()(Arc a,Arc b) const 
  9.2571 +      {
  9.2572 +	return g.target(a)<g.target(b);
  9.2573 +      }
  9.2574 +    };
  9.2575 +    
  9.2576 +  public:
  9.2577 +    
  9.2578 +    ///Constructor
  9.2579 +
  9.2580 +    ///Constructor.
  9.2581 +    ///
  9.2582 +    ///It builds up the search database.
  9.2583 +    DynArcLookUp(const Digraph &g) 
  9.2584 +      : _g(g),_head(g),_parent(g),_left(g),_right(g) 
  9.2585 +    { 
  9.2586 +      Parent::attach(_g.notifier(typename Digraph::Arc()));
  9.2587 +      refresh(); 
  9.2588 +    }
  9.2589 +    
  9.2590 +  protected:
  9.2591 +
  9.2592 +    virtual void add(const Arc& arc) {
  9.2593 +      insert(arc);
  9.2594 +    }
  9.2595 +
  9.2596 +    virtual void add(const std::vector<Arc>& arcs) {
  9.2597 +      for (int i = 0; i < int(arcs.size()); ++i) {
  9.2598 +	insert(arcs[i]);
  9.2599 +      }
  9.2600 +    }
  9.2601 +
  9.2602 +    virtual void erase(const Arc& arc) {
  9.2603 +      remove(arc);
  9.2604 +    }
  9.2605 +
  9.2606 +    virtual void erase(const std::vector<Arc>& arcs) {
  9.2607 +      for (int i = 0; i < int(arcs.size()); ++i) {
  9.2608 +	remove(arcs[i]);
  9.2609 +      }     
  9.2610 +    }
  9.2611 +
  9.2612 +    virtual void build() {
  9.2613 +      refresh();
  9.2614 +    }
  9.2615 +
  9.2616 +    virtual void clear() {
  9.2617 +      for(NodeIt n(_g);n!=INVALID;++n) {
  9.2618 +	_head.set(n, INVALID);
  9.2619 +      }
  9.2620 +    }
  9.2621 +
  9.2622 +    void insert(Arc arc) {
  9.2623 +      Node s = _g.source(arc);
  9.2624 +      Node t = _g.target(arc);
  9.2625 +      _left.set(arc, INVALID);
  9.2626 +      _right.set(arc, INVALID);
  9.2627 +      
  9.2628 +      Arc e = _head[s];
  9.2629 +      if (e == INVALID) {
  9.2630 +	_head.set(s, arc);
  9.2631 +	_parent.set(arc, INVALID);
  9.2632 +	return;
  9.2633 +      }
  9.2634 +      while (true) {
  9.2635 +	if (t < _g.target(e)) {
  9.2636 +	  if (_left[e] == INVALID) {
  9.2637 +	    _left.set(e, arc);
  9.2638 +	    _parent.set(arc, e);
  9.2639 +	    splay(arc);
  9.2640 +	    return;
  9.2641 +	  } else {
  9.2642 +	    e = _left[e];
  9.2643 +	  }
  9.2644 +	} else {
  9.2645 +	  if (_right[e] == INVALID) {
  9.2646 +	    _right.set(e, arc);
  9.2647 +	    _parent.set(arc, e);
  9.2648 +	    splay(arc);
  9.2649 +	    return;
  9.2650 +	  } else {
  9.2651 +	    e = _right[e];
  9.2652 +	  }
  9.2653 +	}
  9.2654 +      }
  9.2655 +    }
  9.2656 +
  9.2657 +    void remove(Arc arc) {
  9.2658 +      if (_left[arc] == INVALID) {
  9.2659 +	if (_right[arc] != INVALID) {
  9.2660 +	  _parent.set(_right[arc], _parent[arc]);
  9.2661 +	}
  9.2662 +	if (_parent[arc] != INVALID) {
  9.2663 +	  if (_left[_parent[arc]] == arc) {
  9.2664 +	    _left.set(_parent[arc], _right[arc]);
  9.2665 +	  } else {
  9.2666 +	    _right.set(_parent[arc], _right[arc]);
  9.2667 +	  }
  9.2668 +	} else {
  9.2669 +	  _head.set(_g.source(arc), _right[arc]);
  9.2670 +	}
  9.2671 +      } else if (_right[arc] == INVALID) {
  9.2672 +	_parent.set(_left[arc], _parent[arc]);
  9.2673 +	if (_parent[arc] != INVALID) {
  9.2674 +	  if (_left[_parent[arc]] == arc) {
  9.2675 +	    _left.set(_parent[arc], _left[arc]);
  9.2676 +	  } else {
  9.2677 +	    _right.set(_parent[arc], _left[arc]);
  9.2678 +	  }
  9.2679 +	} else {
  9.2680 +	  _head.set(_g.source(arc), _left[arc]);
  9.2681 +	}
  9.2682 +      } else {
  9.2683 +	Arc e = _left[arc];
  9.2684 +	if (_right[e] != INVALID) {
  9.2685 +	  e = _right[e];	  
  9.2686 +	  while (_right[e] != INVALID) {
  9.2687 +	    e = _right[e];
  9.2688 +	  }
  9.2689 +	  Arc s = _parent[e];
  9.2690 +	  _right.set(_parent[e], _left[e]);
  9.2691 +	  if (_left[e] != INVALID) {
  9.2692 +	    _parent.set(_left[e], _parent[e]);
  9.2693 +	  }
  9.2694 +	  
  9.2695 +	  _left.set(e, _left[arc]);
  9.2696 +	  _parent.set(_left[arc], e);
  9.2697 +	  _right.set(e, _right[arc]);
  9.2698 +	  _parent.set(_right[arc], e);
  9.2699 +
  9.2700 +	  _parent.set(e, _parent[arc]);
  9.2701 +	  if (_parent[arc] != INVALID) {
  9.2702 +	    if (_left[_parent[arc]] == arc) {
  9.2703 +	      _left.set(_parent[arc], e);
  9.2704 +	    } else {
  9.2705 +	      _right.set(_parent[arc], e);
  9.2706 +	    }
  9.2707 +	  }
  9.2708 +	  splay(s);
  9.2709 +	} else {
  9.2710 +	  _right.set(e, _right[arc]);
  9.2711 +	  _parent.set(_right[arc], e);
  9.2712 +
  9.2713 +	  if (_parent[arc] != INVALID) {
  9.2714 +	    if (_left[_parent[arc]] == arc) {
  9.2715 +	      _left.set(_parent[arc], e);
  9.2716 +	    } else {
  9.2717 +	      _right.set(_parent[arc], e);
  9.2718 +	    }
  9.2719 +	  } else {
  9.2720 +	    _head.set(_g.source(arc), e);
  9.2721 +	  }
  9.2722 +	}
  9.2723 +      }
  9.2724 +    }
  9.2725 +
  9.2726 +    Arc refreshRec(std::vector<Arc> &v,int a,int b) 
  9.2727 +    {
  9.2728 +      int m=(a+b)/2;
  9.2729 +      Arc me=v[m];
  9.2730 +      if (a < m) {
  9.2731 +	Arc left = refreshRec(v,a,m-1);
  9.2732 +	_left.set(me, left);
  9.2733 +	_parent.set(left, me);
  9.2734 +      } else {
  9.2735 +	_left.set(me, INVALID);
  9.2736 +      }
  9.2737 +      if (m < b) {
  9.2738 +	Arc right = refreshRec(v,m+1,b);
  9.2739 +	_right.set(me, right);
  9.2740 +	_parent.set(right, me);
  9.2741 +      } else {
  9.2742 +	_right.set(me, INVALID);
  9.2743 +      }
  9.2744 +      return me;
  9.2745 +    }
  9.2746 +
  9.2747 +    void refresh() {
  9.2748 +      for(NodeIt n(_g);n!=INVALID;++n) {
  9.2749 +	std::vector<Arc> v;
  9.2750 +	for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
  9.2751 +	if(v.size()) {
  9.2752 +	  std::sort(v.begin(),v.end(),ArcLess(_g));
  9.2753 +	  Arc head = refreshRec(v,0,v.size()-1);
  9.2754 +	  _head.set(n, head);
  9.2755 +	  _parent.set(head, INVALID);
  9.2756 +	}
  9.2757 +	else _head.set(n, INVALID);
  9.2758 +      }
  9.2759 +    }
  9.2760 +
  9.2761 +    void zig(Arc v) {        
  9.2762 +      Arc w = _parent[v];
  9.2763 +      _parent.set(v, _parent[w]);
  9.2764 +      _parent.set(w, v);
  9.2765 +      _left.set(w, _right[v]);
  9.2766 +      _right.set(v, w);
  9.2767 +      if (_parent[v] != INVALID) {
  9.2768 +	if (_right[_parent[v]] == w) {
  9.2769 +	  _right.set(_parent[v], v);
  9.2770 +	} else {
  9.2771 +	  _left.set(_parent[v], v);
  9.2772 +	}
  9.2773 +      }
  9.2774 +      if (_left[w] != INVALID){
  9.2775 +	_parent.set(_left[w], w);
  9.2776 +      }
  9.2777 +    }
  9.2778 +
  9.2779 +    void zag(Arc v) {        
  9.2780 +      Arc w = _parent[v];
  9.2781 +      _parent.set(v, _parent[w]);
  9.2782 +      _parent.set(w, v);
  9.2783 +      _right.set(w, _left[v]);
  9.2784 +      _left.set(v, w);
  9.2785 +      if (_parent[v] != INVALID){
  9.2786 +	if (_left[_parent[v]] == w) {
  9.2787 +	  _left.set(_parent[v], v);
  9.2788 +	} else {
  9.2789 +	  _right.set(_parent[v], v);
  9.2790 +	}
  9.2791 +      }
  9.2792 +      if (_right[w] != INVALID){
  9.2793 +	_parent.set(_right[w], w);
  9.2794 +      }
  9.2795 +    }
  9.2796 +
  9.2797 +    void splay(Arc v) {
  9.2798 +      while (_parent[v] != INVALID) {
  9.2799 +	if (v == _left[_parent[v]]) {
  9.2800 +	  if (_parent[_parent[v]] == INVALID) {
  9.2801 +	    zig(v);
  9.2802 +	  } else {
  9.2803 +	    if (_parent[v] == _left[_parent[_parent[v]]]) {
  9.2804 +	      zig(_parent[v]);
  9.2805 +	      zig(v);
  9.2806 +	    } else {
  9.2807 +	      zig(v);
  9.2808 +	      zag(v);
  9.2809 +	    }
  9.2810 +	  }
  9.2811 +	} else {
  9.2812 +	  if (_parent[_parent[v]] == INVALID) {
  9.2813 +	    zag(v);
  9.2814 +	  } else {
  9.2815 +	    if (_parent[v] == _left[_parent[_parent[v]]]) {
  9.2816 +	      zag(v);
  9.2817 +	      zig(v);
  9.2818 +	    } else {
  9.2819 +	      zag(_parent[v]);
  9.2820 +	      zag(v);
  9.2821 +	    }
  9.2822 +	  }
  9.2823 +	}
  9.2824 +      }
  9.2825 +      _head[_g.source(v)] = v;
  9.2826 +    }
  9.2827 +
  9.2828 +
  9.2829 +  public:
  9.2830 +    
  9.2831 +    ///Find an arc between two nodes.
  9.2832 +    
  9.2833 +    ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
  9.2834 +    /// <em>d</em> is the number of outgoing arcs of \c s.
  9.2835 +    ///\param s The source node
  9.2836 +    ///\param t The target node
  9.2837 +    ///\return An arc from \c s to \c t if there exists,
  9.2838 +    ///\ref INVALID otherwise.
  9.2839 +    Arc operator()(Node s, Node t) const
  9.2840 +    {
  9.2841 +      Arc e = _head[s];
  9.2842 +      while (true) {
  9.2843 +	if (_g.target(e) == t) {
  9.2844 +	  const_cast<DynArcLookUp&>(*this).splay(e);
  9.2845 +	  return e;
  9.2846 +	} else if (t < _g.target(e)) {
  9.2847 +	  if (_left[e] == INVALID) {
  9.2848 +	    const_cast<DynArcLookUp&>(*this).splay(e);
  9.2849 +	    return INVALID;
  9.2850 +	  } else {
  9.2851 +	    e = _left[e];
  9.2852 +	  }
  9.2853 +	} else  {
  9.2854 +	  if (_right[e] == INVALID) {
  9.2855 +	    const_cast<DynArcLookUp&>(*this).splay(e);
  9.2856 +	    return INVALID;
  9.2857 +	  } else {
  9.2858 +	    e = _right[e];
  9.2859 +	  }
  9.2860 +	}
  9.2861 +      }
  9.2862 +    }
  9.2863 +
  9.2864 +    ///Find the first arc between two nodes.
  9.2865 +    
  9.2866 +    ///Find the first arc between two nodes in time
  9.2867 +    /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
  9.2868 +    /// outgoing arcs of \c s.  
  9.2869 +    ///\param s The source node 
  9.2870 +    ///\param t The target node
  9.2871 +    ///\return An arc from \c s to \c t if there exists, \ref INVALID
  9.2872 +    /// otherwise.
  9.2873 +    Arc findFirst(Node s, Node t) const
  9.2874 +    {
  9.2875 +      Arc e = _head[s];
  9.2876 +      Arc r = INVALID;
  9.2877 +      while (true) {
  9.2878 +	if (_g.target(e) < t) {
  9.2879 +	  if (_right[e] == INVALID) {
  9.2880 +	    const_cast<DynArcLookUp&>(*this).splay(e);
  9.2881 +	    return r;
  9.2882 +	  } else {
  9.2883 +	    e = _right[e];
  9.2884 +	  }
  9.2885 +	} else {
  9.2886 +	  if (_g.target(e) == t) {
  9.2887 +	    r = e;
  9.2888 +	  }
  9.2889 +	  if (_left[e] == INVALID) {
  9.2890 +	    const_cast<DynArcLookUp&>(*this).splay(e);
  9.2891 +	    return r;
  9.2892 +	  } else {
  9.2893 +	    e = _left[e];
  9.2894 +	  }
  9.2895 +	}
  9.2896 +      }
  9.2897 +    }
  9.2898 +
  9.2899 +    ///Find the next arc between two nodes.
  9.2900 +    
  9.2901 +    ///Find the next arc between two nodes in time
  9.2902 +    /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
  9.2903 +    /// outgoing arcs of \c s.  
  9.2904 +    ///\param s The source node 
  9.2905 +    ///\param t The target node
  9.2906 +    ///\return An arc from \c s to \c t if there exists, \ref INVALID
  9.2907 +    /// otherwise.
  9.2908 +
  9.2909 +    ///\note If \c e is not the result of the previous \c findFirst()
  9.2910 +    ///operation then the amorized time bound can not be guaranteed.
  9.2911 +#ifdef DOXYGEN
  9.2912 +    Arc findNext(Node s, Node t, Arc e) const
  9.2913 +#else
  9.2914 +    Arc findNext(Node, Node t, Arc e) const
  9.2915 +#endif
  9.2916 +    {
  9.2917 +      if (_right[e] != INVALID) {
  9.2918 +	e = _right[e];
  9.2919 +	while (_left[e] != INVALID) {
  9.2920 +	  e = _left[e];
  9.2921 +	}
  9.2922 +	const_cast<DynArcLookUp&>(*this).splay(e);
  9.2923 +      } else {
  9.2924 +	while (_parent[e] != INVALID && _right[_parent[e]] ==  e) {
  9.2925 +	  e = _parent[e];
  9.2926 +	}
  9.2927 +	if (_parent[e] == INVALID) {
  9.2928 +	  return INVALID;
  9.2929 +	} else {
  9.2930 +	  e = _parent[e];
  9.2931 +	  const_cast<DynArcLookUp&>(*this).splay(e);
  9.2932 +	}
  9.2933 +      }
  9.2934 +      if (_g.target(e) == t) return e;
  9.2935 +      else return INVALID;    
  9.2936 +    }
  9.2937 +
  9.2938 +  };
  9.2939 +
  9.2940 +  ///Fast arc look up between given endpoints.
  9.2941 +  
  9.2942 +  ///\ingroup gutils
  9.2943 +  ///Using this class, you can find an arc in a digraph from a given
  9.2944 +  ///source to a given target in time <em>O(log d)</em>,
  9.2945 +  ///where <em>d</em> is the out-degree of the source node.
  9.2946 +  ///
  9.2947 +  ///It is not possible to find \e all parallel arcs between two nodes.
  9.2948 +  ///Use \ref AllArcLookUp for this purpose.
  9.2949 +  ///
  9.2950 +  ///\warning This class is static, so you should refresh() (or at least
  9.2951 +  ///refresh(Node)) this data structure
  9.2952 +  ///whenever the digraph changes. This is a time consuming (superlinearly
  9.2953 +  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
  9.2954 +  ///
  9.2955 +  ///\param G The type of the underlying digraph.
  9.2956 +  ///
  9.2957 +  ///\sa DynArcLookUp
  9.2958 +  ///\sa AllArcLookUp  
  9.2959 +  template<class G>
  9.2960 +  class ArcLookUp 
  9.2961 +  {
  9.2962 +  public:
  9.2963 +    GRAPH_TYPEDEFS(typename G);
  9.2964 +    typedef G Digraph;
  9.2965 +
  9.2966 +  protected:
  9.2967 +    const Digraph &_g;
  9.2968 +    typename Digraph::template NodeMap<Arc> _head;
  9.2969 +    typename Digraph::template ArcMap<Arc> _left;
  9.2970 +    typename Digraph::template ArcMap<Arc> _right;
  9.2971 +    
  9.2972 +    class ArcLess {
  9.2973 +      const Digraph &g;
  9.2974 +    public:
  9.2975 +      ArcLess(const Digraph &_g) : g(_g) {}
  9.2976 +      bool operator()(Arc a,Arc b) const 
  9.2977 +      {
  9.2978 +	return g.target(a)<g.target(b);
  9.2979 +      }
  9.2980 +    };
  9.2981 +    
  9.2982 +  public:
  9.2983 +    
  9.2984 +    ///Constructor
  9.2985 +
  9.2986 +    ///Constructor.
  9.2987 +    ///
  9.2988 +    ///It builds up the search database, which remains valid until the digraph
  9.2989 +    ///changes.
  9.2990 +    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
  9.2991 +    
  9.2992 +  private:
  9.2993 +    Arc refreshRec(std::vector<Arc> &v,int a,int b) 
  9.2994 +    {
  9.2995 +      int m=(a+b)/2;
  9.2996 +      Arc me=v[m];
  9.2997 +      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
  9.2998 +      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
  9.2999 +      return me;
  9.3000 +    }
  9.3001 +  public:
  9.3002 +    ///Refresh the data structure at a node.
  9.3003 +
  9.3004 +    ///Build up the search database of node \c n.
  9.3005 +    ///
  9.3006 +    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
  9.3007 +    ///the number of the outgoing arcs of \c n.
  9.3008 +    void refresh(Node n) 
  9.3009 +    {
  9.3010 +      std::vector<Arc> v;
  9.3011 +      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
  9.3012 +      if(v.size()) {
  9.3013 +	std::sort(v.begin(),v.end(),ArcLess(_g));
  9.3014 +	_head[n]=refreshRec(v,0,v.size()-1);
  9.3015 +      }
  9.3016 +      else _head[n]=INVALID;
  9.3017 +    }
  9.3018 +    ///Refresh the full data structure.
  9.3019 +
  9.3020 +    ///Build up the full search database. In fact, it simply calls
  9.3021 +    ///\ref refresh(Node) "refresh(n)" for each node \c n.
  9.3022 +    ///
  9.3023 +    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
  9.3024 +    ///the number of the arcs of \c n and <em>D</em> is the maximum
  9.3025 +    ///out-degree of the digraph.
  9.3026 +
  9.3027 +    void refresh() 
  9.3028 +    {
  9.3029 +      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
  9.3030 +    }
  9.3031 +    
  9.3032 +    ///Find an arc between two nodes.
  9.3033 +    
  9.3034 +    ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
  9.3035 +    /// <em>d</em> is the number of outgoing arcs of \c s.
  9.3036 +    ///\param s The source node
  9.3037 +    ///\param t The target node
  9.3038 +    ///\return An arc from \c s to \c t if there exists,
  9.3039 +    ///\ref INVALID otherwise.
  9.3040 +    ///
  9.3041 +    ///\warning If you change the digraph, refresh() must be called before using
  9.3042 +    ///this operator. If you change the outgoing arcs of
  9.3043 +    ///a single node \c n, then
  9.3044 +    ///\ref refresh(Node) "refresh(n)" is enough.
  9.3045 +    ///
  9.3046 +    Arc operator()(Node s, Node t) const
  9.3047 +    {
  9.3048 +      Arc e;
  9.3049 +      for(e=_head[s];
  9.3050 +	  e!=INVALID&&_g.target(e)!=t;
  9.3051 +	  e = t < _g.target(e)?_left[e]:_right[e]) ;
  9.3052 +      return e;
  9.3053 +    }
  9.3054 +
  9.3055 +  };
  9.3056 +
  9.3057 +  ///Fast look up of all arcs between given endpoints.
  9.3058 +  
  9.3059 +  ///\ingroup gutils
  9.3060 +  ///This class is the same as \ref ArcLookUp, with the addition
  9.3061 +  ///that it makes it possible to find all arcs between given endpoints.
  9.3062 +  ///
  9.3063 +  ///\warning This class is static, so you should refresh() (or at least
  9.3064 +  ///refresh(Node)) this data structure
  9.3065 +  ///whenever the digraph changes. This is a time consuming (superlinearly
  9.3066 +  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
  9.3067 +  ///
  9.3068 +  ///\param G The type of the underlying digraph.
  9.3069 +  ///
  9.3070 +  ///\sa DynArcLookUp
  9.3071 +  ///\sa ArcLookUp  
  9.3072 +  template<class G>
  9.3073 +  class AllArcLookUp : public ArcLookUp<G>
  9.3074 +  {
  9.3075 +    using ArcLookUp<G>::_g;
  9.3076 +    using ArcLookUp<G>::_right;
  9.3077 +    using ArcLookUp<G>::_left;
  9.3078 +    using ArcLookUp<G>::_head;
  9.3079 +
  9.3080 +    GRAPH_TYPEDEFS(typename G);
  9.3081 +    typedef G Digraph;
  9.3082 +    
  9.3083 +    typename Digraph::template ArcMap<Arc> _next;
  9.3084 +    
  9.3085 +    Arc refreshNext(Arc head,Arc next=INVALID)
  9.3086 +    {
  9.3087 +      if(head==INVALID) return next;
  9.3088 +      else {
  9.3089 +	next=refreshNext(_right[head],next);
  9.3090 +// 	_next[head]=next;
  9.3091 +	_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
  9.3092 +	  ? next : INVALID;
  9.3093 +	return refreshNext(_left[head],head);
  9.3094 +      }
  9.3095 +    }
  9.3096 +    
  9.3097 +    void refreshNext()
  9.3098 +    {
  9.3099 +      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
  9.3100 +    }
  9.3101 +    
  9.3102 +  public:
  9.3103 +    ///Constructor
  9.3104 +
  9.3105 +    ///Constructor.
  9.3106 +    ///
  9.3107 +    ///It builds up the search database, which remains valid until the digraph
  9.3108 +    ///changes.
  9.3109 +    AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
  9.3110 +
  9.3111 +    ///Refresh the data structure at a node.
  9.3112 +
  9.3113 +    ///Build up the search database of node \c n.
  9.3114 +    ///
  9.3115 +    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
  9.3116 +    ///the number of the outgoing arcs of \c n.
  9.3117 +    
  9.3118 +    void refresh(Node n) 
  9.3119 +    {
  9.3120 +      ArcLookUp<G>::refresh(n);
  9.3121 +      refreshNext(_head[n]);
  9.3122 +    }
  9.3123 +    
  9.3124 +    ///Refresh the full data structure.
  9.3125 +
  9.3126 +    ///Build up the full search database. In fact, it simply calls
  9.3127 +    ///\ref refresh(Node) "refresh(n)" for each node \c n.
  9.3128 +    ///
  9.3129 +    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
  9.3130 +    ///the number of the arcs of \c n and <em>D</em> is the maximum
  9.3131 +    ///out-degree of the digraph.
  9.3132 +
  9.3133 +    void refresh() 
  9.3134 +    {
  9.3135 +      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
  9.3136 +    }
  9.3137 +    
  9.3138 +    ///Find an arc between two nodes.
  9.3139 +    
  9.3140 +    ///Find an arc between two nodes.
  9.3141 +    ///\param s The source node
  9.3142 +    ///\param t The target node
  9.3143 +    ///\param prev The previous arc between \c s and \c t. It it is INVALID or
  9.3144 +    ///not given, the operator finds the first appropriate arc.
  9.3145 +    ///\return An arc from \c s to \c t after \c prev or
  9.3146 +    ///\ref INVALID if there is no more.
  9.3147 +    ///
  9.3148 +    ///For example, you can count the number of arcs from \c u to \c v in the
  9.3149 +    ///following way.
  9.3150 +    ///\code
  9.3151 +    ///AllArcLookUp<ListDigraph> ae(g);
  9.3152 +    ///...
  9.3153 +    ///int n=0;
  9.3154 +    ///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
  9.3155 +    ///\endcode
  9.3156 +    ///
  9.3157 +    ///Finding the first arc take <em>O(</em>log<em>d)</em> time, where
  9.3158 +    /// <em>d</em> is the number of outgoing arcs of \c s. Then, the
  9.3159 +    ///consecutive arcs are found in constant time.
  9.3160 +    ///
  9.3161 +    ///\warning If you change the digraph, refresh() must be called before using
  9.3162 +    ///this operator. If you change the outgoing arcs of
  9.3163 +    ///a single node \c n, then
  9.3164 +    ///\ref refresh(Node) "refresh(n)" is enough.
  9.3165 +    ///
  9.3166 +#ifdef DOXYGEN
  9.3167 +    Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
  9.3168 +#else
  9.3169 +    using ArcLookUp<G>::operator() ;
  9.3170 +    Arc operator()(Node s, Node t, Arc prev) const
  9.3171 +    {
  9.3172 +      return prev==INVALID?(*this)(s,t):_next[prev];
  9.3173 +    }
  9.3174 +#endif
  9.3175 +      
  9.3176 +  };
  9.3177 +
  9.3178 +  /// @}
  9.3179 +
  9.3180 +} //END OF NAMESPACE LEMON
  9.3181 +
  9.3182 +#endif
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/lemon/path.h	Thu Mar 20 12:12:24 2008 +0000
    10.3 @@ -0,0 +1,1079 @@
    10.4 +/* -*- C++ -*-
    10.5 + *
    10.6 + * This file is a part of LEMON, a generic C++ optimization library
    10.7 + *
    10.8 + * Copyright (C) 2003-2008
    10.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
   10.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
   10.11 + *
   10.12 + * Permission to use, modify and distribute this software is granted
   10.13 + * provided that this copyright notice appears in all copies. For
   10.14 + * precise terms see the accompanying LICENSE file.
   10.15 + *
   10.16 + * This software is provided "AS IS" with no warranty of any kind,
   10.17 + * express or implied, and with no claim as to its suitability for any
   10.18 + * purpose.
   10.19 + *
   10.20 + */
   10.21 +
   10.22 +///\ingroup paths
   10.23 +///\file
   10.24 +///\brief Classes for representing paths in digraphs.
   10.25 +///
   10.26 +
   10.27 +#ifndef LEMON_PATH_H
   10.28 +#define LEMON_PATH_H
   10.29 +
   10.30 +#include <vector>
   10.31 +#include <algorithm>
   10.32 +
   10.33 +#include <lemon/error.h>
   10.34 +#include <lemon/bits/invalid.h>
   10.35 +#include <lemon/concepts/path.h>
   10.36 +
   10.37 +namespace lemon {
   10.38 +
   10.39 +  /// \addtogroup paths
   10.40 +  /// @{
   10.41 +
   10.42 +
   10.43 +  /// \brief A structure for representing directed paths in a digraph.
   10.44 +  ///
   10.45 +  /// A structure for representing directed path in a digraph.
   10.46 +  /// \param Digraph The digraph type in which the path is.
   10.47 +  ///
   10.48 +  /// In a sense, the path can be treated as a list of arcs. The
   10.49 +  /// lemon path type stores just this list. As a consequence, it
   10.50 +  /// cannot enumerate the nodes of the path and the source node of
   10.51 +  /// a zero length path is undefined.
   10.52 +  ///
   10.53 +  /// This implementation is a back and front insertable and erasable
   10.54 +  /// path type. It can be indexed in O(1) time. The front and back
   10.55 +  /// insertion and erase is done in O(1) (amortized) time. The
   10.56 +  /// implementation uses two vectors for storing the front and back
   10.57 +  /// insertions.
   10.58 +  template <typename _Digraph>
   10.59 +  class Path {
   10.60 +  public:
   10.61 +
   10.62 +    typedef _Digraph Digraph;
   10.63 +    typedef typename Digraph::Arc Arc;
   10.64 +
   10.65 +    /// \brief Default constructor
   10.66 +    ///
   10.67 +    /// Default constructor
   10.68 +    Path() {}
   10.69 +
   10.70 +    /// \brief Template copy constructor
   10.71 +    ///
   10.72 +    /// This constuctor initializes the path from any other path type.
   10.73 +    /// It simply makes a copy of the given path.
   10.74 +    template <typename CPath>
   10.75 +    Path(const CPath& cpath) {
   10.76 +      copyPath(*this, cpath);
   10.77 +    }
   10.78 +
   10.79 +    /// \brief Template copy assignment
   10.80 +    ///
   10.81 +    /// This operator makes a copy of a path of any other type.
   10.82 +    template <typename CPath>
   10.83 +    Path& operator=(const CPath& cpath) {
   10.84 +      copyPath(*this, cpath);
   10.85 +      return *this;
   10.86 +    }
   10.87 +
   10.88 +    /// \brief Lemon style iterator for path arcs
   10.89 +    ///
   10.90 +    /// This class is used to iterate on the arcs of the paths.
   10.91 +    class ArcIt {
   10.92 +      friend class Path;
   10.93 +    public:
   10.94 +      /// \brief Default constructor
   10.95 +      ArcIt() {}
   10.96 +      /// \brief Invalid constructor
   10.97 +      ArcIt(Invalid) : path(0), idx(-1) {}
   10.98 +      /// \brief Initializate the iterator to the first arc of path
   10.99 +      ArcIt(const Path &_path) 
  10.100 +        : path(&_path), idx(_path.empty() ? -1 : 0) {}
  10.101 +
  10.102 +    private:
  10.103 +
  10.104 +      ArcIt(const Path &_path, int _idx) 
  10.105 +        : path(&_path), idx(_idx) {}
  10.106 +
  10.107 +    public:
  10.108 +
  10.109 +      /// \brief Conversion to Arc
  10.110 +      operator const Arc&() const {
  10.111 +        return path->nth(idx);
  10.112 +      }
  10.113 +
  10.114 +      /// \brief Next arc
  10.115 +      ArcIt& operator++() { 
  10.116 +        ++idx;
  10.117 +        if (idx >= path->length()) idx = -1; 
  10.118 +        return *this; 
  10.119 +      }
  10.120 +
  10.121 +      /// \brief Comparison operator
  10.122 +      bool operator==(const ArcIt& e) const { return idx==e.idx; }
  10.123 +      /// \brief Comparison operator
  10.124 +      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
  10.125 +      /// \brief Comparison operator
  10.126 +      bool operator<(const ArcIt& e) const { return idx<e.idx; }
  10.127 +
  10.128 +    private:
  10.129 +      const Path *path;
  10.130 +      int idx;
  10.131 +    };
  10.132 +
  10.133 +    /// \brief Length of the path.
  10.134 +    int length() const { return head.size() + tail.size(); }
  10.135 +    /// \brief Return whether the path is empty.
  10.136 +    bool empty() const { return head.empty() && tail.empty(); }
  10.137 +
  10.138 +    /// \brief Reset the path to an empty one.
  10.139 +    void clear() { head.clear(); tail.clear(); }
  10.140 +
  10.141 +    /// \brief The nth arc.
  10.142 +    ///
  10.143 +    /// \pre n is in the [0..length() - 1] range
  10.144 +    const Arc& nth(int n) const {
  10.145 +      return n < int(head.size()) ? *(head.rbegin() + n) :
  10.146 +        *(tail.begin() + (n - head.size()));
  10.147 +    }
  10.148 +
  10.149 +    /// \brief Initialize arc iterator to point to the nth arc
  10.150 +    ///
  10.151 +    /// \pre n is in the [0..length() - 1] range
  10.152 +    ArcIt nthIt(int n) const {
  10.153 +      return ArcIt(*this, n);
  10.154 +    }
  10.155 +
  10.156 +    /// \brief The first arc of the path
  10.157 +    const Arc& front() const {
  10.158 +      return head.empty() ? tail.front() : head.back();
  10.159 +    }
  10.160 +
  10.161 +    /// \brief Add a new arc before the current path
  10.162 +    void addFront(const Arc& arc) {
  10.163 +      head.push_back(arc);
  10.164 +    }
  10.165 +
  10.166 +    /// \brief Erase the first arc of the path
  10.167 +    void eraseFront() {
  10.168 +      if (!head.empty()) {
  10.169 +        head.pop_back();
  10.170 +      } else {
  10.171 +        head.clear();
  10.172 +        int halfsize = tail.size() / 2;
  10.173 +        head.resize(halfsize);
  10.174 +        std::copy(tail.begin() + 1, tail.begin() + halfsize + 1,
  10.175 +                  head.rbegin());
  10.176 +        std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin());
  10.177 +        tail.resize(tail.size() - halfsize - 1);
  10.178 +      }
  10.179 +    }
  10.180 +
  10.181 +    /// \brief The last arc of the path
  10.182 +    const Arc& back() const {
  10.183 +      return tail.empty() ? head.front() : tail.back();
  10.184 +    }
  10.185 +
  10.186 +    /// \brief Add a new arc behind the current path
  10.187 +    void addBack(const Arc& arc) {
  10.188 +      tail.push_back(arc);
  10.189 +    }
  10.190 +
  10.191 +    /// \brief Erase the last arc of the path
  10.192 +    void eraseBack() {
  10.193 +      if (!tail.empty()) {
  10.194 +        tail.pop_back();
  10.195 +      } else {
  10.196 +        int halfsize = head.size() / 2;
  10.197 +        tail.resize(halfsize);
  10.198 +        std::copy(head.begin() + 1, head.begin() + halfsize + 1,
  10.199 +                  tail.rbegin());
  10.200 +        std::copy(head.begin() + halfsize + 1, head.end(), head.begin());
  10.201 +        head.resize(head.size() - halfsize - 1);
  10.202 +      }
  10.203 +    }
  10.204 +
  10.205 +    typedef True BuildTag;
  10.206 +
  10.207 +    template <typename CPath>
  10.208 +    void build(const CPath& path) {
  10.209 +      int len = path.length();
  10.210 +      tail.reserve(len);
  10.211 +      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
  10.212 +        tail.push_back(it);
  10.213 +      }
  10.214 +    }
  10.215 +
  10.216 +    template <typename CPath>
  10.217 +    void buildRev(const CPath& path) {
  10.218 +      int len = path.length();
  10.219 +      head.reserve(len);
  10.220 +      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
  10.221 +        head.push_back(it);
  10.222 +      }
  10.223 +    }
  10.224 +
  10.225 +  protected:
  10.226 +    typedef std::vector<Arc> Container;
  10.227 +    Container head, tail;
  10.228 +
  10.229 +  };
  10.230 +
  10.231 +  /// \brief A structure for representing directed paths in a digraph.
  10.232 +  ///
  10.233 +  /// A structure for representing directed path in a digraph.
  10.234 +  /// \param Digraph The digraph type in which the path is.
  10.235 +  ///
  10.236 +  /// In a sense, the path can be treated as a list of arcs. The
  10.237 +  /// lemon path type stores just this list. As a consequence it
  10.238 +  /// cannot enumerate the nodes in the path and the zero length paths
  10.239 +  /// cannot store the source.
  10.240 +  ///
  10.241 +  /// This implementation is a just back insertable and erasable path
  10.242 +  /// type. It can be indexed in O(1) time. The back insertion and
  10.243 +  /// erasure is amortized O(1) time. This implementation is faster
  10.244 +  /// then the \c Path type because it use just one vector for the
  10.245 +  /// arcs.
  10.246 +  template <typename _Digraph>
  10.247 +  class SimplePath {
  10.248 +  public:
  10.249 +
  10.250 +    typedef _Digraph Digraph;
  10.251 +    typedef typename Digraph::Arc Arc;
  10.252 +
  10.253 +    /// \brief Default constructor
  10.254 +    ///
  10.255 +    /// Default constructor
  10.256 +    SimplePath() {}
  10.257 +
  10.258 +    /// \brief Template copy constructor
  10.259 +    ///
  10.260 +    /// This path can be initialized with any other path type. It just
  10.261 +    /// makes a copy of the given path.
  10.262 +    template <typename CPath>
  10.263 +    SimplePath(const CPath& cpath) {
  10.264 +      copyPath(*this, cpath);
  10.265 +    }
  10.266 +
  10.267 +    /// \brief Template copy assignment
  10.268 +    ///
  10.269 +    /// This path can be initialized with any other path type. It just
  10.270 +    /// makes a copy of the given path.
  10.271 +    template <typename CPath>
  10.272 +    SimplePath& operator=(const CPath& cpath) {
  10.273 +      copyPath(*this, cpath);
  10.274 +      return *this;
  10.275 +    }
  10.276 +
  10.277 +    /// \brief Iterator class to iterate on the arcs of the paths
  10.278 +    ///
  10.279 +    /// This class is used to iterate on the arcs of the paths
  10.280 +    ///
  10.281 +    /// Of course it converts to Digraph::Arc
  10.282 +    class ArcIt {
  10.283 +      friend class SimplePath;
  10.284 +    public:
  10.285 +      /// Default constructor
  10.286 +      ArcIt() {}
  10.287 +      /// Invalid constructor
  10.288 +      ArcIt(Invalid) : path(0), idx(-1) {}
  10.289 +      /// \brief Initializate the constructor to the first arc of path
  10.290 +      ArcIt(const SimplePath &_path) 
  10.291 +        : path(&_path), idx(_path.empty() ? -1 : 0) {}
  10.292 +
  10.293 +    private:
  10.294 +
  10.295 +      /// Constructor with starting point
  10.296 +      ArcIt(const SimplePath &_path, int _idx) 
  10.297 +        : idx(_idx), path(&_path) {}
  10.298 +
  10.299 +    public:
  10.300 +
  10.301 +      ///Conversion to Digraph::Arc
  10.302 +      operator const Arc&() const {
  10.303 +        return path->nth(idx);
  10.304 +      }
  10.305 +
  10.306 +      /// Next arc
  10.307 +      ArcIt& operator++() { 
  10.308 +        ++idx;
  10.309 +        if (idx >= path->length()) idx = -1; 
  10.310 +        return *this; 
  10.311 +      }
  10.312 +
  10.313 +      /// Comparison operator
  10.314 +      bool operator==(const ArcIt& e) const { return idx==e.idx; }
  10.315 +      /// Comparison operator
  10.316 +      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
  10.317 +      /// Comparison operator
  10.318 +      bool operator<(const ArcIt& e) const { return idx<e.idx; }
  10.319 +
  10.320 +    private:
  10.321 +      const SimplePath *path;
  10.322 +      int idx;
  10.323 +    };
  10.324 +
  10.325 +    /// \brief Length of the path.
  10.326 +    int length() const { return data.size(); }
  10.327 +    /// \brief Return true if the path is empty.
  10.328 +    bool empty() const { return data.empty(); }
  10.329 +
  10.330 +    /// \brief Reset the path to an empty one.
  10.331 +    void clear() { data.clear(); }
  10.332 +
  10.333 +    /// \brief The nth arc.
  10.334 +    ///
  10.335 +    /// \pre n is in the [0..length() - 1] range
  10.336 +    const Arc& nth(int n) const {
  10.337 +      return data[n];
  10.338 +    }
  10.339 +
  10.340 +    /// \brief  Initializes arc iterator to point to the nth arc.
  10.341 +    ArcIt nthIt(int n) const {
  10.342 +      return ArcIt(*this, n);
  10.343 +    }
  10.344 +
  10.345 +    /// \brief The first arc of the path.
  10.346 +    const Arc& front() const {
  10.347 +      return data.front();
  10.348 +    }
  10.349 +
  10.350 +    /// \brief The last arc of the path.
  10.351 +    const Arc& back() const {
  10.352 +      return data.back();
  10.353 +    }
  10.354 +
  10.355 +    /// \brief Add a new arc behind the current path.
  10.356 +    void addBack(const Arc& arc) {
  10.357 +      data.push_back(arc);
  10.358 +    }
  10.359 +
  10.360 +    /// \brief Erase the last arc of the path
  10.361 +    void eraseBack() {
  10.362 +      data.pop_back();
  10.363 +    }
  10.364 +
  10.365 +    typedef True BuildTag;
  10.366 +
  10.367 +    template <typename CPath>
  10.368 +    void build(const CPath& path) {
  10.369 +      int len = path.length();
  10.370 +      data.resize(len);
  10.371 +      int index = 0;
  10.372 +      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
  10.373 +        data[index] = it;;
  10.374 +        ++index;
  10.375 +      }
  10.376 +    }
  10.377 +
  10.378 +    template <typename CPath>
  10.379 +    void buildRev(const CPath& path) {
  10.380 +      int len = path.length();
  10.381 +      data.resize(len);
  10.382 +      int index = len;
  10.383 +      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
  10.384 +        --index;
  10.385 +        data[index] = it;;
  10.386 +      }
  10.387 +    }
  10.388 +
  10.389 +  protected:
  10.390 +    typedef std::vector<Arc> Container;
  10.391 +    Container data;
  10.392 +
  10.393 +  };
  10.394 +
  10.395 +  /// \brief A structure for representing directed paths in a digraph.
  10.396 +  ///
  10.397 +  /// A structure for representing directed path in a digraph.
  10.398 +  /// \param Digraph The digraph type in which the path is.
  10.399 +  ///
  10.400 +  /// In a sense, the path can be treated as a list of arcs. The
  10.401 +  /// lemon path type stores just this list. As a consequence it
  10.402 +  /// cannot enumerate the nodes in the path and the zero length paths
  10.403 +  /// cannot store the source.
  10.404 +  ///
  10.405 +  /// This implementation is a back and front insertable and erasable
  10.406 +  /// path type. It can be indexed in O(k) time, where k is the rank
  10.407 +  /// of the arc in the path. The length can be computed in O(n)
  10.408 +  /// time. The front and back insertion and erasure is O(1) time
  10.409 +  /// and it can be splited and spliced in O(1) time.
  10.410 +  template <typename _Digraph>
  10.411 +  class ListPath {
  10.412 +  public:
  10.413 +
  10.414 +    typedef _Digraph Digraph;
  10.415 +    typedef typename Digraph::Arc Arc;
  10.416 +
  10.417 +  protected:
  10.418 +
  10.419 +    // the std::list<> is incompatible 
  10.420 +    // hard to create invalid iterator
  10.421 +    struct Node {
  10.422 +      Arc arc;
  10.423 +      Node *next, *prev;
  10.424 +    };
  10.425 +
  10.426 +    Node *first, *last;
  10.427 +
  10.428 +    std::allocator<Node> alloc;
  10.429 +
  10.430 +  public:
  10.431 + 
  10.432 +    /// \brief Default constructor
  10.433 +    ///
  10.434 +    /// Default constructor
  10.435 +    ListPath() : first(0), last(0) {}
  10.436 +
  10.437 +    /// \brief Template copy constructor
  10.438 +    ///
  10.439 +    /// This path can be initialized with any other path type. It just
  10.440 +    /// makes a copy of the given path.
  10.441 +    template <typename CPath>
  10.442 +    ListPath(const CPath& cpath) : first(0), last(0) {
  10.443 +      copyPath(*this, cpath);
  10.444 +    }
  10.445 +
  10.446 +    /// \brief Destructor of the path
  10.447 +    ///
  10.448 +    /// Destructor of the path
  10.449 +    ~ListPath() {
  10.450 +      clear();
  10.451 +    }
  10.452 +
  10.453 +    /// \brief Template copy assignment
  10.454 +    ///
  10.455 +    /// This path can be initialized with any other path type. It just
  10.456 +    /// makes a copy of the given path.
  10.457 +    template <typename CPath>
  10.458 +    ListPath& operator=(const CPath& cpath) {
  10.459 +      copyPath(*this, cpath);
  10.460 +      return *this;
  10.461 +    }
  10.462 +
  10.463 +    /// \brief Iterator class to iterate on the arcs of the paths
  10.464 +    ///
  10.465 +    /// This class is used to iterate on the arcs of the paths
  10.466 +    ///
  10.467 +    /// Of course it converts to Digraph::Arc
  10.468 +    class ArcIt {
  10.469 +      friend class ListPath;
  10.470 +    public:
  10.471 +      /// Default constructor
  10.472 +      ArcIt() {}
  10.473 +      /// Invalid constructor
  10.474 +      ArcIt(Invalid) : path(0), node(0) {}
  10.475 +      /// \brief Initializate the constructor to the first arc of path
  10.476 +      ArcIt(const ListPath &_path) 
  10.477 +        : path(&_path), node(_path.first) {}
  10.478 +
  10.479 +    protected:
  10.480 +
  10.481 +      ArcIt(const ListPath &_path, Node *_node) 
  10.482 +        : path(&_path), node(_node) {}
  10.483 +
  10.484 +
  10.485 +    public:
  10.486 +
  10.487 +      ///Conversion to Digraph::Arc
  10.488 +      operator const Arc&() const {
  10.489 +        return node->arc;
  10.490 +      }
  10.491 +
  10.492 +      /// Next arc
  10.493 +      ArcIt& operator++() { 
  10.494 +        node = node->next;
  10.495 +        return *this; 
  10.496 +      }
  10.497 +
  10.498 +      /// Comparison operator
  10.499 +      bool operator==(const ArcIt& e) const { return node==e.node; }
  10.500 +      /// Comparison operator
  10.501 +      bool operator!=(const ArcIt& e) const { return node!=e.node; }
  10.502 +      /// Comparison operator
  10.503 +      bool operator<(const ArcIt& e) const { return node<e.node; }
  10.504 +
  10.505 +    private:
  10.506 +      const ListPath *path;
  10.507 +      Node *node;
  10.508 +    };
  10.509 +
  10.510 +    /// \brief The nth arc.
  10.511 +    ///
  10.512 +    /// This function looks for the nth arc in O(n) time.
  10.513 +    /// \pre n is in the [0..length() - 1] range
  10.514 +    const Arc& nth(int n) const {
  10.515 +      Node *node = first;
  10.516 +      for (int i = 0; i < n; ++i) {
  10.517 +        node = node->next;
  10.518 +      }
  10.519 +      return node->arc;
  10.520 +    }
  10.521 +
  10.522 +    /// \brief Initializes arc iterator to point to the nth arc.
  10.523 +    ArcIt nthIt(int n) const {
  10.524 +      Node *node = first;
  10.525 +      for (int i = 0; i < n; ++i) {
  10.526 +        node = node->next;
  10.527 +      }
  10.528 +      return ArcIt(*this, node);
  10.529 +    }
  10.530 +
  10.531 +    /// \brief Length of the path.
  10.532 +    int length() const {
  10.533 +      int len = 0;
  10.534 +      Node *node = first;
  10.535 +      while (node != 0) {
  10.536 +        node = node->next;
  10.537 +        ++len;
  10.538 +      }
  10.539 +      return len;
  10.540 +    }
  10.541 +
  10.542 +    /// \brief Return true if the path is empty.
  10.543 +    bool empty() const { return first == 0; }
  10.544 +
  10.545 +    /// \brief Reset the path to an empty one.
  10.546 +    void clear() {
  10.547 +      while (first != 0) {
  10.548 +        last = first->next;
  10.549 +        alloc.destroy(first);
  10.550 +        alloc.deallocate(first, 1);
  10.551 +        first = last;
  10.552 +      }
  10.553 +    }
  10.554 +
  10.555 +    /// \brief The first arc of the path
  10.556 +    const Arc& front() const {
  10.557 +      return first->arc;
  10.558 +    }
  10.559 +
  10.560 +    /// \brief Add a new arc before the current path
  10.561 +    void addFront(const Arc& arc) {
  10.562 +      Node *node = alloc.allocate(1);
  10.563 +      alloc.construct(node, Node());
  10.564 +      node->prev = 0;
  10.565 +      node->next = first;
  10.566 +      node->arc = arc;
  10.567 +      if (first) {
  10.568 +        first->prev = node;
  10.569 +        first = node;
  10.570 +      } else {
  10.571 +        first = last = node;
  10.572 +      }
  10.573 +    }
  10.574 +
  10.575 +    /// \brief Erase the first arc of the path
  10.576 +    void eraseFront() {
  10.577 +      Node *node = first;
  10.578 +      first = first->next;
  10.579 +      if (first) {
  10.580 +        first->prev = 0;
  10.581 +      } else {
  10.582 +        last = 0;
  10.583 +      }
  10.584 +      alloc.destroy(node);
  10.585 +      alloc.deallocate(node, 1);
  10.586 +    }
  10.587 +
  10.588 +    /// \brief The last arc of the path.
  10.589 +    const Arc& back() const {
  10.590 +      return last->arc;
  10.591 +    }
  10.592 +
  10.593 +    /// \brief Add a new arc behind the current path.
  10.594 +    void addBack(const Arc& arc) {
  10.595 +      Node *node = alloc.allocate(1);
  10.596 +      alloc.construct(node, Node());
  10.597 +      node->next = 0;
  10.598 +      node->prev = last;
  10.599 +      node->arc = arc;
  10.600 +      if (last) {
  10.601 +        last->next = node;
  10.602 +        last = node;
  10.603 +      } else {
  10.604 +        last = first = node;
  10.605 +      }
  10.606 +    }
  10.607 +
  10.608 +    /// \brief Erase the last arc of the path
  10.609 +    void eraseBack() {
  10.610 +      Node *node = last;
  10.611 +      last = last->prev;
  10.612 +      if (last) {
  10.613 +        last->next = 0;
  10.614 +      } else {
  10.615 +        first = 0;
  10.616 +      }
  10.617 +      alloc.destroy(node);
  10.618 +      alloc.deallocate(node, 1);
  10.619 +    }
  10.620 +
  10.621 +    /// \brief Splice a path to the back of the current path.
  10.622 +    ///
  10.623 +    /// It splices \c tpath to the back of the current path and \c
  10.624 +    /// tpath becomes empty. The time complexity of this function is
  10.625 +    /// O(1).
  10.626 +    void spliceBack(ListPath& tpath) {
  10.627 +      if (first) {
  10.628 +        if (tpath.first) {
  10.629 +          last->next = tpath.first;
  10.630 +          tpath.first->prev = last;
  10.631 +          last = tpath.last;
  10.632 +        }
  10.633 +      } else {
  10.634 +        first = tpath.first;
  10.635 +        last = tpath.last;
  10.636 +      }
  10.637 +      tpath.first = tpath.last = 0;
  10.638 +    }
  10.639 +
  10.640 +    /// \brief Splice a path to the front of the current path.
  10.641 +    ///
  10.642 +    /// It splices \c tpath before the current path and \c tpath
  10.643 +    /// becomes empty. The time complexity of this function
  10.644 +    /// is O(1).
  10.645 +    void spliceFront(ListPath& tpath) {
  10.646 +      if (first) {
  10.647 +        if (tpath.first) {
  10.648 +          first->prev = tpath.last;
  10.649 +          tpath.last->next = first;
  10.650 +          first = tpath.first;
  10.651 +        }
  10.652 +      } else {
  10.653 +        first = tpath.first;
  10.654 +        last = tpath.last;
  10.655 +      }
  10.656 +      tpath.first = tpath.last = 0;
  10.657 +    }
  10.658 +
  10.659 +    /// \brief Splice a path into the current path.
  10.660 +    ///
  10.661 +    /// It splices the \c tpath into the current path before the
  10.662 +    /// position of \c it iterator and \c tpath becomes empty. The
  10.663 +    /// time complexity of this function is O(1). If the \c it is
  10.664 +    /// \c INVALID then it will splice behind the current path.
  10.665 +    void splice(ArcIt it, ListPath& tpath) {
  10.666 +      if (it.node) {
  10.667 +        if (tpath.first) {
  10.668 +          tpath.first->prev = it.node->prev;
  10.669 +          if (it.node->prev) {
  10.670 +            it.node->prev->next = tpath.first;
  10.671 +          } else {
  10.672 +            first = tpath.first;
  10.673 +          }
  10.674 +          it.node->prev = tpath.last;
  10.675 +          tpath.last->next = it.node;
  10.676 +        }
  10.677 +      } else {
  10.678 +        if (first) {
  10.679 +          if (tpath.first) {
  10.680 +            last->next = tpath.first;
  10.681 +            tpath.first->prev = last;
  10.682 +            last = tpath.last;
  10.683 +          }
  10.684 +        } else {
  10.685 +          first = tpath.first;
  10.686 +          last = tpath.last;
  10.687 +        }
  10.688 +      }
  10.689 +      tpath.first = tpath.last = 0;
  10.690 +    }
  10.691 +
  10.692 +    /// \brief Split the current path.
  10.693 +    ///
  10.694 +    /// It splits the current path into two parts. The part before
  10.695 +    /// the iterator \c it will remain in the current path and the part
  10.696 +    /// starting with
  10.697 +    /// \c it will put into \c tpath. If \c tpath have arcs
  10.698 +    /// before the operation they are removed first.  The time
  10.699 +    /// complexity of this function is O(1) plus the the time of emtying
  10.700 +    /// \c tpath. If \c it is \c INVALID then it just clears \c tpath
  10.701 +    void split(ArcIt it, ListPath& tpath) {
  10.702 +      tpath.clear();
  10.703 +      if (it.node) {
  10.704 +        tpath.first = it.node;
  10.705 +        tpath.last = last;
  10.706 +        if (it.node->prev) {
  10.707 +          last = it.node->prev;
  10.708 +          last->next = 0;
  10.709 +        } else {
  10.710 +          first = last = 0;
  10.711 +        }
  10.712 +        it.node->prev = 0;
  10.713 +      }
  10.714 +    }
  10.715 +
  10.716 +
  10.717 +    typedef True BuildTag;
  10.718 +
  10.719 +    template <typename CPath>
  10.720 +    void build(const CPath& path) {
  10.721 +      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
  10.722 +        addBack(it);
  10.723 +      }
  10.724 +    }
  10.725 +
  10.726 +    template <typename CPath>
  10.727 +    void buildRev(const CPath& path) {
  10.728 +      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
  10.729 +        addFront(it);
  10.730 +      }
  10.731 +    }
  10.732 +
  10.733 +  };
  10.734 +
  10.735 +  /// \brief A structure for representing directed paths in a digraph.
  10.736 +  ///
  10.737 +  /// A structure for representing directed path in a digraph.
  10.738 +  /// \param Digraph The digraph type in which the path is.
  10.739 +  ///
  10.740 +  /// In a sense, the path can be treated as a list of arcs. The
  10.741 +  /// lemon path type stores just this list. As a consequence it
  10.742 +  /// cannot enumerate the nodes in the path and the source node of
  10.743 +  /// a zero length path is undefined.
  10.744 +  ///
  10.745 +  /// This implementation is completly static, i.e. it can be copy constucted
  10.746 +  /// or copy assigned from another path, but otherwise it cannot be
  10.747 +  /// modified.
  10.748 +  ///
  10.749 +  /// Being the the most memory efficient path type in LEMON,
  10.750 +  /// it is intented to be
  10.751 +  /// used when you want to store a large number of paths.
  10.752 +  template <typename _Digraph>
  10.753 +  class StaticPath {
  10.754 +  public:
  10.755 +
  10.756 +    typedef _Digraph Digraph;
  10.757 +    typedef typename Digraph::Arc Arc;
  10.758 +
  10.759 +    /// \brief Default constructor
  10.760 +    ///
  10.761 +    /// Default constructor
  10.762 +    StaticPath() : len(0), arcs(0) {}
  10.763 +    
  10.764 +    /// \brief Template copy constructor
  10.765 +    ///
  10.766 +    /// This path can be initialized from any other path type.
  10.767 +    template <typename CPath>
  10.768 +    StaticPath(const CPath& cpath) : arcs(0) {
  10.769 +      copyPath(*this, cpath);
  10.770 +    }
  10.771 +
  10.772 +    /// \brief Destructor of the path
  10.773 +    ///
  10.774 +    /// Destructor of the path
  10.775 +    ~StaticPath() {
  10.776 +      if (arcs) delete[] arcs;
  10.777 +    }
  10.778 +
  10.779 +    /// \brief Template copy assignment
  10.780 +    ///
  10.781 +    /// This path can be made equal to any other path type. It simply
  10.782 +    /// makes a copy of the given path.
  10.783 +    template <typename CPath>
  10.784 +    StaticPath& operator=(const CPath& cpath) {
  10.785 +      copyPath(*this, cpath);
  10.786 +      return *this;
  10.787 +    }
  10.788 +
  10.789 +    /// \brief Iterator class to iterate on the arcs of the paths
  10.790 +    ///
  10.791 +    /// This class is used to iterate on the arcs of the paths
  10.792 +    ///
  10.793 +    /// Of course it converts to Digraph::Arc
  10.794 +    class ArcIt {
  10.795 +      friend class StaticPath;
  10.796 +    public:
  10.797 +      /// Default constructor
  10.798 +      ArcIt() {}
  10.799 +      /// Invalid constructor
  10.800 +      ArcIt(Invalid) : path(0), idx(-1) {}
  10.801 +      /// Initializate the constructor to the first arc of path
  10.802 +      ArcIt(const StaticPath &_path) 
  10.803 +        : path(&_path), idx(_path.empty() ? -1 : 0) {}
  10.804 +
  10.805 +    private:
  10.806 +
  10.807 +      /// Constructor with starting point
  10.808 +      ArcIt(const StaticPath &_path, int _idx) 
  10.809 +        : idx(_idx), path(&_path) {}
  10.810 +
  10.811 +    public:
  10.812 +
  10.813 +      ///Conversion to Digraph::Arc
  10.814 +      operator const Arc&() const {
  10.815 +        return path->nth(idx);
  10.816 +      }
  10.817 +
  10.818 +      /// Next arc
  10.819 +      ArcIt& operator++() { 
  10.820 +        ++idx;
  10.821 +        if (idx >= path->length()) idx = -1; 
  10.822 +        return *this; 
  10.823 +      }
  10.824 +
  10.825 +      /// Comparison operator
  10.826 +      bool operator==(const ArcIt& e) const { return idx==e.idx; }
  10.827 +      /// Comparison operator
  10.828 +      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
  10.829 +      /// Comparison operator
  10.830 +      bool operator<(const ArcIt& e) const { return idx<e.idx; }
  10.831 +
  10.832 +    private:
  10.833 +      const StaticPath *path;
  10.834 +      int idx;
  10.835 +    };
  10.836 +
  10.837 +    /// \brief The nth arc.
  10.838 +    ///
  10.839 +    /// \pre n is in the [0..length() - 1] range
  10.840 +    const Arc& nth(int n) const {
  10.841 +      return arcs[n];
  10.842 +    }
  10.843 +
  10.844 +    /// \brief The arc iterator pointing to the nth arc.
  10.845 +    ArcIt nthIt(int n) const {
  10.846 +      return ArcIt(*this, n);
  10.847 +    }
  10.848 +
  10.849 +    /// \brief The length of the path.
  10.850 +    int length() const { return len; }
  10.851 +
  10.852 +    /// \brief Return true when the path is empty.
  10.853 +    int empty() const { return len == 0; }
  10.854 +
  10.855 +    /// \break Erase all arcs in the digraph.
  10.856 +    void clear() {
  10.857 +      len = 0;
  10.858 +      if (arcs) delete[] arcs;
  10.859 +      arcs = 0;
  10.860 +    }
  10.861 +
  10.862 +    /// \brief The first arc of the path.
  10.863 +    const Arc& front() const {
  10.864 +      return arcs[0];
  10.865 +    }
  10.866 +
  10.867 +    /// \brief The last arc of the path.
  10.868 +    const Arc& back() const {
  10.869 +      return arcs[len - 1];
  10.870 +    }
  10.871 +
  10.872 +
  10.873 +    typedef True BuildTag;
  10.874 +
  10.875 +    template <typename CPath>
  10.876 +    void build(const CPath& path) {
  10.877 +      len = path.length();
  10.878 +      arcs = new Arc[len];
  10.879 +      int index = 0;
  10.880 +      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
  10.881 +        arcs[index] = it;
  10.882 +        ++index;
  10.883 +      }
  10.884 +    }
  10.885 +
  10.886 +    template <typename CPath>
  10.887 +    void buildRev(const CPath& path) {
  10.888 +      len = path.length();
  10.889 +      arcs = new Arc[len];
  10.890 +      int index = len;
  10.891 +      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
  10.892 +        --index;
  10.893 +        arcs[index] = it;
  10.894 +      }
  10.895 +    }
  10.896 +
  10.897 +  private:
  10.898 +    int len;
  10.899 +    Arc* arcs;
  10.900 +  };
  10.901 +
  10.902 +  ///////////////////////////////////////////////////////////////////////
  10.903 +  // Additional utilities
  10.904 +  ///////////////////////////////////////////////////////////////////////
  10.905 +
  10.906 +  namespace _path_bits {
  10.907 +
  10.908 +    template <typename Path, typename Enable = void>
  10.909 +    struct RevTagIndicator {
  10.910 +      static const bool value = false;
  10.911 +    };
  10.912 +
  10.913 +    template <typename Digraph>
  10.914 +    struct RevTagIndicator<
  10.915 +      Digraph, 
  10.916 +      typename enable_if<typename Digraph::RevTag, void>::type
  10.917 +    > {
  10.918 +      static const bool value = true;
  10.919 +    };
  10.920 +
  10.921 +    template <typename Target, typename Source,
  10.922 +              typename BuildEnable = void, typename RevEnable = void>
  10.923 +    struct PathCopySelector {
  10.924 +      static void copy(Target& target, const Source& source) {
  10.925 +        target.clear();
  10.926 +        for (typename Source::ArcIt it(source); it != INVALID; ++it) {
  10.927 +          target.addBack(it);
  10.928 +        }
  10.929 +      }
  10.930 +    };
  10.931 +
  10.932 +    template <typename Target, typename Source, typename BuildEnable>
  10.933 +    struct PathCopySelector<
  10.934 +      Target, Source, BuildEnable, 
  10.935 +      typename enable_if<typename Source::RevPathTag, void>::type> {
  10.936 +      static void copy(Target& target, const Source& source) {
  10.937 +        target.clear();
  10.938 +        for (typename Source::RevArcIt it(source); it != INVALID; ++it) {
  10.939 +          target.addFront(it);
  10.940 +        }
  10.941 +      }
  10.942 +    };
  10.943 +
  10.944 +    template <typename Target, typename Source, typename RevEnable>
  10.945 +    struct PathCopySelector<
  10.946 +      Target, Source, 
  10.947 +      typename enable_if<typename Target::BuildTag, void>::type, RevEnable> {
  10.948 +      static void copy(Target& target, const Source& source) {
  10.949 +        target.clear();
  10.950 +        target.build(source);
  10.951 +      }
  10.952 +    };
  10.953 +
  10.954 +    template <typename Target, typename Source>
  10.955 +    struct PathCopySelector<
  10.956 +      Target, Source, 
  10.957 +      typename enable_if<typename Target::BuildTag, void>::type,
  10.958 +      typename enable_if<typename Source::RevPathTag, void>::type> {
  10.959 +      static void copy(Target& target, const Source& source) {
  10.960 +        target.clear();
  10.961 +        target.buildRev(source);
  10.962 +      }
  10.963 +    };
  10.964 +
  10.965 +  }
  10.966 +
  10.967 +
  10.968 +  /// \brief Make a copy of a path.
  10.969 +  ///
  10.970 +  ///  This function makes a copy of a path.
  10.971 +  template <typename Target, typename Source>
  10.972 +  void copyPath(Target& target, const Source& source) {
  10.973 +    checkConcept<concepts::PathDumper<typename Source::Digraph>, Source>();
  10.974 +    _path_bits::PathCopySelector<Target, Source>::copy(target, source);
  10.975 +  }
  10.976 +
  10.977 +  /// \brief Check the consistency of a path.
  10.978 +  ///
  10.979 +  /// This function checks that the target of each arc is the same
  10.980 +  /// as the source of the next one. 
  10.981 +  /// 
  10.982 +  template <typename Digraph, typename Path>
  10.983 +  bool checkPath(const Digraph& digraph, const Path& path) {
  10.984 +    typename Path::ArcIt it(path);
  10.985 +    if (it == INVALID) return true;
  10.986 +    typename Digraph::Node node = digraph.target(it);
  10.987 +    ++it;
  10.988 +    while (it != INVALID) {
  10.989 +      if (digraph.source(it) != node) return false;
  10.990 +      node = digraph.target(it);
  10.991 +      ++it;
  10.992 +    }
  10.993 +    return true;
  10.994 +  }
  10.995 +
  10.996 +  /// \brief The source of a path
  10.997 +  ///
  10.998 +  /// This function returns the source of the given path.
  10.999 +  template <typename Digraph, typename Path>
 10.1000 +  typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
 10.1001 +    return digraph.source(path.front());
 10.1002 +  }
 10.1003 +
 10.1004 +  /// \brief The target of a path
 10.1005 +  ///
 10.1006 +  /// This function returns the target of the given path.
 10.1007 +  template <typename Digraph, typename Path>
 10.1008 +  typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
 10.1009 +    return digraph.target(path.back());
 10.1010 +  }
 10.1011 +
 10.1012 +  /// \brief Class which helps to iterate through the nodes of a path
 10.1013 +  ///
 10.1014 +  /// In a sense, the path can be treated as a list of arcs. The
 10.1015 +  /// lemon path type stores only this list. As a consequence, it
 10.1016 +  /// cannot enumerate the nodes in the path and the zero length paths
 10.1017 +  /// cannot have a source node.
 10.1018 +  ///
 10.1019 +  /// This class implements the node iterator of a path structure. To
 10.1020 +  /// provide this feature, the underlying digraph should be passed to
 10.1021 +  /// the constructor of the iterator.
 10.1022 +  template <typename Path>
 10.1023 +  class PathNodeIt {
 10.1024 +  private:
 10.1025 +    const typename Path::Digraph *_digraph;
 10.1026 +    typename Path::ArcIt _it;
 10.1027 +    typename Path::Digraph::Node _nd;
 10.1028 +
 10.1029 +  public:
 10.1030 +
 10.1031 +    typedef typename Path::Digraph Digraph;
 10.1032 +    typedef typename Digraph::Node Node;
 10.1033 +    
 10.1034 +    /// Default constructor
 10.1035 +    PathNodeIt() {}
 10.1036 +    /// Invalid constructor
 10.1037 +    PathNodeIt(Invalid) 
 10.1038 +      : _digraph(0), _it(INVALID), _nd(INVALID) {}
 10.1039 +    /// Constructor
 10.1040 +    PathNodeIt(const Digraph& digraph, const Path& path) 
 10.1041 +      : _digraph(&digraph), _it(path) {
 10.1042 +      _nd = (_it != INVALID ? _digraph->source(_it) : INVALID);
 10.1043 +    }
 10.1044 +    /// Constructor
 10.1045 +    PathNodeIt(const Digraph& digraph, const Path& path, const Node& src) 
 10.1046 +      : _digraph(&digraph), _it(path), _nd(src) {}
 10.1047 +
 10.1048 +    ///Conversion to Digraph::Node
 10.1049 +    operator Node() const {
 10.1050 +      return _nd;
 10.1051 +    }
 10.1052 +
 10.1053 +    /// Next node
 10.1054 +    PathNodeIt& operator++() {
 10.1055 +      if (_it == INVALID) _nd = INVALID;
 10.1056 +      else {
 10.1057 +	_nd = _digraph->target(_it);
 10.1058 +	++_it;
 10.1059 +      }
 10.1060 +      return *this;
 10.1061 +    }
 10.1062 +
 10.1063 +    /// Comparison operator
 10.1064 +    bool operator==(const PathNodeIt& n) const { 
 10.1065 +      return _it == n._it && _nd == n._nd; 
 10.1066 +    }
 10.1067 +    /// Comparison operator
 10.1068 +    bool operator!=(const PathNodeIt& n) const { 
 10.1069 +      return _it != n._it || _nd != n._nd; 
 10.1070 +    }
 10.1071 +    /// Comparison operator
 10.1072 +    bool operator<(const PathNodeIt& n) const { 
 10.1073 +      return (_it < n._it && _nd != INVALID);
 10.1074 +    }
 10.1075 +    
 10.1076 +  };
 10.1077 +  
 10.1078 +  ///@}
 10.1079 +
 10.1080 +} // namespace lemon
 10.1081 +
 10.1082 +#endif // LEMON_PATH_H
    11.1 --- a/test/Makefile.am	Tue Mar 18 16:45:21 2008 +0100
    11.2 +++ b/test/Makefile.am	Thu Mar 20 12:12:24 2008 +0000
    11.3 @@ -3,26 +3,34 @@
    11.4  
    11.5  noinst_HEADERS += \
    11.6  	test/digraph_test.h \
    11.7 +	test/heap_test.h \
    11.8  	test/map_test.h \
    11.9          test/test_tools.h
   11.10  
   11.11  check_PROGRAMS += \
   11.12 +	test/bfs_test \
   11.13 +	test/dfs_test \
   11.14  	test/digraph_test \
   11.15          test/dim_test \
   11.16  	test/graph_test \
   11.17          test/maps_test \
   11.18          test/random_test \
   11.19 +        test/path_test \
   11.20          test/test_tools_fail \
   11.21          test/test_tools_pass
   11.22  
   11.23  TESTS += $(check_PROGRAMS)
   11.24  XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
   11.25  
   11.26 +test_bfs_test_SOURCES = test/bfs_test.cc
   11.27 +test_dfs_test_SOURCES = test/dfs_test.cc
   11.28  test_digraph_test_SOURCES = test/digraph_test.cc
   11.29  test_dim_test_SOURCES = test/dim_test.cc
   11.30  #test_error_test_SOURCES = test/error_test.cc
   11.31  test_graph_test_SOURCES = test/graph_test.cc
   11.32 +# test_heap_test_SOURCES = test/heap_test.cc
   11.33  test_maps_test_SOURCES = test/maps_test.cc
   11.34 +test_path_test_SOURCES = test/path_test.cc
   11.35  test_random_test_SOURCES = test/random_test.cc
   11.36  test_test_tools_fail_SOURCES = test/test_tools_fail.cc
   11.37  test_test_tools_pass_SOURCES = test/test_tools_pass.cc
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/test/bfs_test.cc	Thu Mar 20 12:12:24 2008 +0000
    12.3 @@ -0,0 +1,141 @@
    12.4 +/* -*- C++ -*-
    12.5 + *
    12.6 + * This file is a part of LEMON, a generic C++ optimization library
    12.7 + *
    12.8 + * Copyright (C) 2003-2008
    12.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
   12.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
   12.11 + *
   12.12 + * Permission to use, modify and distribute this software is granted
   12.13 + * provided that this copyright notice appears in all copies. For
   12.14 + * precise terms see the accompanying LICENSE file.
   12.15 + *
   12.16 + * This software is provided "AS IS" with no warranty of any kind,
   12.17 + * express or implied, and with no claim as to its suitability for any
   12.18 + * purpose.
   12.19 + *
   12.20 + */
   12.21 +
   12.22 +#include "test_tools.h"
   12.23 +//#include <lemon/smart_graph.h>
   12.24 +#include <lemon/list_graph.h>
   12.25 +#include <lemon/bfs.h>
   12.26 +#include <lemon/path.h>
   12.27 +#include<lemon/concepts/digraph.h>
   12.28 +
   12.29 +using namespace lemon;
   12.30 +
   12.31 +const int PET_SIZE =5;
   12.32 +
   12.33 +
   12.34 +void check_Bfs_Compile() 
   12.35 +{
   12.36 +  typedef concepts::Digraph Digraph;
   12.37 +
   12.38 +  typedef Digraph::Arc Arc;
   12.39 +  typedef Digraph::Node Node;
   12.40 +  typedef Digraph::ArcIt ArcIt;
   12.41 +  typedef Digraph::NodeIt NodeIt;
   12.42 + 
   12.43 +  typedef Bfs<Digraph> BType;
   12.44 +  
   12.45 +  Digraph G;
   12.46 +  Node n;
   12.47 +  Arc e;
   12.48 +  int l;
   12.49 +  bool b;
   12.50 +  BType::DistMap d(G);
   12.51 +  BType::PredMap p(G);
   12.52 +  //  BType::PredNodeMap pn(G);
   12.53 +  
   12.54 +  BType bfs_test(G);
   12.55 +  
   12.56 +  bfs_test.run(n);
   12.57 +  
   12.58 +  l  = bfs_test.dist(n);
   12.59 +  e  = bfs_test.predArc(n);
   12.60 +  n  = bfs_test.predNode(n);
   12.61 +  d  = bfs_test.distMap();
   12.62 +  p  = bfs_test.predMap();
   12.63 +  //  pn = bfs_test.predNodeMap();
   12.64 +  b  = bfs_test.reached(n);
   12.65 +
   12.66 +  Path<Digraph> pp = bfs_test.path(n);
   12.67 +}
   12.68 +
   12.69 +void check_Bfs_Function_Compile() 
   12.70 +{
   12.71 +  typedef int VType;
   12.72 +  typedef concepts::Digraph Digraph;
   12.73 +
   12.74 +  typedef Digraph::Arc Arc;
   12.75 +  typedef Digraph::Node Node;
   12.76 +  typedef Digraph::ArcIt ArcIt;
   12.77 +  typedef Digraph::NodeIt NodeIt;
   12.78 +  typedef concepts::ReadMap<Arc,VType> LengthMap;
   12.79 +   
   12.80 +  Digraph g;
   12.81 +  bfs(g,Node()).run();
   12.82 +  bfs(g).source(Node()).run();
   12.83 +  bfs(g)
   12.84 +    .predMap(concepts::WriteMap<Node,Arc>())
   12.85 +    .distMap(concepts::WriteMap<Node,VType>())
   12.86 +    .reachedMap(concepts::ReadWriteMap<Node,bool>())
   12.87 +    .processedMap(concepts::WriteMap<Node,bool>())
   12.88 +    .run(Node());
   12.89 +  
   12.90 +}
   12.91 +
   12.92 +int main()
   12.93 +{
   12.94 +    
   12.95 +  // typedef SmartDigraph Digraph;
   12.96 +  typedef ListDigraph Digraph;
   12.97 +
   12.98 +  typedef Digraph::Arc Arc;
   12.99 +  typedef Digraph::Node Node;
  12.100 +  typedef Digraph::ArcIt ArcIt;
  12.101 +  typedef Digraph::NodeIt NodeIt;
  12.102 +  typedef Digraph::ArcMap<int> LengthMap;
  12.103 +
  12.104 +  Digraph G;
  12.105 +  Node s, t;
  12.106 +  PetStruct<Digraph> ps = addPetersen(G,PET_SIZE);
  12.107 +   
  12.108 +  s=ps.outer[2];
  12.109 +  t=ps.inner[0];
  12.110 +  
  12.111 +  Bfs<Digraph> bfs_test(G);
  12.112 +  bfs_test.run(s);
  12.113 +  
  12.114 +  check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
  12.115 +
  12.116 +  Path<Digraph> p = bfs_test.path(t);
  12.117 +  check(p.length()==3,"getPath() found a wrong path.");
  12.118 +  check(checkPath(G, p),"path() found a wrong path.");
  12.119 +  check(pathSource(G, p) == s,"path() found a wrong path.");
  12.120 +  check(pathTarget(G, p) == t,"path() found a wrong path.");
  12.121 +  
  12.122 +
  12.123 +  for(ArcIt e(G); e==INVALID; ++e) {
  12.124 +    Node u=G.source(e);
  12.125 +    Node v=G.target(e);
  12.126 +    check( !bfs_test.reached(u) ||
  12.127 +	   (bfs_test.dist(v) > bfs_test.dist(u)+1),
  12.128 +	   "Wrong output.");
  12.129 +  }
  12.130 +
  12.131 +  for(NodeIt v(G); v==INVALID; ++v) {
  12.132 +    check(bfs_test.reached(v),"Each node should be reached.");
  12.133 +    if ( bfs_test.predArc(v)!=INVALID ) {
  12.134 +      Arc e=bfs_test.predArc(v);
  12.135 +      Node u=G.source(e);
  12.136 +      check(u==bfs_test.predNode(v),"Wrong tree.");
  12.137 +      check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
  12.138 +	    "Wrong distance. Difference: " 
  12.139 +	    << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 
  12.140 +			- 1));
  12.141 +    }
  12.142 +  }
  12.143 +}
  12.144 +
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/test/dfs_test.cc	Thu Mar 20 12:12:24 2008 +0000
    13.3 @@ -0,0 +1,130 @@
    13.4 +/* -*- C++ -*-
    13.5 + *
    13.6 + * This file is a part of LEMON, a generic C++ optimization library
    13.7 + *
    13.8 + * Copyright (C) 2003-2008
    13.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
   13.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
   13.11 + *
   13.12 + * Permission to use, modify and distribute this software is granted
   13.13 + * provided that this copyright notice appears in all copies. For
   13.14 + * precise terms see the accompanying LICENSE file.
   13.15 + *
   13.16 + * This software is provided "AS IS" with no warranty of any kind,
   13.17 + * express or implied, and with no claim as to its suitability for any
   13.18 + * purpose.
   13.19 + *
   13.20 + */
   13.21 +
   13.22 +#include "test_tools.h"
   13.23 +// #include <lemon/smart_graph.h>
   13.24 +#include <lemon/list_graph.h>
   13.25 +#include <lemon/dfs.h>
   13.26 +#include <lemon/path.h>
   13.27 +#include <lemon/concepts/digraph.h>
   13.28 +
   13.29 +using namespace lemon;
   13.30 +
   13.31 +const int PET_SIZE =5;
   13.32 +
   13.33 +
   13.34 +void check_Dfs_SmartDigraph_Compile() 
   13.35 +{
   13.36 +  typedef concepts::Digraph Digraph;
   13.37 +
   13.38 +  typedef Digraph::Arc Arc;
   13.39 +  typedef Digraph::Node Node;
   13.40 +  typedef Digraph::ArcIt ArcIt;
   13.41 +  typedef Digraph::NodeIt NodeIt;
   13.42 + 
   13.43 +  typedef Dfs<Digraph> DType;
   13.44 +  
   13.45 +  Digraph G;
   13.46 +  Node n;
   13.47 +  Arc e;
   13.48 +  int l;
   13.49 +  bool b;
   13.50 +  DType::DistMap d(G);
   13.51 +  DType::PredMap p(G);
   13.52 +  //  DType::PredNodeMap pn(G);
   13.53 +  
   13.54 +  DType dfs_test(G);
   13.55 +  
   13.56 +  dfs_test.run(n);
   13.57 +  
   13.58 +  l  = dfs_test.dist(n);
   13.59 +  e  = dfs_test.predArc(n);
   13.60 +  n  = dfs_test.predNode(n);
   13.61 +  d  = dfs_test.distMap();
   13.62 +  p  = dfs_test.predMap();
   13.63 +  //  pn = dfs_test.predNodeMap();
   13.64 +  b  = dfs_test.reached(n);
   13.65 +
   13.66 +  Path<Digraph> pp = dfs_test.path(n);
   13.67 +}
   13.68 +
   13.69 +
   13.70 +void check_Dfs_Function_Compile() 
   13.71 +{
   13.72 +  typedef int VType;
   13.73 +  typedef concepts::Digraph Digraph;
   13.74 +
   13.75 +  typedef Digraph::Arc Arc;
   13.76 +  typedef Digraph::Node Node;
   13.77 +  typedef Digraph::ArcIt ArcIt;
   13.78 +  typedef Digraph::NodeIt NodeIt;
   13.79 +  typedef concepts::ReadMap<Arc,VType> LengthMap;
   13.80 +   
   13.81 +  Digraph g;
   13.82 +  dfs(g,Node()).run();
   13.83 +  dfs(g).source(Node()).run();
   13.84 +  dfs(g)
   13.85 +    .predMap(concepts::WriteMap<Node,Arc>())
   13.86 +    .distMap(concepts::WriteMap<Node,VType>())
   13.87 +    .reachedMap(concepts::ReadWriteMap<Node,bool>())
   13.88 +    .processedMap(concepts::WriteMap<Node,bool>())
   13.89 +    .run(Node());
   13.90 +  
   13.91 +}
   13.92 +
   13.93 +int main()
   13.94 +{
   13.95 +    
   13.96 +  // typedef SmartDigraph Digraph;
   13.97 +  typedef ListDigraph Digraph;
   13.98 +
   13.99 +  typedef Digraph::Arc Arc;
  13.100 +  typedef Digraph::Node Node;
  13.101 +  typedef Digraph::ArcIt ArcIt;
  13.102 +  typedef Digraph::NodeIt NodeIt;
  13.103 +  typedef Digraph::ArcMap<int> LengthMap;
  13.104 +
  13.105 +  Digraph G;
  13.106 +  Node s, t;
  13.107 +  PetStruct<Digraph> ps = addPetersen(G,PET_SIZE);
  13.108 +   
  13.109 +  s=ps.outer[2];
  13.110 +  t=ps.inner[0];
  13.111 +  
  13.112 +  Dfs<Digraph> dfs_test(G);
  13.113 +  dfs_test.run(s);  
  13.114 +  
  13.115 +  Path<Digraph> p = dfs_test.path(t);
  13.116 +  check(p.length()==dfs_test.dist(t),"path() found a wrong path.");
  13.117 +  check(checkPath(G, p),"path() found a wrong path.");
  13.118 +  check(pathSource(G, p) == s,"path() found a wrong path.");
  13.119 +  check(pathTarget(G, p) == t,"path() found a wrong path.");
  13.120 +  
  13.121 +  for(NodeIt v(G); v!=INVALID; ++v) {
  13.122 +    check(dfs_test.reached(v),"Each node should be reached.");
  13.123 +    if ( dfs_test.predArc(v)!=INVALID ) {
  13.124 +      Arc e=dfs_test.predArc(v);
  13.125 +      Node u=G.source(e);
  13.126 +      check(u==dfs_test.predNode(v),"Wrong tree.");
  13.127 +      check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
  13.128 +	    "Wrong distance. (" << dfs_test.dist(u) << "->" 
  13.129 +	    <<dfs_test.dist(v) << ')');
  13.130 +    }
  13.131 +  }
  13.132 +}
  13.133 +
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/test/heap_test.cc	Thu Mar 20 12:12:24 2008 +0000
    14.3 @@ -0,0 +1,140 @@
    14.4 +/* -*- C++ -*-
    14.5 + *
    14.6 + * This file is a part of LEMON, a generic C++ optimization library
    14.7 + *
    14.8 + * Copyright (C) 2003-2008
    14.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
   14.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
   14.11 + *
   14.12 + * Permission to use, modify and distribute this software is granted
   14.13 + * provided that this copyright notice appears in all copies. For
   14.14 + * precise terms see the accompanying LICENSE file.
   14.15 + *
   14.16 + * This software is provided "AS IS" with no warranty of any kind,
   14.17 + * express or implied, and with no claim as to its suitability for any
   14.18 + * purpose.
   14.19 + *
   14.20 + */
   14.21 +
   14.22 +#include <iostream>
   14.23 +#include <fstream>
   14.24 +#include <string>
   14.25 +#include <vector>
   14.26 +
   14.27 +#include <lemon/concept_check.h>
   14.28 +#include <lemon/concepts/heap.h>
   14.29 +
   14.30 +#include <lemon/list_graph.h>
   14.31 +
   14.32 +#include <lemon/digraph_reader.h>
   14.33 +
   14.34 +#include <lemon/bin_heap.h>
   14.35 +#include <lemon/fib_heap.h>
   14.36 +#include <lemon/radix_heap.h>
   14.37 +#include <lemon/bucket_heap.h>
   14.38 +
   14.39 +#include "test_tools.h"
   14.40 +
   14.41 +#include "heap_test.h"
   14.42 +
   14.43 +#include <lemon/time_measure.h>
   14.44 +
   14.45 +using namespace lemon;
   14.46 +using namespace lemon::concepts;
   14.47 +
   14.48 +
   14.49 +int main() {
   14.50 +
   14.51 +  typedef int Item;
   14.52 +  typedef int Prio;
   14.53 +  typedef IntIntMap ItemIntMap;
   14.54 +
   14.55 +  typedef ListDigraph Digraph;
   14.56 +
   14.57 +  typedef Digraph::Arc Arc;
   14.58 +  typedef Digraph::Node Node;
   14.59 +  typedef Digraph::ArcIt ArcIt;
   14.60 +  typedef Digraph::NodeIt NodeIt;
   14.61 +  typedef Digraph::ArcMap<int> LengthMap;
   14.62 +
   14.63 +  Digraph digraph;
   14.64 +  LengthMap length(digraph);
   14.65 +  Node start;
   14.66 +
   14.67 +  /// \todo create own test digraph
   14.68 +
   14.69 +  std::string f_name;
   14.70 +  if( getenv("srcdir") )
   14.71 +    f_name = std::string(getenv("srcdir"));
   14.72 +  else f_name = ".";
   14.73 +  f_name += "/test/dijkstra_test.lgf";
   14.74 +  
   14.75 +  std::ifstream input(f_name.c_str());
   14.76 +  check(input, "Input file '" << f_name << "' not found.");
   14.77 +  DigraphReader<Digraph>(input, digraph).
   14.78 +    readArcMap("capacity", length).
   14.79 +    readNode("source", start).
   14.80 +    run();  
   14.81 + 
   14.82 +  {
   14.83 +    std::cerr << "Checking Bin Heap" << std::endl;
   14.84 +
   14.85 +    typedef BinHeap<Prio, ItemIntMap> IntHeap;
   14.86 +    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
   14.87 +    heapSortTest<IntHeap>(100);
   14.88 +    heapIncreaseTest<IntHeap>(100);
   14.89 +    
   14.90 +    typedef FibHeap<Prio, Digraph::NodeMap<int> > NodeHeap;
   14.91 +    checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>();
   14.92 +    Timer timer;
   14.93 +    dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start);
   14.94 +    std::cout << timer << std::endl;
   14.95 +  }
   14.96 +  {
   14.97 +    std::cerr << "Checking Fib Heap" << std::endl;
   14.98 +
   14.99 +    typedef FibHeap<Prio, ItemIntMap> IntHeap;
  14.100 +    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
  14.101 +    heapSortTest<IntHeap>(100);
  14.102 +    heapIncreaseTest<IntHeap>(100);
  14.103 +
  14.104 +    typedef FibHeap<Prio, Digraph::NodeMap<int> > NodeHeap;
  14.105 +    checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>();
  14.106 +    Timer timer;
  14.107 +    dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start);
  14.108 +    std::cout << timer << std::endl;
  14.109 +  }
  14.110 +  {
  14.111 +    std::cerr << "Checking Radix Heap" << std::endl;
  14.112 +
  14.113 +    typedef RadixHeap<ItemIntMap> IntHeap;
  14.114 +    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
  14.115 +    heapSortTest<IntHeap>(100);
  14.116 +    heapIncreaseTest<IntHeap>(100);
  14.117 +
  14.118 +    typedef RadixHeap<Digraph::NodeMap<int> > NodeHeap;
  14.119 +    checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>();
  14.120 +    Timer timer;
  14.121 +    dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start);
  14.122 +    std::cout << timer << std::endl;
  14.123 +  }
  14.124 +
  14.125 +  {
  14.126 +    std::cerr << "Checking Bucket Heap" << std::endl;
  14.127 +
  14.128 +    typedef BucketHeap<ItemIntMap> IntHeap;
  14.129 +    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
  14.130 +    heapSortTest<IntHeap>(100);
  14.131 +    heapIncreaseTest<IntHeap>(100);
  14.132 +
  14.133 +    typedef BucketHeap<Digraph::NodeMap<int> > NodeHeap;
  14.134 +    checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>();
  14.135 +    Timer timer;
  14.136 +    dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start);
  14.137 +    std::cout << timer << std::endl;
  14.138 +  }
  14.139 +
  14.140 +  std::cout << __FILE__ ": All tests passed.\n";
  14.141 +
  14.142 +  return 0;
  14.143 +}
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/test/heap_test.h	Thu Mar 20 12:12:24 2008 +0000
    15.3 @@ -0,0 +1,123 @@
    15.4 +/* -*- C++ -*-
    15.5 + *
    15.6 + * This file is a part of LEMON, a generic C++ optimization library
    15.7 + *
    15.8 + * Copyright (C) 2003-2008
    15.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
   15.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
   15.11 + *
   15.12 + * Permission to use, modify and distribute this software is granted
   15.13 + * provided that this copyright notice appears in all copies. For
   15.14 + * precise terms see the accompanying LICENSE file.
   15.15 + *
   15.16 + * This software is provided "AS IS" with no warranty of any kind,
   15.17 + * express or implied, and with no claim as to its suitability for any
   15.18 + * purpose.
   15.19 + *
   15.20 + */
   15.21 +
   15.22 +#include <vector>
   15.23 +#include <algorithm>
   15.24 +
   15.25 +#include <lemon/dijkstra.h>
   15.26 +
   15.27 +class IntIntMap : public std::vector<int> {
   15.28 +public:
   15.29 +  typedef std::vector<int> Parent;
   15.30 +
   15.31 +  typedef int Key;
   15.32 +  typedef int Value;
   15.33 +
   15.34 +  IntIntMap() : Parent() {}
   15.35 +  IntIntMap(int n) : Parent(n) {}
   15.36 +  IntIntMap(int n, int v) : Parent(n, v) {}
   15.37 +
   15.38 +  void set(int key, int value) {
   15.39 +    Parent::operator[](key) = value;
   15.40 +  }
   15.41 +};
   15.42 +
   15.43 +
   15.44 +template <typename _Heap>
   15.45 +void heapSortTest(int n) {
   15.46 +  typedef _Heap Heap;
   15.47 +  IntIntMap map(n, -1);
   15.48 +
   15.49 +  Heap heap(map);
   15.50 +  
   15.51 +  std::vector<int> v(n);
   15.52 +
   15.53 +  for (int i = 0; i < n; ++i) {
   15.54 +    v[i] = rnd[1000];
   15.55 +    heap.push(i, v[i]);
   15.56 +  }
   15.57 +  std::sort(v.begin(), v.end());
   15.58 +  for (int i = 0; i < n; ++i) {
   15.59 +    check(v[i] == heap.prio() ,"Wrong order in heap sort.");
   15.60 +    heap.pop();
   15.61 +  }
   15.62 +}
   15.63 +
   15.64 +template <typename _Heap>
   15.65 +void heapIncreaseTest(int n) {
   15.66 +  typedef _Heap Heap;
   15.67 +  IntIntMap map(n, -1);
   15.68 +
   15.69 +  Heap heap(map);
   15.70 +  
   15.71 +  std::vector<int> v(n);
   15.72 +
   15.73 +  for (int i = 0; i < n; ++i) {
   15.74 +    v[i] = rnd[1000];
   15.75 +    heap.push(i, v[i]);
   15.76 +  }
   15.77 +  for (int i = 0; i < n; ++i) {
   15.78 +    v[i] += rnd[1000];
   15.79 +    heap.increase(i, v[i]);
   15.80 +  }
   15.81 +  std::sort(v.begin(), v.end());
   15.82 +  for (int i = 0; i < n; ++i) {
   15.83 +    check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
   15.84 +    heap.pop();
   15.85 +  }
   15.86 +}
   15.87 +
   15.88 +
   15.89 +
   15.90 +template <typename _Digraph, typename _LengthMap, typename _Heap>
   15.91 +void dijkstraHeapTest(_Digraph& digraph, _LengthMap& length,
   15.92 +		      typename _Digraph::Node& start) {
   15.93 +
   15.94 +  typedef _Heap Heap;
   15.95 +  typedef _Digraph Digraph;
   15.96 +  typedef _LengthMap LengthMap;
   15.97 +
   15.98 +  typedef typename Digraph::Node Node;
   15.99 +  typedef typename Digraph::Arc Arc;
  15.100 +  typedef typename Digraph::NodeIt NodeIt;
  15.101 +  typedef typename Digraph::ArcIt ArcIt;
  15.102 +
  15.103 +  typename Dijkstra<Digraph, LengthMap>::template DefStandardHeap<Heap>::
  15.104 +    Create dijkstra(digraph, length);
  15.105 +
  15.106 +  dijkstra.run(start);
  15.107 +
  15.108 +  for(ArcIt e(digraph); e!=INVALID; ++e) {
  15.109 +    Node u=digraph.source(e); 
  15.110 +    Node v=digraph.target(e);
  15.111 +    if (dijkstra.reached(u)) {
  15.112 +      check( dijkstra.dist(v) - dijkstra.dist(u) <= length[e],
  15.113 +      	     "Error in a shortest path tree arc!");
  15.114 +    }
  15.115 +  }
  15.116 +
  15.117 +  for(NodeIt v(digraph); v!=INVALID; ++v) {
  15.118 +    if ( dijkstra.reached(v) && dijkstra.predArc(v) != INVALID ) {
  15.119 +      Arc e=dijkstra.predArc(v);
  15.120 +      Node u=digraph.source(e);
  15.121 +      check( dijkstra.dist(v) - dijkstra .dist(u) == length[e],
  15.122 +	     "Error in a shortest path tree arc!");
  15.123 +    }
  15.124 +  }
  15.125 +
  15.126 +}
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/test/path_test.cc	Thu Mar 20 12:12:24 2008 +0000
    16.3 @@ -0,0 +1,44 @@
    16.4 +/* -*- C++ -*-
    16.5 + *
    16.6 + * This file is a part of LEMON, a generic C++ optimization library
    16.7 + *
    16.8 + * Copyright (C) 2003-2008
    16.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
   16.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
   16.11 + *
   16.12 + * Permission to use, modify and distribute this software is granted
   16.13 + * provided that this copyright notice appears in all copies. For
   16.14 + * precise terms see the accompanying LICENSE file.
   16.15 + *
   16.16 + * This software is provided "AS IS" with no warranty of any kind,
   16.17 + * express or implied, and with no claim as to its suitability for any
   16.18 + * purpose.
   16.19 + *
   16.20 + */
   16.21 +
   16.22 +#include <string>
   16.23 +#include <iostream>
   16.24 +
   16.25 +#include <lemon/concepts/path.h>
   16.26 +#include <lemon/concepts/digraph.h>
   16.27 +
   16.28 +#include <lemon/path.h>
   16.29 +#include <lemon/list_graph.h>
   16.30 +
   16.31 +#include "test_tools.h"
   16.32 +
   16.33 +using namespace std;
   16.34 +using namespace lemon;
   16.35 +
   16.36 +void check_concepts() {
   16.37 +  checkConcept<concepts::Path<ListDigraph>, concepts::Path<ListDigraph> >();
   16.38 +  checkConcept<concepts::Path<ListDigraph>, Path<ListDigraph> >();
   16.39 +  checkConcept<concepts::Path<ListDigraph>, SimplePath<ListDigraph> >();
   16.40 +  checkConcept<concepts::Path<ListDigraph>, StaticPath<ListDigraph> >();
   16.41 +  checkConcept<concepts::Path<ListDigraph>, ListPath<ListDigraph> >();
   16.42 +}
   16.43 +
   16.44 +int main() {
   16.45 +  check_concepts();  
   16.46 +  return 0;
   16.47 +}
    17.1 --- a/test/test_tools.h	Tue Mar 18 16:45:21 2008 +0100
    17.2 +++ b/test/test_tools.h	Thu Mar 20 12:12:24 2008 +0000
    17.3 @@ -20,6 +20,17 @@
    17.4  #define LEMON_TEST_TEST_TOOLS_H
    17.5  
    17.6  #include <iostream>
    17.7 +#include <vector>
    17.8 +
    17.9 +#include <cstdlib>
   17.10 +#include <ctime>
   17.11 +
   17.12 +#include <lemon/concept_check.h>
   17.13 +#include <lemon/concepts/digraph.h>
   17.14 +
   17.15 +#include <lemon/random.h>
   17.16 +
   17.17 +using namespace lemon;
   17.18  
   17.19  //! \ingroup misc
   17.20  //! \file
   17.21 @@ -36,7 +47,7 @@
   17.22  ///For example
   17.23  ///\code check(0==1,"This is obviously false.");\endcode will
   17.24  ///print this (and then exits).
   17.25 -///\verbatim graph_test.cc:123: error: This is obviously false. \endverbatim
   17.26 +///\verbatim digraph_test.cc:123: error: This is obviously false. \endverbatim
   17.27  ///
   17.28  ///\todo It should be in \c error.h
   17.29  #define check(rc, msg) \
   17.30 @@ -45,4 +56,126 @@
   17.31      abort(); \
   17.32    } else { } \
   17.33  
   17.34 +///Structure returned by \ref addPetersen().
   17.35 +
   17.36 +///Structure returned by \ref addPetersen().
   17.37 +///
   17.38 +template<class Digraph> struct PetStruct
   17.39 +{
   17.40 +  ///Vector containing the outer nodes.
   17.41 +  std::vector<typename Digraph::Node> outer;
   17.42 +  ///Vector containing the inner nodes.
   17.43 +  std::vector<typename Digraph::Node> inner;
   17.44 +  ///Vector containing the arcs of the inner circle.
   17.45 +  std::vector<typename Digraph::Arc> incir;
   17.46 +  ///Vector containing the arcs of the outer circle.
   17.47 +  std::vector<typename Digraph::Arc> outcir;
   17.48 +  ///Vector containing the chord arcs.
   17.49 +  std::vector<typename Digraph::Arc> chords;
   17.50 +};
   17.51 +
   17.52 +
   17.53 +
   17.54 +///Adds a Petersen digraph to \c G.
   17.55 +
   17.56 +///Adds a Petersen digraph to \c G.
   17.57 +///\return The nodes and arcs of the generated digraph.
   17.58 +
   17.59 +template<typename Digraph>
   17.60 +PetStruct<Digraph> addPetersen(Digraph &G,int num = 5)
   17.61 +{
   17.62 +  PetStruct<Digraph> n;
   17.63 +
   17.64 +  for(int i=0;i<num;i++) {
   17.65 +    n.outer.push_back(G.addNode());
   17.66 +    n.inner.push_back(G.addNode());
   17.67 +  }
   17.68 +
   17.69 + for(int i=0;i<num;i++) {
   17.70 +   n.chords.push_back(G.addArc(n.outer[i],n.inner[i]));
   17.71 +   n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1) % num]));
   17.72 +   n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2) % num]));
   17.73 +  }
   17.74 + return n;
   17.75 +}
   17.76 +
   17.77 +/// \brief Adds to the digraph the reverse pair of all arcs.
   17.78 +///
   17.79 +/// Adds to the digraph the reverse pair of all arcs.
   17.80 +///
   17.81 +template<class Digraph> void bidirDigraph(Digraph &G)
   17.82 +{
   17.83 +  typedef typename Digraph::Arc Arc;
   17.84 +  typedef typename Digraph::ArcIt ArcIt;
   17.85 +  
   17.86 +  std::vector<Arc> ee;
   17.87 +  
   17.88 +  for(ArcIt e(G);e!=INVALID;++e) ee.push_back(e);
   17.89 +
   17.90 +  for(typename std::vector<Arc>::iterator p=ee.begin();p!=ee.end();p++)
   17.91 +    G.addArc(G.target(*p),G.source(*p));
   17.92 +}
   17.93 +
   17.94 +
   17.95 +/// \brief Checks the bidirectioned Petersen digraph.
   17.96 +///
   17.97 +///  Checks the bidirectioned Petersen digraph.
   17.98 +///
   17.99 +template<class Digraph> void checkBidirPetersen(Digraph &G, int num = 5)
  17.100 +{
  17.101 +  typedef typename Digraph::Node Node;
  17.102 +
  17.103 +  typedef typename Digraph::ArcIt ArcIt;
  17.104 +  typedef typename Digraph::NodeIt NodeIt;
  17.105 +
  17.106 +  checkDigraphNodeList(G, 2 * num);
  17.107 +  checkDigraphArcList(G, 6 * num);
  17.108 +
  17.109 +  for(NodeIt n(G);n!=INVALID;++n) {
  17.110 +    checkDigraphInArcList(G, n, 3);
  17.111 +    checkDigraphOutArcList(G, n, 3);
  17.112 +  }  
  17.113 +}
  17.114 +
  17.115 +///Structure returned by \ref addUPetersen().
  17.116 +
  17.117 +///Structure returned by \ref addUPetersen().
  17.118 +///
  17.119 +template<class Digraph> struct UPetStruct
  17.120 +{
  17.121 +  ///Vector containing the outer nodes.
  17.122 +  std::vector<typename Digraph::Node> outer;
  17.123 +  ///Vector containing the inner nodes.
  17.124 +  std::vector<typename Digraph::Node> inner;
  17.125 +  ///Vector containing the arcs of the inner circle.
  17.126 +  std::vector<typename Digraph::Edge> incir;
  17.127 +  ///Vector containing the arcs of the outer circle.
  17.128 +  std::vector<typename Digraph::Edge> outcir;
  17.129 +  ///Vector containing the chord arcs.
  17.130 +  std::vector<typename Digraph::Edge> chords;
  17.131 +};
  17.132 +
  17.133 +///Adds a Petersen digraph to the undirected \c G.
  17.134 +
  17.135 +///Adds a Petersen digraph to the undirected \c G.
  17.136 +///\return The nodes and arcs of the generated digraph.
  17.137 +
  17.138 +template<typename Digraph>
  17.139 +UPetStruct<Digraph> addUPetersen(Digraph &G,int num=5)
  17.140 +{
  17.141 +  UPetStruct<Digraph> n;
  17.142 +
  17.143 +  for(int i=0;i<num;i++) {
  17.144 +    n.outer.push_back(G.addNode());
  17.145 +    n.inner.push_back(G.addNode());
  17.146 +  }
  17.147 +
  17.148 + for(int i=0;i<num;i++) {
  17.149 +   n.chords.push_back(G.addArc(n.outer[i],n.inner[i]));
  17.150 +   n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1)%5]));
  17.151 +   n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2)%5]));
  17.152 + }
  17.153 + return n;
  17.154 +}
  17.155 +
  17.156  #endif